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

Creating a simple Higher or Lower game

Started by bloodless2010, 18 October 2012 - 07:58 PM
bloodless2010 #1
Posted 18 October 2012 - 09:58 PM
Hey there, I was making a Lua Higher or Lower game, but getting into the code it's confusing me, sometimes it doesn't work, and sometimes it does, and sometimes it just doesn't work at all.





term.clear()
term.setCursorPos(1,1)
print("My print stuff goes here, no worries about this")
print("more print stuff")
num = math.random(1,10)
print("your number is: ", num ," will the next number be higher or lower?")
mistakes = 0
correct = 0
whie true do
guess = read()
nextNum = math.random(1,10)
if guess == "h" and num < nextNum then
num = math.random(1,10)
print("correct! the new number is: ", num ," will the next be higher or lower?")
correct = correct + 1
else if guess == "l" and num > nextNum then
num = math.random(1,10)
print("wrong! the new number is: ", num ," will the next be higher or lower?")
mistakes = mistakes + 1
else
print("Please use h for higher and l for lower!")
end
if mistakes == 3 then
print("You've done 3 mistakes, GAME OVER")
print("Score: ", correct , "")
break
end
end
end

Thank you! :P/>/>
Noodle #2
Posted 18 October 2012 - 10:09 PM
term.clear()
term.setCursorPos(1,1)
print("My print stuff goes here, no worries about this")
print("more print stuff")
num = math.random(1,10)
print("your number is: ", num ," will the next number be higher or lower?")
mistakes = 0
correct = 0
while true do -- WHILE
guess = read()
nextNum = math.random(1,10)
if mistakes == 3 then
print("You've done 3 mistakes, GAME OVER")
print("Score: ", correct , "")
break
end
if guess == "h" then
if num < nextNum then
num = math.random(1,10)
print("correct! the new number is: ", num ," will the next be higher or lower?")
correct = correct + 1
else
num = math.random(1,10)
print("wrong! the new number is: ", num ," will the next be higher or lower?")
mistakes = mistakes + 1
end
elseif guess == "l" then -- Elseif, 1 word
if num > nextNum then -- Number is higher
num = math.random(1,10)
print("wrong! the new number is: ", num ," will the next be higher or lower?")
mistakes = mistakes + 1
else
num = math.random(1,10)
print("Correct! The new number is: ", num, " will the next be higher or lower?")
correct = correct + 1
end
else
print("Please use h for higher and l for lower!")
end
end
Fixed + improved your code
1. Elseif is only one word
2. You only had one result for higher and lower so you only had a 1/6 (1/4 for the if, 1/2 for higher/lower) chance to win.
bloodless2010 #3
Posted 18 October 2012 - 10:14 PM
Thank you!!
I know there are some typos in the
 
but I was typing it quickly from the print screened image, if you see the print screen image it has the ones without typo's xD Just sayin', and thank you ever so much. I appreciate it!
billysback #4
Posted 18 October 2012 - 10:23 PM
Making it readable, you really should learn how to use tabs and spaces:
Spoiler

term.clear()
term.setCursorPos(1,1)

print("My print stuff goes here, no worries about this")
print("more print stuff")

num = math.random(1,10)
print("your number is: ", num ," will the next number be higher or lower?")

mistakes = 0
correct = 0

while true do -- WHILE
	guess = read()
	nextNum = math.random(1,10)
	if mistakes == 3 then
		print("You've done 3 mistakes, GAME OVER")
		print("Score: ", correct , "")
		break
	end
	if guess == "h" then
		if num < nextNum then
			num = math.random(1,10)
			print("correct! the new number is: ", num ," will the next be higher or lower?")
			correct = correct + 1
   	 else
			num = math.random(1,10)
	 	   print("wrong! the new number is: ", num ," will the next be higher or lower?")
			mistakes = mistakes + 1
   	 end
	elseif guess == "l" then -- Elseif, 1 word
		if num > nextNum then -- Number is higher
		num = math.random(1,10)
		print("wrong! the new number is: ", num ," will the next be higher or lower?")
		mistakes = mistakes + 1
	else
		num = math.random(1,10)
		print("Correct! The new number is: ", num, " will the next be higher or lower?")
		correct = correct + 1
	end
else
print("Please use h for higher and l for lower!")
end
end
( the forum messed this up a bit, but hopefully you get the idea :/)
bloodless2010 #5
Posted 18 October 2012 - 10:33 PM
Hey again, I just tried out with noodle's code and it isn't 100% how I really want it to work,

I want it to be similar to this;

But even with the addition to the code, It's still buggy.

I hope you can help, thanks!
remiX #6
Posted 19 October 2012 - 02:21 PM
Here we go:


function clear()
	term.clear()
	term.setCursorPos(1,1)
end
local mistakes = 0
local correct = 0

while true do
	clear()
	if mistakes == 3 then
		print("You've done 3 mistakes, GAME OVER")
		print("Score: "..correct)
		break
	end
	repeat
		Num1 = math.random(1,10)
		Num2 = math.random(1,10)
	until Num1 ~= Num2
	print("Current Score: "..correct..".n----------------nYour number is: "..Num1..".nDo you think the next number will be higher (h) or lower (l)?") write("  > ")
	guess = string.lower(read())
	if guess == "h" and Num1 < Num2 then
		print("Correct! Your number was: "..Num1.." and the other number was: "..Num2.."!")
		correct = correct + 1
	elseif guess == "l" and Num1 > Num2 then
		print("Correct! Your number was: "..Num1.." and the other number was: "..Num2.."!")
		correct = correct + 1
	elseif guess == "h" and Num1 > Num2 then
		print("Wrong! Your number was: "..Num1.." and the other number was: "..Num2.."!")
		mistakes = mistakes + 1
	elseif guess == "l" and Num1 < Num2 then
		print("Wrong! Your number was: "..Num1.." and the other number was: "..Num2.."!")
		mistakes = mistakes + 1
	else
		print("Please use h for higher and l for lower!")
	end
	sleep(2)
end

You can change it how ever you want :P/>/> I made it show your current score everytime.

EDIT: Added a debug line because if the numbers are equal and no matter what you type it will print Please use h for higher and l for lower…

EDIT#2: Changed the code to make it shorter and shows how many chances you have left:

Spoiler

function clear()
	term.clear()
	term.setCursorPos(1,1)
end
local mistakes = 3
local correct = 0

while true do
	clear()
	if mistakes == 0 then
		print("You've done 3 mistakes, GAME OVER")
		print("Score: "..correct)
		break
	end
	repeat
		Num1 = math.random(1,10)
		Num2 = math.random(1,10)
	until Num1 ~= Num2
	print("Current Score: "..correct.."nChances left: "..mistakes.."n-----------------nYour number is: "..Num1.."nDo you think the next number will be higher (h) or lower (l)?") write("  > ")
	guess = string.lower(read())
	if guess == "h" and Num1 < Num2 or guess == "l" and Num1 > Num2 then
		print("Correct! Your number was: "..Num1.." and the other number was: "..Num2.."!")
		correct = correct + 1
	elseif guess == "h" and Num1 > Num2 or guess == "l" and Num1 < Num2 then
		print("Wrong! Your number was: "..Num1.." and the other number was: "..Num2.."!")
		mistakes = mistakes - 1
	else
		print("Please use h for higher and l for lower!")
	end
	sleep(2)
end