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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
void Showblankguide();
void board(char array [][3], int size);
int player_turn(int num);
int Showmenu(int num);
int choicefunc();
int cpuMove(int userMove);
int whoAreYou(char letter);
bool isPlaying();
char plotter();
char grid4[3][3] = {{'8', '1', '6'},
{'3', '5', '7'},
{'4', '9', '2'}};
int main(int argc, char const *argv[]) {
bool isPlaying = true;
cout << fixed << showpoint << setprecision(0); // Set up numeric output formatting
choicefunc();
plotter();
return 0;
}
void Showblankguide()
{
cout <<"Player1 vs. CPU" << endl;
cout << " | | " << endl;
cout << " " << "A1" << " | " << "A2" << " | " << "A3" << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << "B1" << " | " "C2" << " | " << "C3" << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << "C1" << " | " << "C2" << " | " << "C3" << endl;
cout << " | | " << endl << endl;
}
int Showmenu(int choice)
{
const int X_CHOICE = 1, //Constants for the menu choices
O_CHOICE = 2,
QUIT_CHOICE = 3;
Showblankguide();
cout << "\n\t\tTIC TAC TOE MENU \n\n"
<< "\n\t\t1. I want to be X \n"
<< "\n\t\t2. I want to be O \n"
<< "\n\t\t3. Quit the program.\n"
<< "\n\t\t===================================\n"
<< "\n\t\tEnter you choice: ";
cin >> choice;
while (choice < X_CHOICE || choice > QUIT_CHOICE)
{
cout << "Enter a valid choice: ";
cin >> choice;
}
return choice;
}
int choicefunc(){
char player;
char cpu;
int choice = Showmenu(choice);
const int X_CHOICE = 1, //Constants for the menu choices
O_CHOICE = 2,
QUIT_CHOICE = 3;
switch (choice)
{
case X_CHOICE:player = 'X';whoAreYou(player); cpu = 'O'; break;
case O_CHOICE:player = 'O';whoAreYou(player); cpu = 'X'; break;
}
return player;
}
int whoAreYou(char player)
{
int anarray[5];
int turn;
char cpu = 'X';
if (player == 'X') cpu = 'O';
cout << " \n";
cout << "You are " << player << endl;
cout << "Designate where you want " << player << " to move: \n\n";
plotter();
}
int cpuMove(int userMove)
{ //subtracts usersMove from 15, 2 numbers equal the difference, 1 is randomly chosen
int starter = 15-userMove;
char cpu = 'O'; // <<<<<<<<<<< THIS IS WRONG, HARD CODE, BAD CODE :(
srand(time(NULL));
int number1 = rand() % starter + 1;
int counterMove = starter - number1;
if (userMove == 0) counterMove = number1;
switch (counterMove)
{ //assigns cpu to [x][y] and shows updated board
case 8: grid4[0][0] = cpu ; board(grid4, 3); break ;
case 1: grid4[0][1] = cpu ; board(grid4, 3); break ;
case 6: grid4[0][2] = cpu ; board(grid4, 3); break ;
case 3: grid4[1][0] = cpu ; board(grid4, 3); break ;
case 5: grid4[1][1] = cpu ; board(grid4, 3); break ;
case 7: grid4[1][2] = cpu ; board(grid4, 3); break ;
case 4: grid4[2][0] = cpu ; board(grid4, 3); break ;
case 9: grid4[2][1] = cpu ; board(grid4, 3); break ;
case 2: grid4[2][2] = cpu ; board(grid4, 3); break ;
}
}
//Constants for array input, assigns player to position in the grid4 array, gives counterMove the int value of coordinate, & updates board
char plotter(){
string coordinates;
int counterMove;
char player = 'X'; // <<<<<<<<<<< THIS IS WRONG, HARD CODE, BAD CODE :(
Showblankguide();
getline(cin,coordinates);
if (coordinates == "A1") {
grid4[0][0] = player ; counterMove = cpuMove(8); board(grid4, 3);
}
else if (coordinates == "A2") {
grid4[0][1] = player ; counterMove = cpuMove(1); board(grid4, 3);
}
else if (coordinates == "A3") {
grid4[0][2] = player ; counterMove = cpuMove(6); board(grid4, 3);
}
else if (coordinates == "B1") {
grid4[1][0] = player ; counterMove = cpuMove(3); board(grid4, 3);
}
else if (coordinates == "B2") {
grid4[1][1] = player ; counterMove = cpuMove(5); board(grid4, 3);
}
else if (coordinates == "B3") {
grid4[1][2] = player ; counterMove = cpuMove(7); board(grid4, 3);
}
else if (coordinates == "C1") {
grid4[2][0] = player ; counterMove = cpuMove(4); board(grid4, 3);
}
else if (coordinates == "C2" ) {
grid4[2][1] = player ; counterMove = cpuMove(9); board(grid4, 3);
}
else if (coordinates == "C3" ) {
grid4[2][2] = player ; counterMove = cpuMove(2); board(grid4, 3);
return 0;
}
}
void board(char array [][3], int size)
{
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 vs. CPU " << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << array[0][0] << " | " << array[0][1] << " | " << array[0][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << array[1][0] << " | " << array[1][1] << " | " << array[1][2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << array[2][0] << " | " << array[2][1] << " | " << array[2][2] << endl;
cout << " | | " << endl << endl;
}
|