/** * This application implements the game of Chomp * Originally by Martin Gardner (I think) * But I couldn't find a reference anywhere... * * @author Mr J * @version 2004024 */ public class Chomp { // data members of the class // constants values private static final int WIDTH = 20; private static final int HEIGHT = 10; private static final char POISON = '*'; private static final char COOKIE = '.'; private static final char SPACE = ' '; // board array private char[][] board = new char[HEIGHT][WIDTH]; // Applications always start at the public static main method // This one simply calls the following constructor method public static void main(String[] args) { new Chomp(); } /** * Constructor for objects of class Chomp */ public Chomp() { // application code // initialiation boolean gameOver = false; setUpBoard( board ); // main game loop while (!gameOver) { printBoard( board ); getPlayerMove(); gameOver = testGameOver(); } output("Game over, you lose!\n"); } /** * This method sets up the game board * * @param board, the game board */ private void setUpBoard(char[][] board) { for( int row = 0; row < HEIGHT; row ++) { for(int col = 0; col < WIDTH; col++) { board[row][col] = COOKIE; } // add in the poison square board[0][0] = POISON; } } /** * This method prints the game board * * @param board, the game board */ private void printBoard(char[][] board) { System.out.print('\n'); for( int row = 0; row < HEIGHT; row ++) { for(int col = 0; col < WIDTH; col++) { System.out.print(board[row][col]); } // print a newline at the end of each row System.out.print('\n'); } } /** * This method tests if the POISON has been chomped yet * * @return true if the game has been completed */ private boolean testGameOver() { return (board[0][0] == SPACE); } /** * This method gets, validates and processes the player's move */ private void getPlayerMove() { String[] move = new String[2]; // used to split the coordinates String theMove; // the player's move as a String int r = -1; int c = -1; // the player's moves as row column indices boolean gotMove; do { gotMove = true; theMove = input("Input your move (row, col): "); move = theMove.split(","); try { r = Integer.parseInt(move[0].trim()); c = Integer.parseInt(move[1].trim()); } catch(Exception e) { gotMove = false; output("Illegal input - I need 2 whole numbers separated with a comma.\n"); } // check that the coordinates are within the cookie area if (gotMove) { gotMove = ( (r < HEIGHT) && (c < WIDTH) ); if (!gotMove) { output( "You have to chomp on the board " + "(0,0 up to " + (HEIGHT-1) + ", " + (WIDTH-1) + ")\n)"); } } // check that the coordinates have not been chomped yet if (gotMove) { gotMove = ( (board[r][c] != SPACE) || (board[r][c] == POISON) ); if (!gotMove) { output("You can't chomp empty space!\n)"); } } }while ( !gotMove ); // repeat until the move is valid // turn the board into spaces from row to HEIGHT and col to WIDTH for( int row = r; row < HEIGHT; row ++) { for(int col = c; col < WIDTH; col++) { board[row][col] = SPACE; } } } /** * IBIO methods, (c) International Baccalaureate 2003 * Computer Science Subject Guide, Appendix 2. */ static void output(String info) { System.out.println(info); } static void output(double info) { System.out.println(info); } static void output(int info) { System.out.println(info); } static void output(long info) { System.out.println(info); } static void output(char info) { System.out.println(info); } static int inputInt(String Prompt) { int result=0; try{result=Integer.parseInt(input(Prompt).trim());} catch (Exception e){result = 0;} return result; } static long inputLong(String Prompt) { long result=0; try{result=Long.parseLong(input(Prompt).trim());} catch (Exception e){result = 0;} return result; } static double inputDouble(String Prompt) { double result=0; try{result=Double.valueOf(input(Prompt).trim()).doubleValue();} catch (Exception e){result = 0;} return result; } static char inputChar(String Prompt) { char result; try{result=(input(Prompt).trim()).charAt(0);} catch (Exception e){result = '\0';} return result; } static String input(String prompt) { String inputLine = ""; System.out.print(prompt); try { java.io.InputStreamReader sys = new java.io.InputStreamReader(System.in); java.io.BufferedReader inBuffer = new java.io.BufferedReader(sys); inputLine = inBuffer.readLine(); } catch (Exception e) { String err = e.toString(); System.out.println(err); } return inputLine; } static String input() { return input(""); } }