0

Thanks for the input last night Dave.

Anyone willing to help:

I have rewritten parts of my compplay function; I know its redundant, but I will work on that after I can solve some other problems.

The problems I am having are these, imput on any or all of the three would be much appriciated. Thanks.

First, the last else if in the compplay function fills in the remainder of the board instead of just one turn, and I cannot figure out why.

Second, the chckwin function only works for the player, X, and not for the computer O, despite using the same function.

Please, any input on these or other problems would help me out.

Thanks.

Attachments
#include <iostream>
using namespace std;
const int SIZE = 9;
char box[SIZE];
int round, win, compwin;
char stopcode;



void draw(char box [9]);
void pplay(char box [9]);
void compplay(char [9], int& round);
void chkwin(char box [9], char z, int& compwin, int& win);


int main()
{
    do
    {
        round=win=compwin=0;
        for ( int i=0; i<9; i++)
			box[i]='1'+i;
		cout << "Tic Tac Toe!!!" << endl
			 << "Play the computer!";
        draw(box);
        do
        {
			
            pplay(box);
            compplay (box, round);
            draw(box);
            chkwin(box, 'X', win, compwin);
            chkwin(box, 'O', win, compwin);
			
			
        }while((round<4)&&(win<1));
        if (win==1)
        {
            if (compwin==1)
                cout<<"\n YOU WIN!!! ";
            else
                cout<<"\n THE COMPUTER WINS. YOU LOSE ";
        }
        if ((round>=4)&&(win==0))
            cout<<"\n It's a draw.";
        cout<<"\n Do you want to play another game?? Press Y";
        cin>>stopcode;
	}while((stopcode=='Y')||(stopcode=='y'));
	
	return 0;
}


void draw(char box [9])
{
    cout<<"\n     +   +    ";
    cout<<"\n   "<<box[0]<<" | "<<box[1]<<" | "<<box[2]<<"   ";
    cout<<"\n +---+---+---+";
    cout<<"\n   "<<box[3]<<" | "<<box[4]<<" | "<<box[5]<<"   ";
    cout<<"\n +---+---+---+";
    cout<<"\n   "<<box[6]<<" | "<<box[7]<<" | "<<box[8]<<"   ";
    cout<<"\n     +   +    ";
}

void pplay(char box [9])
{
    int s;
	bool repeat = false;
    cout<<"\n Enter the square number you wish to play: ";
    do {
		cin>>s;
		if ((s>0)&&(s<=9))
		{
			if ((!(box[s-1]=='X'))&&(!(box[s-1]=='O')))
			{
				box[s-1]='X';
				repeat = true;
			}
			else
			{
				cout<<"\n Enter an empty square number ";
			}
		}
		else
		{
			cout<<"\n Enter a valid square number(1-9) : ";
		}
	} while (repeat == false);
	
	
}

