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
Code:
#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[i][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.
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.
Yeah thanks for the reply i have fixed it now it was in the wrong place
Just another question i am having trouble adding a second player to the game i havent really got a clue how to do this
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:
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:
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'; }
Or instead of?
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.
Code:
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;
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.
Code:
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
Code:
#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[i][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