Sign in to follow this  
Rowen

1 Last thing about Tic-Tac-Toe

Recommended Posts

Hi! I found 1 other problem about my Tic-Tac-Toe game. Every time I press ESC, place an X or O, press the close window button or move the mouse the turn changes... It is really annoying...Please help! Thanks in advance!

Share this post


Link to post
Share on other sites
Well its hard to tell without the code, but i guess it would be something like this


bool inputactive=true;
while (inputactive)
{

//Your input code

//you should add a inputactive=false; when an x or o was placed

}

//turn changes




well i hope i understood your problem and gave you an idea how to solve it

Share this post


Link to post
Share on other sites
Crap...Forgot code once again...

:(

Sorry, well here it is:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <SDL/SDL.h>

SDL_Surface *back;
SDL_Surface *image1;
SDL_Surface *image2;
SDL_Surface *turn1;
SDL_Surface *turn2;
SDL_Surface *screen;

int gf[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
int winner = 0;
int randturn = 2;
int turn = 0;

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

int InitImages()
{
back = SDL_LoadBMP("tttbg.bmp");
image1 = SDL_LoadBMP("o.bmp");
image2 = SDL_LoadBMP("x.bmp");
turn1 = SDL_LoadBMP("player1turn.bmp");
turn2 = SDL_LoadBMP("player2turn.bmp");
return 0;
}

void DrawScene()
{
SDL_Flip(screen);
}

void place()
{
Uint8* keys;

int keyloop1 = 0;

while(keyloop1 == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{

if ( event.type == SDL_QUIT ) { keyloop1 = 1; }

if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { keyloop1 = 1; }
}

if (turn == 1)
{
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_1] ) { DrawIMG(image2, 100, 300); gf[0][2] = 1;}
if ( keys[SDLK_2] ) { DrawIMG(image2, 200, 300); gf[1][2] = 1;}
if ( keys[SDLK_3] ) { DrawIMG(image2, 300, 300); gf[2][2] = 1;}
if ( keys[SDLK_4] ) { DrawIMG(image2, 100, 200); gf[0][1] = 1;}
if ( keys[SDLK_5] ) { DrawIMG(image2, 200, 200); gf[1][1] = 1;}
if ( keys[SDLK_6] ) { DrawIMG(image2, 300, 200); gf[2][1] = 1;}
if ( keys[SDLK_7] ) { DrawIMG(image2, 100, 100); gf[0][0] = 1;}
if ( keys[SDLK_8] ) { DrawIMG(image2, 200, 100); gf[1][0] = 1;}
if ( keys[SDLK_9] ) { DrawIMG(image2, 300, 100); gf[2][0] = 1;}
keyloop1 = 1;
}

if (turn == 2)
{
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_1] ) { DrawIMG(image1, 100, 300); gf[0][2] = 2;}
if ( keys[SDLK_2] ) { DrawIMG(image1, 200, 300); gf[1][2] = 2;}
if ( keys[SDLK_3] ) { DrawIMG(image1, 300, 300); gf[2][2] = 2;}
if ( keys[SDLK_4] ) { DrawIMG(image1, 100, 200); gf[0][1] = 2;}
if ( keys[SDLK_5] ) { DrawIMG(image1, 200, 200); gf[1][1] = 2;}
if ( keys[SDLK_6] ) { DrawIMG(image1, 300, 200); gf[2][1] = 2;}
if ( keys[SDLK_7] ) { DrawIMG(image1, 100, 100); gf[0][0] = 2;}
if ( keys[SDLK_8] ) { DrawIMG(image1, 200, 100); gf[1][0] = 2;}
if ( keys[SDLK_9] ) { DrawIMG(image1, 300, 100); gf[2][0] = 2;}
keyloop1 = 1;
}

}
}
}

void changeturn()
{
if (turn == 1)
{
turn = turn + 1;
}
else if (turn == 2)
{
turn = turn - 1;
}
if (turn == 1)
{
DrawIMG(turn1, 0, 0);
place();
DrawScene();
}
else if (turn == 2)
{
DrawIMG(turn2, 0, 0);
place();
DrawScene();
}
}

int checkwin()
{
int winplayer = 0;
if (gf[0][0] == 1 && gf[1][1] == 1 && gf[2][2] == 1) {winplayer = 1;}
if (gf[0][2] == 1 && gf[1][1] == 1 && gf[2][0] == 1) {winplayer = 1;}
if (gf[0][0] == 1 && gf[0][1] == 1 && gf[0][2] == 1) {winplayer = 1;}
if (gf[1][0] == 1 && gf[1][1] == 1 && gf[1][2] == 1) {winplayer = 1;}
if (gf[2][0] == 1 && gf[2][1] == 1 && gf[2][2] == 1) {winplayer = 1;}
if (gf[0][0] == 1 && gf[1][0] == 1 && gf[2][1] == 1) {winplayer = 1;}
if (gf[0][1] == 1 && gf[1][1] == 1 && gf[2][1] == 1) {winplayer = 1;}
if (gf[0][2] == 1 && gf[1][2] == 1 && gf[2][2] == 1) {winplayer = 1;}

if (gf[0][0] == 2 && gf[1][1] == 2 && gf[2][2] == 2) {winplayer = 2;}
if (gf[0][2] == 2 && gf[1][1] == 2 && gf[2][0] == 2) {winplayer = 2;}
if (gf[0][0] == 2 && gf[0][1] == 2 && gf[0][2] == 2) {winplayer = 2;}
if (gf[1][0] == 2 && gf[1][1] == 2 && gf[1][2] == 2) {winplayer = 2;}
if (gf[2][0] == 2 && gf[2][1] == 2 && gf[2][2] == 2) {winplayer = 2;}
if (gf[0][0] == 2 && gf[1][0] == 2 && gf[2][1] == 2) {winplayer = 2;}
if (gf[0][1] == 2 && gf[1][1] == 2 && gf[2][1] == 2) {winplayer = 2;}
if (gf[0][2] == 2 && gf[1][2] == 2 && gf[2][2] == 2) {winplayer = 2;}

return winplayer;
}

void DrawIMG(SDL_Surface *img, int x, int y,
int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect src;
src.x = x2;
src.y = y2;
src.w = w;
src.h = h;
SDL_BlitSurface(img, &src, screen, &dest);
}

void DrawBG()
{
DrawIMG(back, 0, 0);
}

int main(int argc, char *argv[])
{
Uint8* keys1;
srand(time(NULL));
turn = rand()%2+1;

// rand()%1+1;

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}

InitImages();
DrawBG();

int done=0;

while(done == 0)
{
SDL_Event event1;

while ( SDL_PollEvent(&event1) )
{
if ( event1.type == SDL_QUIT ) { done = 1; }

if ( event1.type == SDL_KEYDOWN )
{
if ( event1.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}

changeturn();
winner = checkwin();
if (winner != 0)
{
done = 1;
}
}

return 0;
}

Share this post


Link to post
Share on other sites
I'm not too familiar with the rules of Tic-Tac-Toe but you said you don't want change the turn when escape is hit or the window is closed (when the event is SDL_QUIT or SDLK_ESCAPE). Since you set done to 1 when one of these events occur in main and below you call changeturn() you could test whether done == 0 before calling changeturn():

if (done == 0) //only change the turn if not done yet
changeturn();

\Jimmy H

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this