Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Tic Tac Toe

Options
  • 27-02-2008 4:56am
    #1
    Closed Accounts Posts: 11


    Hi there, Im writing a game of tic tac toe up in c++.

    I have most of it working, the board is a 2d array. there is a switch statement to control turns. My problem is the Way to check the winners. I have this for statement.
    for(int i=1; i<4;i++)
        {
            for(int j=1; j<4;j++)
            {
                if( (box[i][j]=='x') && (box[i][j]='x') && (box[i][j]=='x') )
                {
                    cout << " x wins " << endl;
                    exit(0);
                }
            }
        }
    


    It also gives me this error

    Warning W8060 F:\Prog5.cpp 78: Possibly incorrect assignment in function main()

    //BTW line 78 is the if statement line
    box is the name of the array.

    The game just wins if you put a x in any of the places. What am i doing wrong.

    (Tell me if i need to post all of the code.)


Comments

  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,056 Mod ✭✭✭✭AlmightyCushion


    I don't know about the error as I don't know c++, but I might be able to help you with the tic tac toe part.

    The problem is with the if statement. All that does is check each of the 9 positions and sees if its x. If it is then it says it's a winner. So the moment you put down an x it checks every postion until it finds an x and then it says x wins.

    What you need to do is discover all the possible ways you can win (I think there's 8 so you'll need 8 if statements) and then check if x is in each of the locations. If so then show "x wins".

    So for example one of your if statements will be:

    if( (box[0][0]=='x') && (box[0][1]='x') && (box[0][2]=='x') )
    {
    cout << " x wins " << endl;
    exit(0);
    }

    This means that if you have a grid that looks like this:

    xxx
    ooo
    ooo

    it will say that "x wins. Now you just need to write the neccesary statements to check all the other possible ways of winning.

    You won't actually need the nested for loops either.

    Actually I think the error may be to do with the array being out of bounds (I don't know how arrays work in c++ so I may be wrong). Remember the first element in an array is 0 so in the case of your array the last element will be 2 (because we are counting up from 0).


  • Banned (with Prison Access) Posts: 6,201 ✭✭✭KamiKazi


    for(int i=1; i<4;i++)
        {
            for(int j=1; j<4;j++)
            {
                if( (box[i][j]=='x') && ([B]box[i][j]='x'[/B]) && (box[i][j]=='x') )
                {
                    cout << " x wins " << endl;
                    exit(0);
                }
            }
        }
    
    If im not mistaken you are assigning 'x' to box[j] (=) instead of checking its value (==)

    Oh and Cushion was right, the for loops should be for(int j=0; j<3;j++) as opposed to for(int j=1; j<4;j++)


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,056 Mod ✭✭✭✭AlmightyCushion


    KamiKazi wrote: »
    If im not mistaken you are assigning 'x' to box[j] (=) instead of checking its value (==)

    Oh yeah, I didn't even notice that. Good spot.


  • Closed Accounts Posts: 11 Qin


    The reason i used 1 and 4 is that by array is
    char box[4][4];
    

    This is so that the user input can be 1, 2, and 3. Its earsier for zero based indexing.

    What kind of if statement would i need to make it that it will only do it if all the arguments are true, that is what i thought and was?

    I think i got and wrong.

    I need to use loops. got to keep code small.


  • Closed Accounts Posts: 11 Qin


    Oh... wait i see. the i and j are just staying one the during the loop for the if win...
    any one know how i can fix the loop?


  • Advertisement
  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,056 Mod ✭✭✭✭AlmightyCushion


    I don't think you'll be able to use just one if statement. You should be able to get it down to four easily enough, maybe three. The if statement I posted up will give you an idea of how to do that. You'll need to replace the numbers with i and j but not like the way you have it.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,056 Mod ✭✭✭✭AlmightyCushion


    Qin wrote: »
    Oh... wait i see. the i and j are just staying one the during the loop for the if win...

    No they are increasing (thats what the i++ is doing) but your if statement is what's wrong.
    Qin wrote: »
    any one know how i can fix the loop?

    See my post above this for a bit of a push in the right direction.


  • Closed Accounts Posts: 11 Qin


    Alright I think i got it. this is all my code. try it out its fun. tell me if you find any bugs.
    #include <iostream>
    
    using namespace std;
    
    
    
    
    void main()
    {
    
    	const int ROW=4;
    	const int COL=4;
    
    	char box[ROW][COL] = { { '-' , '-' , '-' , '-'}, { '-' , '-' , '-' , '-'}, { '-' , '-' , '-' , '-'}, { '-' , '-' , '-' , '-'}} ;
    	int x, y;
    	char curchar='x';
    	int turn;
    
    
    
    
    cout << "_____________W3lcome_____________" << endl;
    cout << "	Tic-Tak-Toe           " << endl;
    cout << "Use 1-3 to give the row, Press enter" << endl;
    cout << "Then use 1-3 to give the collum, Press enter" << endl;
    cout << "__________Hav3 Fun_______________" << endl << endl << endl;
    
    for(int i=1; i<4; i++)
    		{
    			for(int j=1; j<4; j++)
    			{
    				cout << box[i][j] << "   ";
    			}
    
    			cout << endl;
    	}
    	cout << endl << endl << endl;
    
    for(turn=0; turn<9; turn++)
    {
    	switch(curchar)
    	{
    		// x turn
    		case 'x' :
    		{
    			cout << "Enter move for x: " << endl;
    			cin >> x;
    			cin >> y;
    			cout << endl;
    			if(box[x][y]!='-')
    			{
    				cout << endl << endl << "Error" << endl;
    				exit(0);
    			}
    			if((x>3) || (x<1))
    			{
    				cout << endl << endl << "Error" << endl;
    				exit(0);
    			}
    			box[x][y]='x';
    
    			curchar='o';
    			break;
    		}
    
    	// o turn
    		case 'o' :
    		{
    			cout << "Enter move for o: "<< endl;
    			cin >> x;
    			cin >> y;
    			cout << endl;
    			if(box[x][y]!='-')
    			{
    				cout << endl << endl << "Error" << endl;
    				exit(0);
    			}
    			if((x>3) || (x<1))
    			{
    				cout << endl << endl << "Error" << endl;
    				exit(0);
    			}
    
    				box[x][y]='o';
    			curchar='x';
    			break;
    		}
    
    
    	}
    
    	// print box
    
    	for(int i=1; i<4; i++)
    	{
    		for(int j=1; j<4; j++)
    		{
    			cout << box[i][j] << "   ";
    		}
    
    		cout << endl;
    	}
    
    	//win check
    	// across o
    		for(int i=1; i<4;i++)
    			{
    				for(int j=1; j<4;j++)
    				{
    					if(box[i][j]=='o')
    					j++;
    						if(box[i][j]=='o')
    						j++;
    							if(box[i][j]=='o')
    							{
    								cout << "o" << " wins " << endl;
    								exit(0);
    							}
    				}
    		}
    // across x
    		for(int i=1; i<4;i++)
    			{
    				for(int j=1; j<4;j++)
    				{
    					if(box[i][j]=='x'){
    					j++;
    					}
    						if(box[i][j]=='x'){
    						j++;
    						}
    							if(box[i][j]=='x')
    							{
    								cout << "x" << " wins " << endl;
    								exit(0);
    							}
    				}
    		}
    // down x
    		for(int i=1; i<4; i++)
    		{
    			for(int j=1; j<4; j++)
    			{
    				if(box[j][i]=='x')
    				j++;
    				{
    					if(box[j][i]=='x')
    					j++;
    					{
    						if(box[j][i]=='x')
    						{
    							cout << "x" << " wins " << endl;
    							exit(0);
    						}
    					}
    				}
    			}
    		}
    
    //down o
    		for(int i=1; i<4; i++)
    		{
    			for(int j=1; j<4; j++)
    			{
    				if(box[j][i]=='o')
    				j++;
    				{
    					if(box[j][i]=='o')
    					j++;
    					{
    						if(box[j][i]=='o')
    						{
    							cout << "o" << " wins " << endl;
    							exit(0);
    						}
    					}
    				}
    			}
    		}
    
    // \diagonal x
    
    	for(int i=1;i<4;i++)
    	{
    		if(box[i][i]=='x')
    		{
    			i++;
    			if(box[i][i]=='x')
    			{
    				i++;
    				if(box[i][i]=='x')
    				{
    					cout << "x" << " wins " << endl;
    					exit(0);
    				}
    			}
    		}
    	}
    
    
    // \diognal o
    	for(int i=1;i<4;i++)
    		{
    			if(box[i][i]=='x')
    			{
    				i++;
    				if(box[i][i]=='x')
    				{
    					i++;
    					if(box[i][i]=='x')
    					{
    						cout << "x" << " wins " << endl;
    						exit(0);
    					}
    				}
    			}
    		}
    	// /dioganal x
    
    	for(int i=1; i<4; i++)
    	{
    		for(int j=3; j>0; j--)
    		{
    			if(box[i][j]=='x')
    			{
    				i++;
    				j--;
    				if(box[i][j]=='x')
    				{
    					i++;
    					j--;
    					if(box[i][j]=='x')
    					{
    						cout << "x" << " wins " << endl;
    						exit(0);
    					}
    				}
    			}
    		}
    	}
    
    
    	// /diagonal o
    
    		for(int i=1; i<4; i++)
    		{
    			for(int j=3; j>0; j--)
    			{
    				if(box[i][j]=='o')
    				{
    					i++;
    					j--;
    					if(box[i][j]=='o')
    					{
    						i++;
    						j--;
    						if(box[i][j]=='o')
    						{
    							cout << "o" << " wins " << endl;
    							exit(0);
    						}
    					}
    				}
    			}
    		}
    
    
    
    
    
    
    
    	// win check is over
    
    
    
    
    
    
    
    
    }// end 4 loop
    
    cout << endl <<"_______Tie Game______" << endl;
    // end program
    }
    
    


  • Banned (with Prison Access) Posts: 6,201 ✭✭✭KamiKazi


    for x = 0 < 3
        for y = 0 < 3
            if box[x][y]=='x'
                (checks for horizontal row of x's)
                (checks for vertical row of x's)
                (checks for diagonal row of x's)
            end if
        end for
    end for
    


    Thats how I would do it


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    heres one i did in javascript last month, bit long winded but works perfectly.
    Youd need a switch to see if if a certain row [3 elements in array] are just X or O's, also make use of counters to catch winner/loser/tie
    /* Declaration of a 2D array */
    var myarray=new Array(3);
    
    for (k=0; k<3; k++)
    {
    myarray[k]=new Array(3);
    
    }
    
    /*Locking mechanism to prevent overwriting of cells and the corresponding elements in the array
    0 = locked
    1 = not locked
    */
    var a = 1;
    var b = 1;
    var c = 1;
    var d = 1;
    var e = 1;
    var f = 1;
    var g = 1;
    var h = 1;
    var i = 1;
    
    /* Counter to check how many turns are left */
    var counter = 9;
    
    /*Counter to check for a draw game */
    var draw = 0;
    
    
    var choice
    
    
    /* Player one code */
    while(counter >0)
    {
    
    /*Prompt user to Enter move 
    and according to the position requested, enter move into the 2D array [as long as the cell is not locked].
    Decrement counter to reduce moves left.
    Increment draw counter 
    Check to see if player has won
    Call AI function [computers move]
    */
    
    choice = prompt('Enter a move');
    
    
    
    if((choice=="1") && (a==1)){
    myarray[0][0]="X";
    document.getElementById('1').innerHTML ="X";
    a=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    else if((choice=="2") && (b==1)){
    myarray[0][1]="X";
    document.getElementById('2').innerHTML ="X";
    b=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    
    }
    
    else if((choice=="3") && (c==1)){
    myarray[0][2]="X";
    document.getElementById('3').innerHTML ="X";
    c=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    
    else if((choice=="4") && (d==1)){
    myarray[1][0]="X";
    document.getElementById('4').innerHTML ="X";
    d=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    else if((choice=="5") && (e==1)){
    myarray[1][1]="X";
    document.getElementById('5').innerHTML ="X";
    e=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    else if((choice=="6") && (f==1)){
    myarray[1][2]="X";
    document.getElementById('6').innerHTML ="X";
    f=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    else if((choice=="7") && (g==1)){
    myarray[2][0]="X";
    document.getElementById('7').innerHTML ="X";
    g=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    else if((choice=="8") && (h==1)){
    myarray[2][1]="X";
    document.getElementById('8').innerHTML ="X";
    h=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    
    else if((choice=="9") && (i==1)){
    myarray[2][2]="X";
    document.getElementById('9').innerHTML ="X";
    i=0;
    counter--;
    draw++;
    winCheck();
    ai();
    
    }
    
    /* If player requests anything but cell numbers 1-9 [3x3 table] OR if the cell is occupied [i.e Locked] then ask for Input again */
    else{
    alert("Illegal Move, Please make a different move");}
    
    }
    
    
    
    
    /* Artificial Intelligence method
    Pick a random number between 1-9 and makes the Computer [Player 2's] move Only IF the cell is not locked
    If the cell is locked it picks a different random number.
    -a recursive method */
    
    
    function ai()
    {
    
    var randomMove = Math.round((Math.random()*8)+1);
    
    if((randomMove==1) && (a==1) && (counter>0))
    {
    myarray[0][0]="O";
    document.getElementById('1').innerHTML ="O";
    a=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    else if((randomMove==2) && (b==1) && (counter>0))
    {
    myarray[0][1]="O";
    document.getElementById('2').innerHTML ="O";
    b=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    else if((randomMove==3) && (c==1) && (counter>0))
    {
    myarray[0][2]="O";
    document.getElementById('3').innerHTML ="O";
    c=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    else if((randomMove==4)&& (d==1) && (counter>0))
    {
    myarray[1][0]="O";
    document.getElementById('4').innerHTML ="O";
    d=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    
    else if((randomMove==5) && (e==1) && (counter>0))
    {
    myarray[1][1]="O";
    document.getElementById('5').innerHTML ="O";
    e=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    else if((randomMove==6) && (f==1) && (counter>0))
    {
    myarray[1][2]="O";
    document.getElementById('6').innerHTML ="O";
    f=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    else if((randomMove==7) && (g==1) && (counter>0))
    {
    myarray[2][0]="O";
    document.getElementById('7').innerHTML ="O";
    g=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    
    else if((randomMove==8) && (h==1) && (counter>0))
    {
    myarray[2][1]="O";
    document.getElementById('8').innerHTML ="O";
    h=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    else if((randomMove==9) && (i==1) && (counter>0))
    {
    myarray[2][2]="O";
    document.getElementById('9').innerHTML ="O";
    i=0;
    counter--;
    draw++;
    winCheck2();
    
    }
    
    else
    {
    randomMove = Math.round((Math.random()*8)+1);
    ai();
    }
    
    }
    
    
    
    
    
    
    /* Switch Statement to see if Player 1 has won 
    also checks to see if game is a Draw
    */
    
    function winCheck()
    {
    switch(true)
    {
    case(((myarray[0][0])=="X") && ((myarray[0][1])=="X") && ((myarray[0][2])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(((myarray[1][0])=="X") && ((myarray[1][1])=="X") && ((myarray[1][2])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(((myarray[2][0])=="X") && ((myarray[2][1])=="X") && ((myarray[2][2])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(((myarray[0][0])=="X") && ((myarray[1][0])=="X") && ((myarray[2][0])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(((myarray[0][1])=="X") && ((myarray[1][1])=="X") && ((myarray[2][1])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(((myarray[0][2])=="X") && ((myarray[1][2])=="X") && ((myarray[2][2])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(((myarray[0][0])=="X") && ((myarray[1][1])=="X") && ((myarray[2][2])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(((myarray[2][0])=="X") && ((myarray[1][1])=="X") && ((myarray[0][2])=="X")):
    alert("You Win");
    counter=0;
    break;
    
    case(draw == 9):
    {
    alert("Draw Game");
    break;
    }
    }
    }
    
    
    /* Switch Statement to see if the Computer has won 
    also checks to see if game is a Draw
    */
    
    function winCheck2()
    {
    switch(true)
    {
    case(((myarray[0][0])=="O") && ((myarray[0][1])=="O") && ((myarray[0][2])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(((myarray[1][0])=="O") && ((myarray[1][1])=="O") && ((myarray[1][2])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(((myarray[2][0])=="O") && ((myarray[2][1])=="O") && ((myarray[2][2])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(((myarray[0][0])=="O") && ((myarray[1][0])=="O") && ((myarray[2][0])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(((myarray[0][1])=="O") && ((myarray[1][1])=="O") && ((myarray[2][1])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(((myarray[0][2])=="O") && ((myarray[1][2])=="O") && ((myarray[2][2])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(((myarray[0][0])=="O") && ((myarray[1][1])=="O") && ((myarray[2][2])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(((myarray[2][0])=="O") && ((myarray[1][1])=="O") && ((myarray[0][2])=="O")):
    alert("You Loose");
    counter=0;
    break;
    
    case(draw == 9):
    {
    alert("Draw Game");
    break;
    }
    }
    }
    


  • Advertisement
Advertisement