-
Notifications
You must be signed in to change notification settings - Fork 2
/
Game.java
executable file
·129 lines (108 loc) · 3.43 KB
/
Game.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
package net.exkazuu.battle2048.game;
import net.exkazuu.gameaiarena.api.Direction4;
import net.exkazuu.gameaiarena.api.Point2;
import java.util.List;
public class Game {
private static final int BOARD_NUM_ROWS = 5;
private static final int BOARD_NUM_COLS = 5;
private static final int MAX_TURNS = 1000 * 2;
private final Board[] _boards;
private int _turn;
private int[] _timeLeft;
private GameResult _result;
public Game() {
_boards = new Board[]{
new Board(BOARD_NUM_COLS, BOARD_NUM_ROWS),
new Board(BOARD_NUM_COLS, BOARD_NUM_ROWS),
};
_timeLeft = new int[2];
}
public DefeatReason advanceInitialPhase(int playerIndex, Point2 p) {
Board opponentBoard = getOpponentBoard(playerIndex);
if (!opponentBoard.withinBounds(p)) {
return DefeatReason.INVALID_POSITION_AT_INIT;
}
opponentBoard.setExponent(1, p);
return null;
}
public DefeatReason advanceMainAndAttackPhase(int playerIndex, Direction4 direction, int count, int exponent, List<Point2> ps) {
_turn++;
Board myBoard = getMyBoard(playerIndex);
int countMerged = myBoard.move(direction);
if (countMerged == -1) {
return DefeatReason.CANNOT_MOVE_AT_MAIN;
}
Board opponentBoard = getOpponentBoard(playerIndex);
if ((1 << (countMerged + 1)) != (1 << exponent) * count) {
return DefeatReason.INVALID_M_AND_V_AT_ATTACK;
}
if (count != ps.size()) {
return DefeatReason.INVALID_NUMBER_OF_R_AND_C_AT_ATTACK;
}
for (Point2 p : ps) {
if (!opponentBoard.withinBounds(p)) {
return DefeatReason.INVALID_POSITION_AT_ATTACK;
}
}
for (Point2 p : ps) {
if (!opponentBoard.tileAvailable(p)) {
return DefeatReason.ALREADY_OCCUPIED_AT_ATTACK;
}
opponentBoard.setExponent(exponent, p);
}
return null;
}
public boolean finished() {
return _turn >= MAX_TURNS || _result != null;
}
public Board getMyBoard(int playerIndex) {
return _boards[playerIndex];
}
public Board getOpponentBoard(int playerIndex) {
return _boards[getOpponentIndex(playerIndex)];
}
public int getOpponentIndex(int playerIndex) {
return (playerIndex + 1) & 1;
}
public GameResult getGameResult() {
return _result;
}
public int getTurn() {
return _turn;
}
public int getMyTimeLeft(int playerIndex) {
return _timeLeft[playerIndex];
}
public void setMyTimeLeft(int playerIndex, int value) {
_timeLeft[playerIndex] = value;
}
public String generateMainInput(int playerIndex) {
return String.valueOf(getTurn() / 2 + 1) +
' ' +
getMyTimeLeft(playerIndex) +
' ' +
getMyBoard(playerIndex).getScore() +
' ' +
getOpponentBoard(playerIndex).getScore() +
'\n' +
getMyBoard(playerIndex) +
'\n' +
getOpponentBoard(playerIndex);
}
public GameResult lose(int playerIndex, DefeatReason reason) {
_result = new GameResult(getOpponentIndex(playerIndex), reason);
return _result;
}
public GameResult judge() {
int score1 = getMyBoard(0).getScore();
int score2 = getMyBoard(1).getScore();
if (score1 > score2) {
_result = new GameResult(getOpponentIndex(0), DefeatReason.LESS_SCORE_AT_END);
} else if (score1 < score2) {
_result = new GameResult(getOpponentIndex(1), DefeatReason.LESS_SCORE_AT_END);
} else {
_result = new GameResult(getOpponentIndex(1), DefeatReason.FIRST_BUT_SAME_SCORE_AT_END);
}
return _result;
}
}