-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
206 lines (154 loc) · 6.67 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.util.Scanner;
public class main {
public static void main(String[] args) throws CloneNotSupportedException {
////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------- Program Start -------------------------------------------//
////////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------------------------//
// 1. Max running time ( user input, requirement <25 second ) //
// 2. Algorithm depth ( user input ) //
// 3. First hand ( user input, 'c'/'C': Computer AI, 'o'/'O': Opponent ) //
// 4. Initial board ( 8*8 empty board ) //
//------------------------------------------------------------------------------------------------//
System.out.println("*** Start Game Connect Four ***");
System.out.println();
System.out.print("Enter the maximum running time (this program is required in 25 seconds): ");
Scanner in_0 = new Scanner(System.in);
int time = in_0.nextInt();
System.out.print("Enter the depth: ");
Scanner in = new Scanner(System.in);
int depth = in.nextInt();
System.out.print("Who goes first ( 'C' for Computer AI, 'O' for Opponent ): ");
Scanner in2 = new Scanner(System.in);
char order = in2.next().charAt(0);
while(order != 'C' && order != 'O' && order != 'c' && order != 'o' ) {
System.out.print("Please enter a correct first hand ( 'C' for Computer AI, 'O' for Opponent ): ");
Scanner in3 = new Scanner(System.in);
order = in2.next().charAt(0);
}
System.out.println();
System.out.println(" -- Game Start --");
System.out.println("****************************");
System.out.println("| -Maximum time (sec): " + time + " |");
System.out.println("| -Game Depth: " + depth + " |");
if(order == 'C' || order == 'c') {
System.out.println("| -First Hand: " + "AI" + " |");
}
else {
System.out.println("| -First Hannd: " + "Human" + " |");
}
System.out.println("****************************");
System.out.println();
System.out.println("---Initial Board---");
MiniMaxAgent Agent = new MiniMaxAgent(depth);
Board board = new Board();
board.printBoard();
System.out.println();
//--------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------------------------- Random Move ----------------------------------------//
////////////////////////////////////////////////////////////////////////////////////////////////////
// AI first: For first two move
if (order == 'C' || order == 'c'){
pre_random_move preMove = new pre_random_move();
board = preMove.AI_first_move(board); // Random AI move
board = human_move(board); // human move
board = preMove.AI_second_move(board); // Random AI move
board = human_move(board); // human move
}
// human first + AI first move
if (order == 'O' || order == 'o'){
pre_random_move preMove = new pre_random_move();
board = human_move(board); // human move
board = preMove.AI_connect_move(board); // Random AI move
board = human_move(board); // human move
}
//--------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------------------------ Game Start ------------------------------------------//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Start playing
while(true) {
// check killer move
if(board.killer_check(board)) {
System.out.println("AI win");
break;
}
// check Pre-killer move
Boolean pre_killer_check = board.pre_killer_check(board);
if(pre_killer_check) {
System.out.println("in1");
board = board.prekiller_move(board, 'X');
}
// check Pre-killer move for opponent
Boolean pre_killer_defense_check = board.pre_killer_defense_check();
if(!pre_killer_check ) {
if(pre_killer_defense_check) {
board = board.prekiller_move(board, 'O');
}
}
// decide to defense or attack
Agent.setDepth(board.AttackDfence(board));
// Computer AI play
if (!pre_killer_check){
if(!pre_killer_defense_check) {
board = Agent.Action(board); // AI move
}
}
// check if AI win the game
if(board.isGoal('X')) {
System.out.println("AI win");
break;
}
// Human or opponent play
board = human_move(board); // Human move
// check if opponent win the game
if(board.isGoal('O')) {
System.out.println("Opponent win");
break;
}
// check if draw
if(board.isDraw()) {
System.out.println("Draw !");
break;
}
}
}
//--------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////////
//--------------------------------- Function for human move --------------------------------------//
////////////////////////////////////////////////////////////////////////////////////////////////////
// Function for human move
public static Board human_move(Board board) throws CloneNotSupportedException {
int row=0, col=0;
char c='O';
System.out.print("==================> Enter your next move: ");
Scanner in3 = new Scanner(System.in);
String temp = in3.nextLine();
char[] splitted = temp.toCharArray();
row = splitted[0]-97;
col = Integer.parseInt(String.valueOf(splitted[1]))-1;
System.out.print("here");
while(!board.inputcheck(board, row, col)) {
System.out.println();
System.out.println("The move you want is not valid !!!");
System.out.println();
System.out.print("==================> Enter your next move: ");
in3 = new Scanner(System.in);
temp = in3.nextLine();
splitted = temp.toCharArray();
row = splitted[0]-97;
col = Integer.parseInt(String.valueOf(splitted[1]));
}
board = board.generateSuccessor(c, row, col );
System.out.println();
System.out.println("----- OPPONENT ----");
board.printBoard();
System.out.println("Human move: " + splitted[0] + splitted[1]);
System.out.println();
return board;
}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
// End.