-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShogiMain.java
124 lines (105 loc) · 3.67 KB
/
ShogiMain.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
import actions.*;
import pieces.*;
import java.lang.*;
import java.util.*;
/**
* Main file for running the Shogi Japanese Chess game. Game is played with 2 players
* on a 5x5 board. Best of luck!
*/
public class ShogiMain {
public static void main(String[] args) {
if (args.length == 0 || (!args[0].equals("-i"))) {
usage();
return;
}
interactive(new Board());
}
// Running the the game in interactively
private static void interactive(Board board) {
Scanner input = new Scanner(System.in);
while (!board.gameOver()) {
// Print state of board
printState(board);
String player = board.getCurrentPlayer();
String otherPlayer = board.getOtherPlayer();
// Detect check/checkmate
printCheck(board, false);
// Prompt and get action
System.out.print(player + ">");
String line = input.nextLine();
Action action = parseAction(line);
// Move piece or end via illegal move
boolean succeeded = board.makeAction(action);
if (succeeded) {
System.out.println(player + " player action: " + action);
} else {
System.out.println(otherPlayer + " player wins. Illegal move.");
}
}
input.close();
}
// Displays checkmate or prints move to escape check
private static void printCheck(Board board, boolean print) {
// Detect check/checkmate
if (board.inCheck()) {
List<Action> possibleMoves = board.escapeCheck();
if (possibleMoves.isEmpty()) {
if (print) {
System.out.println(board.getOtherPlayer() + " player wins. Checkmate.");
}
} else {
System.out.println(board.getCurrentPlayer() + " player is in check!");
System.out.println("Available moves:");
for (Action option : possibleMoves) {
System.out.println(option);
}
}
}
}
// Prints the game state of the board
private static void printState(Board board) {
// Print board state
System.out.println(board);
System.out.println("Captures UPPER:" + formatList(board.capturesUpper()));
System.out.println("Captures lower:" + formatList(board.capturesLower()));
System.out.println();
}
// Re-prompt the user due to input error
private static void usage() {
System.out.println("Usage: myShogi -[i]");
}
// Returns Action object representing player input
private static Action parseAction(String line) {
line = line.trim();
String[] split = line.split(" ");
if (split[0].equals("move")) {
// Promote piece
if (split.length == 4 && split[3].equals("promote")) {
return new Move(split[1], split[2], true);
}
return new Move(split[1], split[2]);
} else if (split[0].equals("drop")) {
return new Drop(split[1], split[2]);
}
return null;
}
// Returns String of reason for game end
private static String getEndMessage(Board board) {
if (board.isIllegalMove()) {
return board.getOtherPlayer() + " player wins. Illegal move.";
} else if (board.isCheckMated()) {
return board.getOtherPlayer() + " player wins. Checkmate.";
} else {
return "Tie game. Too many moves.";
}
}
// Returns String formatting the output of Piece captures
private static String formatList(List<Piece> pieceList) {
if (pieceList.isEmpty()) {
return "";
}
String list = pieceList.toString();
list = list.replaceAll(",", "");
return " " + list.substring(1, list.length()-1);
}
}