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

Table not being properly copied

Started by Bedrockbreaker, 20 October 2016 - 11:49 PM
Bedrockbreaker #1
Posted 21 October 2016 - 01:49 AM
I have been creating a card game (http://pastebin.com/Ptvs52uy)thing and the first thing I wanted to create was Uno. I tried adding robots that just select the first card in its hand, but when I view the current players hand (in this case the robots) it didn't copy the robot's hand into the current players. It just shows a blank white card in which I am pretty sure is a nil, and not any card of the robot's hand. The robot continues to draw a card, because it has nothing to match the discard like normal though. I even tried assigning the current player's hand to a set hand (wild, draw 4, skip, reverse, draw 2, and a normal card) but it still didn't show. I checked and re-checked what was wrong, but I couldn't find it. The part of the assigning the hand to the current player is at line 373. I really don't know what to do. And yes, I do know that I have excess variables and things that I could remove, but I will get around to that later. I also know that is throws an error when the robot tries to lay down a card that would change the color. There is a lot of things wrong. Don't question my methods.
KingofGamesYami #2
Posted 21 October 2016 - 02:50 AM
I'm sorry, but I really can't understand what your code is doing. It would be very helpful if you commented your code rather heavily due to the complexity of it. You may even discover the logic behind the undesired behavior while doing so.
Bedrockbreaker #3
Posted 21 October 2016 - 04:08 AM
Okay, I will try. I usually don't comment my code because I can understand it, but I never remember others can't.
Admicos #4
Posted 21 October 2016 - 04:56 AM
Okay, I will try. I usually don't comment my code because I can understand it, but I never remember others can't.
Do it while you can still understand, as in a few days of not looking at it, it will basically be unreadable.
Bomb Bloke #5
Posted 21 October 2016 - 10:33 AM
It's not the lack of comments that bugs me so much as the lack of formatting - books are written in paragraphs for good reason, and unspaced code likewise reads as an amorphous blob of text. Stick an extra line break between your blocks.

If I were you, before attempting to troubleshoot the problem I'd be refactoring the whole script into something shorter. You'll find it's a lot easier to track where things are going wrong.

Don't define functions inside your loops. If you tell Lua to rebuild a function over and over again, it'll do exactly that. If a declaration only needs to be peformed once, then move it near the top of your code.

A dedicated writing function would be advantageous. Here's one I've often used:

local function writeAt(text, x , y, textCol, bgCol)
	if textCol then term.setTextColour(textCol) end
	if bgCol then term.setBackgroundColour(bgCol) end

	term.setCursorPos(x, y)
	term.write(text)
end

This allows you to turn this sort of thing:

term.setCursorPos(21,8)
print("|        |")
term.setCursorPos(21,9)
print("|        |")
term.setCursorPos(21,10)
print("|        |")
term.setCursorPos(21,11)
print("+--------+")
term.setCursorPos(33,11)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print("Arrows: Select Card")

… into this sort of thing:

writeAt("|        |", 21,  8)
writeAt("|        |", 21,  9)
writeAt("|        |", 21, 10)
writeAt("+--------+", 21, 11)
writeAt("Arrows: Select Card", 33, 11, colours.white, colours.black)

Further use of tables is nigh-on essential for keeping these sort of scripts to a manageable size. For example, say we built a table out of this lot:

  --Dealing!
  play1 = {""}
  play2 = {""}
  play3 = {""}
  play4 = {""}
  robot1 = {""}
  robot2 = {""}
  robot3 = {""}

… like so:

  plays = {{""}, {""}, {""}, {""}}
  robs = {{""}, {""}, {""}}

… then we could shrink this sort of thing:

  deal(play1)
  if players >= 2 then
    deal(play2)
  end
  if players >= 3 then
    deal(play3)
  end
  if players == 4 then
    deal(play4)
  end
  if robots >= 1 then
    deal(robot1)
  end
  if robots >= 2 then
    deal(robot2)
  end
  if robots == 3 then
    deal(robot3)
  end

… down like so:

  for i = 1, players do deal(plays[i]) end
  for i = 1, robots do deal(robs[i]) end

Likewise, a table like this:

local shorts = {["red"] = "r", ["blue"] = "b", ["green"] = "g", ["yellow"] = "y"}

… lets you turn the likes of this:

              repeat
                inputcolor = string.lower(read())
              until inputcolor == "red" or inputcolor == "blue" or inputcolor == "green" or inputcolor == "yellow"
              if inputcolor == "red" then
                table.insert(discard, "r"..playcard[2])
              elseif inputcolor == "blue" then
                table.insert(discard, "b"..playcard[2])
              elseif inputcolor == "green" then
                table.insert(discard, "g"..playcard[2])
              elseif inputcolor == "yellow" then
                table.insert(discard, "y"..playcard[2])
              end

… into something much simpler:

              repeat
                inputcolor = string.lower(read())
              until shorts[inputcolor]
              table.insert(discard, shorts[inputcolor]..playcard[2])

In terms of the original problem, you'll want to be a bit more specific. Where, to the line, do the values change in an unexpected manner? What sort of values are expected, and what exactly do you get instead? If you suspect you're getting a nil value where there shouldn't be one, use the type() function to check that.
Edited on 21 October 2016 - 08:36 AM