else/if question

Hello,

I was wondering how I would use elseif (or a better choice) to switch turns every second time.

So one person goes twice, then the other person goes twice, and it loops around. How would I do that?

Use modulo

for i = 1, 10 do if( i % 2 == 0) then print("turn A") else print("turn B") end end

Hello,

I tried this

for i = 1, 10 do if( i % 2 == 0) then playerTurn = "RED" else playerTurn = "BLUE" end end

but when I execute it, “BLUE” goes once and “RED” goes on. I also made it at the beginning that

local playerTurn = "BLUE"

So how can I fix the problem? Thanks

for i=1,10 do q = i%4 if( (q==0) or (q==3) ) then print( "A" ) else print( "B" ) end end

That should give B-B-A-A-B-B-A-A-etc.  If you want to adjust the starting sequence (let A go once before B goes twice), test against q=0 and q=1 (assuming a start index i=1).

@mejon,

Huh?  Show the sequence you want.  You said every second time, which I read as every other time… but clearly that isn’t what you meant, because this sequence produces every other time:

for i = 1, 10 do if( i % 2 == 0) then print("BLUE") else print("RED") end end

Also, I’m unclear on why you changed the print() calls to assignments.  The implications of the print() is that you need to take your programatiic action there OR defer till after the test, but the action still needs to be in the loop.

local doBill() print("Bill") end local doBob() print("Bob") end for i = 1, 10 do local action if( i % 2 == 0) then action = 1 else action = 2 end -- Take alternating actions in the loop, not elsewhere if( action == 1) then doBill() else doBob() end end

@jbpl

How can I do that same sequence B-B-A-A-B-B-A-A to alternate player turns. So one player goes twice then the other player goes twice.

Thank you

@roaminggamer

Hi roaminggamer I want a sequence like this: B-B-A-A-B-B-A-A.

I was looking at your Tic-Tac-Toe template which alternates A-B-A-B-A,

if( currentTurn == "X" ) then currentTurn = "O" else currentTurn = "X" end

but how would I change it to be B-B-A-A-B-B-A-A.?

Thank You

Ah, that is the problem with the Forums sometimes.  It is easy to misunderstand questions. 

I think JBP1 has provided an answer that does what you want to do.  It is a combination of modulo calculations and knowing the right values to test for in order to choose A or B.

Another way to do it would be:

local count = 1 for i = 1, 10 do if( count \> 2 ) then print("A") else print("B") end count = count + 1 if( count \> 4 ) then count = 1 end end

@ roaminggamer

What you posted above does not seem to work. When I execute it, Player 1 goes, then Player 2 goes forever without going back to player 1.

Is there any way I can change this:

if( currentTurn == "X" ) then currentTurn = "O" else currentTurn = "X" end

So it goes B-B-A-A-B-B-A-A.

instead of  B-A-B-A-B-A? I want to make it so one player goes twice then the other player goes twice.

first thing i’d do is clean up all the “A”,“B” or “Bob”,“Bill” or “X”,“O” that make this thread hard to follow (or it that just me? :D)  so let’s at least define an array of names you can look up by their index (1 or 2, per Lua convention of 1-based array indices):

names = {"A","B"}

next, i’d rigorously define how I’m going to number the first and subsequent turns (because it’ll matter if you’re using modulo, else your sequence might be “out of phase” by one).  let’s again adopt Lua’s 1-based convention, so that the first turn is 1, and so on.

so,… you have a “sequence” that is four long, with each subpair of two returning one of two values [1,2] that are essentially “half” (*)  of the turn sequence value [1,2,3,4].  reading that back with our “math glasses” on gives:  modulo by four, divide by two.  (* taking integer result of course AND fixing up all the -1’s and +1’s along the way as required by the 0-based modular math given the 1-based indices elsewhere)

function whose(turn) return math.floor((turn-1)%4/2)+1 end

then (finally) you can do a mock run through a loop of turns to print out player names

for turn = 1,20 do print(names[whose(turn)]) end

that should give you your AABBAABBAABB…, hth

I tested the code before I posted it, so I think you’ve taken it and modified it, and introduced a scope error or other error?

@Dave,

