How to go make players take turns
Jul 21, 2011 at 5:13pm UTC
This piece of code should wait for a click, get the coordinates of the click, and the send the coordinates to a function. Then is should wait for another click, get those coordinates, and send those coordinates to a function.
The first click should make an X appear, the second click should make an O appear, and then it should keep looping that until there is a winner. ( i know i havent made a check for winner function yet )
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
void MultiPlayer::HandleEvents()
{
SDL_Event event;
//While there's events to handle
while ( SDL_PollEvent( &event ) )
{
if ( event.type == SDL_MOUSEBUTTONDOWN )
{
if ( event.button.button == SDL_BUTTON_LEFT )
{
int x = event.button.x;
int y = event.button.y;
PlacePiece( x, y, X );
SDL_Flip( background );
while ( SDL_PollEvent( &event ) )
{
if ( event.type == SDL_MOUSEBUTTONDOWN )
{
if ( event.button.button == SDL_BUTTON_LEFT )
{
int a = event.button.x;
int b = event.button.y;
PlacePiece( a, b, O );
SDL_Flip( background );
}
}
}
}
}
if ( event.type == SDL_QUIT )
{
//Quit the program
nextState = STATE_EXIT;
}
}
}
Jul 21, 2011 at 5:52pm UTC
No no, you can't just keep on nesting deeper.
Instead, you need a simple variable that indicates which player's turn it is, i.e.
1 2 3 4
bool firstPlayersTurn=true ;
[...]
PlacePiece(x,y,firstPlayersTurn? X : O);
firstPlayersTurn=!firstPlayersTurn;
Jul 21, 2011 at 6:45pm UTC
lol im sorry but i couldnt make anything out of that.
Instead i just made seperare functions to handle each players turn :/
Jul 21, 2011 at 7:05pm UTC
Instead i just made seperare functions to handle each players turn :/
That doesn't sound like a good idea, at least if you replicate the event loop in both functions.
lol im sorry but i couldnt make anything out of that.
Which part of it? What I meant was:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
void MultiPlayer::HandleEvents()
{
SDL_Event event;
//While there's events to handle
bool firstPlayersTurn=true ;
while ( SDL_PollEvent( &event ) )
{
if ( event.type == SDL_MOUSEBUTTONDOWN )
{
if ( event.button.button == SDL_BUTTON_LEFT )
{
int x = event.button.x;
int y = event.button.y;
PlacePiece(x,y,firstPlayersTurn? X : O);
firstPlayersTurn=!firstPlayersTurn;
SDL_Flip( background );
}
}
if ( event.type == SDL_QUIT )
{
//Quit the program
nextState = STATE_EXIT;
}
}
}
Having all event handling code inside the main event loop isn't exactly ideal, but that's another story.
Topic archived. No new replies allowed.