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

Mini-Game Problem

Started by applesauce10189, 26 February 2014 - 06:03 PM
applesauce10189 #1
Posted 26 February 2014 - 07:03 PM
I got bored and felt like making a random mini-game, I'm not 100% sure what I've done wrong or how to fix it.
attempt to preform arithmetic __add on nil and number

Code
Spoiler

print("Thanks for playing guess that number!")
local best = fs.open("best", "r")
print("Would you like to use a custom range?")
print("(If you say no it will be 1-100)")
local answer = read()
while true do
  if answer == "yes" then
	print("Sorry this feature is a work in progress,")
	print("Please enter minimum then maximum")
	local min = read()
	local max = read()
	break
  elseif answer == "no" then
	print("Okay.")
	local min = 1
	local max = 100
	break
  else
	print("Pease say 'yes' or 'no'")
  end
end
local correct = math.random(1, 100)
while true do
  local guess = read()
  guess = tonumber(guess)
  tries = tries+1
  if guess > correct then
	print("Too high!")
  elseif guess < correct then
	print("Too low!")
  elseif guess == correct then
	print("juuussst riiight")
	if tries < best then
	  best = tries
	  local best = fs.open("best", "w")
	  best.write(best)
	end
  else
	print("Sorry, there was a problem.")
	break
  end
end
Edited on 26 February 2014 - 06:04 PM
CometWolf #2
Posted 26 February 2014 - 07:21 PM
Give the line number aswell next time… Tries is never defined before you add 1 to it.
applesauce10189 #3
Posted 26 February 2014 - 11:42 PM
line 34 attempt to compare nil with number after fixing what you said


EDIT: line 33 in this code because I haven't updated this code and I took an extra line to define tries
Edited on 26 February 2014 - 10:43 PM
CometWolf #4
Posted 27 February 2014 - 12:13 AM
comon man, you're allowed to think for yourself here… Same issue, best is not defined until after you check if it's greater than tries. Or well, it is defined, but it's a file(table), not the contents of the file. Since you're getting the nil error, im guessing the file is non-existant, otherwise it would error with table instead of nil. A file has to exist before you can read from it.
Edited on 26 February 2014 - 11:15 PM
applesauce10189 #5
Posted 01 March 2014 - 01:28 AM
I think the website is a little broken. I read the comment, scrolled down to make a comment, I saw the top of the website in the comment area and a little scroll bar, I had to scroll down the website within the same website to use the comment box that I'm already using just to look at it. Then when I hit "Post" it says the post is empty. I get the slight feeling I hit the wrong "Post" button. There's basically 2 of everything now. I'll give you a second to try and understand what I just said.
Edited on 01 March 2014 - 08:46 PM
applesauce10189 #6
Posted 01 March 2014 - 09:55 PM
It's been a while since this post has been updated,
for some reason the if statement on line 51 gets ignored but still uses the code within the statement, not sure if I worded that correctly so what I mean is,

if 5&amp;lt;2 then
  term.write("boom shakalaka")
end
That's just an example, I know for a 100% pure fact my code shouldn't be running through the if statement because it doesn't have the right conditions, yet it acts as if it does so it runs the code anyway. Here's the actual code, my problem is on line 51


os.unloadAPI("apple")
os.loadAPI("apple")
apple.reset()
print("Thanks for playing guess that number!")
local file = fs.open("best", "r")
best = file.readLine()
file.close()
print("Would you like to use a custom range?")
print("(If you say no it will be 1-100)")
print("The current best is: ")
print(best)
local answer = read()
local tries = 0
if not best then
  local best = 100
  local file = fs.open("best", "w")
  file.write(best)
  print(best)
  file.close()
end
while true do
  if answer == "yes" then
	print("Sorry this feature is a work in progress,")
	print("Please enter minimum then maximum")
	local min = read()
	local max = read()
	break
  elseif answer == "no" then
	print("Okay.")
	local min = 1
	local max = 100
	break
  else
	print("Pease say 'yes' or 'no'")
  end
end
local correct = math.random(1, 100)
while true do
  local guess = read()
  guess = tonumber(guess)
  tries = tries+1
  local best = 100
  if guess &amp;gt; correct then
	print("Too high!")
  elseif guess &amp;lt; correct then
	print("Too low!")
  elseif guess == correct then
	print("juuussst riiight")
	if tries &amp;lt; best then
	  best = tries
	  print(best)
	  local file = fs.open("best", "w")
	  file.write(best)
	  file.close()
	end
	break
  else
	print("Sorry, there was a problem.")
	break
  end
