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:
struct Gamestate
{
char board [] [];
char symbol;
char nextTurn;
};
now i would like to the following function:
void computerMove(Gamestate &);
int checkWinner(Gamestate, &);
bool saveGame (GameState, char []);
bool loadGame (GameState &, char[]);
Code:#include <iostream> 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 << "\nThanks for playing Connect Four, the game of the clever people!\n"; 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<int>(input) - 48; // convert to integer if(move<0 || move >8) cout << "Move not allowed!\n"; else if(move && board[0][move-1]!='.') cout << "Sorry no more room in this column...\n"; 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 <<"*********************************************** **********\n"; cout <<"* Connect Four *\n"; cout <<"*********************************************** **********\n"; cout <<"Welcome to the Connect Four computer game. You can choose\n"; cout <<"to play against another player or against the computer\n"; cout <<" (not implemented in this version)\n"; cout <<"In this game the first player to position four of his/her\n"; cout <<"pieces on a line will win. The line can be horizontal, \n"; cout <<"vertical or at an angle, but the 4 pieces must be next to\n"; cout <<"each other.\n"; cout <<"*********************************************** **********\n"; cout << endl; return; } char chooseOption() { char option; do { cout <<"*************************************\n"; cout <<"* Choose an Option *\n"; cout <<"*************************************\n"; cout <<"* a) Play against the computer *\n"; cout <<"* b) Play against another player *\n"; cout <<"* c) Quit *\n"; cout <<"*************************************\n"; cout <<"Enter your choice: "; cin >> option; if(option=='a') cout << "\nThis option has not yet been implemented\n\n"; } while (option!='b' && option !='c'); return option; }



LinkBack URL
About LinkBacks


