This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
140 lines (128 loc) · 5.15 KB
/
Board.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
public class Board {
public static (int x, int y) Position { get; set; } = (0, 0);
public static int SpaceWidth { get; set; } = 18;
public static int SpaceHeight { get; set; } = 7;
public Space[,] Spaces { get; set; } = new Space[3,3];
public static Space? ActiveSpace { get; set; }
public Board() {
// Create Spaces --------------/
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
Spaces[x, y] = new(x, y);
}
}
ActiveSpace = Spaces[0,0];
}
public void Render() {
Position = (Console.CursorLeft, Console.CursorTop);
// Render Spaces --------------/
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
Spaces[x, y].Render(x, y);
}
}
}
public void Select() {
ConsoleKey key;
(int x, int y) = ActiveSpace!.Position;
while (true) {
key = Console.ReadKey(true).Key;
switch (key) {
case ConsoleKey.UpArrow:
if (y > 0) {
ActiveSpace = Spaces[x, y - 1];
return;
}
break;
case ConsoleKey.RightArrow:
if (x < 2) {
ActiveSpace = Spaces[x + 1, y];
return;
}
break;
case ConsoleKey.DownArrow:
if (y < 2) {
ActiveSpace = Spaces[x, y + 1];
return;
}
break;
case ConsoleKey.LeftArrow:
if (x > 0) {
ActiveSpace = Spaces[x - 1, y];
return;
}
break;
case ConsoleKey.Enter:
case ConsoleKey.Spacebar:
if (!ActiveSpace.IsClaimed) {
if (TicTacToe.ActivePlayer.Symbol == 'X') {
ActiveSpace.Symbol = new XMark();
} else {
ActiveSpace.Symbol = new OMark();
}
// Update Space Properties
ActiveSpace.ClaimedBy = TicTacToe.ActivePlayer;
ActiveSpace.IsClaimed = true;
// Change Players
TicTacToe.ActivePlayer = (TicTacToe.ActivePlayer == TicTacToe.Players[0]) ? TicTacToe.Players[1] : TicTacToe.Players[0];
return;
}
break;
}
}
}
public void CheckForWin() {
// Check for Horizontal Win -----------/
for (int y = 0; y < 3; y++) {
if (Spaces[0, y].IsClaimed) {
if (Spaces[0, y].ClaimedBy == Spaces[1, y].ClaimedBy && Spaces[1, y].ClaimedBy == Spaces[2, y].ClaimedBy) {
TicTacToe.GameOver = true;
Console.WriteLine();
Player Winner = (TicTacToe.ActivePlayer == TicTacToe.Players[0]) ? TicTacToe.Players[1] : TicTacToe.Players[0];
Utilities.Center($"{Winner.Name} wins!");
return;
}
}
}
// Check for Vertical Win -------------/
for (int x = 0; x < 3; x++) {
if (Spaces[x, 0].IsClaimed) {
if (Spaces[x, 0].ClaimedBy == Spaces[x, 1].ClaimedBy && Spaces[x, 1].ClaimedBy == Spaces[x, 2].ClaimedBy) {
TicTacToe.GameOver = true;
Console.WriteLine();
Player Winner = (TicTacToe.ActivePlayer == TicTacToe.Players[0]) ? TicTacToe.Players[1] : TicTacToe.Players[0];
Utilities.Center($"{Winner.Name} wins!");
return;
}
}
}
// Check for Diagonal Win -------------/
if (Spaces[0, 0].IsClaimed) {
if (Spaces[0, 0].ClaimedBy == Spaces[1, 1].ClaimedBy && Spaces[1, 1].ClaimedBy == Spaces[2, 2].ClaimedBy) {
TicTacToe.GameOver = true;
Console.WriteLine();
Player Winner = (TicTacToe.ActivePlayer == TicTacToe.Players[0]) ? TicTacToe.Players[1] : TicTacToe.Players[0];
Utilities.Center($"{Winner.Name} wins!");
return;
}
}
if (Spaces[2, 0].IsClaimed) {
if (Spaces[2, 0].ClaimedBy == Spaces[1, 1].ClaimedBy && Spaces[1, 1].ClaimedBy == Spaces[0, 2].ClaimedBy) {
TicTacToe.GameOver = true;
Console.WriteLine();
Player Winner = (TicTacToe.ActivePlayer == TicTacToe.Players[0]) ? TicTacToe.Players[1] : TicTacToe.Players[0];
Utilities.Center($"{Winner.Name} wins!");
return;
}
}
// Check for Draw ---------------------/
foreach (Space space in Spaces) {
if (space.Symbol is null) {
return;
}
}
TicTacToe.GameOver = true;
Console.WriteLine();
Utilities.Center("It's a Draw!");
}
}