-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
126 lines (106 loc) · 3.45 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TicTacToe_cs
{
class Board
{
public char[,] gameBoard = new char[3, 3];
int i, j;
public Board()
{
}
public char[,] getgameBoard()
{
return gameBoard;
}
public void printBoard()
{
Console.Out.WriteLine(" A\tB\tC");
for (i = 0; i < 3; i++)
{
Console.Out.Write(i + 1 + " |");
for (j = 0; j < 3; j++)
Console.Out.Write(" " + gameBoard[i, j] + " \t|");
Console.Out.WriteLine("\n-------------------------");
}
}
public bool submitMove(string move, char player)
{
if (move.Equals("A1") || move.Equals("A2") || move.Equals("A3"))
{
j = 0;
}
else if (move.Equals("B1") || move.Equals("B2") || move.Equals("B3"))
{
j = 1;
}
else if (move.Equals("C1") || move.Equals("C2") || move.Equals("C3"))
{
j = 2;
}
i = Convert.ToInt32(Char.GetNumericValue(move, 1));
i--;
if (gameBoard[i, j] != 0)
{
return false;
}
else
{
gameBoard[i, j] = player;
return true;
}
}
public bool isWinner(char player)
{
if (gameBoard[0, 0] == player && gameBoard[1, 1] == player && gameBoard[2, 2] == player)
return true;
else if (gameBoard[0, 0] == player && gameBoard[0, 1] == player && gameBoard[0, 2] == player)
return true;
else if (gameBoard[0, 0] == player && gameBoard[1, 0] == player && gameBoard[2, 0] == player)
return true;
else if (gameBoard[1, 0] == player && gameBoard[1, 1] == player && gameBoard[1, 2] == player)
return true;
else if (gameBoard[2, 0] == player && gameBoard[2, 1] == player && gameBoard[2, 2] == player)
return true;
else if (gameBoard[0, 1] == player && gameBoard[1, 1] == player && gameBoard[2, 1] == player)
return true;
else if (gameBoard[0, 2] == player && gameBoard[1, 1] == player && gameBoard[2, 0] == player)
return true;
else if (gameBoard[0, 2] == player && gameBoard[1, 2] == player && gameBoard[2, 2] == player)
return true;
else
return false;
}
public bool isTie()
{
int p, q;
for (p = 0; p < 3; p++)
{
for (q = 0; q < 3; q++)
{
if (gameBoard[p, q] == 0)
{
return false;
}
}
}
return true;
}
public bool isMoveValid(string move)
{
if (move.Equals("A1") || move.Equals("A2") || move.Equals("A3") ||
move.Equals("B1") || move.Equals("B2") || move.Equals("B3") ||
move.Equals("C1") || move.Equals("C2") || move.Equals("C3"))
{
return true;
}
else
{
return false;
}
}
}
}