-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.java
More file actions
167 lines (103 loc) · 2.95 KB
/
tictactoe.java
File metadata and controls
167 lines (103 loc) · 2.95 KB
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
import java.util.ArrayList;
import java.util.Scanner;
public class tictactoe {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Select a game mode; ");
System.out.println("1. Human Vs Human ");
System.out.println("2. Human Vs Computer ");
System.out.println("3. Simulate AI players ");
int gameMode = keyboard.nextInt();
switch (gameMode){
case 1:
HumanVsHuman();
break;
case 2:
HumanVsAI();
break;
case 3:
AIsimulation();
}
keyboard.close();
}
public static void checkStatus(XandOGame game){
if(game.checkGameStatus() == 0){
System.out.println("the game is a draw ");
game.printGameBoard();
}
else {
System.out.println((char)game.checkGameStatus() + " won the game");
game.printGameBoard();
}
}
public static void checkStatus2(XandOGame game){
if (game.checkGameStatus() != 0){
System.out.println((char)game.checkGameStatus() + " has won the game");
game.printGameBoard();
}
}
public static void HumanVsHuman(){
XandOGame game = new XandOGame();
String player1name = "player1";
char player1sym = 'x';
String player2name = "player2";
char player2sym = 'o';
Player player1 = new HumanPlayer(player1name, player1sym);
Player player2 = new HumanPlayer(player2name, player2sym);
gameplay(game, player1, player2);
}
public static void HumanVsAI(){
XandOGame game = new XandOGame();
String aiplayerName = "aiplayer";
char aiplayersym = 'x';
String humanPlayerName = "humanplayer";
char humanplayersym = 'o';
Player aiplayer = new AiPlayer(aiplayerName, aiplayersym);
Player humanplayer = new HumanPlayer(humanPlayerName, humanplayersym);
gameplay(game, aiplayer, humanplayer);
}
public static void AIsimulation(){
XandOGame game = new XandOGame();
String aiplayer1Name = "aiplayer1";
char aiplayer1sym = 'x';
String aiplayer2Name = "aiplayer2";
char aiplayer2sym = 'o';
Player aiplayer1 = new AiPlayer(aiplayer1Name, aiplayer1sym);
Player aiplayer2 = new AiPlayer(aiplayer2Name, aiplayer2sym);
gameplay(game, aiplayer1, aiplayer2);
}
public static void gameplay(XandOGame game, Player player0, Player player1){
ArrayList<Integer> cellsLeft = game.getAvailableCells();
int gameStatus = 0;
int numberOfCellsLeft = cellsLeft.size();
int count = 0;
while(0 < numberOfCellsLeft){
if(numberOfCellsLeft == 1){
player0.play(game);
numberOfCellsLeft--;
checkStatus(game);
break;
}
if (numberOfCellsLeft > 1){
if (game.checkGameStatus() != 0 && count > 4){
checkStatus2(game);
break;
}
player0.play(game);
count++;
numberOfCellsLeft--;
if(game.checkGameStatus() > 0){
checkStatus2(game);
break;
};
player1.play(game);
count++;
numberOfCellsLeft--;
if(game.checkGameStatus() > 0){
checkStatus2(game);
break;
};
}
}
}
}