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

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
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        @Override
106        public OthelloBoard clone() {
107                OthelloBoard clone = new OthelloBoard();
108                for (int row = 1; row <= BOARD_SIZE; row++) {
109                        for (int col = 1; col <= BOARD_SIZE; col++) {
110                                clone.board[row][col] = board[row][col];
111                        }
112                }
113                return clone;
114        }
115       
116        public double evaluate(Player player) {
117                double result = 0;
118                for (int row = 1; row <= BOARD_SIZE; row++) {
119                        for (int col = 1; col <= BOARD_SIZE; col++) {
120                                result += getValueAt(row, col) * player.getValue(row, col);
121                        }
122                }
123                return result;
124        }
125       
126        public int getValueAt(int row, int col) {
127                if (board[row][col] == OthelloBoard.BLACK) {
128                        return 1;
129                } else if (board[row][col] == OthelloBoard.WHITE) {
130                        return -1;
131                } else {
132                        return 0;
133                }
134        }
135}
Note: See TracBrowser for help on using the repository browser.