Post: A small fix is needed to this c++ project
04-29-2011, 01:27 AM #1
|C++|
< ^ > < ^ >
(adsbygoogle = window.adsbygoogle || []).push({}); ok here is a c++ tic tac toe project, it works just fine but when someone wins(3 x's in a row, it still allows "O" to move, instantly after "O"enters its move it states the winner like it should, but that "O(or x,whatever the case)" should not be able to move after someone has 3 in a row.. can you fix this. If you run this you will see what i mean.
    

#include <iostream>

using namespace std;

int main()
{
//declares selections on the board
char box1('1'Winky Winky;
char box2('2'Winky Winky;
char box3('3'Winky Winky;
char box4('4'Winky Winky;
char box5('5'Winky Winky;
char box6('6'Winky Winky;
char box7('7'Winky Winky;
char box8('8'Winky Winky;
char box9('9'Winky Winky;
char player2icon('O'Winky Winky;
char playericon('X'Winky Winky; //the players icon(x or o)
char player2input;
char playerinput; //what the player inputs(1-9)
bool go;
go = true;
while(go = true)
{
//draws the board
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"X:";
cin>>playerinput;//player 1 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(playerinput == '1' && box1 == '1'Winky Winky
{
box1 = playericon;
}
else if(playerinput == '2' && box2 == '2'Winky Winky
{
box2 = playericon;
}
if(playerinput == '3' && box3 == '3'Winky Winky
{
box3 = playericon;
}
else if(playerinput == '4' && box4 == '4'Winky Winky
{
box4 = playericon;
}
if(playerinput == '5' && box5 == '5'Winky Winky
{
box5 = playericon;
}
else if(playerinput == '6' && box6 == '6'Winky Winky
{
box6 = playericon;
}
if(playerinput == '7' && box7 == '7'Winky Winky
{
box7 = playericon;
}
else if(playerinput == '8' && box8 == '8'Winky Winky
{
box8 = playericon;
}
else if(playerinput == '9' && box9 == '9'Winky Winky
{
box9 = playericon;
}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"O:";
cin>>player2input;//player 2 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(player2input == '1' && box1 == '1'Winky Winky
{
box1 = player2icon;
}
else if(player2input == '2' && box2 == '2'Winky Winky
{
box2 = player2icon;
}
if(player2input == '3' && box3 == '3'Winky Winky
{
box3 = player2icon;
}
else if(player2input == '4' && box4 == '4'Winky Winky
{
box4 = player2icon;
}
if(player2input == '5' && box5 == '5'Winky Winky
{
box5 = player2icon;
}
else if(player2input == '6' && box6 == '6'Winky Winky
{
box6 = player2icon;
}
if(player2input == '7' && box7 == '7'Winky Winky
{
box7 = player2icon;
}
else if(player2input == '8' && box8 == '8'Winky Winky
{
box8 = player2icon;
}
else if(player2input == '9' && box9 == '9'Winky Winky
{
box9 = player2icon;
}
if(box1==box5&&box9==box5)
{break;}
if(box1==box2&&box3==box2)
{break;}
if(box1==box4&&box4==box7)
{break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{break;}
if(box2==box5&&box8==box2)
{break;}
if(box3==box7&&box7==box5)
{break;}
if(box1==box4&&box7==box4)
{break;}
if(box3==box6&&box6==box9)
{break;}
}
if(playericon>player2icon)cout<<playericon<<" WINS!!";
else if(player2icon>playericon )cout<<playericon<<"WINS!!";
char f;
cin>>f;
}

it needs a small fix but i dont really see the problem.
ps. my comments are sort of inaccurate sand outdated so dont rely too much on em
(adsbygoogle = window.adsbygoogle || []).push({});
04-29-2011, 08:58 PM #2
This is happening because there is no check after the X's turn. So if he ends up winning then it will go straight to O and then check for a win.

Heres a working one:

    #include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
//declares selections on the board
char box1('1'Winky Winky,box2('2'Winky Winky,box3('3'Winky Winky,box4('4'Winky Winky,box5('5'Winky Winky,box6('6'Winky Winky,box7('7'Winky Winky,box8('8'Winky Winky,box9('9'Winky Winky;
char player2icon('O'Winky Winky;
char playericon('X'Winky Winky; //the players icon(x or o)
char player2input;
char playerinput; //what the player inputs(1-9)
bool go;
go = true;
while(go = true)
{
//draws the board
system("CLS");
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"X:";
cin>>playerinput;//player 1 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(playerinput == '1' && box1 == '1'Winky Winky
{
box1 = playericon;
}
else if(playerinput == '2' && box2 == '2'Winky Winky
{
box2 = playericon;
}
if(playerinput == '3' && box3 == '3'Winky Winky
{
box3 = playericon;
}
else if(playerinput == '4' && box4 == '4'Winky Winky
{
box4 = playericon;
}
if(playerinput == '5' && box5 == '5'Winky Winky
{
box5 = playericon;
}
else if(playerinput == '6' && box6 == '6'Winky Winky
{
box6 = playericon;
}
if(playerinput == '7' && box7 == '7'Winky Winky
{
box7 = playericon;
}
else if(playerinput == '8' && box8 == '8'Winky Winky
{
box8 = playericon;
}
else if(playerinput == '9' && box9 == '9'Winky Winky
{
box9 = playericon;
}
if(box1==box5&&box9==box5)
{break;}
if(box1==box2&&box3==box2)
{break;}
if(box1==box4&&box4==box7)
{break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{break;}
if(box2==box5&&box8==box2)
{break;}
if(box3==box7&&box7==box5)
{break;}
if(box1==box4&&box7==box4)
{break;}
if(box3==box6&&box6==box9)
{break;}

system("CLS");
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"O:";
cin>>player2input;//player 2 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(player2input == '1' && box1 == '1'Winky Winky
{
box1 = player2icon;
}
else if(player2input == '2' && box2 == '2'Winky Winky
{
box2 = player2icon;
}
if(player2input == '3' && box3 == '3'Winky Winky
{
box3 = player2icon;
}
else if(player2input == '4' && box4 == '4'Winky Winky
{
box4 = player2icon;
}
if(player2input == '5' && box5 == '5'Winky Winky
{
box5 = player2icon;
}
else if(player2input == '6' && box6 == '6'Winky Winky
{
box6 = player2icon;
}
if(player2input == '7' && box7 == '7'Winky Winky
{
box7 = player2icon;
}
else if(player2input == '8' && box8 == '8'Winky Winky
{
box8 = player2icon;
}
else if(player2input == '9' && box9 == '9'Winky Winky
{
box9 = player2icon;
}
if(box1==box5&&box9==box5)
{break;}
if(box1==box2&&box3==box2)
{break;}
if(box1==box4&&box4==box7)
{break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{break;}
if(box2==box5&&box8==box2)
{break;}
if(box3==box7&&box7==box5)
{break;}
if(box1==box4&&box7==box4)
{break;}
if(box3==box6&&box6==box9)
{break;}


}
if(playericon>player2icon)cout<<playericon<<" WINS!!";
else if(player2icon>playericon )cout<<playericon<<"WINS!!";
char f;
cin>>f;
}
05-01-2011, 12:48 PM #3
Originally posted by SLiiTH3R View Post
ok here is a c++ tic tac toe project, it works just fine but when someone wins(3 x's in a row, it still allows "O" to move, instantly after "O"enters its move it states the winner like it should, but that "O(or x,whatever the case)" should not be able to move after someone has 3 in a row.. can you fix this. If you run this you will see what i mean.
    

#include <iostream>

using namespace std;

int main()
{
//declares selections on the board
char box1('1'Winky Winky;
char box2('2'Winky Winky;
char box3('3'Winky Winky;
char box4('4'Winky Winky;
char box5('5'Winky Winky;
char box6('6'Winky Winky;
char box7('7'Winky Winky;
char box8('8'Winky Winky;
char box9('9'Winky Winky;
char player2icon('O'Winky Winky;
char playericon('X'Winky Winky; //the players icon(x or o)
char player2input;
char playerinput; //what the player inputs(1-9)
bool go;
go = true;
while(go = true)
{
//draws the board
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"X:";
cin>>playerinput;//player 1 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(playerinput == '1' && box1 == '1'Winky Winky
{
box1 = playericon;
}
else if(playerinput == '2' && box2 == '2'Winky Winky
{
box2 = playericon;
}
if(playerinput == '3' && box3 == '3'Winky Winky
{
box3 = playericon;
}
else if(playerinput == '4' && box4 == '4'Winky Winky
{
box4 = playericon;
}
if(playerinput == '5' && box5 == '5'Winky Winky
{
box5 = playericon;
}
else if(playerinput == '6' && box6 == '6'Winky Winky
{
box6 = playericon;
}
if(playerinput == '7' && box7 == '7'Winky Winky
{
box7 = playericon;
}
else if(playerinput == '8' && box8 == '8'Winky Winky
{
box8 = playericon;
}
else if(playerinput == '9' && box9 == '9'Winky Winky
{
box9 = playericon;
}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
cout<<"O:";
cin>>player2input;//player 2 input
cout<<endl;
//checks for valid answer(and checks the answer)
if(player2input == '1' && box1 == '1'Winky Winky
{
box1 = player2icon;
}
else if(player2input == '2' && box2 == '2'Winky Winky
{
box2 = player2icon;
}
if(player2input == '3' && box3 == '3'Winky Winky
{
box3 = player2icon;
}
else if(player2input == '4' && box4 == '4'Winky Winky
{
box4 = player2icon;
}
if(player2input == '5' && box5 == '5'Winky Winky
{
box5 = player2icon;
}
else if(player2input == '6' && box6 == '6'Winky Winky
{
box6 = player2icon;
}
if(player2input == '7' && box7 == '7'Winky Winky
{
box7 = player2icon;
}
else if(player2input == '8' && box8 == '8'Winky Winky
{
box8 = player2icon;
}
else if(player2input == '9' && box9 == '9'Winky Winky
{
box9 = player2icon;
}
if(box1==box5&&box9==box5)
{break;}
if(box1==box2&&box3==box2)
{break;}
if(box1==box4&&box4==box7)
{break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{break;}
if(box2==box5&&box8==box2)
{break;}
if(box3==box7&&box7==box5)
{break;}
if(box1==box4&&box7==box4)
{break;}
if(box3==box6&&box6==box9)
{break;}
}
if(playericon>player2icon)cout<<playericon<<" WINS!!";
else if(player2icon>playericon )cout<<playericon<<"WINS!!";
char f;
cin>>f;
}

it needs a small fix but i dont really see the problem.
ps. my comments are sort of inaccurate sand outdated so dont rely too much on em


TicTacToe ! =o i wanna make one now :p brb

The following user thanked Relevant for this useful post:

.Rush.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo