-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.cs
231 lines (216 loc) · 7.84 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
using System;
using System.Collections.Generic;
namespace tic_tac_toe
{
public class TicTacToe
{
private char[,] board = new char[3, 3] { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
private List<int> usedPositions = new List<int>(9);
private Player _playerOne;
private Player _playerTwo;
private Player _winner;
public TicTacToe() { }
public void Start()
{
ReadPlayers();
ShowInstructions();
Initialize();
}
public void ReadPlayers()
{
Console.WriteLine("Player 1:");
_playerOne = ReadPlayer();
Console.WriteLine("\n");
Console.WriteLine("Player 2:");
_playerTwo = ReadPlayer();
Console.WriteLine("\n");
}
public Player ReadPlayer()
{
string playerName;
Console.Write("Name of the player: ");
playerName = Console.ReadLine();
char playerIcon;
Console.Write("Icon for the player: ");
playerIcon = Console.ReadKey().KeyChar;
return new Player { Name = playerName, Icon = playerIcon };
}
public void ShowInstructions()
{
Console.WriteLine("Welcome to Tic-Tac-Toe");
Console.WriteLine("-----------------------\n");
DrawBoard(new char[3, 3] { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' } });
Console.WriteLine("The numbers represent the positions for the input. In the game you just need to enter the position");
Console.WriteLine("If you understood the instructions press enter...");
Console.ReadKey();
Console.WriteLine("\n");
}
public void DrawBoard(char[,] board)
{
Console.Write(" │ │ \n");
Console.Write($" {board[0, 0]} │ {board[0, 1]} │ {board[0, 2]} \n");
Console.Write("────┼────┼────\n");
Console.Write($" {board[1, 0]} │ {board[1, 1]} │ {board[1, 2]} \n");
Console.Write("────┼────┼────\n");
Console.Write($" {board[2, 0]} │ {board[2, 1]} │ {board[2, 2]} \n");
Console.Write(" │ │ \n");
Console.Write("\n");
}
public void Initialize()
{
Console.WriteLine("Game starting...\n");
PlayerQueue<Player> playerQueue = new PlayerQueue<Player>(2);
playerQueue.Enqueue(_playerOne);
playerQueue.Enqueue(_playerTwo);
Player currentPlayer = playerQueue.GetNext();
bool isWinner = false;
while (!isWinner)
{
DrawBoard(board);
int position = ReadBoardPosition(currentPlayer);
while (!InsertIntoBoard(currentPlayer, position))
{
position = ReadBoardPosition(currentPlayer);
}
if (CheckForWinner(currentPlayer))
{
isWinner = true;
}
currentPlayer = playerQueue.GetNext();
}
DrawBoard(board);
if (_winner == null)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("It's a draw\n");
Environment.Exit(0);
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{_winner.Name} is the winner\n");
Environment.Exit(0);
}
public int ReadBoardPosition(Player currentPlayer)
{
int position;
Console.Write($"{currentPlayer.Name} enter a position (1-9): ");
string input = Console.ReadLine();
Int32.TryParse(input, out position);
return position;
}
public bool InsertIntoBoard(Player player, int position)
{
if (usedPositions.Contains(position))
{
return false;
}
else
{
usedPositions.Add(position);
}
switch (position)
{
case 1:
if (board[0, 0] == ' ')
board[0, 0] = player.Icon;
else
return false;
return true;
case 2:
if (board[0, 1] == ' ')
board[0, 1] = player.Icon;
else
return false;
return true;
case 3:
if (board[0, 2] == ' ')
board[0, 2] = player.Icon;
else
return false;
return true;
case 4:
if (board[1, 0] == ' ')
board[1, 0] = player.Icon;
else
return false;
return true;
case 5:
if (board[1, 1] == ' ')
board[1, 1] = player.Icon;
else
return false;
return true;
case 6:
if (board[1, 2] == ' ')
board[1, 2] = player.Icon;
else
return false;
return true;
case 7:
if (board[2, 0] == ' ')
board[2, 0] = player.Icon;
else
return false;
return true;
case 8:
if (board[2, 1] == ' ')
board[2, 1] = player.Icon;
else
return false;
return true;
case 9:
if (board[2, 2] == ' ')
board[2, 2] = player.Icon;
else
return false;
return true;
default:
Console.WriteLine("The position is not valid. Only values from 1-9 are valid");
return false;
}
}
public bool CheckForWinner(Player player)
{
// check if board is full
if (usedPositions.Capacity == usedPositions.Count)
{
return true;
}
// check horizontal
for (int x = 0; x < 3; x++)
{
if (board[x, 0] == board[x, 1] && board[x, 1] == board[x, 2])
{
if (board[x, 0] != ' ' && board[x, 1] != ' ' && board[x, 2] != ' ')
{
_winner = player;
return true;
}
}
}
// check vertically
for (int y = 0; y < 3; y++)
{
if (board[0, y] == board[1, y] && board[1, y] == board[2, y])
{
if (board[0, y] != ' ' && board[1, y] != ' ' && board[2, y] != ' ')
{
_winner = player;
return true;
}
}
}
// check diagonally
if ((board[0, 0] == board[1, 1] && board[1, 1] == board[2, 2])
|| (board[0, 2] == board[1, 1] && board[1, 1] == board[2, 0]))
{
if ((board[0, 0] != ' ' && board[1, 1] != ' ' && board[2, 2] != ' ')
|| (board[0, 2] != ' ' && board[1, 1] != ' ' && board[2, 0] != ' '))
{
_winner = player;
return true;
}
}
return false;
}
}
}