Tic Tac Toe game, trying to use more than 9 choices.

You have written onPress = bingoBalls. This tells the button to call the function bingoBalls when it is pressed. As bingoBalls is not a function, nothing happens.

You need to write a function that contains the shuffleTable(bingoBalls) call. Then change bingoBalls to the name of the new function. Ensure this function is above the code that creates the button so it is in scope.

I have a function called "shuffleTable (t)

local function shuffleTable( t ) if ( type(t) ~= "table" ) then print( "WARNING: shuffleTable() function expects a table" ) return false end local j for i = #t, 2, -1 do j = math.random( i ) t[i], t[j] = t[j], t[i] end return t end shuffleTable( B ) shuffleTable( I ) shuffleTable( N ) shuffleTable( G ) shuffleTable( O ) local B\_spots = {} local I\_spots = {} local N\_spots = {} local G\_spots = {} local O\_spots = {} for i = 1, 5 do B\_spots[i] = display.newText( B[i], 45, i \* 105, native.systemFontBold, 28 ) B\_spots[i] : setFillColor(1, 1, 1 ) I\_spots[i] = display.newText( I[i], 100, i \* 105, native.systemFontBold, 28 ) B\_spots[i] : setFillColor(1, 1, 1 ) N\_spots[i] = display.newText( N[i], 160, i \* 105, native.systemFontBold, 28 ) B\_spots[i] : setFillColor(1, 1, 1 ) G\_spots[i] = display.newText( G[i], 220, i \* 105, native.systemFontBold, 28 ) B\_spots[i] : setFillColor(1, 1, 1 ) O\_spots[i] = display.newText( O[i], 280, i \* 105, native.systemFontBold, 28 ) B\_spots[i] : setFillColor(1, 1, 1 ) B\_spots[i].y = math.floor( ( i - 1 ) / 1 ) \* 60 + 205 I\_spots[i].y = math.floor( ( i - 1 ) / 1 ) \* 60 + 205 N\_spots[i].y = math.floor( ( i - 1 ) / 1 ) \* 60 + 205 G\_spots[i].y = math.floor( ( i - 1 ) / 1 ) \* 60 + 205 O\_spots[i].y = math.floor( ( i - 1 ) / 1 ) \* 60 + 205 B\_spots[i].moveType = nil I\_spots[i].moveType = nil N\_spots[i].moveType = nil G\_spots[i].moveType = nil O\_spots[i].moveType = nil spots[i]:addEventListener( "touch", handleMove ) end N\_spots[3].text = " " for k = 1, 1 do spots[k] = display.newRect( 0, 0, 55, 55) spots[k]:setFillColor( 0.2,0.2,0.2,0 ) spots[k].x = ( k - 1 ) % 5 \* 60 + 40 spots[k].y = math.floor( ( k - 1 ) / 5 ) \* 60 + 205 spots[k].moveText = display.newText( " ", spots[k].x, spots[k].y, native.systemFontBold, 60) spots[k].moveType = nil spots[k]:addEventListener( "touch", handleMove ) end

I also have the “Reset” of the game that resets everything but the shuffle

local function resetGame() for i = 1, #spots do spots[i].moveText.text = " " spots[i].moveType = nil end display.remove( winningLine ) winningLine = nil display.remove( winningRec ) winningRec = nil display.remove( winningImage ) display.remove( resetButton ) resetButton = onPress player = "X" allowMoves = true end 

Is there a way I could add code to this that would allow me to shuffle the numbers from table t when reset is pressed.  I am truely trying to figure this out but I am stuck. 

No, I mean put the CALL to the shuffleTable function inside another one. When you run a function, that is called ‘calling’ the function.

[lua]

local function shuffleBingoBalls()

  tableShuffle(bingoBalls)

end

[/lua]

Then edit button:

[lua]

local button3 = widget.newButton(

{
width = 60,
height = 35,
defaultFile = “btn green.png”,
overFile = “btn yellow.png”,
label = “”,
onPress = shuffleBingoBalls
}
)

[/lua]

We need to ‘wrap’ the call to tableShuffle because the button listener cannot be passed custom parameters. 

We could not do for example: onPress = tableShuffle(bingoBalls)

So we must call an intermediary function that does this step for us.

The table I am trying to shuffle is labeled “t”.  I put the first part of the code up higher in my code as the first function.  I put the button at the bottom of the code. I get the error,

"main.lua35:attempt to call global’tableShuffle’ (a nil value) stack traceback: 

main.lua:35: in function ‘_onPress’

?:in function ‘?’

?:in function ‘?’

