I am trying to create a connect four game for my computer science class. I have a lot of it figured out but I cannot get the x's and o's to appear at the bottom of the collumn rather than at the top. Help would be greatly appreciated.
/*
1. Create/Initialize Board
2. Get Moves
3. See if Winner
*/
char board[144];
int col;
int row;
//Initialize board with spaces
for(int i = 0; i < 144; i++)
{
board[i] = ' ';
}
int boardSpot;
char winner;
winner = ' ';
for(int index = 0; index < 144; index++)
{
if(winner == ' ')
{
bool bMoveValid = false;
int turn = index % 2;
while(!bMoveValid) // While bMoveValid = true
{
if(turn == 0) // Enter move for First Player
{
cout << "Player 1, choose a column to drop a letter in: ";
cin >> col;
}
else // Code for Second Player
{
cout << "Player 2, choose a column to drop a letter in: ";
cin >> col;
}
//Process (validate) move
// a - Col between 1 and 12 inclusive
// b - Spot not taken
// c - Rows start at bottom and work way up
for(row = 12; row >=1; row--)
if(turn == 0)
{
bMoveValid = true;
if((col < 1) || (col > 12))
{
bMoveValid = false;
}
if(bMoveValid == true)
{
boardSpot = ((col - 1) * 12);
if(board[boardSpot] != ' ')
{
bMoveValid = false;
}
}
}
else
{
bMoveValid = true;
if((col < 1) || (col > 12))
{
bMoveValid = false;
}
if(bMoveValid == true)
{
boardSpot = ((col - 1) * 12);
if(board[boardSpot] != ' ')
{
bMoveValid = false;
}
}
}
Please put in a closing code tag so we can see your code in code tags!
What you want to do is look in that column for the first empty cell. Something like:
1 2 3 4 5 6 7 8 9 10 11
//pseudocode
while(n < 7) //Where n starts as whatever the bottom cell is
{
if(cell n is empty)
{
put counter in that cell
break;
}
++n; //Put a suitable increment here so it moves on to checking the next cell up
}