source: java/ecj/cecj/app/othello/OthelloBoard.java @ 28

Last change on this file since 28 was 28, checked in by mszubert, 16 years ago

cecj - coEvolutionary Computation in Java with additional games package

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 3.1 KB
Line 
1package cecj.app.othello;
2
3import java.util.Arrays;
4import games.Board;
5import games.Player;
6
7public class OthelloBoard implements Board {
8        private static final int BOARD_SIZE = 8;
9        private static final int BOARD_MARGIN = 2;
10       
11        private static final int EMPTY = -1;
12       
13        public static final int BLACK = 0;
14        public static final int WHITE = 1;
15       
16        public static final int NUM_DIRECTIONS = 8;
17        public static final int ROW_DIR[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
18        public static final int COL_DIR[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
19
20        private int board[][];
21
22        public OthelloBoard() {
23                board = new int[BOARD_SIZE + BOARD_MARGIN][BOARD_SIZE + BOARD_MARGIN];
24                reset();
25        }
26
27        private void initBoard() {
28                setPiece(4, 4, WHITE);
29                setPiece(4, 5, BLACK);
30                setPiece(5, 4, BLACK);
31                setPiece(5, 5, WHITE);
32        }
33
34        public int countPieces(int player) {
35                int count = 0;
36                for (int row = 1; row <= BOARD_SIZE; row++) {
37                        for (int col = 1; col <= BOARD_SIZE; col++) {
38                                if (getPiece(row, col) == player) {
39                                        count++;
40                                }
41                        }
42                }
43                return count;
44        }
45
46        public static int size() {
47                return BOARD_SIZE;
48        }
49
50        public int getPiece(int row, int col) {
51                return board[row][col];
52        }
53
54        public void setPiece(int row, int col, int player) {
55                board[row][col] = player;
56        }
57
58        public boolean isEmpty(int row, int col) {
59                return (getPiece(row, col) == EMPTY);
60        }
61
62        public int getPiece(int row, int col, int dir, int dist) {
63                return getPiece(row + dist * ROW_DIR[dir], col + dist * COL_DIR[dir]);
64        }
65
66        public void setPiece(int row, int col, int dir, int dist, int player) {
67                setPiece(row + dist * ROW_DIR[dir], col + dist * COL_DIR[dir], player);
68        }
69       
70        public OthelloMove getShiftedMove(int row, int col, int dir, int dist) {
71                return new OthelloMove(row + dist * ROW_DIR[dir], col + dist * COL_DIR[dir]);
72        }
73       
74        @Override
75        public String toString() {
76                StringBuilder builder = new StringBuilder();
77                builder.append("    ");
78                for (int i = 1; i <= BOARD_SIZE; i++) {
79                        builder.append(i + " ");
80                }
81                builder.append("\n");
82               
83                for (int i = 0; i < BOARD_SIZE + BOARD_MARGIN; i++) {
84                        if (i > 0 && i <= BOARD_SIZE) {
85                                builder.append(i + " ");
86                        } else {
87                                builder.append("  ");                           
88                        }
89                        for (int j = 0; j < BOARD_SIZE + BOARD_MARGIN; j++) {
90                                builder.append(((board[i][j] == -1) ? "-" : board[i][j]) + " ");
91                        }
92                        builder.append("\n");
93                }
94                return builder.toString();
95        }
96
97        public void reset() {
98                for (int row = 0; row < board.length; row++) {
99                        Arrays.fill(board[row], EMPTY);
100                }
101
102                initBoard();           
103        }
104       
105        public OthelloBoard clone() {
106                OthelloBoard clone = new OthelloBoard();
107                for (int row = 1; row <= BOARD_SIZE; row++) {
108                        for (int col = 1; col <= BOARD_SIZE; col++) {
109                                clone.board[row][col] = board[row][col];
110                        }
111                }
112                return clone;
113        }
114       
115        public double evaluate(Player player) {
116                double result = 0;
117                for (int row = 1; row <= BOARD_SIZE; row++) {
118                        for (int col = 1; col <= BOARD_SIZE; col++) {
119                                result += getValueAt(row, col) * player.getValue(row, col);
120                        }
121                }
122                return result;
123        }
124       
125        public int getValueAt(int row, int col) {
126                if (board[row][col] == OthelloBoard.BLACK) {
127                        return 1;
128                } else if (board[row][col] == OthelloBoard.WHITE) {
129                        return -1;
130                } else {
131                        return 0;
132                }
133        }
134}
Note: See TracBrowser for help on using the repository browser.