rendered paste bodyimport 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(tcol>=board.length)
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(trow>=board.length)
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;
}
}
}
/**
* Checks diagonally bottom right
*/
public void checkbr(int player,int row, int col)
{
int trow = row+1, tcol = col+1;
int r=0,c=0;
Loc t;
Stack<Loc> temp = new Stack<Loc>();
while(trow<board.length && tcol<board[0].length && board[trow][tcol] != player && board[trow][tcol]!=0)
{
temp.push(new Loc(trow,tcol));
trow++;
tcol++;
}
if(board[trow][tcol]==player)
{
while(!temp.empty())
{
t=temp.pop();
r=t.getRow();
c=t.getCol();
board[r][c] = player;
}
}
}
/**
* Testing and Debugging :)
*/
public static void tester()
{
System.out.println("DEBUGGING");
}
}
public class Loc
{
private int row=0,col=0;
public Loc(int xval, int yval)
{
row=xval;
col=yval;
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
}