/* Bridge-It Game */ // Modified from the original by JD Nov 2001 import java.awt.*; import java.io.*; public class Bridges extends java.awt.Frame { // Constants public static final int MAXSCREENX = 340; public static final int MAXSCREENY = 340; public static final int MAXROWS = 17; public static final int MAXCOLS = 17; public static final int NOTOWER = 0; public static final int BLUETOWER = 1, BLUE = 1; public static final int REDTOWER = 2, RED = 2; public static final int ROW = 0; public static final int COL = 1; public static final int UP = 1; public static final int RIGHT = 2; public static final int LEFT = 3; public static final int DOWN = 4; // global variables static BridgeCanvas cnv1; static Label turnMsg, statusMsg; Button newGameButton; Panel p, p1; public static int board[][], whoseTurn; static boolean redWins, blueWins, debug; // Constructor public Bridges(String title) { super (title); init(); } public void init() { setLayout(null); resize(MAXSCREENX,MAXSCREENY+60); addNotify(); debug = false; cnv1 = new BridgeCanvas(); add(cnv1); cnv1.reshape(0,20,MAXSCREENX, MAXSCREENY); p = new Panel(); turnMsg = new Label(""); turnMsg.setAlignment(Label.CENTER); p.add(turnMsg); whoseTurn = BLUE; toggleTurnMsg(); p.reshape(0,MAXSCREENY+20,MAXSCREENX,30); add(p); p1 = new Panel(); newGameButton = new Button("New Game"); p1.add(newGameButton); if (debug) { statusMsg = new Label(""); statusMsg.setAlignment(Label.CENTER); p1.add(statusMsg); } p1.reshape(0,MAXSCREENY+50,MAXSCREENX,30); add(p1); initBoard(); // setupMem(); cnv1.repaint(); } // Handle new game button press public boolean action(Event e, Object arg) { Object source = e.target; if (source == newGameButton) { initBoard(); cnv1.repaint(); } return true; } public void destroy() { } // reset the game public void initBoard() { int r, c; redWins = false; blueWins = false; whoseTurn = BLUE; toggleTurnMsg(); if (debug) statusMsg.setText(""); board = new int[MAXROWS][MAXCOLS]; for (r = 0; r < MAXROWS; r++) { for (c = 0; c < MAXCOLS; c++) { board[r][c] = NOTOWER; if ((r % 2) == 0) /* even row */ { if ((c % 2) == 1) /* odd col */ { board[r][c] = BLUETOWER; } } else /* odd row */ { if ((c % 2) == 0) /* even col */ { board[r][c] = REDTOWER; } } } } } public static void toggleTurnMsg() { if (whoseTurn == BLUE) { whoseTurn = RED; turnMsg.setText("Red - It is your Move"); } else { whoseTurn = BLUE; turnMsg.setText("Blue - It is your Move"); } } // mark a spot on the board public static boolean toggleBox(int r, int c) { boolean rv; if (board[r][c] == NOTOWER) { rv = true; if (whoseTurn == BLUE) { board[r][c] = BLUETOWER; toggleTurnMsg(); isBlueWinner(); } else { board[r][c] = REDTOWER; toggleTurnMsg(); isRedWinner(); } } else rv = false; return(rv); } public static void isBlueWinner() { int r,c, r1, c1, tryTop, tryPtr; int tries[][]; boolean visited[][]; visited = new boolean[MAXROWS][MAXCOLS]; for (r = 0; r < MAXROWS; r++) for (c = 0; c < MAXCOLS; c++) visited[r][c] = false; tries = new int[MAXROWS*MAXCOLS][2]; blueWins = false; r = 0; for (c = 1; c < MAXCOLS && (!blueWins); c = c + 2) { /* board[0][c] = BLUETOWER */ if (!visited[0][c]) { tryTop = 0; tryPtr = 0; tries[tryTop][ROW] = 0; tries[tryTop++][COL] = c; while ((tryPtr < tryTop) && (!blueWins)) { r1 = tries[tryPtr][ROW]; c1 = tries[tryPtr][COL]; visited[r1][c1] = true; if (r1 == MAXROWS - 1) blueWins = true; else { if ((r1 - 1) >= 0) /* check up */ { if ((!visited[r1-1][c1]) && (board[r1-1][c1] == BLUETOWER)) { tries[tryTop][ROW] = r1-1; tries[tryTop++][COL] = c1; } } /* check down */ if ((!visited[r1+1][c1]) && (board[r1+1][c1] == BLUETOWER)) { { tries[tryTop][ROW] = r1+1; tries[tryTop++][COL] = c1; } } if ((c1 - 1) >= 0) /* check left */ { if ((!visited[r1][c1-1]) && (board[r1][c1-1] == BLUETOWER)) { tries[tryTop][ROW] = r1; tries[tryTop++][COL] = c1-1; } } /* check right */ if ((c1 + 1) < MAXCOLS) { if ((!visited[r1][c1+1]) && (board[r1][c1+1] == BLUETOWER)) { tries[tryTop][ROW] = r1; tries[tryTop++][COL] = c1+1; } } } tryPtr++; } } } if (blueWins) { turnMsg.setText("Blue Wins"); // if (debug) // statusMsg.setText("Blue Wins" + " MS: " + maxSteps + " MO: " + maxOptions + " MP: " + maxPathList + " MM: " + maxMoves); } } public static void isRedWinner() { int r,c, r1, c1, tryTop, tryPtr; int tries[][]; boolean visited[][]; visited = new boolean[MAXROWS][MAXCOLS]; for (r = 0; r < MAXROWS; r++) for (c = 0; c < MAXCOLS; c++) visited[r][c] = false; tries = new int[MAXROWS*MAXCOLS][2]; redWins = false; c = 0; for (r = 1; r < MAXROWS && (!redWins); r = r + 2) { /* board[r][0] = REDTOWER */ if (!visited[r][0]) { tryTop = 0; tryPtr = 0; tries[tryTop][ROW] = r; tries[tryTop++][COL] = 0; while ((tryPtr < tryTop) && (!redWins)) { r1 = tries[tryPtr][ROW]; c1 = tries[tryPtr][COL]; visited[r1][c1] = true; if (c1 == MAXCOLS - 1) redWins = true; else { if ((r1 - 1) >= 0) /* check up */ { if ((!visited[r1-1][c1]) && (board[r1-1][c1] == REDTOWER)) { tries[tryTop][ROW] = r1-1; tries[tryTop++][COL] = c1; } } if ((r1 + 1) < MAXROWS) /* check down */ { if ((!visited[r1+1][c1]) && (board[r1+1][c1] == REDTOWER)) { tries[tryTop][ROW] = r1+1; tries[tryTop++][COL] = c1; } } if ((c1 - 1) >= 0) /* check left */ { if ((!visited[r1][c1-1]) && (board[r1][c1-1] == REDTOWER)) { tries[tryTop][ROW] = r1; tries[tryTop++][COL] = c1-1; } } /* check right */ if ((c1 + 1) < MAXCOLS) { if ((!visited[r1][c1+1]) && (board[r1][c1+1] == REDTOWER)) { tries[tryTop][ROW] = r1; tries[tryTop++][COL] = c1+1; } } } tryPtr++; } } } if (redWins) { turnMsg.setText("Red Wins"); // if (debug) // statusMsg.setText("Red Wins" + " MS: " + maxSteps + " MO: " + maxOptions + " MP: " + maxPathList + " MM: " + maxMoves); } } public static void findBestMove() { int i, r, c, pth, stp, s, curStep, row, col; Point bestMove; // Pick a random move row = (int) ((MAXROWS-1) * Math.random() ); col = (int) ((MAXCOLS-1) * Math.random() ); while (board[row][col]>0) { row = (int) ((MAXROWS-1) * Math.random() ); col = (int) ((MAXCOLS-1) * Math.random() ); } toggleBox(row,col); cnv1.repaint(); } //public static final int COUNT = 2, CLOSEST = 3; //public static final int MAXDIST = MAXROWS * MAXCOLS; public static void main(String args[]) { Bridges bridgeApp = null; bridgeApp = new Bridges("The Game of Bridge-It"); bridgeApp.resize(MAXSCREENX+10, MAXSCREENY+95); bridgeApp.show(true); } }