Hey guys,

im just askign if its possible for sum help with this assignment i have, its a connect 4 game, and there are some bugs that i cant work out of it no matter what i try

here is the code.

problem are:
1. when choosing from the first screen (void chooseOption()) the first time you enter a character it works, but if you do a (against computer) is loops back to itself, and then if u try to put in a char, it doesnt work.

2. when actually playing against another palyer, i havent been able to work out how to alternatee players and their symbol and how to get it to correctly stack on the grid

3. i am unable to get the game to quit when im the use inputs 0 to quit (whilst playing the game)

this is all i have found so far, any help you could give would b awsome

#include <iostream>
using namespace std;

const int ROW=7, COL=5; 

void drawHeader();
void playAgain();
char chooseOption();
void drawBoard(char board[ROW][COL]);
void initialiseBoard(char board[ROW][COL]);
int playerMove(char board[ROW][COL], char, int);

int main()
{
	char option;
	char board[ROW][COL];
	char X;
	int move;
	int status=1; // 1 for start or restart, 2 for play, 3 for quit
	int again;
	drawHeader();
	
	do
	{
		if (status==1) //Just start or Restart
		{
			option = chooseOption();
			if (option=='a')
			{
				cout << "This has not yet been Implemented" << endl;
				chooseOption(); //against computer
			} 
			else if (option=='b')
			{
				status = 2;
				initialiseBoard(board);
				X='X';
				//game play
			}
			else //option is 'q'
			{
				status=3;
			}
		} 
		else if (status==2) //playing
		{
			drawBoard(board);
			
			cout<<"Player 1, Your Move (1-8, 0 to quit): "; // ask for user input
			cin>>move;
			move=playerMove(board,X,move);
			
			if (X=='X')
				X='O';
			else
				X='X';
			if (move==0)
				{
				playAgain();
				
				status=3;
				}
		}
	
	}
	while (status!=3);
	
	if (status == 3)
		playAgain();
		
	
	return 0;
}

void drawHeader()
{
	cout << "*********************************************************" << endl;
	cout << "*                       Connect Four                    *" << endl;
	cout << "*********************************************************" << endl;
	cout << "Welcome to the Connect Four computer game. You can choose" << endl;
	cout << "to play against another player or against the computer " << endl;
	cout << "(not implemented in this version)." << endl;
	cout << "In this game the first player to position four of his/her" << endl;
	cout << "pieces on a line will win. The line can be horizontal," << endl;
	cout << "vertical or at an angle, but the 4 pieces must be next to" << endl;
	cout << "each other." << endl;
	cout << "*********************************************************" << endl;
	
	return;
}

char chooseOption()
{
	char option;
	char board[ROW][COL]; 
	
	cout << "\n*************************************" << endl;
	cout << "*          Choose an Option         *" << endl;
	cout << "*************************************" << endl;
	cout << "* a) Play against the computer      *" << endl;
	cout << "* b) Play against another Player    *" << endl;
	cout << "* c) Quit                           *" << endl;
	cout << "*************************************" << endl;
	cout << "Enter your choice: ";
	cin >> option;

	return option;
}



void playAgain()
{
	char again;
	
	cout << "Do you want to play again (y/n)?: ";
	cin >> again;
	
	if (again == 'y')
		chooseOption();
	else if (again == 'n')
		cout << "\nThanks for playing Connect Four, the game of the clever people!" << endl; 
		
	return;
}

void initialiseBoard(char board[ROW][COL])
{
	char rowComp = 0, colComp = 0;
	
	while(colComp <= COL)
	{	 
		while(rowComp <= ROW)
		{	board[rowComp][colComp] = '.';	
			++rowComp;	 
		}
		rowComp = 0;
		colComp++;
	}
	return;
}
	
void drawBoard(char board[ROW][COL])
{
	int rowComp = 0, colComp = 0;
	
	while(colComp <= COL)
	{	 
		while(rowComp <= ROW)
		{	cout << board[rowComp][colComp];	
			++rowComp;	 
		}
		cout << endl;
		rowComp = 0;
		colComp++;
	}
	cout << "12345678" << endl;
	return;
	
}



int playerMove(char board[ROW][COL], char, int move)
{

	int player = 1;
	
	drawBoard(board);
	cout << "Player "<< player <<", Your Move (1-8, 0 to quit): ";
	cin >> move;
		
	return move;
}

1. initialiseBoard() is writing out-of-bounds in that matrix because those two loops are executing 1 too many times. Replace <= with just <

2. delete line 31.

3. delete line 95. the array is never referenced in that function.

