-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (88 loc) · 2.89 KB
/
Copy pathProgram.cs
File metadata and controls
112 lines (88 loc) · 2.89 KB
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
using System;
class Program
{
static string[,] gameBoard = new string[3, 3]
{
{ " ", " ", " " },
{ " ", " ", " " },
{ " ", " ", " " }
};
static bool playerX = true;
static void Main()
{
bool gameEnded = false;
int movesCount = 0;
while (!gameEnded)
{
Console.Clear();
DrawBoard();
if (playerX)
Console.WriteLine("Player X, enter your move (row [1-3] and column [1-3]):");
else
Console.WriteLine("Player O, enter your move (row [1-3] and column [1-3]):");
int row = GetCoordinate("row");
int col = GetCoordinate("column");
if (gameBoard[row - 1, col - 1] == " ")
{
gameBoard[row - 1, col - 1] = playerX ? "X" : "O";
movesCount++;
}
else
{
Console.WriteLine("Invalid move! That cell is already taken.");
Console.ReadLine();
continue;
}
gameEnded = CheckWin() || movesCount == 9;
playerX = !playerX;
}
Console.Clear();
DrawBoard();
if (movesCount == 9)
Console.WriteLine("It's a draw!");
else
Console.WriteLine("Player " + (playerX ? "O" : "X") + " wins!");
Console.ReadLine();
}
static int GetCoordinate(string coordinate)
{
int value;
bool validInput = false;
do
{
Console.Write("Enter " + coordinate + " [1-3]: ");
string input = Console.ReadLine();
validInput = int.TryParse(input, out value) && value >= 1 && value <= 3;
if (!validInput)
Console.WriteLine("Invalid input! Please enter a number between 1 and 3.");
} while (!validInput);
return value;
}
static void DrawBoard()
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine(" " + gameBoard[i, 0] + " | " + gameBoard[i, 1] + " | " + gameBoard[i, 2]);
if (i < 2)
Console.WriteLine("---+---+---");
}
}
static bool CheckWin()
{
for (int i = 0; i < 3; i++)
{
if (gameBoard[i, 0] == gameBoard[i, 1] && gameBoard[i, 1] == gameBoard[i, 2] && gameBoard[i, 0] != " ")
return true;
}
for (int j = 0; j < 3; j++)
{
if (gameBoard[0, j] == gameBoard[1, j] && gameBoard[1, j] == gameBoard[2, j] && gameBoard[0, j] != " ")
return true;
}
if (gameBoard[0, 0] == gameBoard[1, 1] && gameBoard[1, 1] == gameBoard[2, 2] && gameBoard[0, 0] != " ")
return true;
if (gameBoard[0, 2] == gameBoard[1, 1] && gameBoard[1, 1] == gameBoard[2, 0] && gameBoard[0, 2] != " ")
return true;
return false;
}
}