my code was working, but when i added in a new section to enter playing against a computer player, it now hates me, and wont stop trying to kill me.

//name: *******
//Lab: *
//purpose: *******
//modification date:***********
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int ROW=6, COL=8;
const char player1 = 'O';
const char player2 = 'X';


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

int main()
{
	int status=1; // 1 for start or restart, 2 for play, 3 for quit
	char board[ROW][COL];
	char option, playerChar, startAgain = 'y';
	int move, playerNum;

	
	drawHeader();
	
	do
	{
		if (status==1) //Just start or Restart
		{
			playerNum=1;
			option = chooseOption();
			
			if (option=='a')
			{
				status = 3;
				// play against computer
			} 
			else if (option=='b')
			{
			
				status = 2;
				//game play
			}
			else if (option=='c')//option is 'q'
			{
				status=4;
			}
			else
				cout << "Invalid Choice!";
				
		} 
		else if (status==2) //playing
		{
			
			do 
			{
				if (startAgain == 'y')
				{
					initialiseBoard(board);
					playerNum = 1;
					startAgain = 'n';
				}
		
				if (playerNum == 1)
					playerChar = player1;
				else
					playerChar = player2;
		
				drawBoard(board);
		
				move = playerMove(board, playerChar, playerNum);
				if (move==-1)
				{
					// no switch
				}
				else	 (move != 0)
				{
					if (playerNum == 0)
						playerNum = 1;
					else
						playerNum = 0;
				}
				else
				{
					startAgain = quitGame('y');	   	   	
			   		if (startAgain == 'n')
						status=4;
					else if (startAgain == 'y')
						status=2;
				}
   
			}while (status == 2);
		}
		else if (status == 3)//this is the new section that doesnt like working
		{	 	 	 
			do 
			{
				if (startAgain == 'y')
				{
					initialiseBoard(board);
					playerNum = 1;
					startAgain = 'n';
				}
		
				if (playerNum == 1)
					playerChar = player1;
				else
					playerChar = player2;
		
				drawBoard(board);
		
				move = againstComputer(board, playerChar, playerNum);
				if (move==-1)
				{	 
				} 
				else	 (move != 0)
				{
					if (playerNum == 0)
						playerNum = 1;
					else
						playerNum = 0;
				}
				else
				{
					startAgain = quitGame('y');	   	   	
			   		if (startAgain == 'n')
						status=4;
					else if (startAgain == 'y')
						status=3;
				}
   
			}while (status ==3);
		}
			
	}while (status!=4);
	
	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 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;
}



char quitGame(char quit)
{

	char cont, startAgain;
	
	if (quit == 'y')
	{
		cout << "Do you want to start a new game (y/n)?: ";
		
		cin >> cont;
		
		cin.clear(); cin.ignore(300, '\n');
		
		while ((cont != 'y') && (cont != 'n'))
		{
			cout << "Do you want to start a new game (y/n)?: ";
				
			cin >> cont;
				
			cin.clear(); cin.ignore(300, '\n');
		}
		
		if (cont =='y')
			startAgain = 'y';
		else
			startAgain = 'n';
	}
	

	return startAgain;
}

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

int playerMove(char board[ROW][COL], char playerChar, int playerNum)
{
	int move, index = 5, position;
	bool cont = false;
	
	cout << (playerNum == 1 ? "Player 1" : "Player 2") << " move (1-8, 0 to quit): ";
	cin >> move;
	
	if ((move != 0) && ((move>=1) && (move<=8)))
	{
		do
		{
			
			position = move - 1;
			
			if (board[index][position] != '.')
			{
				if (--index == -1)
				{
					cout << "Sorry no more room in this column..." << endl;
					return -1;
				}
			}
			else
				cont = true;

		} while (!cont);
		
		board[index][position] = playerChar;
	}
	else if (move == 0)
	{
		cont = true;
	}
	else 
	{
		cout << "This move is not allowed!" << endl;	
		return -1;
	}
	return move;
}


int againstComputer(char board[ROW][COL], char playerChar, int playerNum)
{
	int move, index = 5, position, cMove;
	bool cont = false;
	
	cout << (playerNum == 1 ? "Player 1" : "Computer") << " move (1-8, 0 to quit): ";
	
	if (playerNum != 1)
		computerMove(cMove);
		move = cMove;
	else
		cin >> move;
	
	if ((move != 0) && ((move>=1) && (move<=8)))
	{
		do
		{
			
			position = move - 1;
			
			if (board[index][position] != '.')
			{
				if (--index == -1)
				{
					cout << "Sorry no more room in this column..." << endl;
					return -1;
				}
			}
			else
				cont = true;

		} while (!cont);
		
		board[index][position] = playerChar;
	}
	else if (move == 0)
	{
		cont = true;
	}
	else 
	{
		cout << "This move is not allowed!" << endl;	
		return -1;
	}
	return move;
}

int computerMove(int cMove)
{
	srand ( time(NULL) );
	cMove= rand() % 8 + 1;
	
	return cMove;
}

errors i am gettign are :

parse errors on lines 85,102,111,121,307
im also getting told that "IOS C++ forbids decleration of..." on lines 109, 110, 118, 120

there are also other errors withing that part of the code, what is weird to me however, is that it works perfectly fine in the other place that the almost identical code is placed (status 2)

Recommended Answers

All 3 Replies

In againstComputer, the first if statement needs braces. In main, when you test move, you used else instead of else if twice. And you use cMove before giving it a value in againstComputer.

with the part abotu the computer move that u said, how would i go abotu fixing that, i had a try and the several things i tried didnt work, any suggestions /

dw i got it, thnx again mate
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.