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

Implementing High Score?

Started by moneymaster2012, 03 July 2016 - 06:53 PM
moneymaster2012 #1
Posted 03 July 2016 - 08:53 PM
Hey!

I made a custom game from scratch, completely mine. I am pretty proud of it, but I want to add more. I want to make it more fun, so how would I implement high-score code so each time the player guesses with the least amount of guesses overrides the current high-score?

Here is my code:

tries = 1
term.clear()
term.setCursorPos(5,10)
print("Guess The NUMBER!")
sleep(0.25)
term.clear()
sleep(0.25)
term.setCursorPos(5,10)
print("Guess The NUMBER!")
sleep(0.5)
term.clear()
sleep(0.25)
term.setCursorPos(5,10)
print("Guess The NUMBER!")
sleep(2)
term.clear()
numb = math.random(1, 100)
term.setCursorPos(1,1)
print("A number has been chosen by the computer. Guess the number between 1 to 100. You will be given clues.")
term.setCursorPos(1,5)
while true do
  write"Guess: "
  guess = io.read()
  guess = tonumber(guess)
  if guess == numb then
	term.clear()
	term.setCursorPos(1,1)
	print("You won with "..tries.." tries! The number was "..numb)
	sleep(7)
	os.shutdown()
  else
	if guess > numb then
	  print("Your guess was too high!")
	elseif guess < numb then
	  print("Your guess was too low!")
	else
	  print("Guess numbers, not letters!")
	end
	tries = tries + 1
  end
end

Thanks!
LBPHacker #2
Posted 03 July 2016 - 09:11 PM
If you want to keep the high scores only as long as the program runs, you could wrap the entire program in a loop and save the high score in a variable outside the loop. If you want high scores to persist, though, you're in for a tougher ride. You'll want to write the high score to a file and read it back on program startup. The FS API will help with that.
moneymaster2012 #3
Posted 03 July 2016 - 09:16 PM
If you want to keep the high scores only as long as the program runs, you could wrap the entire program in a loop and save the high score in a variable outside the loop. If you want high scores to persist, though, you're in for a tougher ride. You'll want to write the high score to a file and read it back on program startup. The FS API will help with that.

Ugh haha. I've done this before with floppy disk writing and locks. I guess I have to get back to it