end

EDIT: I looked over my code and found my problem. When I was without internet I was testing and stuff to see what was wrong and I forgot to delete
 local best = 100 
so I'm gonna do some testing to see if it's fixed now.

EDIT#2: Attempt to compare string with number expected, got string when I do

if tries < best then="" --code="" end="" [="" code]<="" code="">
	
</best>
Note: I hit edit and the quotation marks and <>'s popped up randomly, I thought hitting save changes would fix it. Didn't.
Edited on 01 March 2014 - 11:30 PM
Engineer #7
Posted 01 March 2014 - 11:20 PM
First of all, you are assuming the file is already there. Create it if its not there :P/> And localize variables.

Anyway, while I was testing it, I got a Too Long Without Yielding. I got that because in the first while loop I got into the else block of the if statement. I would suggest putting a answer = read() there.

Also when your second read returns nul because the user just pressed enter, it will try to compare number with nil. I would suggest to replace that with:

local guess
while not guess do
   guess = read()
end

So, that is anoter error fixed. Lets carry on:
It seems that I cant reproduce your error, sorry.
Code with fixes &amp; extra formatting: http://pastebin.com/spFNBpei
applesauce10189 #8
Posted 02 March 2014 - 12:39 AM
Okay I changed my code a little, sorry about that little else block problem. I moved the answer = read() as you recommended. Line 50 attempt to compare string with number expected, got string.
By the way, is there a way to check if there is an API active or if a program is on the computer? I want there to be a way to have the program auto-install my API that I'm working on using shell.run("pastebin get (code) apple")

EDIT: One more problem I'm having, sorry for the guy who has to run through my code like "Okay, he wants this this this and this, how's he gonna do it?" as you can see I have a couple variables, min and max. Originally they were meant to be used in the math.random but that didn't work too well. How can I make that work?

EDIT#2: Earlier I mention my error on line 50. To reproduce this error, just run the code normally like a regular program, type "no" then type a number between 1 and 100 and guess higher or lower depending on what it says, once you correctly guess the number you get that error.

Spoiler

os.unloadAPI("apple")
os.loadAPI("apple")
apple.reset()
print("Thanks for playing guess that number!")
local file = fs.open("best", "r")
best = file.readLine()
file.close()
print("Would you like to use a custom range?")
print("(If you say no it will be 1-100)")
print("The current best is: ")
print(best)
print("?  ")
term.setCursorPos(3,6)
local tries = 0
if not best then
  local best = 100
  local file = fs.open("best", "w")
  file.write(best)
  print(best)
  file.close()
end
while true do
  local answer = read()
  if answer == "yes" then
	print("Sorry this feature is a work in progress,")
	print("Please enter minimum then maximum")
	local min = read()
	local max = read()
	break
  elseif answer == "no" then
	print("Okay.")
	local min = 1
	local max = 100
	break
  else
	print("Pease say 'yes' or 'no'")
  end
end
local correct = math.random(1, 100)
while true do
  local guess = read()
  guess = tonumber(guess)
  tries = tries+1
  if guess > correct then
	print("Too high!")
  elseif guess < correct then
	print("Too low!")
  elseif guess == correct then
	print("juuussst riiight")
	if tries<best then
	  best = tries
	  print(best)
	  local file = fs.open("best", "w")
	  file.write(best)
	  file.close()
	end
	break
  else
	print("Sorry, there was a problem.")
	break
  end
end
Edited on 02 March 2014 - 12:00 AM
Thib0704 #9
Posted 02 March 2014 - 01:02 AM
Okay I changed my code a little, sorry about that little else block problem. I moved the answer = read() as you recommended. Line 50 attempt to compare string with number expected, got string.
By the way, is there a way to check if there is an API active or if a program is on the computer? I want there to be a way to have the program auto-install my API that I'm working on using shell.run("pastebin get (code) apple")

EDIT: One more problem I'm having, sorry for the guy who has to run through my code like "Okay, he wants this this this and this, how's he gonna do it?" as you can see I have a couple variables, min and max. Originally they were meant to be used in the math.random but that didn't work too well. How can I make that work?

Spoiler

