-
Notifications
You must be signed in to change notification settings - Fork 0
/
game2.java
186 lines (173 loc) · 4.72 KB
/
game2.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class game2 {
player hum;
player comp;
matA mA;
mat currM;
mat boards [][]= new mat[3][3];
int tiles;
int turn = 0;
boolean end = false;
public game2(player hum, player comp, matA mA, int turn, mat lastM) {//(human, comp, matA, turn, curr mat)
currM = lastM;
//this.tiles = tiles;//tiles played, once we hit 9 and no winner the game is a tie.
this.hum = hum;
this.comp = comp;
this.mA = mA;
this.turn = turn;
if(end != true) {
setup();
if(turn == 1) {
turn(hum);
}
else {
turn(comp);
}
}
}
private void turn(player curr) {
if(curr.getPlayer() == 1) {
while(currM == null) {
System.out.println("Which game board would you like to go on? Enter 1-9");
Scanner in = new Scanner(System.in);
int matIn = in.nextInt();
playBoard(matIn);
}
}
else if(curr.getPlayer() == 2) {
while(currM == null) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if(boards[i][j] != null) {
currM = boards[i][j];
}
}
}
}
}
playPiece(curr);
}
private void playPiece(player curr) {
if(curr.getPlayer() == 1) {
System.out.println("We are playing on board: " + currM.number);
System.out.println("What square would you like to go on? Enter 1-9");
Scanner in = new Scanner(System.in);
String input = in.nextLine();
char c = input.charAt(0);
currM.playPiece(c, curr);
mA.update(currM.number, currM);//mat number, mat
turn = 2;
}
else if(curr.getPlayer() == 2) {
algo2 a = new algo2(hum, comp, currM);//generates best move (human, comp, current mat)
currM = a.m;//grabs resulting map after move is played
mA.update(currM.number, currM);//mat number, mat
turn = 1;// sets turn to human
}
win(curr);//checks win conditions
if(end == false) {
game2 g2 = new game2(hum,comp,mA, turn, currM);
}
}
public void win(player curr) {
List<Integer> check = new ArrayList<Integer>();
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
counter++;
//System.out.println(m.board[i][j]);
if(currM.board[i][j] == curr.getPiece()) {//finds all the current player's tiles
check.add(counter);//adds the tiles to list
//System.out.println("adding"+ counter);
}
}
}
boolean winner = winCheck(check);
if(winner) {
if(curr.getPlayer() == 1) {
System.out.println("You Win");
end = true;
}
if(curr.getPlayer() == 2) {
System.out.println("You Lose");
end = true;
}
}
if((winner == false) && currM.tiles == 9) {
System.out.println("TIE BOARD");
currM.full = true;
mA.update(currM.number, currM);
currM = null;
}
if(end == false) {
System.out.println("No win conditions met");
}
}
private boolean winCheck(List<Integer> check) {
for (int i = 0; i < check.size(); i++) {
System.out.println(check.get(i));
}
System.out.println("turn ended checking winner");
boolean res = false;
if(check.contains(1) && check.contains(2) && check.contains(3)) {
res = true;
}
else if(check.contains(4) && check.contains(5) && check.contains(6)) {
res = true;
}
else if(check.contains(7) && check.contains(8) && check.contains(9)) {
res = true;
}
else if(check.contains(1) && check.contains(4) && check.contains(7)) {
res = true;
}
else if(check.contains(2) && check.contains(5) && check.contains(8)) {
res = true;
}
else if(check.contains(3) && check.contains(6) && check.contains(9)) {
res = true;
}
else if(check.contains(1) && check.contains(5) && check.contains(9)) {
res = true;
}
else if(check.contains(3) && check.contains(5) && check.contains(7)) {
res = true;
}
return res;
}
private void playBoard(int matIn) {
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
counter++;
if(matIn == counter) {
if(boards[i][j] != null) {
currM = boards[i][j];
}
else {
System.out.println("Invalid board");
}
}
}
}
}
private void setup() {
int counter = 0;
String temp = "Boards left: ";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
counter++;
if(!mA.masterB[i][j].full){
boards[i][j] = mA.masterB[i][j];
temp+=Integer.toString(counter)+", ";
}
else {
boards[i][j] = null;
}
}
}
System.out.println(temp);
}
}