Every three numbers?

Hi,
I'm back making 2 more games for playdate. One of them Im trying to switch player turns every 3 moves based on 1-21.
if "whosturn" is 1-3 player1 goes (shake camera test)
if "whosturn" is 4-6 player2 goes (invert test)
if "whosturn" is 7-9 player1 goes (shake test) and so forth.
is this best done with if/elseifs or just if? I'm not sure best way to code 1-3, 4-6 etc. any ideas will help!

This should be all you need.

// start with player one

on load do
player_one = 1
player_two = 0
end

on turn do
if turns>=1 then
if turns<=3 then
player_one = 1
player_two = 0
end
elseif turns>=4 then
if turns<=6 then
player_one = 0
player_two = 1
end
elseif turns>=7 then
if turns<=9 then
player_one = 1
player_two = 0
end
end
end
end
end

Hope this helped.

thank you. Its only workin for player1 right now. Im putting it all in the on update, is that the best place for it? thanks.

This is the fixed version:

on load do
player_one = 1
player_two = 0
end

on update do
if turns>=1 then
if turns<=3 then
player_one = 1
player_two = 0
end
if turns>=4 then
if turns<=6 then
player_one = 0
player_two = 1
end
if turns>=7 then
if turns<=9 then
player_one = 1
player_two = 0
end
end
end
end
end

wonderful yes that works!!!! thank you!

Two things of note. One, branching can be expensive (and verbose in code). Two, whenever you recognize a pattern, there's almost always a formula that you can use to derive what you want; this is almost always faster, more efficient, and much shorter.

for turn = 1, 21 do
	whosturn = ((turn - 1) // 3) % 2 + 1
	print("Turn " .. turn .. " is player " .. whosturn)
end

In this example, we want to do things in groups of three, so we integer divide ( // ) by 3:
0 // 3 = 0, 1 // 3 = 0, 2 // 3 = 0, 3 // 3 = 1, 4 // 3 = 1, 5 // 3 = 1, 6 // 3 = 2, ...

Since turns start at 1 and the formula needs them to start at 0, we just subtract one from turn.

Now we have a sequence of 1, 1, 1, 2, 2, 2, 3, 3, 3,... that we need to convert to two values.

The %2 does that, by essentially dividing by 2 and looking at the remainder;
0 % 2 = 0, 1 % 2 = 1, 2 % 2 = 0, 3 % 2 = 1, 4 % 2 = 0,...

Applied to our list of 1, 1, 1, 2, 2, 2, 3, 3, 3,... it becomes 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1,....

Since we want player 1 or 2, instead of 0 or 1, we can just add one.

Alternatively, you could do this:

for turn = 1, 21 do
	playerOne = ((turn + 2 ) // 3) % 2
	playerTwo = 1 - playerOne

	print("Turn " .. turn .. "  playerOne: " .. playerOne .. "  playerTwo: " .. playerTwo)
end

This does the same thing as above, with a simple twist.

Since we want playerOne to be either a 0 or 1 (representing false and true), and we know % 2 gives us 0s and 1s, we add two to start further in the sequence so the first value we get is 1 for the first three times, then 0 for the next 3, and so on. No final adjustment needed.

Then playerTwo becomes 1 - playerOne, because 1 - 0 = 1, and 1 - 1 = 0. It is just the opposite.

And if you wanted true / false instead of 1 and 0, you could do this:

for turn = 1, 21 do
	playerOne = ((turn + 2 ) // 3) % 2 ~= 0
	playerTwo = not playerOne

	print("Turn " .. turn .. "  playerOne: " .. tostring(playerOne) .. "  playerTwo: " .. tostring(playerTwo))
end

Hope this helps,
-wls

thank you very much.

In terms of pulpscript, you can avoid having to hardcode everything for 1-21 by just increasing a counter each turn, and then switching player and resetting the counter when it hits 3. Something like:

// call this event with call "game_start" when starting a new game
on game_start do
  // start at turn 1 for player 1
  turn_count = 1
  player= 1
end

// call this event when a turn ends, to move to the next turn and see if players switch
on next_turn do
  turn_count++
  log "Turn count for player {player} is now {turn_count}"
  if turn_count > 3 then
    log "Player {player} has just had 3 turns, so switching player."
    // switch player
    if player == 1 then
      player = 2
    else
      player = 1
    end
    log "Player is now {player}"
    // reset counter
    turn_count = 1
  end
end

You can change the player switching to use player_one and player_two variables as above, of course. Hope it helps!

thank you ! playdate is fun.

1 Like

finally got the every three moves working for hotseat pass and play game of darts! Thanks all.
DART OF WAR by A Novel Approach (itch.io)