-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaughtsAndCrosses.cs
191 lines (188 loc) · 6.8 KB
/
NaughtsAndCrosses.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
// Written by Nick. https//www.github.com/nsgwick/
// Version 1.0.0
using System;
using System.Collections.Generic;
// Also known as Tic Tac Toe
namespace NaughtsAndCrosses
{
class Program
{
private const string Version = "1.0.0";
private static int[] _board;
private static int _size;
private static void Main(string[] args)
{
bool playAgain;
do
{
SetSize();
int won = -1, stop = 0, current = 1;
_board = Empty();
while(won < 0)
{
WriteBoard();
Console.WriteLine("Turn: " + GetChar(current));
Console.WriteLine("Column, row (layout: x,y. E.g. 1,2)");
var choice = Console.ReadLine();
while (!ProcessChoice(current, choice, out stop))
{
Console.WriteLine("Invalid value.");
choice = Console.ReadLine();
}
if (stop > 0) break;
current = current == 1 ? 2 : 1;
CheckForWin(out won);
}
if (stop == 2) break;
if (stop == 1) playAgain = true;
else
{
WriteBoard();
Console.WriteLine(won == 0 ? "Draw!" : "The winner is player #" + won
+ " (" + GetPlayer(won) + ")!");
Console.WriteLine("Do you want to play again? (y/n...)");
ConsoleKey play;
do
{
play = Console.ReadKey().Key;
} while (!(play == ConsoleKey.Y || play == ConsoleKey.N));
playAgain = play == ConsoleKey.Y;
}
} while (playAgain);
}
private static void ClearConsole()
{
Console.Clear();
Console.WriteLine("Naughts and Crosses v" + Version + " by Nick. Website: https://nsgwick.com/coding/projects/#naughts-crosses");
}
private static void SetSize()
{
ClearConsole();
Console.WriteLine("Choose board size (default 3, min. 3, max 9):");
_size = !int.TryParse(Console.ReadLine(), out _size) ? 3 : _size > 9 ? 9 : _size < 3 ? 3 : _size;
}
private static bool ProcessChoice(int player, string choice, out int stop)
{
if (choice.ToLower().StartsWith("s"))
{
stop = 0;
return true;
}
if (choice.ToLower().StartsWith("r"))
{
stop = 1;
return true;
}
if (choice.ToLower().StartsWith("q"))
{
stop = 2;
return true;
}
stop = 0;
var split = choice.Split(',');
if(split.Length < 2) return false;
if (!int.TryParse(split[1].Trim(), out int row)) return false;
if (!int.TryParse(split[0].Trim(), out int col)) return false;
if(!(0 < row && row <= _size && 0 < col && col <= _size)) return false;
row--;
col--;
var bidding = _board[row * _size + col];
if(!CheckForPlayer(bidding))
{
_board.SetValue(player,(row * _size) + col);
return true;
}
Console.WriteLine("That box is taken.");
return false;
}
private static void WriteBoard()
{
ClearConsole();
Console.WriteLine("Type s to skip, r to restart or q to quit.\n");
var spacer = new string('-', _size * 4 + 2);
for(var i = 1; i <= _size; i++) Console.Write("---" + i);
Console.Write("--\n");
for(var i = 0; i < _size; i++)
{
var line = i + 1 + "|";
for(int j = 0; j < _size; j++) line += " " + GetChar(_board[i * _size + j]) + " |";
Console.WriteLine(line);
Console.WriteLine(spacer);
}
}
private static char GetChar(int n)
{
return n == 1 ? 'O' : n == 2 ? 'X' : ' ';
}
private static char GetPlayer(int current)
{
return GetChar(current);
}
private static void CheckForWin(out int winner)
{
for(var i = 0; i < _size; i++)
{
List<int> verticalSquares = new List<int>(),
horizontalSquares = new List<int>();
for (var col = 0; col < _size; col++)
{
verticalSquares.Add(_board[i + col * _size]);
horizontalSquares.Add(_board[(i * _size) + col]);
}
if (SquaresMatch(horizontalSquares.ToArray()) && CheckForPlayer(horizontalSquares[0]))
{
winner = horizontalSquares[0];
return;
}
if (SquaresMatch(verticalSquares.ToArray()) && CheckForPlayer(verticalSquares[0]))
{
winner = verticalSquares[0];
return;
}
}
var squaresFromDiagonal = new List<int>();
for(var i = 0; i < _size; i++)
{
squaresFromDiagonal.Add(_board[i * (_size + 1)]);
}
if (SquaresMatch(squaresFromDiagonal.ToArray()) && squaresFromDiagonal.TrueForAll(CheckForPlayer))
{
winner = squaresFromDiagonal[0];
return;
}
squaresFromDiagonal = new List<int>();
for (var i = _size - 1; i <= _size * (_size - 1); i += _size - 1) squaresFromDiagonal.Add(_board[i]);
if (SquaresMatch(squaresFromDiagonal.ToArray()) && squaresFromDiagonal.TrueForAll(CheckForPlayer))
{
winner = squaresFromDiagonal[0];
return;
}
if (!Array.TrueForAll(_board, CheckForPlayer))
{
winner = -1;
return;
}
winner = 0;
}
private static bool CheckForPlayer(int n)
{
return n == 1 || n == 2;
}
private static bool SquaresMatch(params int[] squares)
{
var previous = squares[0];
foreach (var square in squares)
{
if (previous != square) return false;
previous = square;
}
return true;
}
private static int[] Empty()
{
var list = new List<int>();
for(var i = 0; i < _size * _size; i++) list.Add(GetChar(0));
return list.ToArray();
}
}
}