Yeah I was giving some varied responses to try to focus on the concept and not a specific solution.

The problem is there is a bit of a disconnect between the concept of toggling between choices and getting it integrated into mejon’s exact situation/code.

This is one of those cases where the forums fail because there is missing context and I can’t know for sure that mejon knows the things necessary to take my general solution suggestion and apply it to the problem at hand.

As always, the problem comes down to context.  :(

@mejon,

Hi again.  I think we’re probably just not connecting here. i.e. I am not clear on how you want to apply this, so do me a favor.

If you are OK with me posting a small piece of the solution, including part of your code here later,

  1. Edit the code where you’re having an issue and put these initials on the lines you think are pertinent: EFM

  2. Zip up or Rar the project.

  3. e-mail it to me here:

rgemail2.png

  1. I’ll give it about 15 minutes tonight or tomorrow at the latest and try to help you out.

-Ed

btw jic - no offense meant from my remark, everyones’ attempts to help have been valid

my two cents, having sensed some “struggle” still in the op to see how ANY answer fit, was just if maybe it were broken up a bit then op might find what was needed in a more isolated snippet.

there’s surely dozens of good ways to solve the question as originally asked, but yep, perhaps it’s not the whole question.

@roaminggamer

You know how you used:

if( currentTurn == "X" ) then currentTurn = "O" else currentTurn = "X" end

in the your Tic Tac Toe template? Well I want to do the same thing except instead of X-O-X-O-X-O, I want XX-OO-XX-OO-XX-OO.  thanks for the help so far

turnLimit defines how many turns a player gets.

[lua]local turnLimit = 2
local currentTurnNum = 0
local currentTurn = “X”

local function takeTurn()
    – Swap players every turnLimit turns.
    if ( ( currentTurnNum % turnLimit ) == 0 and currentTurnNum ~= 0 ) then
        if ( currentTurn == “X” ) then
            currentTurn = “O”
        else
            currentTurn = “X”
        end
    end

    – Do turn stuff. Here we’re just building a turn list.
    print( “Turn #” … currentTurnNum … ": " … currentTurn )

    currentTurnNum = currentTurnNum + 1
end

for i=1,10 do
    takeTurn()
end[/lua]

@mejon,

I just e-mailed the update, but this is the primary bit I changed:

  1. Made a file-level visible counter at top of file (local count = 1).

  2. Added this code for deciding the current turn:

    if( count > 2 ) then currentTurn = “O” else currentTurn = “X” end count = count + 1 if( count > 4 ) then count = 1 currentTurn = “X” end

  3. Added code to reset count (to 1) on game restart

@everyone

thanks to all that replied

@roaminggamer

Thank you Very very much Ed. I appreciate the help as I was stuck on this for a while. Thanks again. mejon

Use modulo

for i = 1, 10 do if( i % 2 == 0) then print("turn A") else print("turn B") end end

Hello,

I tried this

for i = 1, 10 do if( i % 2 == 0) then playerTurn = "RED" else playerTurn = "BLUE" end end

but when I execute it, “BLUE” goes once and “RED” goes on. I also made it at the beginning that

local playerTurn = "BLUE"

So how can I fix the problem? Thanks

for i=1,10 do q = i%4 if( (q==0) or (q==3) ) then print( "A" ) else print( "B" ) end end

That should give B-B-A-A-B-B-A-A-etc.  If you want to adjust the starting sequence (let A go once before B goes twice), test against q=0 and q=1 (assuming a start index i=1).

@mejon,

Huh?  Show the sequence you want.  You said every second time, which I read as every other time… but clearly that isn’t what you meant, because this sequence produces every other time:

for i = 1, 10 do if( i % 2 == 0) then print("BLUE") else print("RED") end end

Also, I’m unclear on why you changed the print() calls to assignments.  The implications of the print() is that you need to take your programatiic action there OR defer till after the test, but the action still needs to be in the loop.

local doBill() print("Bill") end local doBob() print("Bob") end for i = 1, 10 do local action if( i % 2 == 0) then action = 1 else action = 2 end -- Take alternating actions in the loop, not elsewhere if( action == 1) then doBill() else doBob() end end