Advertisement

Some ideas for a tic tac toe

Started by November 30, 2013 03:55 PM
12 comments, last by bratiefanut 11 years, 2 months ago


I started counting from 0 and it works now. The problem I am facing now is that I render when when the square in the XnO table is empty and when is the turn of player 1 but when is the turn of player 2 what was rendered befor disapears.. The problem is that Xs are rendered only when is player1 turn and Os only when is player2 turn. Maybe i should add a new condition?


//getting input 
if (mousex >= 140 && mousex <= 250 && mousey <= 200 && mousey >= 115 && hge->Input_GetKey() == HGEK_LBUTTON && table1 == true)
{
table1x = 190; table1y = 150; table1 = false; matrix[0][0] = 1; turn++;
}
if (mousex >= 250 && mousex <= 375 && mousey <= 200 && mousey >= 115 && hge->Input_GetKey() == HGEK_LBUTTON && table2 == true)
{
table2x = 315; table2y = 150; table2 = false; matrix[0][1] = 1; turn++;
}
.........
//rendering

if (table1 == false && player1 == true)
x_spr->Render(table1x, table1y);
else if (table1 == false && player2 == true)
o_spr->Render(table1x, table1y);
 
if (table2 == false && player1 == true)
x_spr->Render(table2x, table2y);
else if (table2 == false && player2 == true)
o_spr->Render(table2x, table2y);

Your render should work a bit differently. You say you are using 1, 0, and -1, so do this for your render:


for(int i = 0; i < 3; i++)
{
    for(int j = 0; j < 3; j++)
    {
        // Draw XnO[i][j]; -- Here you can do if statements for determining whether to draw an X, draw an O, or just not draw anything
    }
}

EDIT: Why do you have table1x, table1y, table2x, table2y? Scratch that. It's late....



I started counting from 0 and it works now. The problem I am facing now is that I render when when the square in the XnO table is empty and when is the turn of player 1 but when is the turn of player 2 what was rendered befor disapears.. The problem is that Xs are rendered only when is player1 turn and Os only when is player2 turn. Maybe i should add a new condition?


//getting input 
if (mousex >= 140 && mousex <= 250 && mousey <= 200 && mousey >= 115 && hge->Input_GetKey() == HGEK_LBUTTON && table1 == true)
{
table1x = 190; table1y = 150; table1 = false; matrix[0][0] = 1; turn++;
}
if (mousex >= 250 && mousex <= 375 && mousey <= 200 && mousey >= 115 && hge->Input_GetKey() == HGEK_LBUTTON && table2 == true)
{
table2x = 315; table2y = 150; table2 = false; matrix[0][1] = 1; turn++;
}
.........
//rendering

if (table1 == false && player1 == true)
x_spr->Render(table1x, table1y);
else if (table1 == false && player2 == true)
o_spr->Render(table1x, table1y);
 
if (table2 == false && player1 == true)
x_spr->Render(table2x, table2y);
else if (table2 == false && player2 == true)
o_spr->Render(table2x, table2y);

Your render should work a bit differently. You say you are using 1, 0, and -1, so do this for your render:


for(int i = 0; i < 3; i++)
{
    for(int j = 0; j < 3; j++)
    {
        // Draw XnO[i][j]; -- Here you can do if statements for determining whether to draw an X, draw an O, or just not draw anything
    }
}

EDIT: Why do you have table1x, table1y, table2x, table2y? Scratch that. It's late....

I will try this. Thank you.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

Advertisement

Going off what I saw, without doing win checking and whether the space already has a X or O, here is some code that could help. The stillPlaying could be implemented differently, and I have no clue what event system you have for your game, but when an event is fired and you are checking to see if it is a key or button, no need to check every time. Once you make it within a block { } you know you passed your initial check.

int player = 1;
unsigned int turn = 1;
bool stillPlaying = true;
int board[3][3];
 
for(int i = 0; i < 3; i++)
{
    for(int j = 0; j < 3; j++)
    {
        board[i][j] = 0;
    }
}
 
while(stillPlaying)
{
 
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            // Draw board[i][j]; -- Here you can do if statements for determining whether to draw an X, draw an O, or just not draw anything
        }
    }
 
    if(event.MouseClicked && hge->Input_GetKey() == HGEK_LBUTTON)
    {
        // Check which box is clicked, and since player is our X or O (1 or -1) we can just assign it without checking for which player is playing
        if(mousex >= 140 && mousex <= 250 && mousey <= 200 && mousey >= 115)
        {
            board[0][0] = player;
        }
        // Repeat for each square
 
       turn++;
       player *= -1; // 1 * -1 = -1 and -1 * -1 = 1 so we are just swapping which player's turn it is.
    }
}

I will consider and implement your ideea. I will attach my source code if you want to take a look.

"Don't gain the world and lose your soul. Wisdom is better than silver or gold." - Bob Marley

This topic is closed to new replies.

Advertisement