-
Notifications
You must be signed in to change notification settings - Fork 0
/
game1.java
129 lines (120 loc) · 3.21 KB
/
game1.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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class game1 {
player hum;
player comp;
mat m;
char board [][]= new char[3][3];
int tiles;
int turn = 0;
boolean end = false;
public game1(player hum, player comp, mat m, int turn, int tiles) {
this.tiles = tiles;//tiles played, once we hit 9 and no winner the game is a tie.
this.hum = hum;
this.comp = comp;
this.m = m;
this.turn = turn;
if(turn == 1) {
turn(hum);
}
else {
turn(comp);
}
}
private void turn(player curr) {
if(curr.getPlayer() == 1) {
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);
m.playPiece(c, curr);
tiles++;
turn = 2;
}
else if(curr.getPlayer() == 2) {
algo a = new algo(hum, comp, m);//generates best move (human, comp, current mat)
m = a.m;//grabs resulting map after move is played
tiles++;//increases tile count
turn = 1;// sets turn to human
}
win(curr);//checks win conditions
if(end == false) {
game1 g1 = new game1(hum,comp,m, turn,tiles);//generates new game obj with the updated data(human, comp, mat, turn, tiles)
}
}
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(m.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) && tiles == 9) {
System.out.println("TIE");
end = true;
}
else if(end == false) {
System.out.println("No win conditions met");
}
}
private Integer occupant(char c) {
Integer ans = 0;
if(c == hum.getPiece()) {
ans = 1;
}
else if(c == comp.getPiece()) {
ans = 2;
}
return ans;
}
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;
}
}