Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
148 lines (135 loc) · 5.67 KB
/
Copy pathMain.java
File metadata and controls
148 lines (135 loc) · 5.67 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
import engine.Board;
import engine.MoveGenerator;
import ai.Searcher;
import ui.ConsoleUtils;
import ui.ChessNotationParser;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Board board = new Board();
List<String> history = new ArrayList<>();
String lastMove = "";
int moveNumber = 1;
ConsoleUtils.clearScreen();
ConsoleUtils.printWelcomeHeader();
ConsoleUtils.printGameBoard(board);
ConsoleUtils.printGameStatus(board, lastMove, moveNumber);
while (true) {
// Check for checkmate or stalemate
if (MoveGenerator.isInCheckmate(board)) {
String winner = board.whiteToMove ? "Black" : "White";
System.out.println("⚔️ CHECKMATE! " + winner + " wins!");
break;
}
if (MoveGenerator.isInStalemate(board)) {
System.out.println("🤝 STALEMATE! The game is a draw.");
break;
}
String input = ConsoleUtils.getPlayerInput(scanner);
if (input.equalsIgnoreCase("quit")) {
System.out.println("Thanks for playing!");
break;
}
String normalized = input.toLowerCase();
if (normalized.equals("help")) {
ConsoleUtils.printHelp();
continue;
}
if (normalized.equals("print")) {
ConsoleUtils.clearScreen();
ConsoleUtils.printGameBoard(board);
ConsoleUtils.printGameStatus(board, lastMove, moveNumber);
continue;
}
if (normalized.equals("moves")) {
ConsoleUtils.printAvailableMoves(board);
continue;
}
if (normalized.equals("history")) {
if (history.isEmpty()) {
System.out.println("No moves have been played yet.");
} else {
System.out.println("Move history: " + String.join(" ", history));
}
continue;
}
if (normalized.equals("eval")) {
ConsoleUtils.printEvaluationSummary(board);
continue;
}
if (normalized.equals("ai")) {
System.out.println("AI is thinking...");
String aiMove = Searcher.findBestMove(board, 4);
if (aiMove != null && board.makeMove(aiMove)) {
history.add(aiMove);
lastMove = aiMove;
moveNumber++;
ConsoleUtils.clearScreen();
ConsoleUtils.printGameBoard(board);
ConsoleUtils.printGameStatus(board, lastMove, moveNumber);
} else {
System.out.println("AI could not find a legal move.");
}
continue;
}
// Try to parse as chess notation first, then fall back to UCI
String moveToPlay = null;
// Check if it's already valid UCI format
if (board.isPseudoLegal(input)) {
moveToPlay = input;
} else {
// Try to parse as standard chess notation
String parsedMove = ChessNotationParser.parseNotationToUCI(input, board);
if (parsedMove != null && board.isPseudoLegal(parsedMove)) {
moveToPlay = parsedMove;
}
}
if (moveToPlay == null) {
System.out.println("❌ Invalid move. Use chess notation (e4, Nf3, O-O) or UCI format (e2e4).\n");
ConsoleUtils.printAvailableMoves(board);
continue;
}
if (!board.makeMove(moveToPlay)) {
System.out.println("❌ That move is illegal in the current position.\n");
ConsoleUtils.printAvailableMoves(board);
continue;
}
history.add(moveToPlay);
lastMove = moveToPlay;
moveNumber++;
ConsoleUtils.clearScreen();
ConsoleUtils.printGameBoard(board);
ConsoleUtils.printGameStatus(board, lastMove, moveNumber);
// AUTO-PLAY: AI responds for Black immediately
if (!board.whiteToMove) { // It's now Black's turn (AI's turn)
// Check for checkmate or stalemate before AI move
if (MoveGenerator.isInCheckmate(board)) {
String winner = board.whiteToMove ? "Black" : "White";
System.out.println("\n⚔️ CHECKMATE! " + winner + " wins!");
break;
}
if (MoveGenerator.isInStalemate(board)) {
System.out.println("\n🤝 STALEMATE! The game is a draw.");
break;
}
System.out.println("\nAI is thinking...");
String aiMove = Searcher.findBestMove(board, 4);
if (aiMove != null && board.makeMove(aiMove)) {
history.add(aiMove);
lastMove = aiMove;
moveNumber++;
ConsoleUtils.clearScreen();
ConsoleUtils.printGameBoard(board);
ConsoleUtils.printGameStatus(board, lastMove, moveNumber);
System.out.println("AI played: " + aiMove);
} else {
System.out.println("❌ AI could not find a legal move.");
}
}
}
scanner.close();
}
}