os.unloadAPI("apple")
os.loadAPI("apple")
apple.reset()
print("Thanks for playing guess that number!")
local file = fs.open("best", "r")
best = file.readLine()
file.close()
print("Would you like to use a custom range?")
print("(If you say no it will be 1-100)")
print("The current best is: ")
print(best)
print("?  ")
term.setCursorPos(3,6)
local tries = 0
if not best then
  local best = 100
  local file = fs.open("best", "w")
  file.write(best)
  print(best)
  file.close()
end
while true do
  local answer = read()
  if answer == "yes" then
	print("Sorry this feature is a work in progress,")
	print("Please enter minimum then maximum")
	local min = read()
	local max = read()
	break
  elseif answer == "no" then
	print("Okay.")
	local min = 1
	local max = 100
	break
  else
	print("Pease say 'yes' or 'no'")
  end
end
local correct = math.random(1, 100)
while true do
  local guess = read()
  guess = tonumber(guess)
  tries = tries+1
  if guess > correct then
	print("Too high!")
  elseif guess < correct then
	print("Too low!")
  elseif guess == correct then
	print("juuussst riiight")
	if tries<best then
	  best = tries
	  print(best)
	  local file = fs.open("best", "w")
	  file.write(best)
	  file.close()
	end
	break
  else
	print("Sorry, there was a problem.")
	break
  end
end

To check if an API is loaded:

if apple then
   os.unloadAPI("apple")
end
  os.loadAPI("/apis/apple")

Try doing:

if tonumber(best)>tonumber(tries) then
    #code here
end
applesauce10189 #10
Posted 02 March 2014 - 01:07 AM
Error on the same line, but instead of the error it was last time, now it's attempt to compare nil with number. I don't want to check if the API is loaded, the load/unload is because I'm using the same computer for both the API and this program so if the API gets updated I want all my programs to get it. I want to check whether or not the API/program exists on the computer so it knows whether or not to run the shell.run part I want to implement.

EDIT: Best wasn't defined. I'll add something to set it to 100 if it isn't defined.

EDIT#2: For some reason it's defined but as {}.0?

EDIT#3: Because it was for some reason defined as {}.0 I had to go into lua and manually set it to 100. If I'm not mistaken, using

if not tonumber(best) then
  local best = 100
end
should work, right?
Edited on 02 March 2014 - 12:17 AM
Thib0704 #11
Posted 02 March 2014 - 01:13 AM
Error on the same line, but instead of the error it was last time, now it's attempt to compare nil with number. I don't want to check if the API is loaded, the load/unload is because I'm using the same computer for both the API and this program so if the API gets updated I want all my programs to get it. I want to check whether or not the API/program exists on the computer so it knows whether or not to run the shell.run part I want to implement.

EDIT: Best wasn't defined. I'll add something to set it to 100 if it isn't defined.

EDIT#2: For some reason it's defined but as {}.0?
Use tonumber(best[1]) then.
Well use fs.exists(pathToAPI) …
Thib0704 #12
Posted 02 March 2014 - 01:20 AM
EDIT#3: Because it was for some reason defined as {}.0 I had to go into lua and manually set it to 100. If I'm not mistaken, using

if not tonumber(best) then
  local best = 100
end
should work, right?

Should.
applesauce10189 #13
Posted 02 March 2014 - 01:30 AM
EDIT#3: Because it was for some reason defined as {}.0 I had to go into lua and manually set it to 100. If I'm not mistaken, using

if not tonumber(best) then
  local best = 100
end
should work, right?

Should.
I've never used fs.exists before, did I use it right?

if not fs.exists("rom/apple") then
  shell.run("pastebin get (code) apple")
end

EDIT: I tested the code and it works just fine.
Edited on 02 March 2014 - 12:45 AM
CometWolf #14
Posted 02 March 2014 - 12:01 PM
EDIT#3: Because it was for some reason defined as {}.0 I had to go into lua and manually set it to 100. If I'm not mistaken, using

if not tonumber(best) then
  local best = 100
end
should work, right?

Should.
This will not work, because you're defining best locally to the if block.

I've never used fs.exists before, did I use it right?

if not fs.exists("rom/apple") then
  shell.run("pastebin get (code) apple")
end

EDIT: I tested the code and it works just fine.
That is indeed the correct usage, however i doubt you have your api stored in the rom folder.
Edited on 02 March 2014 - 11:02 AM