How to go make players take turns

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;
		}
	}
}
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;
lol im sorry but i couldnt make anything out of that.

Instead i just made seperare functions to handle each players turn :/
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.