Tic Tac Toe error
Jun 29, 2012 at 2:26pm UTC
I am doing a Tic Tac Toe program and it compiles correctley (DevC++) and runs fine untill you enter 3 for the third place on the grid. As soon as any player hits 3 they automaticly win, whether it's the first choise or not. Could you please check over my code and give me some pointers.
Many thanks.
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
//-------------------------------------------------------------------------
// TicTacToe.cpp
//
// A simple naughts & crosses game.
//
//-------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char GridA [10] = {'i' ,'i' ,'i' ,'i' ,'i' ,'i' ,'i' ,'i' ,'i' ,'i' };
int Player = 1;
int choise = 0;
bool Play;
Play = true ; // Set this to true for game loop.
cout << "+===========+ +===========+\n" // Display the game board
<< "|[" << GridA[1] << "]|[" << GridA[2] << "]|["
<< GridA[3] << "]| |[1]|[2]|[3]|\n"
<< "+---+---+---+ +---+---+---+\n"
<< "|[" << GridA[4] << "]|[" << GridA[5] << "]|["
<< GridA[6] << "]| |[4]|[5]|[6]|\n"
<< "+---+---+---+ +---+---+---+\n"
<< "|[" << GridA[7] << "]|[" << GridA[8] << "]|["
<< GridA[9] << "]| |[7]|[8]|[9]|\n"
<< "+===========+ +===========+ \n" << endl;
while (Play == true ) // Start of Game loop
{ // - loops until the gam is won.
if (Player == 3)
{ // Checks if player is Player 2 then resets
Player = 1; // to Player 1.
}
cout << "Player " << Player << " : " ;
cin >> choise;
if (GridA[choise] == 'i' )// Tests to see if that part of the grid is free
{
if (Player == 1)
{
GridA[choise] = 'X' ;
}
else if (Player == 2)
{
GridA[choise] = 'O' ;
}
Player++;
system("CLS" ); // Yeh Yeh! I know some programmers don't like it.
// But with limited programming skills it's staying
// until I learn a better way.
cout << "+===========+ +===========+\n" // Place the players choise on screen
<< "|[" << GridA[1] << "]|[" << GridA[2] << "]|["
<< GridA[3] << "]| |[1]|[2]|[3]|\n"
<< "+---+---+---+ +---+---+---+\n"
<< "|[" << GridA[4] << "]|[" << GridA[5] << "]|["
<< GridA[6] << "]| |[4]|[5]|[6]|\n"
<< "+---+---+---+ +---+---+---+\n"
<< "|[" << GridA[7] << "]|[" << GridA[8] << "]|["
<< GridA[9] << "]| |[7]|[8]|[9]|\n"
<< "+===========+ +===========+ \n" << endl;
if (GridA[1] && GridA[2] && GridA[3] == 'X' || // Test to see if player 1 has won.
GridA[4] && GridA[5] && GridA[6] == 'X' ||
GridA[7] && GridA[8] && GridA[9] == 'X' ||
GridA[1] && GridA[4] && GridA[7] == 'X' ||
GridA[2] && GridA[5] && GridA[6] == 'X' ||
GridA[3] && GridA[6] && GridA[9] == 'X' ||
GridA[1] && GridA[5] && GridA[9] == 'X' ||
GridA[3] && GridA[5] && GridA[7] == 'X' )
{
cout << "Player 1 WINS!!!!" << endl;
Play = false ; // Player 1 has won exit the loop
}
if (GridA[1] && GridA[2] && GridA[3] == 'O' || // Test to see if player 2 has won.
GridA[4] && GridA[5] && GridA[6] == 'O' ||
GridA[7] && GridA[8] && GridA[9] == 'O' ||
GridA[1] && GridA[4] && GridA[7] == 'O' ||
GridA[2] && GridA[5] && GridA[6] == 'O' ||
GridA[3] && GridA[6] && GridA[9] == 'O' ||
GridA[1] && GridA[5] && GridA[9] == 'O' ||
GridA[3] && GridA[5] && GridA[7] == 'O' )
{
cout << "Player 2 WINS!!!!" << endl; // Player 1 has won exit the loop
Play = false ;
}
}
else if (GridA[choise] != 'i' ) // if place on grid is taken show
{ // show message
cout << "Invalid Choise - Try Again!" << endl;
}
}
system("pause" );
return EXIT_SUCCESS;
}
Jun 29, 2012 at 2:47pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if (GridA[1] && GridA[2] && GridA[3] == 'X' || // Test to see if player 1 has won.
GridA[4] && GridA[5] && GridA[6] == 'X' ||
GridA[7] && GridA[8] && GridA[9] == 'X' ||
GridA[1] && GridA[4] && GridA[7] == 'X' ||
GridA[2] && GridA[5] && GridA[6] == 'X' ||
GridA[3] && GridA[6] && GridA[9] == 'X' ||
GridA[1] && GridA[5] && GridA[9] == 'X' ||
GridA[3] && GridA[5] && GridA[7] == 'X' )
if (GridA[1] && GridA[2] && GridA[3] == 'O' || // Test to see if player 2 has won.
GridA[4] && GridA[5] && GridA[6] == 'O' ||
GridA[7] && GridA[8] && GridA[9] == 'O' ||
GridA[1] && GridA[4] && GridA[7] == 'O' ||
GridA[2] && GridA[5] && GridA[6] == 'O' ||
GridA[3] && GridA[6] && GridA[9] == 'O' ||
GridA[1] && GridA[5] && GridA[9] == 'O' ||
GridA[3] && GridA[5] && GridA[7] == 'O' )
Look at this and think what you might have done wrong.
@down. fine, if we want to give him the answer straight away.. make the lines the same, just put each line in a parenthesis. ( check first line) || (check second line) || etc
Last edited on Jun 29, 2012 at 2:54pm UTC
Jun 29, 2012 at 2:49pm UTC
It's lines like that that are the problem.
GridA[1] && GridA[2] && GridA[3] == 'X'
It should be something like:
GridA[0] == GridA[1] && GridA[0] == GridA[2]
Last edited on Jun 29, 2012 at 2:50pm UTC
Jun 30, 2012 at 9:39am UTC
Thank you guys!!!
if ((GridA[1] == GridA[2]) && (GridA[1] == GridA[3]) && (GridA[1] == 'X' ) ||
Works great!
Topic archived. No new replies allowed.