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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
void Intro();
void TicTacDefault(vector<int>& rboard);
void TicTacDisplay(vector<int>& rboard);
char playerXorO();
char computerXorO(char PLAYER);
char MakeaMove(char PLAYER, char COMP);
void Pmove(char);
void Cmove(char);
int CompCalc(char);
bool IsLegal(int, vector<int>& rboard);
void UpdateBoard(char, int, vector<int>& rboard);
bool WhoWins(char a);
bool WinningMoves(char a, vector<int>& rboard);
vector<int> board;
vector<int>& rboard = board;
int main()
{
Intro();
vector<int> board;
vector<int>& rboard = board;
TicTacDefault(rboard);
TicTacDisplay(rboard);
const char PLAYER=playerXorO();
const char COMP=computerXorO(PLAYER);
cout << PLAYER << endl;
cout << COMP << endl;
if (MakeaMove(PLAYER, COMP)==PLAYER)
cout << "Player Wins!";
else
cout << "Computer Wins";
return 0;
}
void Intro()
{
cout << "\nTic Tac Toe!\n\n";
cout << "\nYou and a computer will go at it to see who the Tic-Tac-Toe champion is!\n";
cout << "See if you can beat him!\n";
cout << "Simply input the number that corresponds to each position.\n";
}
void TicTacDefault (vector<int>& rboard)
{
rboard.push_back(1);
rboard.push_back(2);
rboard.push_back(3);
rboard.push_back(4);
rboard.push_back(5);
rboard.push_back(6);
rboard.push_back(7);
rboard.push_back(8);
rboard.push_back(9);
}
void TicTacDisplay (vector<int>& rboard)
{
cout << "\n\t " << rboard[0] << " | " << rboard[1] << " | " << rboard[2] << '\n';
cout << "\t---|---|---\n";
cout << "\t " << rboard[3] << " | " << rboard[4] << " | " << rboard[5] << '\n';
cout << "\t---|---|---\n";
cout << "\t " << rboard[6] << " | " << rboard[7] << " | " << rboard[8] << '\n';
return;
}
//WHO IS WHO?
char playerXorO()
{
char playerChoice;
cout << "\nBut first, take your pick,";
TypeAgain:
cout << " X or O? X goes first.\n";
cin >> playerChoice;
toupper(playerChoice);
if (playerChoice=='X')
{
cout << "\nYou'll go first\n";
return 'X';
}
else if (playerChoice=='O')
{
cout << "\nLooks like i'll be going first\n";
return 'O';
}
else ({ //ask for player input again if answer is nonexistent
cout << "That's not a choice\n";
goto TypeAgain;
});
}
inline char computerXorO (char PLAYER)
{
if (PLAYER=='X')
{
const char COMP='O';
return COMP;
}
else
{
const char COMP='X';
return COMP;
}
}
//Dictate order, update board with moves, check if they are legal
char MakeaMove(char PLAYER, char COMP) //dictate order
{
if(PLAYER=='X' && COMP=='O') //player goes first
{
do
{
Pmove(PLAYER);
if (WhoWins(PLAYER)==true)
return PLAYER;
Cmove(COMP);
} while(WhoWins(COMP)!=true);
return COMP;
}
else if(PLAYER=='O' && PLAYER=='X') //computer goes first
{
do
{
Cmove(COMP);
if (WhoWins(COMP)==true)
return COMP;
Pmove(PLAYER);
} while (WhoWins(PLAYER)!=true);
return PLAYER;
}
}
void Pmove(char PLAYER) //the player enters a move, checks if legal, updates board
{
int i;
cout << "\nYour move\n";
TicTacDisplay(rboard);
cout << endl;
do
{
cin >> i;
IsLegal(i,rboard);
} while (IsLegal(i, rboard)!=1);
UpdateBoard(PLAYER, i, rboard);
}
void Cmove(char COMP)//computer calculates a move, checks if legal, updates board
{
int i;
cout << "\nMy move\n";
do
{
i=CompCalc(COMP);
IsLegal(i,rboard);
} while (IsLegal(i,rboard)!=true);
UpdateBoard(COMP, i, rboard);
}
bool IsLegal(int i, vector<int>&rboard)
{
cout << "isLegal";
if (rboard[i]!='X' || rboard[i]!='O' || i<9)
{
return true;
}
cout << "\nThat move is illegal! Try again.\n";
return false;
}
void UpdateBoard (char a, int i, vector<int>& rboard)
{
rboard[i]=a;
TicTacDisplay(rboard);
}
int CompCalc(char a) //right now just stupid.
{
cout << "peepee";
for (vector<int>::const_iterator iter=rboard.begin(); iter<rboard.end(); ++iter)
{
if (*iter=='X' || *iter=='O' || *iter<9)
{
break;
return *iter;
}
}
}
//WINNING CHECKS
bool WhoWins(char a)
{
if (WinningMoves(a, rboard)==true)
return true;
else
return false;
}
bool WinningMoves(char a, vector<int>& rboard)//a is PLAYER or COMP
{
if (rboard[0]==a && rboard[1]==a && rboard[2]==a)
return true;
else if (rboard[0]==a && rboard[3]==a && rboard[6]==a)
return true;
else if (rboard[0]==a && rboard[4]==a && rboard[8]==a)
return true;
else if (rboard[1]==a && rboard[4]==a && rboard[7]==a)
return true;
else if (rboard[2]==a && rboard[5]==a && rboard[8]==a)
return true;
else if (rboard[2]==a && rboard[4]==a && rboard[6]==a)
return true;
else if (rboard[3]==a && rboard[4]==a && rboard[5]==a)
return true;
else if (rboard[6]==a && rboard[7]==a && rboard[8]==a)
return true;
else
return false;
}
|