-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
157 lines (128 loc) · 5.82 KB
/
Copy pathMain.java
File metadata and controls
157 lines (128 loc) · 5.82 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
import java.util.Scanner;
import static java.lang.Thread.sleep;
/**
* Othello - MiniMax
*
* An Othello game that uses the MiniMax algorithm with a-b pruning and allows for Human vs AI.
*
* @authors: P3200005, P3210255, P3200262
* @info: Made for the course of Artificial Intelligence @ AUEB 2022-2023
**/
public class Main
{
public static void main(String[] args){
intro(); // Intro Message
int gameMode = getGameMode(); // Get the desired mode of the game
int depth = 0;
Board board = new Board();
PlayerAB black, white;
if(gameMode == 1){
depth = getDepth(); // Get the desired depth of the tree
boolean playFirst = playFirst();
black = new PlayerAB(depth, Board.B, !playFirst);
white = new PlayerAB(depth, Board.W, playFirst);
} else if (gameMode == 2){
depth = getDepth(); // Get the desired depth of the tree
black = new PlayerAB(depth, Board.B, true);
white = new PlayerAB(depth, Board.W, true);
} else {
black = new PlayerAB(depth, Board.B, false);
white = new PlayerAB(depth, Board.W, false);
}
boolean canPlayW, canPlayB;
canPlayB = canPlayW = true;
System.out.println("\nThe game has started! This is the starting board:\n");
board.print();
while(!board.isFull() && (canPlayW || canPlayB)) {
switch (board.getLastPlayer()) {
case Board.W -> {
if (canPlayB) {
System.out.println("\nBlack's Turn");
Move move = black.move(board);
try {
if(black.isAI() && depth <= 9)
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Black's move: " + (move.getRow() + 1) + " " + (move.getCol() + 1));
board.placeDisk(move.getRow(), move.getCol(), Board.B);
} else {
System.out.println("\nBlack cannot play! Skipping turn...");
}
canPlayW = board.canPlay(Board.W);
board.setLastPlayer(Board.B);
}
case Board.B -> {
if (canPlayW) {
System.out.println("\nWhite's Turn");
Move move = white.move(board);
try {
if(white.isAI() && depth <= 9)
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("White's move: " + (move.getRow() + 1) + " " + (move.getCol() + 1));
board.placeDisk(move.getRow(), move.getCol(), Board.W);
} else {
System.out.println("\nWhite cannot play! Skipping turn...");
}
board.setLastPlayer(Board.W);
canPlayB = board.canPlay(Board.B);
}
}
board.print();
}
board.printWinner();
board.printScore();
}
public static void intro() {
System.out.println("Welcome to Othello!");
System.out.println("The game is played on an 8x8 board. You will be matched against the computer.");
System.out.println("The player with the black disks goes first and the player with the white disks goes second.");
}
public static int getGameMode() {
Scanner scanner = new Scanner(System.in);
int gameMode;
System.out.println("\nChoose the game mode:");
System.out.println("1. Human vs AI");
System.out.println("2. AI vs AI");
System.out.println("3. Human vs Human");
do {
System.out.println("Give game mode: ");
try{
gameMode = Integer.parseInt(scanner.nextLine());
} catch (Exception e) {
gameMode = 0;
System.out.println("Invalid input! Please try again.");
}
} while (gameMode != 1 && gameMode != 2 && gameMode != 3);
return gameMode;
}
public static boolean playFirst(){
Scanner scanner = new Scanner(System.in);
System.out.println("\nDo you want to play first with the black disks? (y/n)");
String answer = scanner.nextLine();
while(!answer.equals("y") && !answer.equals("n")) {
System.out.println("Invalid answer. Please try again.");
System.out.println("Do you want to play first with the black disks? (y/n)");
answer = scanner.nextLine();
}
return answer.equals("y");
}
public static int getDepth() {
Scanner scanner = new Scanner(System.in);
int depth;
do {
System.out.println("\nPlease pick the maximum depth for the AI.\nRecommendations:\n(2) Easy\n(6) Medium\n(10) Hard");
try{
depth = Integer.parseInt(scanner.nextLine());
} catch (Exception e) {
depth = 0;
}
} while (depth < 1);
System.out.println("\nYou have chosen a depth of " + depth + ". Best of luck!");
return depth;
}
}