Public feed
#528332
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;
}
}
}
/**
* Checks if there is space
*/
/**public boolean hasSpace()
{
boolean x = false;
for(int r=0;r<board.length;r++)
{
for(int c=0;c<board[0].length;c++)
{
if(board[r][c] == 0)
{
x = true;
break;
}
}
}
return x;
}**/
/**
* Prints out the board
*/
public void printBoard()
{
int x = 1;
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();
}
public int[][] pB()
{
return board;
}
/**
* Makes move for a player and ensures its a legit move
*/
public void makeMove(int player,int row, int col)
{
if(player==-1)
player=2;
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(player, row, col);
checktr(player, row, col);
checkbl(player, row, col);
checkbr(player, row, col);
}
/**
* 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(tcol>=board.length)
{
tcol--;
}
if(board[trow][tcol]==player)
{
while(!temp.empty())
{
t=temp.pop();
r=t.getRow();
c=t.getCol();
board[r][c] = player;
}
}
}
/**
* Checks diagonally top left
*/
public void checktl(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>=0 && board[trow][tcol] != player && board[trow][tcol]!=0)
{
temp.push(new Loc(trow,tcol));
trow--;
tcol--;
}
if(trow==-1)
{
trow=0;
tcol=0;
}
if(board[trow][tcol]==player)
{
while(!temp.empty())
{
t=temp.pop();
r=t.getRow();
c=t.getCol();
board[r][c] = player;
}
}
}
/**
* Checks diagonally top right
*/
public void checktr(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>=0 && board[trow][tcol] != player && board[trow][tcol]!=0)
{
temp.push(new Loc(trow,tcol));
trow--;
tcol++;
}
if(tcol>=board[0].length)
{
tcol--;
}
if(trow<0)
trow++;
if(board[trow][tcol]==player)
{
while(!temp.empty())
{
t=temp.pop();
r=t.getRow();
c=t.getCol();
board[r][c] = player;
}
}
}
/**
* Checks diagonally bottom left
*/
public void checkbl(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(tcol>=0 && board[trow][tcol] != player && board[trow][tcol]!=0)
{
temp.push(new Loc(trow,tcol));
trow++;
tcol--;
}
if(trow>=board.length)
{
trow--;
}
if(tcol<0)
tcol++;
if(board[trow][tcol]==player)
{
while(!temp.empty())
{
t=temp.pop();
r=t.getRow();
c=t.getCol();
board[r][c] = player;
}
}
}
}