-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
506 lines (451 loc) · 17.7 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace BattleShipCS
{
public partial class Form1 : Form
{
PictureBox[] playerBoard = new PictureBox[100];
PictureBox[] computerBoard = new PictureBox[100];
string[] playerShots = Enumerable.Repeat("e", 100).ToArray();
string[] computerShots = new string[100];
List<int> pShotsAvailable = Enumerable.Range(0, 100).ToList();
List<int> cShotsAvailable = Enumerable.Range(0, 100).ToList();
List<int> positionsAvailable = Enumerable.Range(0, 100).ToList();
//int playerShip1, playerShip2, playerShip3;
int computerShip1, computerShip2, computerShip3;
int turn = 0;
public enum Position { Vertical, Horizontal };
int playerShip; //counter for the number of ships positionned by player
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
PrintBoard();
//playerShip1 = PlaceBoats(isVertical: true); //Using Named arguments
try
{
/*if (playerShip == 0)
{*/
PlaceBoats();
Console.WriteLine($"value for playership : {playerShip}");
MessageBox.Show("Welcome to Battleship! Please start by positioning your 3 boats : 1st one will be vertical, the next 2 will be horizontal.");
/*}
else if (playerShip < 3)
{*/
/*PlaceBoats(Position.Horizontal);
Console.WriteLine($"value for playership : {playerShip}");
PlaceBoats(Position.Horizontal);*/
//}
//Using enum
/*playerShip2 = PlaceBoats(Position.Horizontal);
playerShip3 = PlaceBoats(Position.Horizontal);*/
}
catch (IndexOutOfRangeException ex)
{
MessageBox.Show(ex.Message);
}
}
public void PlaceBoats()
{
foreach (var (tile, index) in playerBoard.Select((Name, index) => (Name, index)))
{
/*tile.MouseHover += (sender, EventArgs) => { Tile_MouseHover(sender, EventArgs, position); };*/
/*if (position == Position.Vertical)
{*/
tile.Click += Place_Boat;
tile.MouseHover += Tile_MouseHover;
//tile.MouseEnter += Tile_MouseEnter;
//tile.MouseMove += Tile_MouseMove;
tile.MouseLeave += Tile_MouseLeave;
/*if (index < 90)
{
tile.Click += (sender, EventArgs) => { Place_Boat(sender, EventArgs, Position.Vertical); };
}
*//*}*/
/*else if (position == Position.Horizontal)
{*//*
if (index % 10 < 9)
{
tile.Click += (sender, EventArgs) => { Place_Boat(sender, EventArgs, Position.Horizontal); };
}*/
//return index;
/*}*/
//return -1;
//return index;
/*else if (position == Position.Horizontal)
{
return index, index + 1;
}*/
}
}
private void Tile_MouseLeave(object sender, EventArgs e)
{
//throw new NotImplementedException();
var tile = sender as PictureBox;
var index = Array.IndexOf(playerBoard, tile);
if (playerShip == 0)
{
if (index >= 0 && index < 90)
{
if (playerShots[index] != "b")
{
/*tile.Size = new Size(45, 90);
tile.Image = Properties.Resources.boatVert;*/
playerBoard[index].Image = Properties.Resources.water;
playerBoard[index + 10].Image = Properties.Resources.water;
}
}
}
else
{
if (playerShots[index] != "b" && playerShots[index + 1] != "b")
{
/*tile.Size = new Size(90, 45);
tile.Image = Properties.Resources.boat;*/
playerBoard[index].Image = Properties.Resources.water;
playerBoard[index + 1].Image = Properties.Resources.water;
}
}
}
private void Tile_MouseMove(object sender, MouseEventArgs e)
{
//throw new NotImplementedException();
var tile = sender as PictureBox;
var index = Array.IndexOf(playerBoard, tile);
/*if (tile.Image == Properties.Resources.water)
{*/
Console.WriteLine(index);
if (playerShip == 0)
{
if (index >= 0 && index < 90)
{
/*tile.Size = new Size(45, 90);
tile.Image = Properties.Resources.boatVert;*/
playerBoard[index].Image = Properties.Resources.boatTop;
playerBoard[index + 10].Image = Properties.Resources.boatBottom;
}
}
else
{
tile.Size = new Size(90, 45);
tile.Image = Properties.Resources.boat;
}
/*}*/
}
public void Place_Boat(object sender, EventArgs e)
{
//throw new NotImplementedException();
int index = Array.IndexOf(playerBoard, sender);
Console.WriteLine($"index {index} on playerBoard");
if (playerShip == 0)
{
/*if (position == Position.Vertical)
{*/
//After click, add image then remove all events.
if (index < 90)
{
playerBoard[index].Image = Properties.Resources.boatTop;
playerBoard[index + 10].Image = Properties.Resources.boatBottom;
playerShots[index] = "b";
playerShots[index + 10] = "b";
playerBoard[index].MouseHover -= Tile_MouseHover;
playerBoard[index + 10].MouseHover -= Tile_MouseHover;
playerBoard[index].MouseLeave -= Tile_MouseLeave;
playerBoard[index + 10].MouseLeave -= Tile_MouseLeave;
}
else
{
MessageBox.Show("Please position your vertical boat in the allowed tiles");
return;
}
}
else if (playerShip <= 2)
{
/*if (position == Position.Horizontal)
{*/
if (index % 10 < 9)
{
playerBoard[index].Image = Properties.Resources.boatLeft;
playerBoard[index + 1].Image = Properties.Resources.boatRight;
playerShots[index] = "b";
playerShots[index + 1] = "b";
playerBoard[index].MouseHover -= Tile_MouseHover;
playerBoard[index + 1].MouseHover -= Tile_MouseHover;
playerBoard[index].MouseLeave -= Tile_MouseLeave;
playerBoard[index + 1].MouseLeave -= Tile_MouseLeave;
}
else
{
MessageBox.Show("Please position your horizontal boat in the allowed tiles");
return;
}
/*}*/
/*inRange = false;
throw new IndexOutOfRangeException("The vertical boat won't fit. Play stay in a higher range");*/
}
if (playerShip == 2)
{
//exit the click events on the player board
MessageBox.Show("Now the computer will place it's boats.");
RemoveClicksOnPlayerBoard();
LoadRest();
turn = 0;
//load rest of the form.
}
///return index;
playerShip++;
}
public void Tile_MouseHover(object sender, EventArgs e)
{
//throw new NotImplementedException();
var tile = sender as PictureBox;
var index = Array.IndexOf(playerBoard, sender);
if (playerShip == 0)
{
if (index >= 0 && index < 90)
{
if (playerShots[index] != "b")
{
/*tile.Size = new Size(45, 90);
tile.Image = Properties.Resources.boatVert;*/
playerBoard[index].Image = Properties.Resources.boatTop;
playerBoard[index + 10].Image = Properties.Resources.boatBottom;
}
}
Console.WriteLine($"index hover : {index}");
}
else if (playerShip <= 2)
{
if (index % 10 < 9)
{
if (playerShots[index] != "b" && playerShots[index + 1] != "b")
{
playerBoard[index].Image = Properties.Resources.boatLeft;
playerBoard[index + 1].Image = Properties.Resources.boatRight;
//tile.Image = Properties.Resources.boat;
}
}
}
}
public void RemoveClicksOnPlayerBoard()
{
foreach (var tile in playerBoard)
{
//tile.Size = new Size(45, 45);
tile.Click -= Place_Boat;
tile.MouseLeave -= Tile_MouseLeave;
//tile.MouseMove -= Tile_MouseMove;
tile.MouseHover -= Tile_MouseHover;
}
}
public void LoadRest()
{
RandomAssignments();
foreach (var (tile, index) in computerBoard.Select((Name, index) => (Name, index)))
{
tile.Cursor = Cursors.Hand;
tile.Click += Tile_Click;
}
if (turn == 0)
{
MessageBox.Show("The game can start! Please select the tiles on the computer side to try to hit the Computers' boats!");
}
}
public void WinCheck(string[] board)
{
int counter = 0;
for (int i = 0; i < board.Length; i++)
{
if (board[i] == "h")
{
counter++;
}
}
if (counter == 6)
{
DialogResult dr = MessageBox.Show("Game over! Would you like to play again?", "Game Over", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
//Form1_Load(null, EventArgs.Empty);
Application.Restart();
}
else
{
Close();
}
}
}
private void Tile_Click(object sender, EventArgs e)
{
//throw new NotImplementedException();
var tile = sender as PictureBox;
int index = Array.IndexOf(computerBoard, tile);
Console.WriteLine($"this is tile {tile.Name} at index {index}, identified as {computerShots[index]}");
if (computerShots[index] == null)
{
computerShots[index] = "m"; //for miss
tile.Image = Properties.Resources.splash;
}
else if (computerShots[index] == "b") //for boat
{
computerShots[index] = "h";
tile.Image = Properties.Resources.hit;
MessageBox.Show("Target hit!");
}
else if (computerShots[index] == "m")
{
MessageBox.Show("This target was already missed!");
}
WinCheck(computerShots);
turn++;
//commputer's turn
if (turn % 2 == 1)
{
MessageBox.Show("The computer will play!");
ComputerTurn();
turn++;
}
}
public void ComputerTurn()
{
Random rdm = new Random();
int compTarget = rdm.Next(0, playerShots.Length);
if (playerShots[compTarget] == "e")
{
playerShots[compTarget] = "m"; //for miss
playerBoard[compTarget].Image = Properties.Resources.splash;
}
else if (playerShots[compTarget] == "b") //for boat
{
playerShots[compTarget] = "h";
playerBoard[compTarget].Image = Properties.Resources.hit;
MessageBox.Show("Target hit!");
}
else if (playerShots[compTarget] == "m")
{
ComputerTurn();
}
WinCheck(playerShots);
}
public void PrintBoard()
{
//player side - tiles 45x45, with a 10x10 grid.
int posXstart = 50;
int posYstart = 90;
//foreach (PictureBox tile in playerBoard)
for (int i = 0; i < playerBoard.Length; i++)
{
playerBoard[i] = new PictureBox
{
Name = "tile" + i,
Location = new Point(posXstart + (i % 10) * 45, posYstart + (i / 10) * 45),
Size = new Size(44, 44),
BackgroundImage = Properties.Resources.water,
Visible = Enabled
};
this.Controls.Add(playerBoard[i]);
}
posXstart = 600;
posYstart = 90;
// computer side
for (int i = 0; i < computerBoard.Length; i++)
{
computerBoard[i] = new PictureBox
{
Name = "tile" + i,
Location = new Point(posXstart + (i % 10) * 45, posYstart + (i / 10) * 45),
Size = new Size(44, 44),
BackgroundImage = Properties.Resources.water,
Visible = Enabled
};
this.Controls.Add(computerBoard[i]);
}
}
public void RandomAssignments()
{
//assign boat to random. and hits? And taking off from the positionsAvailable list.
computerShip1 = AssignBoatsVertical();
computerShip2 = AssignBoatsHorizontal();
computerShip3 = AssignBoatsHorizontal();
Debug.WriteLine($"position 1 : {computerShip1} \nposition 2 : {computerShip2} \nposition 3 : {computerShip3}");
//Once we assigned the 2 different position, that will be different, we refill the list? If needed?
}
public int AssignBoatsHorizontal()
{
//assign a random value to the boat and remove it and its next one for the horizontal boat. For vertical, remove the one 10 after.
Random boatPosition = new Random();
int ship = boatPosition.Next(0, positionsAvailable.Count);
if (computerShots[ship] != "b")
{
//to avoid the overlap on the edges.
if (ship % 10 == 9)
{
ship--;
}
computerBoard[ship].Image = Properties.Resources.boatLeft;
computerShots[ship] = "b";
computerBoard[ship + 1].Image = Properties.Resources.boatRight;
computerShots[ship + 1] = "b";
HideComputerBoats(ship, Position.Horizontal);
return positionsAvailable[ship];
}
else
{
return AssignBoatsHorizontal();
}
}
public int AssignBoatsVertical()
{
//assign a random value to the boat position. If there are no boats, but if there is, we recurse through and recall the function.
Random boatPosition = new Random();
int ship = boatPosition.Next(0, positionsAvailable.Count - 10);
if (computerShots[ship] != "b" && computerShots[ship + 10] != "b")
{
computerBoard[ship].Image = Properties.Resources.boatTop;
computerShots[ship] = "b";
computerBoard[ship + 10].Image = Properties.Resources.boatBottom;
computerShots[ship + 10] = "b";
HideComputerBoats(ship, Position.Vertical);
return positionsAvailable[ship];
}
else
{
return AssignBoatsVertical();
}
}
//hides the computer's position for play
public void HideComputerBoats(int index, Position position)
{
if (position == Position.Vertical)
{
computerBoard[index].Image = Properties.Resources.water;
computerBoard[index + 10].Image = Properties.Resources.water;
}
else if (position == Position.Horizontal)
{
computerBoard[index].Image = Properties.Resources.water;
computerBoard[index + 1].Image = Properties.Resources.water;
}
}
/*public void RemovePosition(int ship)
{
positionsAvailable.Remove(ship);
positionsAvailable.Remove(ship + 1);
positionsAvailable.Remove(ship + 10);
}
public void ReturnPosition(int boat)
{
positionsAvailable.Add(boat);
}*/
}
}