-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
154 lines (136 loc) · 3.24 KB
/
Game.java
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
public class Game
{
//0 means nothing, 1 means O, 2 means X
int[][] state;
Player a;
Player b;
public Game()
{
state = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
state[i][j] = 0;
}
}
//a goes first. b goes second.
a = new MachinePlayer(state, 2); //x
b = new MachinePlayer(state, 1); //o
}
public static int win(int[][] state)
{
//diaganols
if (state[0][0] != 0 && state[0][0] == state[1][1] && state[1][1] == state[2][2]) {
return state[0][0];
} else if (state[0][2] != 0 && state[0][2] == state[1][1] && state[1][1] == state[2][0]) {
return state[0][2];
//rows
} else if (state[0][0] != 0 && state[0][0] == state[0][1] && state[0][1] == state[0][2]) {
return state[0][0];
} else if (state[1][0] != 0 && state[1][0] == state[1][1] && state[1][1] == state[1][2]) {
return state[1][0];
} else if (state[2][0] != 0 && state[2][0] == state[2][1] && state[2][1] == state[2][2]) {
return state[2][0];
// columns
} else if (state[0][0] != 0 && state[0][0] == state[1][0] && state[1][0] == state[2][0]) {
return state[0][0];
} else if (state[0][1] != 0 && state[0][1] == state[1][1] && state[1][1] == state[2][1]) {
return state[0][1];
} else if (state[0][2] != 0 && state[0][2] == state[1][2] && state[1][2] == state[2][2]) {
return state[0][2];
}
boolean draw = true;
for (short i = 0; i < 3; i++)
{
for (short j = 0; j < 3; j++)
{
if (state[i][j] == 0)
{
draw = false;
}
}
}
if (draw)
{
return -1;
}
return 0;
}
public static int[][] copy2D(int[][] arr)
{
int[][] copy = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
{
copy[i] = arr[i].clone();
}
return copy;
}
public int play()
{
Player curr = a;
while (Game.win(this.state) == 0)
{
//System.out.println(this + "\n");
int[] move = curr.getMove();
state[move[0]][move[1]] = curr.side;
//System.out.println("last move: " + move[0] + ", " + move[1]);
curr = (curr == a) ? b : a;
}
//System.out.println(" \nGAME OVER ");
int status = Game.win(this.state);
// if (status == -1)
// {
// //System.out.println("DRAW!");
// }
// else
// {
// //System.out.println("Winner: " + (status == 2 ? "x" : "b"));
// }
//System.out.println(this);
return status;
}
public static void main(String [] args)
{
Game g = new Game();
for (int i = 0; i < 100; i++)
{
int status = g.play();
System.out.println(status);
}
}
public static char val(int[][] grid, int a, int b)
{
int item = grid[a][b];
if (item == 0)
{
return ' ';
}
else if (item == 1)
{
return 'o';
}
else
{
return 'x';
}
}
public static String gridView(int[][] grid)
{
String str = "";
str += " | | \n";
str += " " + val(grid, 0, 0) + " | " + val(grid, 0, 1) + " | " + val(grid, 0, 2) + " \n";
str += "___|___|___\n";
str += " | | \n";
str += " " + val(grid, 1, 0) + " | " + val(grid, 1, 1) + " | " + val(grid, 1, 2) + " \n";
str += "___|___|___\n";
str += " | | \n";
str += " " + val(grid, 2, 0) + " | " + val(grid, 2, 1) + " | " + val(grid, 2, 2) + " \n";
str += " | | \n";
return str;
}
public String toString()
{
return Game.gridView(state);
}
}