1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <iostream>
using namespace std;
// global functions
void init(); // reset data values
void printBoard();
void printSquare(int);
void playerMove(bool*, bool*);
void compMove(); // to do <==================
// global data
bool board[10]; // only 1 - 9 are used
bool isWinner; // indicate there is a winner
bool taken1[10]; // 1st player's taken spots
bool pairs1[16]; // 1st player's possible winning right away spots
bool taken2[10]; // 2nd player or computer taken spots
bool pairs2[16]; // 2nd player or computer winning right away spots
int main(int argc, char **argv)
{
init();
printBoard();
// your to do: allow the user to repeat the game until he/she decides to quit
// if command line input indicated playerplayer mode
if (strcmp(argv[1], "playerplayer") == 0){
// 1st phase to do: handle player - player mode
}
else{ // otherwise, treat it as playercomputer mode
// 2nd phase to do: handle player - computer mode
}
}
// purpose: perform/reset data before a new game
void init()
{
isWinner = false; // start the game
for (int i = 0; i < 10; i++)
board[i] = 0; // false
for (int i = 0; i < 10; i++){
taken1[i] = 0;
taken2[i] = 0;
}
for (int i = 0; i < 16; i++){
pairs1[i] = 0;
pairs2[i] = 0;
}
}
// purpose: lay out the gameboard visual user interface
void printBoard()
{
cout << "\n";
printSquare(8); printSquare(1); printSquare(6);
cout << "\n------------\n";
printSquare(3); printSquare(5); printSquare(7);
cout << "\n------------\n";
printSquare(4); printSquare(9); printSquare(2);
}
// purpose: given a spot, if it was taken by player1, prints 'X'.
// if it was taken by player 2, prints 'O'.
// Otherwise, prints the spot number
void printSquare(int spot)
{
if (taken1[spot])
cout << "| X ";
else if (taken2[spot])
cout << "| O ";
else
cout << "| " << spot << " ";
}
// purpose: 1st, reads and checks user input
// 2nd, check to see if this player can win
// right away with the inputed spot.
// If yes, prints message, set winner bit,
// and returns.
// Otherwise, update the pairs array for the
// future attempts.
// 3rd, update the taken array and board
void playerMove(bool *taken, bool *pairs)
{
int spot;
cout << "\n\nEnter your position (1 - 9): ";
cin >> spot;
// to do: validate user input <========
board[spot] = true; // update board
if (pairs[15 - spot]){ // can player win with the "spot"
cout << "\nPlayer Won!";
isWinner = true;
}
else{ // if not, update the pairs array
for (int i = 1; i < 10; i++){
if (taken[i] && i + spot < 15)
pairs[i + spot] = true;
}
}
taken[spot] = true; // also update the taken1 array
}
void compMove()
{
int spot;
// your code for the computer moves here
}
|