ALGORITHM
// create board
// LOOP
// init board
// currentPlayer = 1, numMoves = 0;
LOOP UP TO 42 TIMES
// display board
// get move (includes validation)
// place piece
// check winner
// check tie
// change currentPlayer
END LOOP
// Display congratulations/tie
// do you want to play again
// END LOOP
DATA NEEDED
Int board[][]
Int currentPlayer
bool playAgain
int numMoves
GAME BOARD
int board [] [] = new int[7][8]
Using rows 1-6 and columns 1-7 and ignoring element 0
1 2 3 4 5 6 7
6 6,1 6,2 6,3 6,4 6,5 6,6 6,7
5 5,1 5,2 5,3 5,4 5,5 5,6 5,7
4 4,1 4,2 4,3 4,4 4,5 4,6 4,7
3 3,1 3,2 3,3 3,4 3,5 3,6 3,7
2 2,1 2,2 2,3 2,4 2,5 2,6 2,7
1 1,1 1,2 1,3 1,4 1,5 1,6 1,7
DISPLAYING BOARD
See the last in class problem
CONVERT ROUTINE
public String convert(int i)
{
if (I == 1)
{
Return “X”;
}
else if (I == 2)
{
Return “O”;
}
else
{
Return “-”;
}
}
VALIDATE MOVE
do
{
// get move
}
while (col < 1 || col <> 7 || board [6][col] != 0);
PLACEMENT
for(int i = 1; i<=6; i++);
{
if (board[i][col]==0)
{
board[i][col] = current player;
break;
}
}
HORIZONTAL CHECK FOR WIN
for (int r = 1; r < = 6; r++)
{
for int c = 1; c <=4; c++)
{
if (board [r][c] == board [r][c+1] &&
board[r][c+1] == board[r][c+2] &&
board[r][c+2] == board[r][c+3] &&
board[r][c] != 0)
{
win = true;
}
}
}