package ex15; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** The additional functionality required of a Kalah Board representation. * Moves are numbered 1 to 6. * The players are numbered 0 and 1. **/ public class KalahBoard extends Board implements Cloneable { private Player[] players = new Player[2]; private int defaultStones; private final int PLAYERONE = 0; private final int PLAYERTWO = 1; private final int PITNUM = 7; private int[] PlayerOnePits = new int[7]; private int[] PlayerTwoPits = new int[7]; private int playerTurn = 0; private boolean gameOver = false; // used for AIPlayer to recognise when a capture occurs public boolean capture = false; public KalahBoard(Player playerone, Player playertwo, int stones, int turn){ players[0] = playerone; players[1] = playertwo; playerTurn = turn; initialise(stones); // create array of Player - size 2. will easily link indexes with players // this means fix to only worries of class } // ----------------------------------------------------------------------------- // Intialise Board with given number of stones in each pit // ----------------------------------------------------------------------------- public void initialise(int stonesPerPit){ defaultStones=stonesPerPit; for(int i=1;i<7;i++) { PlayerOnePits[i]=defaultStones; PlayerTwoPits[i]=defaultStones; } PlayerOnePits[0]=0; PlayerTwoPits[0]=0; } // ----------------------------------------------------------------------------- // Return number of stones in specified players pit // Non-Defensive, pitNum must conform with board setup. playerNum defaults to 2 if erroneous value given //------------------------------------------------------------------------------ public int getStones(int playerNum, int pitNum) { return getPitArr(playerNum)[pitNum]; } // ----------------------------------------------------------------------------- // Return number of stones in players end or Kalah pit // ----------------------------------------------------------------------------- public int getKalah(int playerNum){ return getStones(playerNum, 0); } // ----------------------------------------------------------------------------- // Returns difference in Stones between Player 1 and 2. negative indicates Player 2 advantage // Adds Player 1 Pit totals, subtracts Player 2 pit totals // ----------------------------------------------------------------------------- public int score() { int temp = 0; for(int i=0;i= PITNUM) throw new IllegalMoveException(); else if(temp<=0) throw new IllegalMoveException(); else{ getPitArr(turn)[move] = 0; for(int i=temp;i>0;i--) { if(j < 0) { j = 6; if(turn == 0) turn = 1; else turn = 0; } if(j==0 && playerTurn != turn){ i++; j--; } else{ if(j != 0 && i == 1 && playerTurn == turn && getPitArr(turn)[j] == 0){ getPitArr(turn)[j]++; getPitArr(playerTurn)[0] = getKalah(playerTurn) + getStones(turn, j) + getPitArr((turn+1)%2)[7-j]; getPitArr(turn)[j] = 0; getPitArr((turn+1)%2)[7-j] = 0; capture = true; }else{ getPitArr(turn)[j]++; j--; } } } if((j= j+1) != 0){ if(playerTurn ==0) playerTurn = 1; else playerTurn = 0; } temp = 0; for(int i=1;i0;i--){ temp = temp + " " + getStones(1,i); } temp = temp + "\n Player Two "; return temp; } // ---------------------------------------------------------------------------- // Returns which player (0,1) who is currrently playing a move // ---------------------------------------------------------------------------- public int getTurn(){ return playerTurn; } // ---------------------------------------------------------------------------- // Returns true if game is over // ---------------------------------------------------------------------------- public boolean gameOver(){ return gameOver; } public int getDefaultStones(){ return defaultStones; } }