Jan 28, 2014 at 7:10pm UTC
Hey people, I've just started messing around with c++.
I made this Tic Tac Toe game but I felt there was too much if else going on.
Can anyone point out how i could simplify this.
I'm wondering if
-I can do the same without using so many if else
-Can you point me towards a professional Tic Tac Toe code
-I tried using a class but could not acces the multidimensional array from the class so i gave that up and did it all on the main cpp.
Thanks for any help.
First time post so please excuse any weirdness if it comes out wrong.
ok its in two parts cuz it is over the max.
[code]
#include "Tac.h"
#include <iostream>
#include <string>
#include "stdlib.h"
using namespace std;
void printBoard();
void humanMove();
void firstMove();
void secondMove();
void thirdMove();
void fourthMove();
void cpuMove();
void turn1();
void turn2();
void turn3();
void turn4();
void turnlast();
void winCheck();
void playAgain();
string e[3][3] = {{"-","-","-"},{"-","-","-"},{"-","-","-"}};
string *p1 = &e[0][0];//SETTING POINTERS FOR EACH BOX IN PHONE ORDER
string *p2 = &e[0][1];
string *p3 = &e[0][2];
string *p4 = &e[1][0];
string *p5 = &e[1][1];
string *p6 = &e[1][2];
string *p7 = &e[2][0];
string *p8 = &e[2][1];
string *p9 = &e[2][2];
int main()
{
cout << "Welcome to Tic Tac Tow" << endl << "Good Luck" << endl << endl;
turn1();
turn2();
turn3();
turn4();
turnlast();
playAgain();
}
void printBoard()
{
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
cout <<e[x][y] << " ";
}
cout << endl;
}
cout << endl << endl << endl;
}
void humanMove()
{
cout << "Pick where you want to place an X" << endl << "1 2 3"<< endl << "4 5 6" << endl << "7 8 9" << endl << endl << endl;
int x;
cin >> x;
switch(x)
{
case 1:
if((*p1=="-") && (*p1!="X")){
*p1 = "X";}else{ humanMove();} break;
case 2:
if((*p2=="-") && (*p2!="X")){
*p2 = "X";}else{ humanMove();}break;
case 3:
if((*p3=="-") && (*p3!="X")){
*p3 = "X";}else{ humanMove();}break;
case 4:
if((*p4=="-") && (*p4!="X")){
*p4 = "X";}else{ humanMove();}break;
case 5:
if((*p5=="-") && (*p5!="X")){
*p5 = "X";}else{ humanMove();}break;
case 6:
if((*p6=="-") && (*p6!="X")){
*p6 = "X";}else{ humanMove();}break;
case 7:
if((*p7=="-") && (*p7!="X")){
*p7 = "X";}else{ humanMove();}break;
case 8:
if((*p8=="-") && (*p8!="X")){
*p8 = "X";}else{ humanMove();}break;
case 9:
if((*p9=="-") && (*p9!="X")){
*p9 = "X";}else{ humanMove();}break;
default:
cout << "Please try to pick a number from 1-9" << endl;
humanMove();
}
}
void cpuMove()
{
//ROW 0
if(((*p1 == *p2) && (*p3 == "-")) && ((*p1 != "-") && (*p1 == "O"))){*p3 = "O";}//p3
else if(((*p2 == *p3) && (*p1 == "-")) && ((*p2 != "-") && (*p2 == "O"))){*p1 = "O";}//p1
else if(((*p1 == *p3) && (*p2 == "-")) && ((*p1 != "-") && (*p1 == "O"))){*p2 = "O";}//p2
//ROW 1
else if(((*p4 == *p5) && (*p6 == "-")) && ((*p4 != "-") && (*p4 == "O"))){*p6 = "O";}//p6
else if(((*p5 == *p6) && (*p4 == "-")) && ((*p5 != "-") && (*p5 == "O"))){*p4 = "O";}//p4
else if(((*p4 == *p6) && (*p5 == "-")) && ((*p4 != "-") && (*p4 == "O"))){*p6 = "O";}//p5
//ROW 2
else if(((*p7 == *p8) && (*p9 == "-")) && ((*p7 != "-") && (*p7 == "O"))){*p9 = "O";}//p9
else if(((*p8 == *p9) && (*p7 == "-")) && ((*p8 != "-") && (*p8 == "O"))){*p6 = "O";}//p7
else if(((*p7 == *p9) && (*p8 == "-")) && ((*p7 != "-") && (*p7 == "O"))){*p8 = "O";}//p8
//COLUMN 0
else if(((*p1 == *p4) && (*p7 == "-")) && ((*p1 != "-") && (*p1 == "O"))){*p7 = "O";}//p7
else if(((*p4 == *p7) && (*p1 == "-")) && ((*p4 != "-") && (*p4 == "O"))){*p1 = "O";}//p1
else if(((*p1 == *p7) && (*p4 == "-")) && ((*p1 != "-") && (*p1 == "O"))){*p4 = "O";}//p4
//COLUMN 1
else if(((*p2 == *p5) && (*p8 == "-")) && ((*p2 != "-") && (*p2 == "O"))){*p8 = "O";}//p8
else if(((*p5 == *p8) && (*p2 == "-")) && ((*p5 != "-") && (*p5 == "O"))){*p2 = "O";}//p2
else if(((*p2 == *p8) && (*p5 == "-")) && ((*p2 != "-") && (*p2 == "O"))){*p5 = "O";}//p5\
//COLUMN 2
else if(((*p3 == *p6) && (*p9 == "-")) && ((*p3 != "-") && (*p3 == "O"))){*p9 = "O";}//p9
else if(((*p6 == *p9) && (*p3 == "-")) && ((*p6 != "-") && (*p6 == "O"))){*p3 = "O";}//p3
else if(((*p3 == *p9) && (*p6 == "-")) && ((*p3 != "-") && (*p3 == "O"))){*p6 = "O";}//p6
//DIAGNAL 0
else if(((*p1 == *p5) && (*p9 == "-")) && ((*p1 != "-") && (*p1 == "O"))){*p9 = "O";}//p9
else if(((*p5 == *p9) && (*p1 == "-")) && ((*p5 != "-") && (*p5 == "O"))){*p1 = "O";}//p1
else if(((*p1 == *p9) && (*p5 == "-")) && ((*p1 != "-") && (*p1 == "O"))){*p5 = "O";}//p5
//DIAGNAL 1
else if(((*p3 == *p5) && (*p7 == "-")) && ((*p3 != "-") && (*p3 == "O"))){*p7 = "O";}//p7
else if(((*p5 == *p7) && (*p3 == "-")) && ((*p5 != "-") && (*p5 == "O"))){*p3 = "O";}//p3
else if(((*p3 == *p7) && (*p5 == "-")) && ((*p3 != "-") && (*p3 == "O"))){*p5 = "O";}//p5
//ROW 0
else if(((*p1 == *p2) && (*p3 == "-")) && ((*p1 != "-") && (*p1 == "X"))){*p3 = "O";}//p3
else if(((*p2 == *p3) && (*p1 == "-")) && ((*p2 != "-") && (*p2 == "X"))){*p1 = "O";}//p1
else if(((*p1 == *p3) && (*p2 == "-")) && ((*p1 != "-") && (*p1 == "X"))){*p2 = "O";}//p2
//ROW 1
else if(((*p4 == *p5) && (*p6 == "-")) && ((*p4 != "-") && (*p4 == "X"))){*p6 = "O";}//p6
else if(((*p5 == *p6) && (*p4 == "-")) && ((*p5 != "-") && (*p5 == "X"))){*p4 = "O";}//p4
else if(((*p4 == *p6) && (*p5 == "-")) && ((*p4 != "-") && (*p4 == "X"))){*p6 = "O";}//p5
//ROW 2
else if(((*p7 == *p8) && (*p9 == "-")) && ((*p7 != "-") && (*p7 == "X"))){*p9 = "O";}//p9
else if(((*p8 == *p9) && (*p7 == "-")) && ((*p8 != "-") && (*p8 == "X"))){*p6 = "O";}//p7
else if(((*p7 == *p9) && (*p8 == "-")) && ((*p7 != "-") && (*p7 == "X"))){*p8 = "O";}//p8
//COLUMN 0
else if(((*p1 == *p4) && (*p7 == "-")) && ((*p1 != "-") && (*p1 == "X"))){*p7 = "O";}//p7
else if(((*p4 == *p7) && (*p1 == "-")) && ((*p4 != "-") && (*p4 == "X"))){*p1 = "O";}//p1
else if(((*p1 == *p7) && (*p4 == "-")) && ((*p1 != "-") && (*p1 == "X"))){*p4 = "O";}//p4
//COLUMN 1
else if(((*p2 == *p5) && (*p8 == "-")) && ((*p2 != "-") && (*p2 == "X"))){*p8 = "O";}//p8
else if(((*p5 == *p8) && (*p2 == "-")) && ((*p5 != "-") && (*p5 == "X"))){*p2 = "O";}//p2
else if(((*p2 == *p8) && (*p5 == "-")) && ((*p2 != "-") && (*p2 == "X"))){*p5 = "O";}//p5\
//COLUMN 2
else if(((*p3 == *p6) && (*p9 == "-")) && ((*p3 != "-") && (*p3 == "X"))){*p9 = "O";}//p9
else if(((*p6 == *p9) && (*p3 == "-")) && ((*p6 != "-") && (*p6 == "X"))){*p3 = "O";}//p3
else if(((*p3 == *p9) && (*p6 == "-")) && ((*p3 != "-") && (*p3 == "X"))){*p6 = "O";}//p6
//DIAGNAL 0
else if(((*p1 == *p5) && (*p9 == "-")) && ((*p1 != "-") && (*p1 == "X"))){*p9 = "O";}//p9
else if(((*p5 == *p9) && (*p1 == "-")) && ((*p5 != "-") && (*p5 == "X"))){*p1 = "O";}//p1
else if(((*p1 == *p9) && (*p5 == "-")) && ((*p1 != "-") && (*p1 == "X"))){*p5 = "O";}//p5
//DIAGNAL 1
else if(((*p3 == *p5) && (*p7 == "-")) && ((*p3 != "-") && (*p3 == "X"))){*p7 = "O";}//p7
else if(((*p5 == *p7) && (*p3 == "-")) && ((*p5 != "-") && (*p5 == "X"))){*p3 = "O";}//p3
else if(((*p3 == *p7) && (*p5 == "-")) && ((*p3 != "-") && (*p3 == "X"))){*p5 = "O";}//p5
}
Jan 28, 2014 at 7:10pm UTC
void firstMove()// Special first move situations
{
if (*p5 == "X"){*p7 = "O";}
else if(*p5 == "-"){*p5 = "O";}
}
void secondMove()// Special second move situations
{
if (*p5 == "O")
{
if((*p1 == "X")&&(*p8 == "X")){*p7 = "O";}
else if((*p1 == "X")&&(*p6 == "X")){*p3 = "O";}
else if((*p7 == "X")&&(*p2 == "X")){*p1 = "O";}
else if((*p7 == "X")&&(*p6 == "X")){*p9 = "O";}
else if((*p9 == "X")&&(*p2 == "X")){*p3 = "O";}
else if((*p9 == "X")&&(*p4 == "X")){*p7 = "O";}
else if((*p3 == "X")&&(*p4 == "X")){*p1 = "O";}
else if((*p3 == "X")&&(*p8 == "X")){*p9 = "O";}
else if((*p1 == "X")&&(*p9 == "X")){*p4 = "O";}
else if((*p3 == "X")&&(*p7 == "X")){*p2 = "O";}
else if((*p2 == "X")&&(*p4 == "X")){*p1 = "O";}
else if((*p2 == "X")&&(*p6 == "X")){*p3 = "O";}
else if((*p6 == "X")&&(*p8 == "X")){*p9 = "O";}
else if((*p8 == "X")&&(*p4 == "X")){*p7 = "O";}
else if((*p2 == "X")&&(*p8 == "X")){*p4 = "O";}
else if((*p4 == "X")&&(*p6 == "X")){*p2 = "O";}
}
if(*p5 == "X")
{
if((*p1 == "O")&&(*p9 == "X")){*p7 = "O";}
else if((*p9 == "O")&&(*p1 == "X")){*p7 = "O";}
else if((*p7 == "O")&&(*p3 == "X")){*p1 = "O";}
else if((*p3 == "O")&&(*p7 == "X")){*p1 = "O";}
}
}
void thirdMove()// Special third move situations
{
if(((*p2 == "X")&&(*p8 == "X")) && (*p4 == "O")){*p7 = "O";}
else if(((*p4 == "X")&&(*p6 == "X")) && (*p2 == "O")){*p3 = "O";}
else if(((*p4 == "X")&&(*p9 == "X")) && (*p3 == "X")){*p1 = "O";}
else if(((*p2 == "X")&&(*p7 == "X")) && (*p9 == "X")){*p1 = "O";}
else if(((*p6 == "X")&&(*p1 == "X")) && (*p7 == "X")){*p3 = "O";}
else if(((*p8 == "X")&&(*p1 == "X")) && (*p3 == "X")){*p9 = "O";}
}
void fourthMove()// Special fourth move situations
{
if(((*p4 == "X")&&(*p9 == "X")) && ((*p3 == "X") && (*p2 == "X"))){*p8 = "O";}
else if(((*p7 == "X")&&(*p9 == "X")) && ((*p2 == "X") && (*p4 == "X"))){*p6 = "O";}
else if(((*p1 == "X")&&(*p7 == "X")) && ((*p6 == "X") && (*p2 == "X"))){*p8 = "O";}
else if(((*p1 == "X")&&(*p3 == "X")) && ((*p8 == "X") && (*p6 == "X"))){*p4 = "O";}
else if(((*p4 == "X")&&(*p5 == "X")) && ((*p3 == "X") && (*p9 == "X"))){*p2 = "O";}
else if(((*p2 == "X")&&(*p5 == "X")) && ((*p7 == "X") && (*p9 == "X"))){*p4 = "O";}
else if(((*p1 == "X")&&(*p3 == "X")) && ((*p5 == "X") && (*p8 == "X"))){*p4 = "O";}
}
void turn1()
{
printBoard();
humanMove();
printBoard();
cpuMove();
printBoard();
firstMove();
printBoard();
}
void turn2()
{
humanMove();
printBoard();
cpuMove();
printBoard();
secondMove();
printBoard();
}
void turn3()
{
humanMove();
printBoard();
cpuMove();
printBoard();
thirdMove();
printBoard();
winCheck();
}
void turn4()
{
humanMove();
printBoard();
cpuMove();
printBoard();
fourthMove();
printBoard();
winCheck();
}
void turnlast()
{
humanMove();
printBoard();
cpuMove();
winCheck();
cout << "NOBODY WINS BOOOOOOO" << endl;
}
void winCheck()
{
//WIN DETERMINATOR
if (((*p1 == *p2) && (*p1 == *p3)) && (*p1 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p1 == *p2) && (*p1 == *p3)) && (*p1 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
else if(((*p4 == *p5) && (*p4 == *p6)) && (*p4 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p4 == *p5) && (*p4 == *p6)) && (*p4 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
else if(((*p7 == *p8) && (*p7 == *p9)) && (*p7 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p7 == *p8) && (*p7 == *p9)) && (*p7 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
else if(((*p1 == *p4) && (*p1 == *p7)) && (*p1 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p1 == *p4) && (*p1 == *p7)) && (*p1 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
else if(((*p2 == *p5) && (*p2 == *p8)) && (*p2 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p2 == *p5) && (*p2 == *p8)) && (*p2 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
else if(((*p3 == *p6) && (*p3 == *p9)) && (*p3 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p3 == *p6) && (*p3 == *p9)) && (*p3 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
else if(((*p1 == *p5) && (*p1 == *p9)) && (*p1 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p1 == *p5) && (*p1 == *p9)) && (*p1 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
else if(((*p3 == *p5) && (*p3 == *p7)) && (*p3 == "X")){cout << "How is is that man beat machine" << endl;exit(0);}
else if(((*p3 == *p5) && (*p3 == *p7)) && (*p3 == "O")){cout << "Lowly human. Now I will digitize you" << endl;exit(0);}
}
[/code]
And thats it
Jan 28, 2014 at 7:21pm UTC
You could use Switch statement, instead of using all those functions try one, if you like I can get my code and show you what I mean.
Jan 28, 2014 at 8:11pm UTC
I don't know what you mean. How would I use a switch instead of functions
Jan 28, 2014 at 8:44pm UTC
Thanks guys.
So let me explain a little what i was trying to accomplish with this program.
I wanted to put all the functions in a class and just call them but I couldn't find a way to make the board global to the class and the main. I'm guessing this is because, like Mats said, i haven't locked down passing parameters around functions.
Basically what a I did now was how to create an object to call a function from the class. So
Is there a way to call a variable from the main.cpp to a class.cpp?
What was really bothering me while writing this though was that I was using so many if else statements, but I couldn't think of any other way. I was trying to think of a way to use more switch statements but the way I thought would still have a large amount of if else statements in each case.
Yea I'm a noob. This isn't for class or nothing like that just trying to solve these puzzles.
Once again, Thanks for your time and responses
Jan 28, 2014 at 9:23pm UTC
That is a massive amount of functions. I've written mine without function/arrays/pointers and it had way less lines.
Feb 1, 2014 at 6:29pm UTC
Last edited on Feb 1, 2014 at 6:30pm UTC