KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > gp > tictactoe > Board


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package examples.gp.tictactoe;
11
12 import java.util.*;
13
14 /**
15  * A Tic Tac Toe board (3x3).
16  *
17  * @author Klaus Meffert
18  * @since 3.2
19  */

20 public class Board {
21   private int[][] m_board;
22
23   private int m_lastColor;
24
25   public static int WIDTH = 2;
26
27   public static int HEIGHT = WIDTH;
28
29   private int movesInRound;
30   private int movesInTurn;
31   private Map m_readPositions;
32   private int m_readPositionCount;
33
34   public Board() {
35     m_board = new int[WIDTH][HEIGHT];
36     m_readPositions = new Hashtable();
37     startNewRound();
38   }
39
40   public void resetBoard() {
41     for (int x = 0; x < WIDTH; x++) {
42       for (int y = 0; y < HEIGHT; y++) {
43         m_board[x][y] = 0;
44       }
45     }
46   }
47
48   public void startNewRound() {
49     m_lastColor = 0;
50     movesInRound = 0;
51     resetBoard();
52   }
53
54   public void beginTurn() {
55     movesInTurn = 0;
56     m_readPositionCount = 0;
57     m_readPositions.clear();
58   }
59
60   public void endTurn() {
61     if (movesInTurn != 1) {
62       throw new IllegalStateException JavaDoc("One stone must be set by player!");
63     }
64 // System.out.println("*** Valid turn!");
65
}
66
67   public void endRound() {
68     if (m_lastColor == 0) {
69       throw new IllegalStateException JavaDoc("No stone set within the round!");
70     }
71     if (movesInRound != 2) {
72       throw new IllegalStateException JavaDoc("Both players have to move."
73                                       + " Moves registered: "
74                                       + movesInRound);
75     }
76     System.out.println("*** Valid round!");
77   }
78
79   public boolean putStone(int x, int y, int a_color)
80       throws IllegalArgumentException JavaDoc {
81     if (x < 1 || y < 1
82         || x > WIDTH || y > HEIGHT) {
83       throw new IllegalArgumentException JavaDoc("x and y must be between 1..3");
84     }
85     if (m_board[x - 1][y - 1] != 0) {
86       throw new IllegalArgumentException JavaDoc("Position already occupied");
87     }
88     if (a_color != 1 && a_color != 2) {
89       throw new IllegalArgumentException JavaDoc("Color must be 1 or 2");
90     }
91     if (m_lastColor == a_color) {
92       throw new IllegalArgumentException JavaDoc("Oponent must move first");
93     }
94     m_lastColor = a_color;
95     m_board[x - 1][y - 1] = a_color;
96     movesInTurn++;
97     movesInRound++;
98     if (isEndOfGame()) {
99       return true;
100     }
101     return false;
102   }
103
104   public int readField(int x, int y)
105       throws IllegalArgumentException JavaDoc {
106     if (x < 1 || y < 1
107         || x > WIDTH || y > HEIGHT) {
108       throw new IllegalArgumentException JavaDoc("x and y must be between 1.." + WIDTH);
109     }
110     if (m_readPositions.get(x+"_"+y) == null) {
111       m_readPositionCount++;
112       m_readPositions.put(x + "_" + y, "jgap");
113     }
114     return m_board[x - 1][y - 1];
115   }
116
117   public int getReadPositionCount() {
118     return m_readPositionCount;
119   }
120
121   public int readField(int a_index)
122       throws IllegalArgumentException JavaDoc {
123     if (a_index < 1 || a_index > (WIDTH * HEIGHT)) {
124       throw new IllegalArgumentException JavaDoc("Index must be between 1.." +
125           (WIDTH * HEIGHT));
126     }
127     int x = (a_index - 1) / WIDTH;
128     int y = (a_index - 1) % WIDTH;
129     return m_board[x][y];
130   }
131
132   public boolean isEndOfGame() {
133     for (int y = 0; y < HEIGHT; y++) {
134       if (isWinner(0, WIDTH-1, y, y)) {
135         return true;
136       }
137     }
138     for (int x = 0; x < HEIGHT; x++) {
139       if (isWinner(x, x, 0, HEIGHT-1)) {
140         return true;
141       }
142     }
143     if (isWinnerDiag(0, 0, 1)) {
144       return true;
145     }
146     if (isWinnerDiag(WIDTH-1, 0, -1)) {
147       return true;
148     }
149     return false;
150   }
151
152   private boolean isWinner(int a_firstX, int a_lastX, int a_firstY, int a_lastY) {
153     int count = 0;
154     int lastcolor = -1;
155     for (int x = a_firstX; x <= a_lastX; x++) {
156       for (int y = a_firstY; y <= a_lastY; y++) {
157         int color = m_board[x][y];
158         if (color == 0) {
159           return false;
160         }
161         if (lastcolor == -1) {
162           lastcolor = color;
163           count++;
164         }
165         else {
166           if (lastcolor != color) {
167             return false;
168           }
169           else {
170             count++;
171           }
172         }
173       }
174     }
175     return true;
176   }
177
178   private boolean isWinnerDiag(int a_startX, int a_startY, int a_increment) {
179     int count = 0;
180     int lastcolor = -1;
181     int x = a_startX;
182     int y = a_startY;
183     while (count < WIDTH) {
184       int color = m_board[x][y];
185       if (color == 0) {
186         return false;
187       }
188       if (lastcolor == -1) {
189         lastcolor = color;
190         count++;
191       }
192       else {
193         if (lastcolor != color) {
194           return false;
195         }
196         else {
197           count++;
198         }
199       }
200       x = x + a_increment;
201       y = y + 1;
202     }
203     return true;
204   }
205
206   public int getLastColor() {
207     return m_lastColor;
208   }
209 }
210
Popular Tags