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;i++)
temp = temp+ getPitArr(0)[i]+ getPitArr(1)[i];
return temp;
}
// -----------------------------------------------------------------------------
// Returns total score of given player
// -----------------------------------------------------------------------------
public int score(int playerNum)
{
int temp = 0;
for(int i=0;i<PITNUM;i++)
temp = temp+getPitArr(playerNum)[i];
return temp;
}
// ----------------------------------------------------------------------------
// Returns a boolean if the game is over (Player cups are all empty)
// ----------------------------------------------------------------------------
public boolean gameFinished(int playerNum)
{
if(score(playerNum) <= (0-getKalah(playerNum))){
gameOver = true;
return true;
}
else
return false;
}
// ----------------------------------------------------------------------------
// Load Board from file and display in UI
// ----------------------------------------------------------------------------
public void loadFrom(ObjectInputStream in) throws IOException, ClassNotFoundException
{
// yeh, err.. whats the point of this method ? load a game ? why bother, it'll just give the final scores and shit
// ok, maybe there is a point.. ill look into later
}
// ----------------------------------------------------------------------------
// Returns copy of the current board status
// ----------------------------------------------------------------------------
public KalahBoard makeCopy(){
/* try{
KalahBoard temp = (KalahBoard) this.clone();
int[] tempArr = new int[7];
tempArr = PlayerOnePits.clone();
int[] tempArr2 = new int[7];
tempArr2 = PlayerTwoPits.clone();
for(int i = 0;i<7;i++){
temp.getPitArr(temp.getTurn())[i] = tempArr[i];
temp.getPitArr((temp.getTurn()+1)%2)[i] = tempArr2[i];
}
return temp;
}catch(CloneNotSupportedException cnse){System.out.println("Object Not Cloneable"); return null;} */
KalahBoard temp = new KalahBoard(players[0], players[1], getDefaultStones(), playerTurn);
for(int i = 0;i<7;i++){
temp.getPitArr(temp.getTurn())[i] = getStones(temp.getTurn(),i);
temp.getPitArr((temp.getTurn()+1)%2)[i] = getStones((temp.getTurn()+1)%2,i);
}
return temp;
}
// ----------------------------------------------------------------------------
// Play move based depending on which box is selected, given by move and the current player whose turn it is
// ----------------------------------------------------------------------------
public void makeMove(int turn, int move) throws IllegalMoveException {
capture = false;
int j = move-1;
int temp = getStones(playerTurn,move);
if(turn != playerTurn)
throw new IllegalMoveException();
else
if(move <= 0 || move >= 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;i<PITNUM;i++){
temp = temp + getStones(playerTurn, i);
}
if(temp <=0)
gameOver = true;
}
//return move;
}
// ----------------------------------------------------------------------------
// Save the current board
// ----------------------------------------------------------------------------
public void saveTo(ObjectOutputStream out) throws IOException {
// TODO Auto-generated method stub
}
// ----------------------------------------------------------------------------
// Returns Pit Array that corresponds to player number
// ----------------------------------------------------------------------------
public int[] getPitArr(int x){
if(x == PLAYERONE)
return PlayerOnePits;
else
if(x == PLAYERTWO)
return PlayerTwoPits;
else return null;
}
// ----------------------------------------------------------------------------
// Returns a string summary of the board
// ----------------------------------------------------------------------------
public String toString(){
String temp = "";
temp = temp + " Player One \n";
for(int i=1;i<7;i++){
temp = temp + " " + getStones(0,i);
}
temp = temp + "\n" + getStones(0,0) + " " + getStones(1,0) + "\n";
for(int i=6;i>0;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;
}
}