-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cs
125 lines (117 loc) · 3.88 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
using System.Diagnostics;
namespace Dimensions;
internal class Board
{
// Member variable
private string _name;
private string[,] _matrix;
private string _charWinner;
// Default Constructor
public Board()
{
_name = "DefaultBoard";
_matrix = new string[,]
{
{ "1", "2", "3" },
{ "4", "5", "6" },
{ "7", "8", "9" }
};
Debug.WriteLine($"Board {_name} was created.");
_charWinner = "";
}
public void PrintBoard()
{
Console.WriteLine(" + + \n" +
" {0} | {1} | {2} \n" +
" +---+---+---+ \n" +
" {3} | {4} | {5} \n" +
" +---+---+---+ \n" +
" {6} | {7} | {8} \n" +
" + + ", _matrix[0, 0], _matrix[0, 1], _matrix[0, 2],
_matrix[1, 0], _matrix[1, 1], _matrix[1, 2],
_matrix[2, 0], _matrix[2, 1], _matrix[2, 2]);
Debug.WriteLine("{0} board was printed.", _name);
}
public void ResetBoard()
{
int numberCount = 1;
for (int i=0; i < _matrix.GetLength(0); i++)
{
for (int j = 0; j < _matrix.GetLength(1); j++)
{
_matrix[i, j] = numberCount.ToString();
numberCount++;
}
}
Debug.WriteLine("{0} board was reset.", _name);
}
public bool ReplaceMatrixPosition(string playerChar, int[] position)
{
int intMatrix;
if ((int.TryParse(_matrix[position[0], position[1]], out intMatrix)))
{
// Successfully parsed
_matrix[position[0], position[1]] = playerChar;
Debug.WriteLine($"ReplaceMatrixPosition: value was: {intMatrix}, value now is {playerChar}.");
return true;
}
else
{
// Position is not an integer (already occupied by a character)
Debug.WriteLine("Value didn't change. Position already occupied.");
return false;
}
}
// BOARD CHECKER
public bool Checker()
{
// CHECK HORIZONTAL
for (int i = 0; i < _matrix.GetLength(0); i++)
{
if (_matrix[i, 1] == _matrix[i, 0] && _matrix[i, 2] == _matrix[i, 0])
{
_charWinner = _matrix[i, 1];
Debug.WriteLine("Checker - status: Winner, type: horizontal");
return true;
}
}
// CHECK VERTICAL
for (int i = 0; i < _matrix.GetLength(1); i++)
{
if (_matrix[1, i] == _matrix[0, i] && _matrix[2, i] == _matrix[0, i])
{
_charWinner = _matrix[1, i];
Debug.WriteLine("Checker - status: winner, type: vertical");
return true;
}
}
// CHECK DIAGONAL
if (_matrix[0, 0] == _matrix[1, 1] && _matrix[2, 2] == _matrix[1, 1] ||
_matrix[0, 2] == _matrix[1, 1] && _matrix[2, 0] == _matrix[1, 1])
{
_charWinner = _matrix[1, 1];
Debug.WriteLine("Checker - status: winner, type: diagonal");
return true;
}
// CHECK DRAW
for (int i = 0; i < _matrix.GetLength(0); i++)
{
for (int j = 0; j < _matrix.GetLength(1); j++)
{
if ((int.TryParse(_matrix[i, j], out _)))
{
// No winner and no draw, board still in play
Debug.WriteLine("Checker - status: in-game, type: n/a");
return false;
}
}
}
// Draw
Debug.WriteLine("Checker - status: draw, type: all slots are full");
return true;
}
public string GetCharWinner()
{
return _charWinner;
}
}