package april6_gameproto; /** * * @author levenick */ public class Board { static final boolean DEBUG = true; static final int X = 1; static final int O = -X; static final int EMPTY = 0; static final char displayX = 'X'; static final char displayO = 'O'; static final char displayEMPTY = '-'; int n; // how big the board is int whoseTurn; int[][] sqs; // the array! Board(int n) { init(n); } /** * Almost clone() -- makes the new Board *almost* a clone of the old one... * but, does only a shallow copy!!!? Plays at (row,col), so the differ that * way * * @param oldBoard * @param row * @param col */ private Board(Board oldBoard, int row, int col) { n = oldBoard.n; whoseTurn = oldBoard.whoseTurn; // sqs = oldBoard.sqs; // No!! Now they are both pointing the same place!! I.e. to the same array!! sqs = new int[n][n]; for (int r = 0; r < n; r++) { for (int c = 0; c < n; c++) { sqs[r][c] = oldBoard.sqs[r][c]; } } playMove(row, col); } /** * Initialize a Board; X always goes first! * * @param n -- how big the board is... but isn't tic tac toe always 3x3?? */ final void init(int n ) { this.n = n; whoseTurn = X; sqs = new int[n][n]; } /** * Construct a Board by reading from a text file. Infers the Board size by * the length of the first line. Assumes the input file is formatted * correctly no error checking! * * @param mr - a MyReader pointed at a file */ Board(MyReader mr ) { StringList list = new StringList(); while (mr.hasMoreData()) { list.add(mr.giveMeTheNextLine()); } init(list.get(0).length()); int row = 0; int col = 0; for (String nextS : list) { col = 0; if (DEBUG) { System.out.println("nextS = " + nextS); } for (int i = 0; i < n; i++) { sqs[row][col] = displayInverse(nextS.charAt(i)); col++; // next column } // inner for loop row++; // next row } // outer for loop } boolean gameOver() { return true; } BoardList generateNextLegalBoards() { BoardList returnMe = new BoardList(); /* pseudocode! for each legal move, theMove clone the board (i.e. this!) play theMove on the clone add the clone to returnMe */ /* this code was WRONG! It did a shallow copy of the array... ...but... now that the Board(Board, int, int) constructor does a deep copy, everything is COPASETIC!*/ for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { if (sqs[row][col] == EMPTY) { // i.e. an empty square returnMe.add(new Board(this, row, col)); // create a new Board where we have played there, and add it to the list } // if empty } // col loop } // row loop return returnMe; } public String toString() { String returnMe = "I am a Board\n"; for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { returnMe += displayChar(sqs[row][col]); } // inner returnMe += "\n"; } // outer return returnMe; } char displayChar(int sq) { switch (sq) { case X: return displayX; case O: return displayO; case EMPTY: return displayEMPTY; default: return '?'; // oops } // switch } /** * Converts a display char back to the underlying int that represents that * * @param ch * @return 0, 1, or -1... oh... wait... what about the constants?? Jeez! */ private int displayInverse(char ch) { switch (ch) { case displayX: return X; case displayO: return O; case displayEMPTY: return EMPTY; default: System.out.println("bad value!! " + ch); Thread.dumpStack(); System.exit(99999); // This is serious; bail! return 10; // will *never* get here! } // switch } private void playMove(int row, int col) { sqs[row][col] = whoseTurn; whoseTurn = -whoseTurn; } public static void main(String[] args) { Board oldBoard = new Board(3); oldBoard.playMove(0, 0); System.out.println("oldBoard = " + oldBoard); Board newBoard = new Board(oldBoard, 1, 1); System.out.println("newBoard = " + newBoard.toString()); System.out.println("does the 1,1 move show up??"); try { Thread.sleep(3000); } catch (Exception e) { } System.out.println("well.. yes..."); try { Thread.sleep(3000); } catch (Exception e) { } System.out.println("but..."); try { Thread.sleep(3000); } catch (Exception e) { } System.out.println("look what happened to the old board..."); try { Thread.sleep(3000); } catch (Exception e) { } System.out.println("oldBoard = " + oldBoard); try { Thread.sleep(3000); } catch (Exception e) { } System.out.println("ACK!!"); } }