I don't know how you will test any of the rest of the code because none of the functions are ever called. You need to add a little code for options a and b to call the other functions.

ok i did that but i am still comeing up with errors except these errors that are coming up shouldnt be there as what they are telling me to do, is correct from what i can see :(

#include <iostream>
using namespace std;

const int ROW=8, COL=7;  
char option;

void drawHeader();
void playAgain();
char chooseOption();
void drawBoard(char board[ROW][COL]);
void initialiseBoard(char board[ROW][COL]);
int playerMove(char board[ROW][COL], char, int);
bool win;

int main()
{
	
	char board[ROW][COL];
	char sym;
	int move;
	int status=1; // 1 for start or restart, 2 for play, 3 for quit
	int again;
	int player;
	
	drawHeader();
	
	do
	{
		if (status==1) //Just start or Restart
		{
			chooseOption();
			
			if (option=='a')
			{
				cout << "This has not yet been Implemented" << endl;
				 //against computer
			} 
			else if (option=='b')
			{
				status = 2;
				initialiseBoard(board);
				sym='X';
				//game play
			}
			else //option is 'q'
			{
				status=3;
			}
		} 
		else if (status==2) //playing
		{
			
			do 
			{
				drawBoard(board);	
				cout<<"Player "<< player << " , Your Move (1-8, 0 to quit): "; // ask for user input
				cin >> move;
				
			
				if ((move >='1') && (move <= '8'))
					cout << "working on this, his is a tag";
					//need to enter function to add in the X or the O to the position
				else if (move = '0')
					playAgain();
					status=3;
				else
					cout << "Move not allowed!" << endl;
					
				 	 	 
			//	  if (sym=='X')
			//	  	  sym='O';
			//	  else
			//	  	  sym='X';
			}while (win = false)
		}	
	}while (status!=3);
	
	if (status == 3)
		playAgain();
		
	
	return 0;
}

void drawHeader()
{
	cout << "*********************************************************" << endl;
	cout << "*                       Connect Four                    *" << endl;
	cout << "*********************************************************" << endl;
	cout << "Welcome to the Connect Four computer game. You can choose" << endl;
	cout << "to play against another player or against the computer " << endl;
	cout << "(not implemented in this version)." << endl;
	cout << "In this game the first player to position four of his/her" << endl;
	cout << "pieces on a line will win. The line can be horizontal," << endl;
	cout << "vertical or at an angle, but the 4 pieces must be next to" << endl;
	cout << "each other." << endl;
	cout << "*********************************************************" << endl;
	
	return;
}

char chooseOption()
{	 
	cout << "\n*************************************" << endl;
	cout << "*          Choose an Option         *" << endl;
	cout << "*************************************" << endl;
	cout << "* a) Play against the computer      *" << endl;
	cout << "* b) Play against another Player    *" << endl;
	cout << "* c) Quit                           *" << endl;
	cout << "*************************************" << endl;
	cout << "Enter your choice: ";
	cin >> option;

	return option;
}

void playAgain()
{
	char again;
	
	cout << "Do you want to play again (y/n)?: ";
	cin >> again;
	
	if (again == 'y')
		chooseOption();
	else if (again == 'n')
		cout << "\nThanks for playing Connect Four, the game of the clever people!" << endl; 
		
	return;
}

void initialiseBoard(char board[ROW][COL])
{
	char rowComp = 0, colComp = 0;
	
	while(colComp <= COL)
	{	 
		while(rowComp <= ROW)
		{	board[rowComp][colComp] = '.';	
			++rowComp;	 
		}
		rowComp = 0;
		colComp++;
	}
	return;
}
	
void drawBoard(char board[ROW][COL])
{
	int rowComp = 0, colComp = 0;
	
	while(colComp < COL)
	{	 
		while(rowComp < ROW)
		{	cout << board[rowComp][colComp];	
			++rowComp;	 
		}
		cout << endl;
		rowComp = 0;
		colComp++;
	}
	cout << "12345678" << endl;
	return;
	
}

int playerMove(char board[ROW][COL], char, int move)
{

	int player = 1;
	
	drawBoard(board);
	cout << "Player "<< player <<", Your Move (1-8, 0 to quit): ";
	cin >> move;
		
	return move;
}
char rowComp = 0, colComp = 0;
while(colComp <= COL)
{	 		
    while(rowComp <= ROW)

Without my ESP hat on I can't tell what the error messages are that you are getting. But I can tell you that the above should probably be changed to:

int rowComp = 0, colComp = 0;
while(colComp < COL)	
{	 		
  while(rowComp < ROW)

Please note my change in first line and Ancient Dragons in line 2 and 4.

>>i did that but i am still comeing up with errors except these errors that are coming up shouldnt be there

what are the errors? Compilers do not produce errors unless there are infact errors. For example: look at lines 63-66. Line 66 is an error because of missing braces to enclose lines 64 and 65 in the same block.

Line 74: two problems here. (1) using the '=' assignment operator instead of "==" boolean operator (very common mistake), and (2) missing semicolon at the end of the line.

ok, it now compies (yay) however, when i enter a, b or c for the chooseoption part, it says that its an invalid choiuce, where they are meant to be the values used

#include <iostream>
using namespace std;

const int ROW=8, COL=7;

void drawHeader();
void playAgain();
char chooseOption();
void drawBoard(char board[ROW][COL]);
void initialiseBoard(char board[ROW][COL]);
int playerMove(char board[ROW][COL], char, int);
bool win;

int main()
{
	
	char board[ROW][COL];
	char sym;
	char option;
	int move;
	int status=1; // 1 for start or restart, 2 for play, 3 for quit
	int again;
	int player;
	drawHeader();
	
	do
	{
		if (status==1) //Just start or Restart
		{
			chooseOption();
			
			if (option=='a')
			{
				cout << "This has not yet been Implemented" << endl;
				 //against computer
			} 
			else if (option=='b')
			{
				status = 2;
				initialiseBoard(board);
				sym='O';
				//game play
			}
			else if (option=='c')//option is 'q'
			{
				status=3;
			}
			else
				cout << "Invalid Choice!";
				
		} 
		else if (status==2) //playing
		{
			
			do 
			{
				drawBoard(board);	
				cout<<"Player "<< player << " , Your Move (1-8, 0 to quit): "; // ask for user input
				cin >> move;
				
				playerMove(board,sym,move);
				
				if ((sym=='O') && (player == 1))
				{
					sym='X';
					player = 2;
				}
				else
					sym='O';
					player = 1;	   	   
							
			}while (win == false);
		}	
	}while (status!=3);
	
	if (status == 3)
		playAgain();
		
	
	return 0;
}

void drawHeader()
{
	cout << "*********************************************************" << endl;
	cout << "*                       Connect Four                    *" << endl;
	cout << "*********************************************************" << endl;
	cout << "Welcome to the Connect Four computer game. You can choose" << endl;
	cout << "to play against another player or against the computer " << endl;
	cout << "(not implemented in this version)." << endl;
	cout << "In this game the first player to position four of his/her" << endl;
	cout << "pieces on a line will win. The line can be horizontal," << endl;
	cout << "vertical or at an angle, but the 4 pieces must be next to" << endl;
	cout << "each other." << endl;
	cout << "*********************************************************" << endl;
	
	return;
}

char chooseOption()
{	 
	char option;
	
	cout << "\n*************************************" << endl;
	cout << "*          Choose an Option         *" << endl;
	cout << "*************************************" << endl;
	cout << "* a) Play against the computer      *" << endl;
	cout << "* b) Play against another Player    *" << endl;
	cout << "* c) Quit                           *" << endl;
	cout << "*************************************" << endl;
	cout << "Enter your choice: ";
	cin >> option;

	return option;
}



void playAgain()
{
	char again;
	
	cout << "Do you want to play again (y/n)?: ";
	cin >> again;
	
	if (again == 'y')
		chooseOption();
	else if (again == 'n')
		cout << "\nThanks for playing Connect Four, the game of the clever people!" << endl; 
		
	return;
}

void initialiseBoard(char board[ROW][COL])
{
	char rowComp = 0, colComp = 0;
	
	while(colComp < COL)
	{	 
		while(rowComp < ROW)
		{	board[rowComp][colComp] = '.';	
			++rowComp;	 
		}
		rowComp = 0;
		colComp++;
	}
	return;
}
	
void drawBoard(char board[ROW][COL])
{
	char rowComp = 0, colComp = 0;
	
	while(colComp < COL)
	{	 
		while(rowComp < ROW)
		{	cout << board[rowComp][colComp];	
			++rowComp;	 
		}
		cout << endl;
		rowComp = 0;
		colComp++;
	}
	cout << "12345678" << endl;
	return;
	
}

int playerMove(char board[ROW][COL], char, int move)
{

	int player = 1;
	char sym;
	
	if ((move >='1') && (move <= '8'))
		cout << "working on this, this is a tag";
					//need to enter function to add in the X or the O to the position
	else if (move = '0')
		playAgain();
		
	else
		cout << "Move not allowed!" << endl; 	 
		
	return move;
}

ok i solved that problem, now when i go to choose weather or not im gunna start a new game, or quit (playAgain()) when i enter y, it works fine, but when i enter n it doesnt terminate the program, it continues from where it was up to.

#include <iostream>
using namespace std;

const int ROW=8, COL=7;

char option;
int status;

void drawHeader();
void playAgain();
char chooseOption(char);
void drawBoard(char board[ROW][COL]);
void initialiseBoard(char board[ROW][COL]);
int playerMove(char board[ROW][COL], char, int);
bool win;

int main()
{
	
	char board[ROW][COL];
	char sym;
	int move;
	int status=1; // 1 for start or restart, 2 for play, 3 for quit
	int again;
	int player=1;
	drawHeader();
	
	do
	{
		if (status==1) //Just start or Restart
		{
			chooseOption(option);
			
			if (option=='a')
			{
				cout << "This has not yet been Implemented" << endl;
				 //against computer
			} 
			else if (option=='b')
			{
				status = 2;
				initialiseBoard(board);
				sym='O';
				//game play
			}
			else if (option=='c')//option is 'q'
			{
				status=3;
			}
			else
				cout << "Invalid Choice!";
				
		} 
		else if (status==2) //playing
		{
			
			do 
			{
				drawBoard(board);	
				cout<<"Player "<< player << " , Your Move (1-8, 0 to quit): "; // ask for user input
				cin >> move;
				
				playerMove(board,sym,move);
				
				if ((sym=='O') && (player == 1))
				{
					sym='X';
					player = 2;
				}
				else
					sym='O';
					player = 1;	   	   
							
			}while (win == false);
		}	
	}while (status!=3);
	
	cout << "\nThanks for playing Connect Four, the game of the clever people!" << endl;
	
	return 0;
}

void drawHeader()
{
	cout << "*********************************************************" << endl;
	cout << "*                       Connect Four                    *" << endl;
	cout << "*********************************************************" << endl;
	cout << "Welcome to the Connect Four computer game. You can choose" << endl;
	cout << "to play against another player or against the computer " << endl;
	cout << "(not implemented in this version)." << endl;
	cout << "In this game the first player to position four of his/her" << endl;
	cout << "pieces on a line will win. The line can be horizontal," << endl;
	cout << "vertical or at an angle, but the 4 pieces must be next to" << endl;
	cout << "each other." << endl;
	cout << "*********************************************************" << endl;
	
	return;
}

char chooseOption(char)
{	 
	
	cout << "\n*************************************" << endl;
	cout << "*          Choose an Option         *" << endl;
	cout << "*************************************" << endl;
	cout << "* a) Play against the computer      *" << endl;
	cout << "* b) Play against another Player    *" << endl;
	cout << "* c) Quit                           *" << endl;
	cout << "*************************************" << endl;
	cout << "Enter your choice: ";
	cin >> option;

	return option;
}



void playAgain()
{
	char again;
	
	cout << "Do you want to play again (y/n)?: ";
	cin >> again;
	
	if (again == 'y')
		chooseOption(option);
	else if(again == 'n')
		status = 3;
	else
		cout << "Invalid Option!";
		
	return;
}

void initialiseBoard(char board[ROW][COL])
{
	char rowComp = 0, colComp = 0;
	
	while(colComp < COL)
	{	 
		while(rowComp < ROW)
		{	board[rowComp][colComp] = '.';	
			++rowComp;	 
		}
		rowComp = 0;
		colComp++;
	}
	return;
}
	
void drawBoard(char board[ROW][COL])
{
	char rowComp = 0, colComp = 0;
	
	while(colComp < COL)
	{	 
		while(rowComp < ROW)
		{	cout << board[rowComp][colComp];	
			++rowComp;	 
		}
		cout << endl;
		rowComp = 0;
		colComp++;
	}
	cout << "12345678" << endl;
	return;
	
}

int playerMove(char board[ROW][COL], char , int move)
{

	int player = 1;
	char sym;
	
	if ((move >='1') && (move <= '8'))
		cout << "working on this, this is a tag";
					//need to enter function to add in the X or the O to the position
	else if (move = '0')
		playAgain();
		
	else
		cout << "Move not allowed!" << endl; 	 
		
	return move;
}

The only way to stop the game is to change the value of status to 3. That can only be done within playAgain() which is only called within playerMove() which is only called within the inner loop of the nested loop in main(). Unfortunately status is never assessed in the inner loop of main(), just in the outer. So somehow you have to stop the inner loop to get status evaluated by the outer loop.

Within your current code, my advice would be to add a second condition to the inner while statement assessing the value of status in addition to the status of win on line 74.

This question has already been answered. Start a new discussion instead.