?:in function <?:190>"

If I change the 

tableShuffle(bingoBalls)

to

tableShuffle(t)

I get the same error.

Mark,

I am coming into this late… and know nothing of all the issues or the initial question…  but to the last error message posted…   if you look closely at the error messages, there are clues.

your note list error as  :

attempt to call global’tableShuffle’ (a nil value)

I think you have, in your sample code,  just above nick_sherman’s last post the function called ‘shuffleTable’ …

so tableShuffle is wrong … it should be shuffleTable

I hope this helps, and again I have not read any of this post except your last two posts to this issue.  But I think that is the cause of the error you posted.

Good luck

Bob

it gives the same error, except with shuffleTable instead of tableShuffle

My guess is that the function you are calling is not visible in the scope you are calling it.

i.e. If you did this, you’d get a similar error:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/10/scopeAndVisibility.zip

local function test2() print("\n\nGets an error...") doit() end local function doit() print("YO I'm visible to test1(), but not test2()") end local function test1() print("\n\nWorks fine...") doit() end test1() test2()

Produces this log:

15:28:06.036 Works fine... 15:28:06.036 YO I'm visible to test1(), but not test2() 15:28:06.036 15:28:06.036 15:28:06.036 Gets an error... 15:28:06.036 ERROR: Runtime error 15:28:06.036 X:\Work\00\_CurentProjects\Corona\RG\_FreeStuff\AskEd\2018\10\scopeAndVisibility\main.lua:8: attempt to call global 'doit' (a nil value) 15:28:06.036 stack traceback: 15:28:06.036 X:\Work\00\_CurentProjects\Corona\RG\_FreeStuff\AskEd\2018\10\scopeAndVisibility\main.lua:8: in function 'test2' 15:28:06.036 X:\Work\00\_CurentProjects\Corona\RG\_FreeStuff\AskEd\2018\10\scopeAndVisibility\main.lua:22: in main chunk

I have tried everything I can think of, everything that I can guess, everything I can research.  I know it isn’t working.  I really need help with a solution rather than posts that what I am doing isn’t working.  I do know that. Is there anyone out there that can help Please?

Sorry Mark, but that’s how it works.  We respond the best we can to the posts you write.  

Sometimes folks feel generous enough to really dig in and help re-write a project, but often times we’re too busy so we post back help and hints instead.

What it really sounds like you need is  a partner, mentor, or contractor.  

Would it be of any value to you at all if someone posted code that could do this?:

  • Draw a bingo card with these attributes
    • 5x5 alternating color grid separated by lines
    • Free spot filled with circle
    • Other grid spots filled with numbers that you can define in code as a table.
    • each grid is a button that can be toggled to show it is selected
  • Includes logic to detect a BINGO and if so, to highlight the line and show a message. “BINGO”.
  • Includes a reset button to clear all selections and essentially restart the game.

Thank you so much for all of your help.  I really do appreciate it.  I can’t waste any more time on this.  I really truly do appreciate your help. 

First, you can’t do:

tableShuffle( t )

because you don’t have a table named “t”. Second of all, when people are helping you here, they are hand typing in examples that **should** work, but sometimes typos get in the way. It’s really important for you to learn how to debug your code. There is a typo in some provided code, but the answer is in your error message.

"main.lua35:attempt to call global 'tableShuffle' (a nil value) stack traceback:&nbsp;main.lua:35: in function '\_onPress' ?:in function '?' ?:in function '?' ?:in function \<?:190\>"

 

Look at your main.lua.

Go to line 35

You are calling a function named “tableShuffle”. 

Corona says it can’t find it (a nil value)

 

You need to ask yourself a question? Why is “tableShuffle” nil or non-existent? 

Then you need to look through your code and see why it can’t find a function named “tableShuffle”

 

If you do that, the answer is clear. You don’t have one named tableShuffle, but you do have one named shuffleTable. In this case, 

 

This code:

 

local function shuffleBingoBalls() tableShuffle(bingoBalls) end

probably should be

local function shuffleBingoBalls() shuffleTable(bingoBalls) end

Now order of code matters in Lua. This function must exist before you create your button. This code calls shuffleTable and references the table “bingoBalls”, so it has to exist **after** both of those items in your code.

 

Rob
 

Mark,

If you truly want to code and make games, don’t give up.  It will make you pull your hair out alot of times.  Take a break for a day and give it another go.

If you don’t mind me saying, learning from scratch as you apparently are doing, is the way I did it - and it took a lot of time and a lot of frustration… and I am still not at the level of Roaming Gamer and Nick_Sherman and Rob Miracle to just name a few… but for me it was worth it.

