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
|
#include <iostream>
#include <string>
using namespace std;
class TicTacToe {
public:
//This constructor sets all the values in the list to 0
TicTacToe()
{
for (int i = 0; i < 10; i++)
pos[i] = 0;
}
//Converts numbers (1 or 2) into X or O respectively from printing.
string numToChar(int x)
{
if(x == 1)
return "X";
else if(x == 2)
return "O";
else
return " ";
}
//Prints out the board and uses the numToChar() func to replace 1 and 2 with X or O.
void printBoard()
{
cout << numToChar(pos[0]) << " | " << numToChar(pos[1]) << " | " << numToChar(pos[2]) << endl;
cout << "----------" << endl;
cout << numToChar(pos[3]) << " | " << numToChar(pos[4]) << " | " << numToChar(pos[5]) << endl;
cout << "----------" << endl;
cout << numToChar(pos[6]) << " | " << numToChar(pos[7]) << " | " << numToChar(pos[8]) << endl << endl;
};
void startUp(string multiOrSing)
{
if(multiOrSing == "multiplayer")
{
while(turns < 10)
{
humanMove(1);
printBoard();
winner();
humanMove(2);
printBoard();
winner();
}
}
else if(multiOrSing == "singleplayer")
{
cout << "How skilled would you like your opponent to be; begginer, easy, medium or hard? ";
cin >> difficulty;
}
}
//Accepts human input and sets it in the array pos[9] accordingly.
void humanMove(int player) //Player is either 1 or 2
{
int move;
cout << "Please enter your move Player " << player << ": ";
cin >> move;
cout << endl;
move--;
//Makes sure no player has already marked the location and that the input value is between 1 and 9.
if(pos[move] == 0 && move < 10 && move >= 0)
{
pos[move] = player;
turns++;
}
else
{
cout << "INVALID MOVE." << endl << endl;
humanMove(player);
}
}
//Determins whether or not a player has won yet.
void winner()
{
//This is a list of the winning combinations.
int winCombo[8][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3 , 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
for(int x = 0; x < 8; x++)
{
if(pos[winCombo[x][0]] == pos[winCombo[x][1]] && pos[winCombo[x][1]] == pos[winCombo[x][2]] && pos[winCombo[x][0]] == 1)
cout << "Player 1 wins!" << endl << endl;
else if(pos[winCombo[x][0]] == pos[winCombo[x][1]] && pos[winCombo[x][1]] == pos[winCombo[x][2]] && pos[winCombo[x][0]] == 2)
cout << "Player 2 wins!";
}
}
private:
int pos[9];
int turns;
string difficulty;
};
int main()
{
TicTacToe ttt;
string todo;
cout << "Welcome to Tic Tac Toe!" << endl << endl << endl;
cout << "Would you like to play a game of Tic Tac Toe? (yes/no) ";
cin >> todo;
cout << endl << endl << endl;
if(todo == "yes")
{
cout << "Below is a plotted grid, enter the number corresponding\nto the location you wish to place your marker." << endl << endl;
cout << "1 | 2 | 3" << endl;
cout << "----------" << endl;
cout << "4 | 5 | 6" << endl;
cout << "----------" << endl;
cout << "7 | 8 | 9" << endl << endl << endl << endl;
cout << "Would you like to play against a friend or the pc? ";
cin >> todo;
cout << endl << endl;
ttt.startUp(todo);
}
else
return 0;
};
|