void compplay(char box [9], int& round)
{
	int i,a;
	a=round;
	if (round==0)
	{
		if (box[4]!='X')
			box[4]='O';
		else
			box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='O')&&(box[1]=='O'))&&((box[1]=='O')&&(box[2]=='3'))))
	{
		box[2]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='O')&&(box[2]=='O'))&&((box[2]=='O')&&(box[1]=='O'))))
	{
		box[1]='O';
		round++;
	}	
	else if ((round==a) && (((box[1]=='O')&&(box[2]=='O'))&&((box[2]=='O')&&(box[0]=='1'))))
	{
		box[0]='O';
		round++;
	}
	else if ((round==a) && (((box[3]=='O')&&(box[4]=='O'))&&((box[4]=='O')&&(box[5]=='6'))))
	{
		box[6]='O';
		round++;
	}
	else if ((round==a) && (((box[3]=='O')&&(box[5]=='O'))&&((box[5]=='O')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='O')&&(box[5]=='O'))&&((box[5]=='O')&&(box[3]=='4'))))
	{
		box[3]='O';
		round++;
	}	
	else if ((round==a) && (((box[6]=='O')&&(box[7]=='O'))&&((box[7]=='O')&&(box[8]=='9'))))
	{
		box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[6]=='O')&&(box[8]=='O'))&&((box[8]=='O')&&(box[7]=='8'))))
	{
		box[7]='O';
		round++;
	}
	else if ((round==a) && (((box[7]=='O')&&(box[8]=='O'))&&((box[8]=='O')&&(box[6]=='7'))))
	{
		box[7]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='O')&&(box[3]=='O'))&&((box[3]=='O')&&(box[6]=='7'))))
	{
		box[6]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='O')&&(box[6]=='O'))&&((box[6]=='O')&&(box[3]=='4'))))
	{
		box[3]='O';
		round++;
	}	
	else if ((round==a) && (((box[3]=='O')&&(box[6]=='O'))&&((box[6]=='O')&&(box[0]=='1'))))
	{
		box[0]='O';
		round++;
	}
	else if ((round==a) && (((box[1]=='O')&&(box[4]=='O'))&&((box[4]=='O')&&(box[7]=='8'))))
	{
		box[7]='O';
		round++;
	}
	else if ((round==a) && (((box[1]=='O')&&(box[7]=='O'))&&((box[7]=='O')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='O')&&(box[7]=='O'))&&((box[7]=='O')&&(box[1]=='2'))))
	{
		box[1]='O';
		round++;
	}	
	else if ((round==a) && (((box[2]=='O')&&(box[5]=='O'))&&((box[5]=='O')&&(box[8]=='9'))))
	{
		box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[2]=='O')&&(box[8]=='O'))&&((box[8]=='O')&&(box[5]=='6'))))
	{
		box[5]='O';
		round++;
	}
	else if ((round==a) && (((box[5]=='O')&&(box[8]=='O'))&&((box[8]=='O')&&(box[2]=='3'))))
	{
		box[3]='O';
		round++;
	}
	else if ((round==a) && (((box[2]=='O')&&(box[4]=='O'))&&((box[4]=='O')&&(box[6]=='7'))))
	{
		box[6]='O';
		round++;
	}
	else if ((round==a) && (((box[2]=='O')&&(box[6]=='O'))&&((box[6]=='O')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='O')&&(box[6]=='O'))&&((box[6]=='O')&&(box[2]=='3'))))
	{
		box[2]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='O')&&(box[4]=='O'))&&((box[4]=='O')&&(box[8]=='9'))))
	{
		box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='O')&&(box[8]=='O'))&&((box[8]=='O')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='O')&&(box[8]=='O'))&&((box[8]=='O')&&(box[0]=='1'))))
	{
		box[0]='O';
		round++;
	}



	else if ((round==a) && (((box[0]=='X')&&(box[1]=='X'))&&((box[1]=='X')&&(box[2]=='3'))))
	{
		box[2]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='X')&&(box[2]=='X'))&&((box[2]=='X')&&(box[1]=='2'))))
	{
		box[1]='O';
		round++;
	}	
	else if ((round==a) && (((box[1]=='X')&&(box[2]=='X'))&&((box[2]=='X')&&(box[0]=='1'))))
	{
		box[0]='O';
		round++;
	}
	else if ((round==a) && (((box[3]=='X')&&(box[4]=='X'))&&((box[4]=='X')&&(box[5]=='6'))))
	{
		box[5]='O';
		round++;
	}
	else if ((round==a) && (((box[3]=='X')&&(box[5]=='X'))&&((box[5]=='X')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='X')&&(box[5]=='X'))&&((box[5]=='X')&&(box[3]=='4'))))
	{
		box[3]='O';
		round++;
	}	
	else if ((round==a) && (((box[6]=='X')&&(box[7]=='X'))&&((box[7]=='X')&&(box[8]=='9'))))
	{
		box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[6]=='X')&&(box[8]=='X'))&&((box[8]=='X')&&(box[7]=='8'))))
	{
		box[7]='O';
		round++;
	}
	else if ((round==a) && (((box[7]=='X')&&(box[8]=='X'))&&((box[8]=='X')&&(box[6]=='7'))))
	{
		box[7]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='X')&&(box[3]=='X'))&&((box[3]=='X')&&(box[6]=='7'))))
	{
		box[6]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='X')&&(box[6]=='X'))&&((box[6]=='X')&&(box[3]=='4'))))
	{
		box[3]='O';
		round++;
	}	
	else if ((round==a) && (((box[3]=='X')&&(box[6]=='X'))&&((box[6]=='X')&&(box[0]=='1'))))
	{
		box[0]='O';
		round++;
	}
	else if ((round==a) && (((box[1]=='X')&&(box[4]=='X'))&&((box[4]=='X')&&(box[7]=='8'))))
	{
		box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[1]=='X')&&(box[7]=='X'))&&((box[7]=='X')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='X')&&(box[7]=='X'))&&((box[7]=='X')&&(box[1]=='2'))))
	{
		box[1]='O';
		round++;
	}	
	else if ((round==a) && (((box[2]=='X')&&(box[5]=='X'))&&((box[5]=='X')&&(box[8]=='9'))))
	{
		box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[2]=='X')&&(box[8]=='X'))&&((box[8]=='X')&&(box[5]=='6'))))
	{
		box[5]='O';
		round++;
	}
	else if ((round==a) && (((box[5]=='X')&&(box[8]=='X'))&&((box[8]=='X')&&(box[2]=='3'))))
	{
		box[2]='O';
		round++;
	}
	else if ((round==a) && (((box[2]=='X')&&(box[4]=='X'))&&((box[4]=='X')&&(box[6]=='7'))))
	{
		box[6]='O';
		round++;
	}
	else if ((round==a) && (((box[2]=='X')&&(box[6]=='X'))&&((box[6]=='X')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='X')&&(box[6]=='X'))&&((box[6]=='X')&&(box[2]=='3'))))
	{
		box[2]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='X')&&(box[4]=='X'))&&((box[4]=='X')&&(box[8]=='9'))))
	{
		box[8]='O';
		round++;
	}
	else if ((round==a) && (((box[0]=='X')&&(box[8]=='X'))&&((box[8]=='X')&&(box[4]=='5'))))
	{
		box[4]='O';
		round++;
	}
	else if ((round==a) && (((box[4]=='X')&&(box[8]=='X'))&&((box[8]=='X')&&(box[0]=='1'))))
	{
		box[0]='O';
		round++;
	}
	else 
	{
		for (i=0; i<9; i++)
		{	
			do{
				
				if ((box[i]!='X')&&(box[i]!='O'))
				{
					box[i]='O';
					round++;
				}
				
			} while (round == a);
		}
		
		
	}
}
void chkwin(char box [9], char z, int& compwin, int& win)
{
	
	for (int i=0; i<7; i+=3)
	{
		if (((box[i]==z)&&(box[i+1]==z))&&((box[i+1]==z)&&(box[i+2]==z)))
			win = 1;
	}
	for (int j=0; j<3; j++)
	{
		if (((box[j]==z)&&(box[j+3]==z))&&((box[j+3]==z)&&(box[j+6]==z)))    
			win = 1;
	}
	if (((box[0]==z)&&(box[4]==z))&&((box[4]==z)&&(box[8]==z)))
		win=1;
	if (((box[2]==z)&&(box[4]==z))&&((box[4]==z)&&(box[6]==z)))
		win=1;
	if ((win==1)&&(z=='X'))
		compwin=1;
}
2
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by BountyX
0

I solved my first problem with a break, so it is the second one only I could use some help on. Thanks.


Thanks for the input last night Dave.

Anyone willing to help:

I have rewritten parts of my compplay function; I know its redundant, but I will work on that after I can solve some other problems.

The problems I am having are these, imput on any or all of the three would be much appriciated. Thanks.

First, the last else if in the compplay function fills in the remainder of the board instead of just one turn, and I cannot figure out why.

Second, the chckwin function only works for the player, X, and not for the computer O, despite using the same function.

Please, any input on these or other problems would help me out.

Thanks.

0

Hey i have a tic tac toe program I can send you if you would like an example to work off of. Just give me a PM if your intrested.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.