If I can add; I had to take apart sample code over and over and over… and go thru countless tutorials - changing code in those tutorials to get a feel of what was actually happening…  it is time consuming and aggravating; especially when you are anxious to get to making something real.  I also started with the simplest of sample code.  Simple tic tac toe type game. And moved up from there.

Just a few last points:

  1. As Roaming Gamer mentioned ‘scope’ is the likely issue to the last error… very common for someone new to coding.  Look at as many tutorials as you can find that strictly talk about ‘scope’.

  2. It is not an easy; and one of the most common mistakes I made and I see others make, is not reading the error message completely and misspelling the name of the variable or functions and not matching ‘case’.

I hope you give it another try after a short break… that is if you truly want to code games.  Corona is a great tool for doing that.

Best of luck

Bob

As advised before Mark, you cannot code without learning to code. Countless hours of frustration you have experienced would have been avoided by learning the basics. And probably saved you hours of trial and error.

For example, the whole table named ‘t’ mix up.

When an object is used in a function, it is often given a different name than that of the object provided. We can pass any table to shuffleTable and all these could have different names.

While it is being processed in that function, it will be called ‘t’. It’s kind of a nickname, a handle. ‘t’ is used because it’s easy to type over and over and short for table.

A more accurate name might be ‘tableThatIsToBeShuffled’. However, in this small function, an abbreviation is fine. If we had a global variable called ‘t’ however, we’d come back to it 6 months later and have no idea what it stood for.

Once that shuffleTable function is over, the table must still be referred to by its original name. The nickname, or reference, ‘t’ no longer exists.

https://www.youtube.com/watch?v=ZugCYGLF12Q&feature=youtu.be

https://github.com/roaminggamer/RG_FreeStuff/raw/master/gamesIn250/002_bingoCard.zip

I took a glimpse at your code, which, btw, you should post to the forums by using the code insert button <>.

It seemed that you had only changed the way that you create those “spots”. The rest of it seems to simply be lifted off the tic-tac-toe, including the game logic itself. This means that the game is trying to determine the winner in a 3x3 grid, even though you’ve given it a 5x5 grid.

As for the reason why they don’t line up, it’s most likely due to your math being incorrect. I just did this in my head, so it might not be correct, but try changing the x and y value generation to:

 

 spots[i].x = ( i - 1 ) % 5\* 60 + 120 spots[i].y = math.floor( ( i - 1 ) / 5 ) \* 60 + 40

Finally, you have numerous variables with duplicate names, e.g. leftVerticalLine. You don’t want to do that, because the moment that you create a new variable with the same name, you can no longer control the previously created variable and it’ll just stay there until the app closes.

Tutorials are a great place to get started and to pick some new tricks, but I wouldn’t recommend starting to build a game that relies on entirely different logic upon a tutorial, like you now have. You’ll just end up with messy code and problems that should have never existed.

Agree with @XeduR. By all means it’s a good idea to mess around with the tutorial code to get it to do slightly different things, so you understand better how it works. I started with Corona by reversing an angry birds tutorial so that you designed the tower and the AI fired the birds.

However once you’ve learned all you can, it’s best to start fresh. I get the impression you don’t yet fully understand how the tic-tac-toe code is working, which makes it difficult to build something more complex with it.

The only reference to the logic I have is the code supplied in the tutorial.  I am new.  I don’t know the logic of the math or what I am manipulating. My point is I am trying to make this work on a grid that is 5x5 rather than 3x3.  I can build the grid.  I have changed the names of the lines to make each unique.  I have changed the number in the [i] sections.  It still gives me the 5x5 grid with background colors 3 across.  I can change the size of the background square but I don’t see where to change the grid to 5x5.  There is nothing in the tutorial.  I have watched both segments of it several times.  The math on how to move the square doesn’t give any logic on how and what the numbers represent.  The narrator simply says that he already calculated the math and we might want to play around with it.  I did this and it moves the squares and numbers in illogical areas.  Sometimes changing the order.  I have no reference point.  All I can do is guess.  I have been working on this for over a week.  Could someone help me.  All I get is so far is to watch the tutorials. This isn’t helping. I can completely make the tic tac toe game.  But this isn’t my end goal. 

This is the code I am using.

<-----------------------------------------------------------------------------------------

– main.lua


– Your code here

local firstVerticalLine = display.newRect( display.contentCenterX - 90, display.contentCenterY, 5, 300 )

local secondVerticalLine = display.newRect( display.contentCenterX - 30, display.contentCenterY, 5, 300 )

