Post: Need help with enhanced tic tac toe program
05-06-2011, 09:07 PM #1
|C++|
< ^ > < ^ >
(adsbygoogle = window.adsbygoogle || []).push({}); ok i recently decided to enhance my tic tac toe to a higher level, i added a menu for 2 players and single player. in single player i added ai for the cpu, it works buy the cpu creating a random number then if it is equal to 1-9 it replaces which number it chose with player2(its really cpu) icon which is 'O'. for some reason it gives me weird text when the cpu goes. run it and you will see whats wrong, then try playing 2 player, it works perfectly using almost the same strategy with the single player. please ignore comments they are pretty off, my code is pretty easy to understand anyway.
    
#include <iostream>
#include<ctime>
#include <cstdlib>
using namespace std;

void player()
{
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;
char player2input;
char playerinput;
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;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}

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)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"YOU WIN!";break;}

}
cout<<endl;
}
void singleplayer()
{
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;
int player2input;
char playerinput;
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
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)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
srand(time(0));
player2input = (rand() % 9) + 1;
cout<<"O:\n"<<player2input;
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '1' && box1 == '1'Winky Winky
{
box1 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '2' && box2 == '2'Winky Winky
{
box2 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '3' && box3 == '3'Winky Winky
{
box3 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '4' && box4 == '4'Winky Winky
{
box4 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '5' && box5 == '5'Winky Winky
{
box5 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '6' && box6 == '6'Winky Winky
{
box6 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '7' && box7 == '7'Winky Winky
{
box7 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '8' && box8 == '8'Winky Winky
{
box8 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(player2input == '9' && box9 == '9'Winky Winky
{
box9 = player2icon;
}
srand(time(0));
player2input = (rand() % 9) + 1;
if(box1==box5&&box9==box5)
{
cout<<"CPU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"CPU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"CPU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"CPU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"CPU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"CPU WIN!";break;}

}

}
int main()
{
char c;
cout<<"Welcome to tic tac toe, single player [X] or 2 player[O]:";
cin>>c;
if(c == 'X'|| c == 'x'Winky Winkyplayer();
if(c == 'O'|| c == 'o'Winky Winkysingleplayer();

system("pause");
}
(adsbygoogle = window.adsbygoogle || []).push({});
05-08-2011, 02:21 AM #2
|C++|
< ^ > < ^ >
bumb..........
05-08-2011, 06:55 AM #3
Your treating playerinput2 as a CHAR when its really a int. You are defining it incorrectly on your statement on the CPU turn. Instead of using ' ', take that out around the numbers and then it should work.

    #include <iostream> 
#include<ctime>
#include <cstdlib>
using namespace std;

void player()
{
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;
char player2input;
char playerinput;
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;
}
if(box1==box5&&box9==box5)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}

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)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"YOU WIN!";break;}

}
cout<<endl;
}


void singleplayer()
{
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;
int player2input;
char playerinput;
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
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)
{
cout<<"YOU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"YOU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"YOU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"YOU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"YOU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"YOU WIN!";break;}
if(box3==box6&&box6==box9)
{
cout<<"YOU WIN!";break;}
cout<<box1<<"|"<<box2<<"|"<<box3<<endl;
cout<<"-+-+-"<<endl;
cout<<box4<<"|"<<box5<<"|"<<box6<<endl;
cout<<"-+-+-"<<endl;
cout<<box7<<"|"<<box8<<"|"<<box9<<endl;
srand(time(0));
player2input = (rand() % 9) + 1;
cout<<"O:\n";
if(player2input == 1 && box1 == '1'Winky Winky
{
box1 = player2icon;
}
if(player2input == 2 && box2 == '2'Winky Winky
{
box2 = player2icon;
}
if(player2input == 3 && box3 == '3'Winky Winky
{
box3 = player2icon;
}
if(player2input == 4 && box4 == '4'Winky Winky
{
box4 = player2icon;
}
if(player2input == 5 && box5 == '5'Winky Winky
{
box5 = player2icon;
}
if(player2input == 6 && box6 == '6'Winky Winky
{
box6 = player2icon;
}
if(player2input == 7 && box7 == '7'Winky Winky
{
box7 = player2icon;
}
if(player2input == 8 && box8 == '8'Winky Winky
{
box8 = player2icon;
}
if(player2input == 9 && box9 == '9'Winky Winky
{
box9 = player2icon;
}
if(box1==box5&&box9==box5)
{
cout<<"CPU WIN!";break;}
if(box1==box2&&box3==box2)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box4==box7)
{cout<<"CPU WIN!";break;}
if(box7==box8&&box9==boxCool Man (aka Tustin)
{cout<<"CPU WIN!";break;}
if(box2==box5&&box8==box2)
{cout<<"CPU WIN!";break;}
if(box3==box7&&box7==box5)
{cout<<"CPU WIN!";break;}
if(box1==box4&&box7==box4)
{cout<<"CPU WIN!";break;}
if(box3==box6&&box6==box9)
{cout<<"CPU WIN!";break;}

}

}
int main()
{
char c;
cout<<"Welcome to tic tac toe, single player [X] or 2 player[O]:";
cin>>c;
if(c == 'X'|| c == 'x'Winky Winkyplayer();
if(c == 'O'|| c == 'o'Winky Winkysingleplayer();

system("pause");
}


---------- Post added at 01:55 AM ---------- Previous post was at 01:54 AM ----------

Originally posted by KingDeath360 View Post
i doubt anyone here can code.
go on hf & post a "help" thread like all the other skids
:thumbsup:


You thought wrong.
05-10-2011, 07:48 PM #4
|C++|
< ^ > < ^ >
silly me, making silly mistakes

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo