This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
minecraftwarlock's profile picture

Checkers game move function not working

Started by minecraftwarlock, 19 September 2014 - 10:15 PM
minecraftwarlock #1
Posted 20 September 2014 - 12:15 AM
I've been making a checkers game and the move piece buttons aren't registering a click.
Here are the APIs that are used:
Button API: http://pastebin.com/Q75mfYV9
Checkers utility (cutil) API: http://pastebin.com/LyKFntWp

Here is the code:
http://pastebin.com/ASMidjC8
Edited on 24 September 2014 - 03:43 AM
Lyqyd #2
Posted 20 September 2014 - 12:54 AM
It should return nil, not an empty table. You've localized the mods table to the if block, so it doesn't exist when you try to return it. Declare it before the if block.
minecraftwarlock #3
Posted 21 September 2014 - 05:11 AM
I am trying to make a checkers game but when I try and run the program I get this error:
cutil:116: bad argument: table expected, got nil

Here is the pastebin:
http://pastebin.com/4byr8hit
Dog #4
Posted 21 September 2014 - 05:35 AM
This is just a guess, but since you're using pairs, you probably want the second value returned (the table), not just the first (the key). Also, the way you're referencing the table, I *think*, is incorrect.

Try changing…

function drawWhite()
  for i in pairs(pieces[white]) do

To…

function drawWhite()
  for _,i in pairs(pieces.white) do

I keep feeling like I'm seeing something else, but I can't put my finger on it. Try that and see if that helps.
minecraftwarlock #5
Posted 21 September 2014 - 05:56 AM
Didn't help still getting the same error.
Bomb Bloke #6
Posted 21 September 2014 - 06:26 AM
The original line will error due to lack of quotes around "white". Dog's fix should prevent that specific problem, but switching to "_,i" will cause other issues on the next line.

I'd do it like this:

        for key,value in pairs(pieces.white) do
                button.define(key,"X",getRealCoords(value[x],value[y]),colors.white,colors.red,false)
        end
Dog #7
Posted 21 September 2014 - 06:45 AM
Ninja'd by BB
Edited on 21 September 2014 - 04:45 AM
minecraftwarlock #8
Posted 22 September 2014 - 07:13 AM
I'm making a checkers game and when I try to run the code I get this error:
cutil:46: '}' expected (to close '{' at line 45)

Here is the link to the code:
http://pastebin.com/Bg44ZK4Y
Bomb Bloke #9
Posted 22 September 2014 - 07:49 AM
When defining your key names, if you're going to stick quotes around them, use square brackets as well.

Eg:

myTable = {piece = 2}     -- Valid.
myTable = {"piece" = 2}   -- Invalid.
myTable = {["piece"] = 2} -- Valid.

Square brackets with quotes are required for certain key names (eg, anything with a space in it). Personally I use them for all key names.
minecraftwarlock #10
Posted 22 September 2014 - 08:38 AM
I've been trying to code a checkers game but the pieces won't be drawn and there is no error.

Here are the APIs used:
Button API http://pastebin.com/4i7sJ7ev
Checkers Utility (aka cutil) API http://pastebin.com/Wf6eUr5V

Here is the code itself:
http://pastebin.com/xStDSahr

All the APIs and the code are made by me.
Bomb Bloke #11
Posted 22 September 2014 - 11:27 AM
On line 191 of cutil, you're using ipairs on "pieces" - but "pieces" has no numeric indexes, so the loop does nothing. Use "pairs" instead.
Lyqyd #12
Posted 22 September 2014 - 06:09 PM
Threads merged. Please stick to one topic for all questions about a given program. Feel free to edit the title of the post by using the full editor on the first post.
minecraftwarlock #13
Posted 25 September 2014 - 06:39 AM
I've been making a checkers game and the move piece buttons aren't registering a click.
Here are the APIs that are used:
Button API: http://pastebin.com/Q75mfYV9
Checkers utility (cutil) API: http://pastebin.com/LyKFntWp

Here is the code:
http://pastebin.com/ASMidjC8
Bomb Bloke #14
Posted 25 September 2014 - 03:24 PM
I guess line 346, "button.deactivate("all")", is the cause of that. Maybe swap its place with the line above it.
minecraftwarlock #15
Posted 26 September 2014 - 08:39 PM
I guess line 346, "button.deactivate("all")", is the cause of that. Maybe swap its place with the line above it.

That's fixed the problem but now I get this error:
button:166: bad argument: number expected, got string

I believe the lines causing the problem are lines 355 and 364 of cutil.
Bomb Bloke #16
Posted 27 September 2014 - 05:27 AM
Line 166 of button reads:

table.remove(buttons,button)

However, table.remove() is intended for use on numeric table indexes. You're using keys in the form of strings, so it's not suitable.

Switch it to this:

buttons[button] = nil

Edit:

Some more details are probably worthwhile here.

There are two ways you can use a Lua table: One is as an array, one is as a hashmap. These are somewhat different animals, but Lua allows you to treat one table as either at the same time.

When you add entries to your table, if you use numerals to refer to their position, then you're treating the table as an array. If you use strings (or other objects), then you're treating it as a hashmap.

Let's say you build a table like this:

local myTable = {["someKey"] = "moo", ["someOtherKey"] = "moo2"}

Lua hashes the key names "someKey" and "someOtherKey", and uses the resulting numeric values to work out where to store the related data values ("moo" and "moo2") in memory. To find them again later - say you try to print myTable.someKey - all it needs to do is rehash the key name, and get the data value from the corresponding memory location. This is called hash-mapping, and while it's very inefficient in terms of memory usage, it's very convenient to use.

Let's say you build a table like this:

local myTable = {[1] = "moo", [2] = "moo2"}

In this case, Lua just uses the numbers directly to work out where to stick the data values - this is how an array is treated. Much more efficient to store, but it can potentially be more trouble to find certain values in a numerically indexed table - hash-maps are potentially much faster, depending on what you're doing.

Certain commands, such as ipairs, table.insert() / table.remove(), #myTable and so on are only really intended for working on arrays. If you try to use them on a table which only uses hash-map style keys, they'll act like the table is empty. If you have a table with both hash-map style keys and numeric indexes, these commands will only operate on the numeric indexes.

If, on the other hand, you build your table up like an array, you won't be able to do what you're trying to do on line 359 in cutil. Your "moves" table is built that way (through your use of table.insert() all through the getMoves() function), but there you're trying to index into it using your "move" string.
Edited on 27 September 2014 - 03:44 AM