I need help with Connect four game!!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

I need help with Connect four game!!!

hi all i know this might be a simple game for you guys but i need help with it, if anyone is willing to help then i thank u in advance.

ive define this structure:

[b]struct Gamestate
{
char board [] [];
char symbol;
char nextTurn;
};[/b]

now i would like to the following function:
[b]void computerMove(Gamestate &);
int checkWinner(Gamestate, &);
bool saveGame (GameState, char []);
bool loadGame (GameState &, char[]);

[/b]

[code]
#include
using namespace std;
const int ROW=7;
const int COL=8;

const char PLAYER1='O';
const char PLAYER2='X';

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

int main()
{
int move=1;
bool quit=false, startGame=true;
char board[ROW][COL], playAgain, option;

drawHeader();
initialiseBoard(board);
option = chooseOption();
if (option!='c') // quit not selected
{
while(!quit) //while game not finished
{
if(startGame)
{
initialiseBoard(board);
startGame=false;
}

drawBoard(board);
move=playerMove(board, PLAYER1, 1);
if (move!=0) // player 1 has not quit current game
{
drawBoard(board);
move=playerMove(board, PLAYER2, 2);
}
if (move==0) // quit set by any of the players
{
cout << "Do you want to start a new game (y/n)?: ";
cin >> playAgain;
if(playAgain == 'n' || playAgain =='N')
quit=true;
else
startGame=true;
}
}
}
cout << "
Thanks for playing Connect Four, the game of the clever people!
";

return 0;
}

int playerMove(char board[ROW][COL], char symbol, int player)
{
char input;
int move;
bool legalMove=false, positionFound=false;

do{
cout << "Player " << player << " move (1-8, 0 to quit): ";
cin >> input; //read as char to avoid cin errors
move = static_cast(input) - 48; // convert to integer
if(move<0 || move >8)
cout << "Move not allowed!
";
else if(move && board[0][move-1]!='.')
cout << "Sorry no more room in this column...
";
else
legalMove = true;
}while(move<0 || move >8 || !legalMove);

if(move!=0)
{
for(int row=ROW-2; row>=0 && !positionFound; row--)
{
if(board[row][move-1]=='.')
{
board[row][move-1]= symbol;
positionFound=true;
}
}
}
return move;
}


void drawBoard(char board[ROW][COL])
{
for(int row=0; row < ROW; row++)
{
for(int col=0; col< COL; col++)
cout << board[row][col];
cout << endl;
}
return;
}
void initialiseBoard(char board[ROW][COL])
{
//create bottom line of game board: 12345678
for(int col=0; col < COL ; col++)
board[ROW-1][col] = col + '1';
//fill the rest of the board with .'s, that is initialise the rest of the 2D array to .'s
for(int row=0; row < ROW-1; row++)
for(int col=0; col < COL; col++)
board[row][col]='.';

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

return;
}
char chooseOption()
{
char option;
do
{
cout <<"*************************************
";
cout <<"* Choose an Option *
";
cout <<"*************************************
";
cout <<"* a) Play against the computer *
";
cout <<"* b) Play against another player *
";
cout <<"* c) Quit *
";
cout <<"*************************************
";
cout <<"Enter your choice: ";

cin >> option;
if(option=='a')
cout << "
This option has not yet been implemented

";
} while (option!='b' && option !='c');

return option;
}
[code]

Sign In or Register to comment.