All pastes #491219 Raw Edit

Game

public java v1 · immutable
·published 2007-05-16 14:48 UTC
rendered paste body
import java.util.Stack;/** * Write a description of class Board here. *  * @author (your name)  * @version (a version number or a date) */public class Game{  int[][] board;   public Game()  {    board = new int[4][4];    for(int r=0;r<board.length;r++)    {      for(int c=0;c<board[0].length;c++)      {        board[r][c] = 0;      }    }  }  /**   * Prints out the board   */  public void printBoard()  {    for(int r=0;r<board.length;r++)    {      for(int c=0;c<board[0].length;c++)      {        System.out.print(board[r][c]+" ");      }      System.out.println();    }    System.out.println();  }  /**   * Makes move for a player and ensures its a legit move   */  public void makeMove(int player,int row, int col)  {    if(board[row][col]==0 && row<board.length && col<board[0].length)    {      board[row][col]=player;      checker(player, row, col);    }    else      System.out.println("Sorry invalid move");  }  /**   * Checker checks for all possible switches   */  public void checker(int player,int row,int col)  {    checku(player, row, col);    checkd(player, row, col);    checkl(player, row, col);    checkr(player, row, col);   // checktl();   // checktr();   // checkbl();   // checkbr();      }  /**   * Checks right of the last players piece for possible switches   */  public void checkr(int player, int row, int col)  {    int tcol = col + 1;    int r=0,c=0;    Loc t;    Stack<Loc> temp = new Stack<Loc>();    while(tcol<board[0].length && board[row][tcol] != player &&board[row][tcol]!=0 )    {      temp.push(new Loc(row,tcol));      tcol++;    }    if(board[row][tcol]==player)    {        while(!temp.empty())      {        t=temp.pop();        r=t.getRow();        c=t.getCol();        board[r][c] = player;      }    }  }  /**   * Checks left of the last players piece for possible switches   */  public void checkl(int player,int row,int col)  {    int tcol = col-1;    int r=0,c=0;    Loc t;    Stack<Loc> temp = new Stack<Loc>();    while(tcol>=0 && board[row][tcol] != player && board[row][tcol]!=0 )    {      temp.push(new Loc(row,tcol));      tcol--;    }    if(tcol==-1)      tcol=0;    if(board[row][tcol]==player)    {        while(!temp.empty())      {        t=temp.pop();        r=t.getRow();        c=t.getCol();        board[r][c] = player;      }    }  }  /**   * Checks below the last players peice for possible switches   */  public void checkd(int player, int row, int col)  {    int trow = row + 1;    int r=0,c=0;    Loc t;    Stack<Loc> temp = new Stack<Loc>();    while(trow<board.length && board[trow][col] != player &&board[trow][col]!=0 )    {      temp.push(new Loc(trow,col));      trow--;    }    if(board[trow][col]==player)    {        while(!temp.empty())      {        t=temp.pop();        r=t.getRow();        c=t.getCol();        board[r][c] = player;      }    }  }  /**   * Checks above the last players piece for possible switches   */  public void checku(int player,int row,int col)  {    int trow = row-1;    int r=0,c=0;    Loc t;    Stack<Loc> temp = new Stack<Loc>();    while(trow>=0 && board[trow][col] != player && board[trow][col]!=0 )    {      temp.push(new Loc(trow,col));      trow--;    }    if(trow==-1)      trow=0;    if(board[trow][col]==player)    {        while(!temp.empty())      {        t=temp.pop();        r=t.getRow();        c=t.getCol();        board[r][c] = player;      }    }  }  public void checkbr(int player,int row, int col)  {    int trow = row+1,     int r=0,c=0;    Loc t;    Stack<Loc> temp = new Stack<Loc>();    while(trow>=0 && board[trow][col] != player && board[trow][col]!=0 )    {      temp.push(new Loc(trow,col));      trow--;    }    if(trow==-1)      trow=0;    if(board[trow][col]==player)    {        while(!temp.empty())      {        t=temp.pop();        r=t.getRow();        c=t.getCol();        board[r][c] = player;      }    }  }  }