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 183 184
|
// Tic Tac Toe, by Zach
// Modified by James
#include <iostream>
#include <string>
#include <vector>
#define SIZE 9
using namespace std;
char BOARD[SIZE] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
// The layout of our new type: Player
struct Player
{
string name;
char mark;
Player(string n, char m)
{
name = n;
mark = m;
}
~Player() {};
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// These functions appear in the order they are first called in the program //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void PLAY_GAME(); // Start the game instructions //
string get_names(string*, int); // Enter the players' names //
bool making_move(vector<Player>*, int*); // Handles everything related to a single player's turn on the board //
void show_board(); // Displays the board and its marks //
bool valid_move(char*, int*); // Determines if a given move can be made //
void place_mark(char*, int*); // Places the player's mark whose turn it is //
int winner(); // Returns 0 if a tie, 1 if player 1 has won, and 2 if player 2 has won //
void determine_winner(vector<Player>*, int*); // Determines winner if all spaces are filled //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
PLAY_GAME();
return 0;
}
void PLAY_GAME()
{
// What each element of "data" will represent //
// [0] = Turn - [1] = Current Move - [2] = Total Moves //
/////////////////////////////////////////////////////////
int* data = new int[3];
data[0] = 0;
data[1] = 0;
data[2] = 0;
// Adds two instances of "Player" to a vector "v" //
/////////////////////////////////////////////////////////
string name;
vector<Player>* v = new vector<Player>;
v->push_back(Player(get_names(&name, 1), 'X'));
name.clear();
v->push_back(Player(get_names(&name, 2), 'O'));
name.clear();
// Prevents you from attempting to play longer than the game allows
while(data[2] < SIZE)
{
// Exists solely to keep track of how many turns have occured
cout << "\tTurn " << data[2] + 1 << endl;
// Executes the function until the player makes a valid move
while(making_move(v, data)) {}
// This bit means we will begin checking for a winner
// once enough moves have been made for player 1 to win
if(data[2] >= 4)
if(winner() != 0)
break;
}
show_board();
determine_winner(v, &data[1]);
delete v;
delete[] data;
}
string get_names(string* name, int num)
{
cout << "Enter the name for player " << num << ": ";
cin >> *name; cout << endl;
return *name;
}
bool making_move(vector<Player>* v, int* data)
{
show_board();
cout << "\t" << v->at(data[0]).name << " ... It's your turn.\n";
cout << "\tEnter the number of the spot where you'd like to move: ";
cin >> data[1]; cout << endl;
return valid_move(&v->at(data[0]).mark, data);
}
void show_board()
{
cout << endl;
cout << "\t\t\t\t" << BOARD[0] << " | " << BOARD[1] << " | " << BOARD[2] << endl;
cout << "\t\t\t\t--+---+--" << endl;
cout << "\t\t\t\t" << BOARD[3] << " | " << BOARD[4] << " | " << BOARD[5] << endl;
cout << "\t\t\t\t--+---+--" << endl;
cout << "\t\t\t\t" << BOARD[6] << " | " << BOARD[7] << " | " << BOARD[8] << endl;
cout << endl;
}
bool valid_move(char* mark, int* data)
{
// Determines if the selected spot has already been marked
if (BOARD[data[1]] != 'X' && BOARD[data[1]] != 'O')
{
place_mark(mark, data);
return false;
}
cout << "Invalid Move. Try again." << endl;
return true;
}
void place_mark(char* mark, int* data)
{
BOARD[data[1]] = *mark; // Places mark of the current player at the chosen spot
++data[0] %= 2; // Switches turns
++data[2]; // Updates total number of moves made
}
int winner()
{
if(BOARD[0] == BOARD[1] && BOARD[1] == BOARD[2])
{
if(BOARD[0] == 'X') return 1;
return 2;
}
if(BOARD[3] == BOARD[4] && BOARD[4] == BOARD[5])
{
if(BOARD[3] == 'X') return 1;
return 2;
}
if(BOARD[6] == BOARD[7] && BOARD[7] == BOARD[8])
{
if(BOARD[6] == 'X') return 1;
return 2;
}
if(BOARD[0] == BOARD[3] && BOARD[3] == BOARD[6])
{
if(BOARD[0] == 'X') return 1;
return 2;
}
if(BOARD[1] == BOARD[4] && BOARD[4] == BOARD[7])
{
if(BOARD[1] == 'X') return 1;
return 2;
}
if(BOARD[2] == BOARD[5] && BOARD[5] == BOARD[8])
{
if(BOARD[2] == 'X') return 1;
return 2;
}
if(BOARD[0] == BOARD[4] && BOARD[4] == BOARD[8])
{
if(BOARD[0] == 'X') return 1;
return 2;
}
if(BOARD[2] == BOARD[4] && BOARD[4] == BOARD[6])
{
if(BOARD[2] == 'X') return 1;
return 2;
}
return 0;
}
void determine_winner(vector<Player>* v, int* move)
{
if(winner() != 0)
cout << v->at(winner() - 1).name << " has won the game!\n" << endl;
else
cout << "It's a tie game!" << endl;
}
|