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
|
#include <cstdlib>
#include <iostream>
using namespace std;
// =================== Structures ==================
struct player
{ string name;
int score;
char symbol;
};
// ================== Prototypes =================
void intro();
void displayBoard();
void checkForWinner(player);
player makeMove(player);
// ================== Global Variables =================
player player1, player2;
bool gameOn = true;
char board[3][3] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
int scores[3][3] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
// These values are all primes. The products (multiplication) of any of these
// values will produce a unique score for every configuration of the board.
//
// There are only a 8 possible products that represent a winning combination
// Ex: 2 X 3 X 5 = 15 represents a win along the top row. So any score that is
// divisible by 15 represents a board where the player has won along the top row
// The 8 possible winning values are
// top row: 2 X 3 X 5 = 15 left column: 2 X 7 X 17 = 238
// middle row: 7 X 11 X 13 = 1001 middle column: 3 X 11 X 19 = 627
// bottom row: 17 X 19 X 23 = 7429 right column: 5 X 13 X 23 = 1495
// diagonal from top left: 2 X 11 X 23 = 506
// diagonal from top right: 5 X 11 X 17 = 935
// =================== Definitions ==================
void checkForWinner(player _player) // THERE ARE NO ERRORS IN THIS FUNCTION
{ if ((_player.score % 15==0) or (_player.score % 1001 ==0)or (_player.score %7429 ==0)or (_player.score %238 ==0)or (_player.score % 627 ==0)or (_player.score % 1495==0)or (_player.score % 506 ==0)or (_player.score %935 ==0))
{ cout << _player << " WINS!!!!!" << endl;
gameOn = false;
}
}
makeMove(player _player)
{ int xcoordinate=0;
int ycoordinate=0;
cout << "It is " << _player.name << "'s turn (" _player.symbol<< ")" << endl;
do
{
cout << "Enter the column in which you want to play " << endl;
cout << "(left side = 1, middle = 2, right side = 3)" ;
cin >> ycoordinate
cout <<"Enter the row in which you want to play " << endl;
cout "(top = 1, middle = 2, bottom = 3)" ;
cin >> xcoordinate;
}while (board[xcoordinate-1][ycoordinate-1]!=' ');
board[xcoordinate-1][ycoordinate-1] == _player.symbol;
_player.score *= scores[xcoordinate-1][ycoordinate-1];
system("CLS");
return _player;
}
void displayBoard()
{ for (int i=0; i< 2; i++)
{ cout << "-------------" << endl;
for (int j=0; j<2; j++)
{ cout << "| " << board[i][j] <<" "
}
cout << "|" << endl;
}
cout << "-------------"<< endl;
}
void intro()
{ cout << "Player 1, what is your name ";
cin >> player1.name;
cout << "Player 2, what is your name ";
cin >> player2.name;
player1.score=1;
player2.score=1;
player1.symbol=='O';
player2.symbol = 'X';
}
int main(int argc, char *argv[])
{ intro;
displayBoard();
while (gameOn)
{ player1 = makeMove(player1);
displayBoard();
checkForWinner(player1);
if (gameOn)
{ player2 = makeMove(player2);
displayBoard();
checkForWinner(player2);
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
|