C++ Help


 
Thread Tools Search this Thread
Top Forums Programming C++ Help
# 1  
Old 01-04-2009
C++ Help

Hi i need a bit of help here.

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

Image

Thanks Ross
# 2  
Old 01-05-2009
I'm thinking the issue is this:
Code:
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.
# 3  
Old 01-07-2009
Quote:
Originally Posted by Corona688
I'm thinking the issue is this:
Code:
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 Smilie

Just another question i am having trouble adding a second player to the game i havent really got a clue how to do this SmilieSmilie
# 4  
Old 01-07-2009
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:
Code:
int player=2;

while(! gameover)
{
  int value;

  if(player == 1)
  {
    player=2;
  }
  else 
  {
    player=1;
  }

  cout << "Player " << player << "'s turn:" << endl;
  cin >> value;
  board[value]=player;

  check_if_game_over;
}

cout << "Player " << player << " wins!" << endl;

# 5  
Old 01-07-2009
Quote:
Originally Posted by Corona688
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:
Code:
int player=2;

while(! gameover)
{
  int value;

  if(player == 1)
  {
    player=2;
  }
  else 
  {
    player=1;
  }

  cout << "Player " << player << "'s turn:" << endl;
  cin >> value;
  board[value]=player;

  check_if_game_over;
}

cout << "Player " << player << " wins!" << endl;

So that would go before

Code:
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?
# 6  
Old 01-07-2009
Quote:
Originally Posted by RossMc
So that would go before

Code:
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;

# 7  
Old 01-07-2009
Quote:
Originally Posted by Corona688
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

Thanks RossSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question