Oct 18, 2011 at 2:03pm UTC
Im creating a tic tac toe program as my project.But i have problem.
even if someone wins,the program keeps on asking the user for move until the moves ran out
what should i do for the program to stop asking when there's a winner?
here is the code
thanks in advance for the help!
#include<iostream>
#include<conio.h>
#include<windows.h>
//declaration of functions
void gotoxy(int x, int y);//location of boxes
void box1(int x1, int x2, int locy);//horizontal lines
void box2(int y1, int y2, int locx);//vertical lines
void box_number();//numbering for columns and rows
void check_win();
void player_moves();
void locxo();
using namespace std;
int player1[3],player2[3],x;//global variables
int main ()
{
box1(5,35,5);//1st line 5 is for start,35 end,5 location
box1(5,35,10);//2nd line
box1(5,35,15);//3rd line
box1(5,35,20);//4th line
box2(5,20,5);//1st line
box2(5,20,15);//2nd line
box2(5,20,25);//3rd line
box2(5,20,35);//4th line
box_number();//for columns and rows
locxo();
player_moves();
check_win();
cin.get();
}
void player_moves()
{
int y1=5,y; //[3] number of moves
for(x=0;x<3;x++)
{
for(y=0;y<1;y++)
{ //y1=5 is spacing
gotoxy(45,y1); //location player1 //x is number of moves
cout<<"Player 1 move (1-9): "; //y is 1 move every player
cin>>player1[x];
locxo(); //function of location of X
y1++; //increment of moves
gotoxy(45,y1); //location player2
cout<<"Player 2 move (1-9): ";
cin>>player2[x];
locxo(); //function of location of O
y1++;
}
}
}
void locxo()//location of X and O
{
//player 1 moves
if(player1[x] == 1)
{
gotoxy(10,7);
cout<<"X";
}
else if(player1[x] == 2)
{
gotoxy(20,7);
cout<<"X";
}
else if(player1[x] == 3)
{
gotoxy(30,7);
cout<<"X";
}
//1st 3 lines
else if(player1[x] == 4)
{
gotoxy(10,12);
cout<<"X";
}
else if(player1[x] == 5)
{
gotoxy(20,12);
cout<<"X";
}
else if(player1[x] == 6)
{
gotoxy(30,12);
cout<<"X";
}
//2nd 3 lines
else if(player1[x] == 7)
{
gotoxy(10,17);
cout<<"X";
}
else if(player1[x] == 8)
{
gotoxy(20,17);
cout<<"X";
}
else if(player1[x] == 9)
{
gotoxy(30,17);
cout<<"X";
}
//last 3 lines
// Player 2 moves
if(player2[x] == 1)
{
gotoxy(10,7);
cout<<"O";
}
else if(player2[x] == 2)
{
gotoxy(20,7);
cout<<"O";
}
else if(player2[x] == 3)
{
gotoxy(30,7);
cout<<"O";
}
//1st 3 lines
else if(player2[x] == 4)
{
gotoxy(10,12);
cout<<"O";
}
else if(player2[x] == 5)
{
gotoxy(20,12);
cout<<"O";
}
else if(player2[x] == 6)
{
gotoxy(30,12);
cout<<"O";
}
//2nd 3 lines
else if(player2[x] == 7)
{
gotoxy(10,17);
cout<<"O";
}
else if(player2[x] == 8)
{
gotoxy(20,17);
cout<<"O";
}
else if(player2[x] == 9)
{
gotoxy(30,17);
cout<<"O";
}
//last 3 lines
}
void check_win ()
{
if(player1[0] == 1 && player1[1] == 2 && player1[2] == 3||
player1[0] == 1 && player1[1] == 3 && player1[2] == 2||
player1[0] == 2 && player1[1] == 1 && player1[2] == 3||
player1[0] == 2 && player1[1] == 3 && player1[2] == 1||
player1[0] == 3 && player1[1] == 2 && player1[2] == 1||
player1[0] == 3 && player1[1] == 1 && player1[2] == 2)
{
gotoxy(20,22);
cout<<"Player X Wins the Game!Congratulations!";
gotoxy(20,23);
cout<<"Player O Loses the Game,Better luck next time!";
//box 1 2 3
}
if(player1[0] == 4 && player1[1] == 5 && player1[2] == 6||
player1[0] == 5 && player1[1] == 4 && player1[2] == 6||
player1[0] == 5 && player1[1] == 6 && player1[2] == 4||
player1[0] == 6 && player1[1] == 5 && player1[2] == 4||
player1[0] == 6 && player1[1] == 4 && player1[2] == 5)
{
gotoxy(20,22);
cout<<"Player X Wins the Game!Congratulations!";
gotoxy(20,23);
cout<<"Player O Loses the Game,Better luck next time!";
//box 4 5 6
}
if(player1[0] == 7 && player1[1] == 8 && player1[2] == 9||
player1[0] == 7 && player1[1] == 9 && player1[2] == 8||
player1[0] == 8 && player1[1] == 7 && player1[2] == 9||
player1[0] == 8 && player1[1] == 9 && player1[2] == 7||
player1[0] == 9 && player1[1] == 8 && player1[2] == 7||
player1[0] == 9 && player1[1] == 7 && player1[2] == 8)
{
gotoxy(20,22);
cout<<"Player X Wins the Game!Congratulations!";
gotoxy(20,23);
cout<<"Player O Loses the Game,Better luck next time!";
//box 7 8 9
}
if(player1[0] == 1 && player1[1] == 4 && player1[2] == 7||
player1[0] == 1 && player1[1] == 7 && player1[2] == 4||
player1[0] == 4 && player1[1] == 7 && player1[2] == 1||
player1[0] == 7 && player1[1] == 1 && player1[2] == 4||
player1[0] == 4 && player1[1] == 7 && player1[2] == 1||
player1[0] == 7 && player1[1] == 4 && player1[2] == 1)
{
gotoxy(20,22);
cout<<"Player X Wins the Game!Congratulations!";
gotoxy(20,23);
cout<<"Player O Loses the Game,Better luck next time!";
//box 1 4 7
}
if(player1[0] == 1 && player1[1] == 5 && player1[2] == 9||
player1[0] == 1 && player1[1] == 9 && player1[2] == 5||
player1[0] == 5 && player1[1] == 9 && player1[2] == 1||
player1[0] == 9 && player1[1] == 1 && player1[2] == 5||
player1[0] == 5 && player1[1] == 1 && player1[2] == 9||
player1[0] == 9 && player1[1] == 5 && player1[2] == 1)
{
gotoxy(20,22);
cout<<"Player X Wins the Game!Congratulations!";
gotoxy(20,23);
cout<<"Player O Loses the Game,Better luck next time!";
//box 1 5 9
}
if(player1[0] == 3 && player1[1] == 5 && player1[2] == 7||
player1[0] == 3 && player1[1] == 7 && player1[2] == 5||
player1[0] == 5 && player1[1] == 7 && player1[2] == 3||
player1[0] == 7 && player1[1] == 3 && player1[2] == 5||
player1[0] == 5 && player1[1] == 3 && player1[2] == 7||
player1[0] == 7 && player1[1] == 5 && player1[2] == 3)
{
gotoxy(20,22);
cout<<"Player X Wins the Game!Congratulations!";
gotoxy(20,23);
cout<<"Player O Loses the Game,Better luck next time!";
//box 3 5 7
}
cin.get();
}
void box_number()
{
gotoxy(10,7);
cout<<"1";
gotoxy(20,7);
cout<<"2";
gotoxy(30,7);
cout<<"3";
gotoxy(10,12);
cout<<"4";
gotoxy(20,12);
cout<<"5";
gotoxy(30,12);
cout<<"6";
gotoxy(10,17);
cout<<"7";
gotoxy(20,17);
cout<<"8";
gotoxy(30,17);
cout<<"9";
}
void box1(int x1,int x2,int locy)
{
int z;//local variables
for(z=x1;z<=x2;z++) //loop for horizontal lines
{
gotoxy(z,locy);
cout<<"-";
}
}
void box2(int y1,int y2,int locx)
{
int z;
for(z=y1;z<=y2;z++)
{
gotoxy(locx,z);
cout<<"|";
}
}
void gotoxy( int x, int y )
{
COORD c;
c.X = x - 1 ;
c.Y = y - 1 ;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), c ) ;
}