local thirdVerticalLine = display.newRect( display.contentCenterX + 30, display.contentCenterY, 5, 300 )

local forthVerticalLine = display.newRect( display.contentCenterX + 90, display.contentCenterY, 5, 300 )

local firstHorzontalLine = display.newRect( display.contentCenterX, display.contentCenterY - 90, 300, 5 )

local secondHorzontalLine = display.newRect( display.contentCenterX, display.contentCenterY - 30, 300, 5 )

local thirdHorizontalLine = display.newRect( display.contentCenterX, display.contentCenterY + 30, 300, 5 )

local forthHorizontalLine = display.newRect( display.contentCenterX, display.contentCenterY + 90, 300, 5 )

local spots = {}

for i = 1, 25 do

    spots[i] = display.newRect( 0, 0, 60, 60)

    spots[i]:setFillColor( 0.2, 0.2, 0.2 )

    spots[i].x = ( i - 1 ) % 3 * 100 + 60

    spots[i].moveType = nil

end>

The source initially is from the source code for the tic tac toe game.  I didn’t see in it or on the tutorial where and how it was set to 3x3.  This is where I am stuck. I have started over again in a new project typing the code in line by line.  It still doesn’t give a 5x5 grid.

Well, it seems you are trying to run before you can walk. There’s not much point in a tutorial if you don’t understand every bit of code, what it’s doing and why. 

I’d advise you to continue learning the basics (perhaps with tutorials that actually explain the code) before trying to make something fairly complex like a bingo game. Hacking it together with guesswork won’t do you any good in the long run.

Do I need to watch every tutorial that there is on LUA  before I can get the answer to a simple question like how to convert a 3x3 grid to a 5x5?  I don’t think this is the most difficult question.  But it is like a combination to a safe.  If you don’t know the answer just simply guessing isn’t going to help.  If you don’t know I understand.  If someone out there knows where I can find the information needed to solve this please tell me.  If it is in another tutorial, that is fine.  I have no reference point and telling me to look to the tutorial.  I can’t find it in the tutorial.  I have watched it several times.  It does not explain where to set the grid to 3x3.  

Of course it’s not a difficult question, but you’re asking someone to read through 200 lines of unformatted code they haven’t seen before, work out what it’s doing (there are a hundred ways you could implement tic-tac-toe) then work out exactly what you actually want it to do, which isn’t at all clear from the original post. 

It is unfortunate the tutorial seems to be poorly done, simply skipping over key lines of code. Pretty amazed that’s an official Corona video really…

Here’s what that bit of code does:

[lua]

for i = 1, 9 do   – loop through the nine squares

    spots[i] = display.newRect( 0, 0, 90, 90)   – create a new 90 x 90 square and add it to the spots table

    spots[i]:setFillColor( 0.2, 0.2, 0.2 )    – set the colour to grey

    

    spots[i].x = ( i - 1 ) % 3 * 100 + 60   – calculate the X position of the square (see below)

        

        – i - 1    – if we are doing square one, use 0. if we are doing square 6, use 5.

        – % 3      – get the remainder of (i-1) when divided by 3. This gives us our ‘column ID’

                    – 0 % 3 = 0. 1 % 3 = 1. 2 % 3 = 2. 3 % 3 = 0. 4 % 3 = 1. etc.

        – * 100    – multiply the column ID by 100. So the first column will be 0, the last column 200.  

        – + 60     – finally add 60 to the calculation. Column 0 = 60. Column 1 = 160. Column 2 = 260

        

    spots[i].y = math.floor( ( i - 1 ) / 3 ) * 100 + 140 – calculate the Y position of the square (see below)

    

        – i - 1      – if we are doing square one, use 0. if we are doing square 6, use 5.

        – / 3        – divide this number by 3. 1 / 3 = 0.333. 4 / 3 = 1.333.

        – math.floor – round this number down to the nearest whole number.

                      – therefore, 0.333 = 0, 1.333 = 1

                      – this gives us our ‘row ID’

      – * 100        – multiply the row ID by 100. Row 0 = 0. Row 2 = 200.

      – + 140        – add 140 to the calculation. Row 0 = 140. Row 1 = 240. Row 2 = 340

      

    spots[i].moveText = display.newText( i, spots[i].x, spots[i].y, native.systemFontBold, 80)

                – create a text object in the same place as our square

    spots[i].moveType = nil   – pointless line…already nil

    spots[i]:addEventListener( “touch”, handleMove )    – add a touch listener to this square, which will call handleMove function.

    

end

[/lua]