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

Problem comparing 2 variables

Started by EpicnessTwo, 23 May 2014 - 09:51 AM
EpicnessTwo #1
Posted 23 May 2014 - 11:52 AM
Hi, all

I creating a game called NumberRoulette but there is something wrong with it. When i get the answer correct, it shoots me but if i get the answer wrong it shoots me. The code it is going to is correct but i have narrowed it down to where it compares the users input with the chosen random number:


userInput = read()
if not(userInput==gameNumber) == true then
gameIncorrect()
else
gameCorrect()
end

Here is the full program so far. (I know about the error when losing, its there to allow me to leave the game instantly)
Spoiler


--Written by EpicnessTwo
local rounds = 0 --Dont Change!
--Config Items
local lives = 10 --Number of lives at the start
local deathPoints = 1 --Lives lost upon losing
local healPoints = 1 --Lives gained upon winning
local lowestValue = 1 --Lowest random number
local highestValue = 5 --Highest random number

function resetStats()
local lives = 10
local deathPoints = 1
local healPoints = 1
local lowestValue = 1
local highestValue = 5
end
resetStats()
--Main Code
function writeStats()
term.setTextColor(colors.green)
term.setCursorPos(40, 1)
term.write("Your Stats")
term.setCursorPos(40, 2)
term.write("Lives: "..lives)
term.setCursorPos(40, 3)
term.write("Debug: "..gameNumber)
end
function chooseNumber()
gameNumber = math.random(lowestValue, highestValue)
end
function editStats(changeLives)
lives = lives + changeLives
end
function gameCorrect()
term.clear()
term.setCursorPos(10, 10)
term.write("Correct!")
editStats(1)
os.sleep(.3)
startGame()
end
function gameIncorrect()
term.clear()
term.setCursorPos(10, 10)
term.write("BANG!")
editStats(-1)
os.sleep(.3)
startGame()
end
function gameOver(Score)
term.clear()
term.setCursorPos(10, 10)
term.write("Your Dead!")
term.setCursorPos(10, 11)
term.write("Your Final Score: "..Score)
os.pullEvents()
os.reboot()
end
--Main Game Code
function startGame()
if lives==0 then
gameOver(rounds)
end
rounds = rounds + 1
while true do
term.clear()
chooseNumber()
writeStats()
term.setCursorPos(2, 3)
term.setTextColor(colors.blue)
term.write("Choose a number between "..lowestValue.." and "..highestValue..": ")
userInput = read()
if not(userInput==gameNumber) == true then
gameIncorrect()
else
gameCorrect()
end

end
end
---Menu
term.clear()
term.setCursorPos(1, 2)
--print("  _ ________,	   ,________ _")
--print("  >'/==/----'	   '----\==\'<")
--print(" /__/--'				 '--\__\")
--print(" ")
print("			  _____________________")
print("			   |				 |")
print("			   | Number Roulette |")
print("			   |		By	   |")
print("			   |   EpicnessTwo   |")
print("			   |_________________|")

local menu_options = { --This is our menu table. It contains basic data about the menu
  [1] = {text="Start Game", color=colors.blue},
  [2] = {text="Options", color=colors.orange},
  [3] = {text="How To Play", color=colors.cyan}
}
local termX, termY = term.getSize() --The x/y size of the terminal
local function menuDraw(selected) --Our main draw function
  local yPos = termY/2 - #menu_options/2 + 1 --The initial y position
  for index, data in pairs(menu_options) do
	menu_options[index].bounds = { --Create a new table in each option with the boundary data
	  x1 = termX/2 - (#data.text+4)/2;
	  x2 = termX/2 + (#data.text+4)/2;
	  y = yPos;
	}
	term.setTextColor(data.color)
	term.setCursorPos(data.bounds.x1, data.bounds.y)

	local text =
	  index==selected and "> "..data.text.." <" or
	  "  "..data.text.."  " --Essentially an if statement, but in a contracted form
	term.write(text)
	yPos = yPos+1 --Increment the initial y pos so we can move on the next line
  end
end

local function checkClick(x,y) --Check the mouse click to see if there's a menu option
  for index, data in pairs(menu_options) do
	if x>= data.bounds.x1 and x<= data.bounds.x2 and y==data.bounds.y then
	  return index --Returns the index of the clicked option
	end
  end
  return false --If it went through the entire for loop without success, return false
end

--term.setBackgroundColor(colors.white)
--term.clear()

local selector = 1 --Our selector
while true do --The main loop. I would generally put this inside of a function for a program.
  menuDraw(selector) --Draw the menu first
  local e = {os.pullEvent()} --Pull an event and put the returned values into a table
  if e[1] == "key" then --If it's a key...
	if e[2] == keys.down then -- ... and it's the down arrow
	  selector = selector < #menu_options and selector+1 or 1 --Increment the selector if the selector < #menu_options. Otherwise reset it to 1
	elseif e[2] == keys.up then
	  selector = selector > 1 and selector-1 or #menu_options --Decrement the selector if the selector > 1. Otherwise, reset it to #menu_options
	elseif e[2] == keys.enter then
	  break --Break out of the loop
	end
  elseif e[1] == "mouse_click" then
	local value = checkClick(e[3], e[4]) --Check the mouse click
	if value then --If checkClick returns a value and not false
	  selector = value --Set the selector to that value and break out of the loop
	  break
	end
  end
end

if selector==1 then
startGame()
else if selector==2 then
gameOptions()
else if selector==3 then
gameCredits()
end
end
end
Edited on 23 May 2014 - 09:55 AM
MKlegoman357 #2
Posted 23 May 2014 - 12:02 PM
The problem is that gameNumber is a number and userInput is a string. You should either make gameNumber a string or userInput a number before you compare them.


--#tonumber(string) -> converts a string to a number (if possible)
--#example:

local x = tonumber("2") --// Convert string "2" to a number

print(type(x)) -->> number

--#tostring(anything) -> converts anything to a string
--#example:

local x = tostring(5) --// Convert a number '5' to a string

print(type(x)) -->> string
Edited on 23 May 2014 - 10:03 AM