I am creating a connect 4 programme in c++. The programme runs ok but when the programme asks what cell i want to put the X in and i enter a cell referance the X doesn't go in the cell like it should
I was just wondering if anyone would be able to have a look at the code and tell me what wrong
The Code is
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <limits>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char a[] = {'|','_','|','_','|','_','|','_','|','_','|','_','|','_','|'};
char b[] = {'|','1','|','2','|','3','|','4','|','5','|','6','|','7','|'};
char board[6][7];
int i, j, k =0;
int row;
int column;
int game =1;
for (i=0; i<6; i )
for (j=0; j<7; j )
board[j] = ' ';
cout<<" ";
for( j = 0; j<15; j )
cout <<b[j];
cout <<endl;
for ( i=0; i<6; i )
{ cout <<i 1;
for( j = 0; j<15; j )
cout <<a[j];
cout <<endl;
}
while(game ==1)
{
cout << "Player 1 Your Turn Please Enter Which Cell You Would Like" << endl;
cin >> row >> column;
}
if(board[row-1][column-1] ==' ')
board[row-1][column-1] = 'X'; }
// game over
Here is a programme of what the programme actually does
while(game ==1)
{
cout << "Player 1 Your Turn Please Enter Which Cell You Would Like" << endl;
cin >> row >> column;
}
It will repeat these two lines over and over, never doing anything else, since that's how while works -- it repeats what's inside it until its condition becomes false. I suspect you've put your while() loop in the wrong place, if you want it to print the game board every time you take a turn it needs to surround that part too. At the bottom, when you detect a game over condition, you should set game to zero so the loop finishes.
You're putting X-es into the array for player one, how about using 1's and 2's instead? That'll make it easier to keep track of the players.
I would declare a variable outside the loop to keep track of whose turn it is, and toggle it back and forth at the top of the loop. Here's a really bare skeleton of the idea:
My example replaces your entire while() loop. It's also just a bare skeleton, like I said; there's bits you need to fill in yourself. I'll comment it better to give you more idea what it's doing and what's not finished. I've also fixed some problems I just noticed in my first version.
char player='2'; /* Keeps track of who's playing */
/* Loop through this block until the game ends */
while(! gameover)
{
int row, column;
/* Put the stuff to print the board here */
/* Switch players */
if(player == '1')
{
player='2';
}
else
{
player='1';
}
/* Print a message for the player */
cout << "Player " << player << "'s turn:" << endl;
/* Read two values from the keyboard */
cin >> row >> column;
/* Change the game board */
board[row-1][column-1]=player;
/* You need to check if the game's over. */
}
cout << "Player " << player << " wins!" << endl;
Thanks for the reply i don't think i have done it right but i have put the code in there and deleted some like this
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <limits>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char a[] = {'|','_','|','_','|','_','|','_','|','_','|','_','|','_','|'};
char b[] = {'|','1','|','2','|','3','|','4','|','5','|','6','|','7','|'};
char board[6][7];
int i, j, k =0;
int row;
int column;
int game =1;
for (i=0; i<6; i++)
for (j=0; j<7; j++)
board[j] = ' ';
cout<<" ";
for( j = 0; j<15; j++)
cout <<b[j];
cout <<endl;
for ( i=0; i<6; i++)
{ cout <<i+1;
for( j = 0; j<15; j++)
cout <<a[j];
cout <<endl;
}
char player='2'; /* Keeps track of who's playing */
/* Loop through this block until the game ends */
while(! gameover)
{
int row, column;
/* Put the stuff to print the board here */
/* Switch players */
if(player == '1')
{
player='2';
}
else
{
player='1';
}
/* Print a message for the player */
cout << "Player " << player << "'s turn:" << endl;
/* Read two values from the keyboard */
cin >> row >> column;
/* Change the game board */
board[row-1][column-1]=player;
/* You need to check if the game's over. */
}
cout << "Player " << player << " wins!" << endl;
}
cout <<endl;
}
k++;
if( k ==42){
game =0;
}
}
cout << "Game Over" <<endl;
return 0;
}
I get this error
board.cpp: In function �int main()':
board.cpp:66: error: �gameover' was not declared in this scope
board.cpp: At global scope:
board.cpp:95: error: expected constructor, destructor, or type conversion before �<<' token
board.cpp:97: error: expected declaration before �}' token
Is there bits i need to change because i don't really understand it
board.cpp: In function �int main()':
board.cpp:66: error: �gameover' was not declared in this scope
That means that gameover wasn't declared. Looking back, I see you called it 'game', not 'gameover', and made it an int, not a bool, so the compiler gives up when it can't find the variable gameover anywhere. Fine, change the top of the while loop into while(game == 1)
board.cpp: At global scope:
board.cpp:95: error: expected constructor, destructor, or type conversion before �<<' token
board.cpp:97: error: expected declaration before �}' token
{ }'s always come in pairs surrounding one or more lines of code, if the compiler finds a } without a { it will stop with an error like it did on line 97. You have lots and lots of extra }'s, which also indirectly causes the error on line 95 -- the compiler thinks main()'s code block has already closed at this point, and won't let you call cout outside of a function. Remove these lines:
}
cout <<endl;
}
k++;
if( k ==42){
game =0;
}
}
Also, take a close look at where you have these lines:
k++;
if( k ==42){
game =0;
}
This is outside the loop I gave you, and would never have ran until the game already ended, which I don't think is what you want. Look for where I say you need to check for game over.
What would you like explained? I wrote it as clearly as I know how.