Jump to content

Table not being properly copied

game help

4 replies to this topic

#1 Bedrockbreaker

  • Members
  • 8 posts
  • LocationU.S.A.

Posted 21 October 2016 - 12: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.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 21 October 2016 - 01: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.

#3 Bedrockbreaker

  • Members
  • 8 posts
  • LocationU.S.A.

Posted 21 October 2016 - 03: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.

#4 Admicos

  • Members
  • 207 posts
  • LocationTurkey

Posted 21 October 2016 - 03:56 AM

View PostBedrockbreaker, on 21 October 2016 - 03:08 AM, said:

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.

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 21 October 2016 - 09: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)

This allows you to turn this sort of thing:

print("|        |")
print("|        |")
print("|        |")
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:

  play1 = {""}
  play2 = {""}
  play3 = {""}
  play4 = {""}
  robot1 = {""}
  robot2 = {""}
  robot3 = {""}

... like so:

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

... then we could shrink this sort of thing:

  if players >= 2 then
  if players >= 3 then
  if players == 4 then
  if robots >= 1 then
  if robots >= 2 then
  if robots == 3 then

... 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:

                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])

... into something much simpler:

                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 by Bomb Bloke, 21 October 2016 - 09:36 AM.

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users