-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.cs
611 lines (547 loc) · 17.9 KB
/
tictactoe.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
using System;
namespace cstictactoe
{
class Program
{
//Options: 0 = (grid size), 1 = (player count)
static int[] oplist = options();
//Following on from the options
static int size = oplist[0];
static int com = oplist[1];
//For use in the computer algorithim
//comrun is how many computer turns there have been
//prev is a store of recent moves
static int comrun = 0;
static int[] prev = new int[2];
//Turn and moves determine the charachters to place on the board and how may times that has occoured
static char turn = 'x';
static int moves = 0;
//Tic tac to grid
static char[,] grid = new char[size, size];
//Determines if game is on
static bool gameon = true;
public static void Main(string[] args)
{
//Console.Clear();
Console.WriteLine("Enter 'esc' to end game\n");
Console.WriteLine("***");
Console.WriteLine("\nTIC TAC TOE\n");
//Determines what turn is the computer's
char comturn = '-';
//Change comturn based of the player options
if (com == 2)
{
comturn = 'o';
}
if (com == 3)
{
comturn = 'x';
}
//Add dashes to grid
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
grid[x, y] = '-';
}
}
while (moves < grid.Length && gameon == true)
{
Console.WriteLine("***");
Console.WriteLine("Player " + turn + "'s turn\n");
//Used to help check and convert inputs to positions on grid
int sx = 0;
int sy = 0;
string movex = " ";
string movey = " ";
//Player's turn
if (com == 1 || comturn != turn)
{
Console.WriteLine("Enter x-position");
movex = Console.ReadLine();
if (movex != "esc")
{
Console.WriteLine("Enter y-position");
movey = Console.ReadLine();
}
}
else
{
// Computer's turn
int[] cm = RunCom(comturn);
movex = cm[0].ToString();
movey = cm[1].ToString();
}
if (movex == "esc" || movey == "esc")
{
Console.WriteLine("\nEnding Game ...");
gameon = false;
}
//Check if the input was an integer
else if (!int.TryParse(movex, out sx))
{
Console.WriteLine("\nImproper input, Try again\n");
}
else if (!int.TryParse(movey, out sy))
{
Console.WriteLine("\nImproper input, Try again\n");
}
else if (sx >= 1 && sx <= size && sy >= 1 && sy <= size)
{
//Checks if grid position is empty and places the marker
if (grid[sx - 1, sy - 1] == '-')
{
grid[sx - 1, sy - 1] = turn;
Console.Clear();
Console.WriteLine(PrintGrid());
prev[0] = sx;
prev[1] = sy;
//Checks if there has been a win, if not, switches turn
if (CheckWin() == true)
{
Console.WriteLine("Player " + turn + " has won!");
gameon = false;
}
else
{
if (turn == 'x')
{
turn = 'o';
}
else
{
turn = 'x';
}
moves++;
}
}
else
{
Console.WriteLine("\nImproper input, Try again\n");
}
}
else
{
Console.WriteLine("\nImproper input, Try again\n");
}
}
}
static bool CheckWin()
{
//cor is used to check individual sets of grid size
//def is the default marker to make sure it counts only one type of marker
//run is a total of how many times one marker occours in a set of grid size
char[] cor = new char[size];
char def = ' ';
int run = 0;
//Column Wins
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
cor[y] = grid[x, y];
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
}
if (run == size)
{
return true;
}
run = 0;
def = ' ';
}
//Row Wins
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
cor[x] = grid[x, y];
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
}
if (run == size)
{
return true;
}
run = 0;
def = ' ';
}
//Diagonal Wins
for (int i = 0; i < size; i++)
{
cor[i] = grid[i, i];
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
}
if (run == size)
{
return true;
}
run = 0;
def = ' ';
//Reverse Diagonal Wins
for (int i = 0; i < size; i++)
{
cor[i] = grid[size - i - 1, i];
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
}
if (run == size)
{
return true;
}
run = 0;
def = ' ';
//No win
return false;
}
//Prints grig using for loop
static string PrintGrid()
{
string gridstring = "";
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
gridstring += grid[x, y] + " ";
}
gridstring += "\n";
}
return gridstring;
}
//Helps set the grid size and Player options
static int[] options()
{
//The bool is used to repeat the loop is the wrong options are entered
//The int list is the option list returned and put into the size and player variables
bool whilecheck = true;
int[] ops = new int[2];
//Alter grid size from 1 to infinity
Console.WriteLine("Enter Grid Size: ");
while (whilecheck)
{
whilecheck = false;
if (!int.TryParse(Console.ReadLine(), out ops[0]))
{
Console.WriteLine("\nImproper input, Try again\n");
whilecheck = true;
}
else if (ops[0] < 1)
{
Console.WriteLine("\nImproper input, Try again\n");
whilecheck = true;
}
}
//Player options from 1 to 3
//1 = no computer
//2 = computer goes last
//3 = computer goes first)
Console.WriteLine("Enter play options:"); Console.WriteLine("1 = no computer, 2 = computer goes last, 3 = computer goes first ");
whilecheck = true;
while (whilecheck)
{
whilecheck = false;
if (!int.TryParse(Console.ReadLine(), out ops[1]))
{
Console.WriteLine("\nImproper input, Try again\n");
whilecheck = true;
}
else if (ops[1] < 1 || ops[1] > 3)
{
Console.WriteLine("\nImproper input, Try again\n");
whilecheck = true;
}
}
return ops;
}
//Algorithim function, runs when comturn is equal to turn
static int[] RunCom(char comturn)
{
//commove is the set of where the computer's next move is
//The middle decimal is the centre point of the grid, will be rounded
int[] commove = new int[2];
decimal middle = Convert.ToDecimal(size) / 2;
//Move if no threats
//Corners, then centre, then any other position(s)
if (grid[0, 0] == '-')
{
commove[0] = 1;
commove[1] = 1;
comrun++;
}
else if (grid[size - 1, 0] == '-')
{
commove[0] = size;
commove[1] = 1;
comrun++;
}
else if (grid[size - 1, size - 1] == '-')
{
commove[0] = size;
commove[1] = size;
comrun++;
}
else if (grid[0, size - 1] == '-')
{
commove[0] = 1;
commove[1] = size;
comrun++;
}
else if (grid[Convert.ToInt32(middle) - 1, 1] == '-')
{
commove[0] = Convert.ToInt32(middle);
commove[1] = Convert.ToInt32(middle);
comrun++;
}
else
{
//Scan rest of grid for a position
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
if (grid[x, y] == '-')
{
commove[0] = x + 1;
commove[1] = y + 1;
comrun++;
}
}
}
}
// Check For Checks
//dash checks is there is a dash in the set
//cor is used to check individual sets of grid size
//def is the default marker to make sure it counts only one type of marker
//run is a total of how many times one marker occours in a set of grid size
//winning helps prioritize winning moves. If one is found, all other options immediately stop
//valst is a store to help determine position of the empty space
bool dash = false;
char[] cor = new char[size];
char def = ' ';
int run = 0;
bool winning = true;
int valst = 0;
//Column Wins
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
cor[y] = grid[x, y];
if (cor[y] == '-')
{
valst = y;
}
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
if (c == '-')
{
dash = true;
}
}
if (winning && dash && run == size - 1)
{
if (def == comturn)
{
winning = false;
}
commove[0] = x + 1;
commove[1] = valst + 1;
}
run = 0;
dash = false;
def = ' ';
}
//Row Wins
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
cor[x] = grid[x, y];
if (cor[x] == '-')
{
valst = x;
}
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
if (c == '-')
{
dash = true;
}
}
if (winning && dash && run == size - 1)
{
if (def == comturn)
{
winning = false;
}
commove[1] = y + 1;
commove[0] = valst + 1;
}
run = 0;
dash = false;
def = ' ';
}
//Diagonal Wins
for (int i = 0; i < size; i++)
{
cor[i] = grid[i, i];
if (cor[i] == '-')
{
valst = i;
}
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
if (c == '-')
{
dash = true;
}
}
if (winning && dash && run == size - 1)
{
if (def == comturn)
{
winning = false;
}
commove[1] = valst + 1;
commove[0] = valst + 1;
}
run = 0;
dash = false;
def = ' ';
//Reverse Diagonal Wins
for (int i = 0; i < size; i++)
{
cor[i] = grid[size - i - 1, i];
if (cor[i] == '-')
{
valst = i;
}
}
foreach (char c in cor)
{
if (def == ' ')
{
if (c == 'x' || c == 'o')
{
def = c;
run++;
}
}
else if (def == c)
{
run++;
}
if (c == '-')
{
dash = true;
}
}
if (winning && dash && run == size - 1)
{
if (def == comturn)
{
winning = false;
}
commove[1] = valst + 1;
commove[0] = size - valst;
}
run = 0;
dash = false;
def = ' ';
//Return the computer's move
return commove;
}
}
}