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

need help with comparing numbers and tables

Started by 9TYNINE, 23 January 2018 - 01:04 AM
9TYNINE #1
Posted 23 January 2018 - 02:04 AM
ok so I am basically making the 2048 game and where I do my FillCheck function I get
attempt to compare table with number and I don't know what's wrong (Code edited as recommended)

function Game()
  Page() -- clears and sets out page
  Board = {
	  {{0},{0},{0},{0},{0}},
	  {{0},{0},{0},{0},{0}},
	  {{0},{0},{0},{0},{0}},
	  {{0},{0},{0},{0},{0}},
	  {{0},{0},{0},{0},{0}}
	  }
  Filled ={
	  {{},{},{},{},{}},
	  {{},{},{},{},{}},
	  {{},{},{},{},{}},
	  {{},{},{},{},{}},
	  {{},{},{},{},{}}
	  }
  function FillCheck()
	for i = 1,5 do
	  for I = 1,5 do
		if Board[i][I] > 0 then --this line causing error
		  Filled[i][I] = true
		else
		  TempGenFun()
		end
	  end
	end
  end
  function TempGenFun()
	TempX = math.random(1,5)
	TempY = math.random(1,5)
	TempGen = math.random(1,3)
	if TempGen == 3 then
	  TempGen = 4
	end
  end
  TempGenFun()
  FillCheck()
  Board[TempY][TempX] = TempGen
  TempGenFun()
  FillCheck()
  Board[TempY][TempX] = TempGen
  for i = 1,5 do
	for I = 1,5 do
	  term.write(board[i][I])
	end
  end
end
Edited on 23 January 2018 - 01:38 AM
Dog #2
Posted 23 January 2018 - 02:11 AM
The first thing I notice is that you've got a variable for holding a number and a function both named TempGen. You should rename one or the other.
9TYNINE #3
Posted 23 January 2018 - 02:25 AM
Named function to TempGenFun() still happening
9TYNINE #4
Posted 23 January 2018 - 03:22 AM
tried seperating table by

function FillCheck()
  for i = 1,5 do
  BoardTemp = Board[i]
   for I = 1,5 do
	if BoardTemp[I] > 0 then --this line causing error
	 Filled[i][I] = true
	else
	 TempGenFun()
	end
   end
  end
end
didn't work
Edited on 23 January 2018 - 02:25 AM
Dog #5
Posted 23 January 2018 - 03:24 AM
For future reference, please don't edit the OP and change stuff - it makes it hard for others to track what was wrong and what was changed to fix it.

The problem you're running in to has to do with your table construction. You have two choices - change the construction of your tables to match your queries, or change your queries to match your table construction. I recommend the former as it's easier and quicker:

  Board = {
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0}
          }
  Filled = {
           {},
           {},
           {},
           {},
           {}
           }

Another problem you're going to run in to is at the end of your code. You try to write board when you should be writing Board (notice the capitalization)

With those two fixes, the code churns out numbers for me, so that should get you going.
9TYNINE #6
Posted 23 January 2018 - 04:16 AM
still getting it i realy dont understand whats going on

  shell.run("clear")
  Board = {
		  {1},
		  {{0},{0},{0},{0},{0}},
		  {{0},{0},{0},{0},{0}},
		  {{0},{0},{0},{0},{0}},
		  {{0},{0},{0},{0},{0}}
		  }
  Filled ={
		  {},
		  {},
		  {},
		  {},
		  {}
		  }
  function FillCheck()
    for i = 1,5 do
	  BoardTemp = Board[i]
	  for I = 1,5 do
	  BoardTempRes = BoardTemp[I]
	  term.write(Board[1])
	  sleep(1)
	    if BoardTemp > 0 then --this line causing error
		  Filled[i][I] = true
		  term.write("genned")
	    else
	    TempGenFun()
	    term.write("Re genned")
	    end
	  end
    end
  end 
  function TempGenFun()
    TempX = math.random(1,5)
    TempY = math.random(1,5)
    TempGen = math.random(1,3)
    if TempGen == 3 then
	  TempGen = 4
    end
  end
  term.write("Genning num")
  TempGenFun()
  sleep(1)
  FillCheck()
  Board[TempY][TempX] = TempGen
  TempGenFun()
  FillCheck()
  Board[TempY][TempX] = TempGen
  term.write(TempGen)
  term.write(TempX)
  term.write(TempY)
  for i = 1,5 do
  term.write("i")
    for I = 1,5 do
	  term.write("I")
	  term.write(Board[i][I])
    end
  end

where i have it write Board[1] it just gives a table
Dog #7
Posted 23 January 2018 - 04:19 AM
That's because your Board table still isn't constructed the same as the one I provided. In your table, your first value (the number 1) would be Board[1][1]. Take another look at my Board table and yours and you should see the difference.
9TYNINE #8
Posted 23 January 2018 - 08:01 AM
That's because your Board table still isn't constructed the same as the one I provided. In your table, your first value (the number 1) would be Board[1][1]. Take another look at my Board table and yours and you should see the difference.
haha lol i didnt realise that it was different
Edited on 23 January 2018 - 07:01 AM