1 | package cecj.app.othello; |
---|
2 | |
---|
3 | import games.Board; |
---|
4 | import games.BoardGame; |
---|
5 | import games.GameMove; |
---|
6 | import games.Player; |
---|
7 | |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | public class OthelloGame implements BoardGame { |
---|
12 | |
---|
13 | private static final int NUM_PLAYERS = 2; |
---|
14 | |
---|
15 | private int currentPlayer; |
---|
16 | private OthelloBoard board; |
---|
17 | |
---|
18 | public OthelloGame(OthelloBoard board) { |
---|
19 | this.board = board; |
---|
20 | } |
---|
21 | |
---|
22 | public boolean endOfGame() { |
---|
23 | for (int row = 1; row <= OthelloBoard.size(); row++) { |
---|
24 | for (int col = 1; col <= OthelloBoard.size(); col++) { |
---|
25 | if (canPlace(row, col, currentPlayer) |
---|
26 | || canPlace(row, col, getOpponent(currentPlayer))) { |
---|
27 | return false; |
---|
28 | } |
---|
29 | } |
---|
30 | } |
---|
31 | return true; |
---|
32 | } |
---|
33 | |
---|
34 | public double evalMove(Player player, GameMove move) { |
---|
35 | List<Integer> directions = findDirections(move.getRow(), move.getCol(), currentPlayer); |
---|
36 | |
---|
37 | float result = 0; |
---|
38 | for (int dir : directions) { |
---|
39 | int dist = 1; |
---|
40 | while (board.getPiece(move.getRow(), move.getCol(), dir, dist) == getOpponent(currentPlayer)) { |
---|
41 | GameMove shifted = board.getShiftedMove(move.getRow(), move.getCol(), dir, dist); |
---|
42 | result += 2 * player.getValue(shifted.getRow(), shifted.getCol()); |
---|
43 | dist++; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | return result + player.getValue(move.getRow(), move.getCol()); |
---|
48 | } |
---|
49 | |
---|
50 | public List<? extends GameMove> findMoves() { |
---|
51 | List<OthelloMove> moves = new ArrayList<OthelloMove>(); |
---|
52 | for (int row = 1; row <= OthelloBoard.size(); row++) { |
---|
53 | for (int col = 1; col <= OthelloBoard.size(); col++) { |
---|
54 | if (canPlace(row, col, currentPlayer)) { |
---|
55 | moves.add(new OthelloMove(row, col)); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | return moves; |
---|
60 | } |
---|
61 | |
---|
62 | public void makeMove(GameMove move) { |
---|
63 | List<Integer> directions = findDirections(move.getRow(), move.getCol(), currentPlayer); |
---|
64 | for (int dir : directions) { |
---|
65 | int dist = 1; |
---|
66 | board.setPiece(move.getRow(), move.getCol(), currentPlayer); |
---|
67 | while (board.getPiece(move.getRow(), move.getCol(), dir, dist) == getOpponent(currentPlayer)) { |
---|
68 | board.setPiece(move.getRow(), move.getCol(), dir, dist, currentPlayer); |
---|
69 | dist++; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | List<Integer> findDirections(int row, int col, int player) { |
---|
75 | List<Integer> directions = new ArrayList<Integer>(); |
---|
76 | |
---|
77 | if (board.isEmpty(row, col)) { |
---|
78 | for (int dir = 0; dir < OthelloBoard.NUM_DIRECTIONS; dir++) { |
---|
79 | int dist = 1; |
---|
80 | while (board.getPiece(row, col, dir, dist) == getOpponent(player)) { |
---|
81 | dist++; |
---|
82 | } |
---|
83 | if (dist > 1 && board.getPiece(row, col, dir, dist) == player) { |
---|
84 | directions.add(dir); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | return directions; |
---|
89 | } |
---|
90 | |
---|
91 | boolean canPlace(int row, int col, int curPlayer) { |
---|
92 | return (!findDirections(row, col, curPlayer).isEmpty()); |
---|
93 | } |
---|
94 | |
---|
95 | int getOpponent(int player) { |
---|
96 | return ((player + 1) % NUM_PLAYERS); |
---|
97 | } |
---|
98 | |
---|
99 | public void switchPlayer() { |
---|
100 | currentPlayer = getOpponent(currentPlayer); |
---|
101 | } |
---|
102 | |
---|
103 | public int getCurrentPlayer() { |
---|
104 | return currentPlayer; |
---|
105 | } |
---|
106 | |
---|
107 | public int getOutcome() { |
---|
108 | return (board.countPieces(0) - board.countPieces(1)); |
---|
109 | } |
---|
110 | |
---|
111 | public void reset() { |
---|
112 | board.reset(); |
---|
113 | currentPlayer = 0; |
---|
114 | } |
---|
115 | |
---|
116 | public Board getBoard() { |
---|
117 | return board; |
---|
118 | } |
---|
119 | } |
---|