-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
77 lines (62 loc) · 2.28 KB
/
Program.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
namespace TicTacToe;
class Program
{
static void Main(string[] args)
{
// String array with 9 elements to represent the grid
string[] grid = new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
bool player1Turn = true;
int numTurns = 0;
while (!CheckVictory() && numTurns != 9)
{
PrintGrid();
if (player1Turn)
Console.WriteLine("Player 1 Turn!");
else
Console.WriteLine("Player 2 Turn!");
string choice = Console.ReadLine();
if (grid.Contains(choice) && choice != "X" && choice != "O")
{
int gridIndex = Convert.ToInt32(choice) - 1;
if (player1Turn)
grid[gridIndex] = "X";
else
grid[gridIndex] = "O";
numTurns++;
Console.WriteLine(numTurns);
}
player1Turn = !player1Turn;
}
PrintGrid();
if (CheckVictory())
Console.WriteLine("You win!");
else
Console.WriteLine("Tie!");
// Method uses and outer and inner loop to print out rows and columns of the grid
void PrintGrid()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(grid[i * 3 + j] + "|");
}
Console.WriteLine("");
Console.WriteLine("------");
}
}
// Method checks which pattern is created and determines victory
bool CheckVictory()
{
bool row1 = grid[0] == grid[1] && grid[1] == grid[2];
bool row2 = grid[3] == grid[4] && grid[4] == grid[5];
bool row3 = grid[6] == grid[7] && grid[7] == grid[8];
bool col1 = grid[0] == grid[3] && grid[3] == grid[6];
bool col2 = grid[1] == grid[4] && grid[4] == grid[7];
bool col3 = grid[2] == grid[5] && grid[5] == grid[8];
bool diagDown = grid[0] == grid[4] && grid[4] == grid[8];
bool diagUp = grid[6] == grid[4] && grid[4] == grid[2];
return row1 || row2 || row3 || col1 || col2 || col3 || diagDown || diagUp;
}
}
}