-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReversiModel.java
111 lines (102 loc) · 2.98 KB
/
ReversiModel.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
package com.mizo0203.shiki.domain.model;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
public class ReversiModel {
public static final int A = 1;
public static final int B = 2;
public static final int C = 3;
public static final int D = 4;
public static final int E = 5;
public static final int F = 6;
public static final int G = 7;
public static final int H = 8;
private static final int PERIMETER = 8;
private final Pieces[][] board = new Pieces[10][10];
private final int[] dx_array = new int[] {-1, 0, 1, -1, 1, -1, 0, 1};
private final int[] dy_array = new int[] {-1, -1, -1, 0, 0, 1, 1, 1};
public boolean play(@Min(1) @Max(8) int x, @Min(1) @Max(8) int y, Pieces pieces) {
if (reversePieces(x, y, pieces) == 0) {
return false;
}
board[y][x] = pieces;
return true;
}
public void setPieces(@Min(1) @Max(8) int x, @Min(1) @Max(8) int y, Pieces pieces) {
board[y][x] = pieces;
}
@NotNull
public Pieces getPieces(@Min(1) @Max(8) int x, @Min(1) @Max(8) int y) {
return board[y][x];
}
public void reset() {
for (Pieces[] piecesLine : board) {
Arrays.fill(piecesLine, Pieces.EMPTY);
}
board[4][D] = Pieces.WHITE;
board[4][E] = Pieces.BLACK;
board[5][D] = Pieces.BLACK;
board[5][E] = Pieces.WHITE;
}
private int reversePieces(@Min(1) @Max(8) int x, @Min(1) @Max(8) int y, Pieces oneself) {
if (board[y][x] != Pieces.EMPTY) {
return 0;
}
int cnt = 0;
Pieces opponent = oneself.getOpponent();
for (int i = 0; i < PERIMETER; i++) {
int dy = dy_array[i];
int dx = dx_array[i];
int d = 1;
while (board[y + (dy * d)][x + (dx * d)] == opponent) {
d++;
}
if (board[y + (dy * d)][x + (dx * d)] == oneself) {
for (int j = 1; j < d; j++) {
board[y + (dy * j)][x + (dx * j)] = oneself;
cnt++;
}
}
}
return cnt;
}
private boolean canReversePieces(@Min(1) @Max(8) int x, @Min(1) @Max(8) int y, Pieces oneself) {
if (board[y][x] != Pieces.EMPTY) {
return false;
}
Pieces opponent = oneself.getOpponent();
for (int i = 0; i < PERIMETER; i++) {
int dy = dy_array[i];
int dx = dx_array[i];
int d = 1;
while (board[y + (dy * d)][x + (dx * d)] == opponent) {
d++;
}
if (board[y + (dy * d)][x + (dx * d)] == oneself) {
if (1 < d) {
return true;
}
}
}
return false;
}
public Pieces nextPieces(Pieces oneself) {
Pieces opponent = oneself.getOpponent();
for (int y = 1; y <= 8; y++) {
for (int x = 1; x <= 8; x++) {
if (canReversePieces(x, y, opponent)) {
return opponent;
}
}
}
for (int y = 1; y <= 8; y++) {
for (int x = 1; x <= 8; x++) {
if (canReversePieces(x, y, oneself)) {
return oneself;
}
}
}
return null;
}
}