Tic-Tac-Toe - Detecting a "tie" error
Nov 30, 2012 at 3:43am UTC
Everything seems to be working fine unless the game ends in a tie (all tic-tac-toe squares are filled and no one has won the game). Can someone take a look at my code and see if you can identify the problem I am missing. The error is a logic one - not compile error, and is most likely with the code in the very last function at the bottom. :/
(If you enter 4, 0,1,2,3,5,6,7,8 you will quickly reach a tie game)
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//Global Variable
vector<string> contents (9);
//Declare Functions
void DisplayGrid();
string CheckEnd();
void NextPlayer(string&);
int main()
{
//Main Scope Variables
bool gameRunning = true ;
string thisPlayer = "X" ;
int gridCode;
bool validMove = false ;
//Clear board contents
for (int i=0; i < contents.size(); ++i)
contents[i] = " " ;
//Show the grid
DisplayGrid();
//Run game loop
do {
//Get a valid response from user
while (validMove == false )
{
cout << "\nEnter a valid grid code to place " << thisPlayer << ": " ;
cin >> gridCode;
if (cin.fail()) return 0;
if (gridCode <= 8 && gridCode >= 0)
if (contents[gridCode] == " " ) validMove = true ;
}
//Set the contents of the grid square to player response
contents[gridCode] = thisPlayer;
//Show the updated grid
DisplayGrid();
//Check for a win
string aWin = CheckEnd();
if (aWin != "" )
{
//GAME OVER!
if (aWin != "c" ) cout << "Player " << aWin << " has won the game!\n\n" ;
if (aWin == "c" ) cout << "Awww.. nobody won this round.\n\n" ;
gameRunning = false ;
}
//Toggle next player's turn
NextPlayer(thisPlayer);
//Reset for next loop iteration
validMove = false ;
} while (gameRunning);
cout << "\n\n\n" ;
system("pause" );
return 0;
}
void NextPlayer(string &thisplyr) { thisplyr = (thisplyr == "X" ) ? "O" : "X" ; }
void DisplayGrid()
{
system("CLS" );
cout << "GRID CODES: \n" ;
cout << " " << 0 << " | " << 1 << " | " << 2 << "\n" ;
cout << "-----------\n" ;
cout << " " << 3 << " | " << 4 << " | " << 5 << "\n" ;
cout << "-----------\n" ;
cout << " " << 6 << " | " << 7 << " | " << 8 << "\n" ;
cout << "\n\n" ;
cout << " " << contents[0] << " | " << contents[1] << " | " << contents[2] << "\n" ;
cout << "-----------\n" ;
cout << " " << contents[3] << " | " << contents[4] << " | " << contents[5] << "\n" ;
cout << "-----------\n" ;
cout << " " << contents[6] << " | " << contents[7] << " | " << contents[8] << "\n" ;
}
string CheckEnd()
{
string w = "" ;
//Check for a winning combination
if (contents[0] != " " && contents[0] == contents[1] && contents[1] == contents[2]) w = contents[0];
if (contents[3] != " " && contents[3] == contents[4] && contents[4] == contents[5]) w = contents[3];
if (contents[6] != " " && contents[6] == contents[7] && contents[7] == contents[8]) w = contents[6];
if (contents[0] != " " && contents[0] == contents[3] && contents[3] == contents[6]) w = contents[0];
if (contents[1] != " " && contents[1] == contents[4] && contents[4] == contents[7]) w = contents[1];
if (contents[2] != " " && contents[2] == contents[5] && contents[5] == contents[8]) w = contents[2];
if (contents[0] != " " && contents[0] == contents[4] && contents[4] == contents[8]) w = contents[0];
if (contents[2] != " " && contents[2] == contents[4] && contents[4] == contents[6]) w = contents[2];
//If no win, check to see if game ended in tie
bool NOTtied = false ;
if (w == "" )
{
for (int i =0; i<contents.size(); ++i)
if (contents[i] == " " ) NOTtied = true ;
if (NOTtied = false ) w = "c" ;
}
return w;
}
Nov 30, 2012 at 4:03am UTC
Check your if statement on line 110
(Hint: =, ==)
Nov 30, 2012 at 4:05am UTC
DOH... What a noob mistake. :p Got it fixed, thanks! :D
Topic archived. No new replies allowed.