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;
}