-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cs
115 lines (106 loc) · 3.24 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TresEnRaya
{
class Board
{
private TileStates[,] boardData = new TileStates[3, 3];
public enum TileStates { Empty = 0, Player = 1, Enemy = 2 };
public Board()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
setTile(i, j, TileStates.Empty);
}
}
}
public void clear()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
boardData[i,j] = TileStates.Empty;
}
}
}
public void setTile(int x, int y, TileStates value)
{
if (boardData[x, y] != TileStates.Empty)
{
throw new TileOccupiedException(boardData[x, y]);
}
else
{
boardData[x, y] = value;
}
}
public void setTile(string name, TileStates value)
{
switch (name)
{
case "c0_0":
this.setTile(0, 0, value);
break;
case "c0_1":
this.setTile(0, 1, value);
break;
case "c0_2":
this.setTile(0, 2, value);
break;
case "c1_0":
this.setTile(1, 0, value);
break;
case "c1_1":
this.setTile(1, 1, value);
break;
case "c1_2":
this.setTile(1, 2, value);
break;
case "c2_0":
this.setTile(2, 0, value);
break;
case "c2_1":
this.setTile(2, 1, value);
break;
case "c2_2":
this.setTile(2, 2, value);
break;
}
}
public TileStates getTile(int x, int y)
{
return boardData[x, y];
}
public TileStates evaluateBoard()
{
for (int i = 0; i < 3; i++) {
if (boardData[i,0]== boardData[i, 1] && boardData[i, 0] == boardData[i, 2] && boardData[i, 0]!=TileStates.Empty)
{
return boardData[i, 0];
}
}
for (int i = 0; i < 3; i++)
{
if (boardData[0,i] == boardData[ 1,i] && boardData[0,i] == boardData[2,i] && boardData[0,i] != TileStates.Empty)
{
return boardData[0,i];
}
}
if (boardData[0, 0] == boardData[1, 1] && boardData[0, 0] == boardData[2, 2] && boardData[0, 0] != TileStates.Empty)
{
return boardData[0, 0];
}
if (boardData[0, 2] == boardData[1, 1] && boardData[0, 2] == boardData[2, 0] && boardData[0, 2] != TileStates.Empty)
{
return boardData[0, 2];
}
return TileStates.Empty;
}
}
}