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

[Error] until expected to close repeat?

Started by Zoroark173, 10 October 2012 - 10:16 PM
Zoroark173 #1
Posted 11 October 2012 - 12:16 AM
Hello everybody, I am trying to make a program, but I keep getting an error. It says that I need a repeat to close the until, but I already have an until at the end of my program, so this really makes no sense…

I am still new to Computercraft programming, so if this code is extremely long for what it should be doing, or it has terrible formatting, I am sorry.

--testproj
--Made by Zoroark173

local var = 0
local var2 = 10
local var3 = 7
local var4 = 1
local i = 1

function clean()
term.clear()
term.setCursorPos(1,1)
end

function creature()
var = math.random(1,6)
if var==1 then
  monster = "Goblin"
elseif var==2 then
  monster = "Giant Spider"
elseif var==3 then
  monster = "Dragon Hatchling"  
elseif var==4 then
  monster = "Evil Guy"
elseif var==5 then
  monster = "Zombie"
elseif var==6 then
  monster = "Skeleton"
end
end

--Declaring variables and functions
--------------------------------------
--Bulk of the program

clean()
print("Hello, welcome to SwordQuest!")
print("Type 1 to start the game!")
print("Or 2 to see the Help section!")
startmenu = read()
  if startmenu == "2" then
   print("Type 1 at the main screen to start the game")
   print("Commands avalible in-game are Move, Attack, and Potion")
   print("Move brings you to a new monster.")
   print("Attack attacks the monster.")
   print("Potion uses a healing potion.")
   print("Contact Zoroark173 for more info!")
   sleep(7)
   print("Please restart me")
  end

--Help section
--------------------------------
--In battle

if startmenu == "1" then
repeat
   clean()
   creature()
   print("A "..monster.." is attacking!")
   print("Move, Attack, or Potion?")
	battle = read()
   if battle == "Potion" then
	 var2 = 10
	 print("Heal Complete!")
	 print("HP: "..var2)
	 print(monster.." attacks!")
	 var2 = var2-1
	 print("HP: "..var2)
   end
   elseif battle == "Attack" then
	 var3 = var3-1
	 print("Did 1 damage to "..monster)
	 var2 = var2-1
	 print(monster.." did 1 damage to you.")
   end
   elseif battle == "Move" then
	 clean()
	 creature()
	 i = i+1
  end
  until var4 == 100
end
MysticT #2
Posted 11 October 2012 - 12:20 AM
You have ends before the elseif's, remove them and it should work.
Zoroark173 #3
Posted 11 October 2012 - 12:22 AM
But now it says that I need an end to close if at line 63
Lyqyd #4
Posted 11 October 2012 - 01:02 AM
The last block of code should look vaguely thus:


if startmenu == "1" then
    repeat
        clean()
        creature()
        print("A "..monster.." is attacking!")
        print("Move, Attack, or Potion?")
        battle = read()
        if battle == "Potion" then
            var2 = 10
            print("Heal Complete!")
            print("HP: "..var2)
            print(monster.." attacks!")
            var2 = var2-1
            print("HP: "..var2)
        elseif battle == "Attack" then
            var3 = var3-1
            print("Did 1 damage to "..monster)
            var2 = var2-1
            print(monster.." did 1 damage to you.")
        elseif battle == "Move" then
            clean()
            creature()
            i = i+1
        end
    until var4 == 100
end
Zoroark173 #5
Posted 11 October 2012 - 03:15 AM
Thanks, but isn't that basically what I had before?
Lyqyd #6
Posted 11 October 2012 - 03:33 AM
Thanks, but isn't that basically what I had before?

No. I removed the incorrectly placed ends. If the rest of the code is correct, it should run fine.
Zoroark173 #7
Posted 18 October 2012 - 12:59 AM
Thanks, but isn't that basically what I had before?

No. I removed the incorrectly placed ends. If the rest of the code is correct, it should run fine.
Hi! Whenever i try to run this piece, no matter what I do, it does the Move option, even when I type in Attack or Potion…
Note- I have tried this too, but it wouldn't work…

--testproj
--Made by Zoroark173
local creaturevar = 0
local playerhp = 10
local monhp = 7
local var4 = 1
local i = 1
function clean()
term.clear()
term.setCursorPos(1,1)
end
function creature()
creaturevar = math.random(1,6)
if creaturevar==1 then
  monster = "Goblin"
elseif creaturevar==2 then
  monster = "Giant Spider"
elseif creaturevar==3 then
  monster = "Dragon Hatchling" 
elseif creaturevar==4 then
  monster = "Evil Guy"
elseif creaturevar==5 then
  monster = "Zombie"
elseif creaturevar==6 then
  monster = "Skeleton"
end
end
function attack()
monhp = monhp-1
playerhp = playerhp-1
print(monster.."'s HP is: "..monhp)
sleep(1)
print(monster.." Attacked you! HP: "..playerhp)
end
function potion()
playerhp = 10
print("You used a potion to go back to full health!")
playerhp = playerhp-1
print(monster.."Attacked you! HP: "..playerhp)
end
function move()
clean()
creature()
end
function game()
move()
print("A "..monster.." is attacking you!")
print("What do you do?")
battle = read()
if battle == "Attack" then
	  attack()
elseif battle == "Move" then
	  move()
elseif battle == "Potion" then
	  potion()
end
--Declaring variables and functions
--------------------------------------
--Bulk of the program
clean()
print("Hello, welcome to SwordQuest!")
print("Type 1 to start the game!")
print("Or 2 to see the Help section!")
startmenu = read()
  if startmenu == "2" then
   print("Type 1 at the main screen to start the game")
   print("Commands avalible in-game are Move, Attack, and Potion")
   print("Move brings you to a new monster.")
   print("Attack attacks the monster.")
   print("Potion uses a healing potion.")
   print("Contact Zoroark173 for more info!")
   sleep(7)
   print("Please restart me")
  end
 
--Help section
--------------------------------
--In battle
 
if startmenu == "1" then
   repeat
	    game()
    until var4 == 100
end
Lyqyd #8
Posted 18 October 2012 - 01:02 AM
Please paste the code you're currently using in its entirety. I can't see anything glaringly wrong in the snippet I posted above, so there may be slight differences in the implementation causing the problem.
Zoroark173 #9
Posted 18 October 2012 - 05:57 AM
Please paste the code you're currently using in its entirety. I can't see anything glaringly wrong in the snippet I posted above, so there may be slight differences in the implementation causing the problem.

--testproj
--Made by Zoroark173
local var = 0
local var2 = 10
local var3 = 7
local var4 = 1
local i = 1
function clean()
term.clear()
term.setCursorPos(1,1)
end
function creature()
var = math.random(1,6)
if var==1 then
  monster = "Goblin"
elseif var==2 then
  monster = "Giant Spider"
elseif var==3 then
  monster = "Dragon Hatchling" 
elseif var==4 then
  monster = "Evil Guy"
elseif var==5 then
  monster = "Zombie"
elseif var==6 then
  monster = "Skeleton"
end
end
--Declaring variables and functions
--------------------------------------
--Bulk of the program
clean()
print("Hello, welcome to SwordQuest!")
print("Type 1 to start the game!")
print("Or 2 to see the Help section!")
startmenu = read()
  if startmenu == "2" then
   print("Type 1 at the main screen to start the game")
   print("Commands avalible in-game are Move, Attack, and Potion")
   print("Move brings you to a new monster.")
   print("Attack attacks the monster.")
   print("Potion uses a healing potion.")
   print("Contact Zoroark173 for more info!")
   sleep(7)
   print("Please restart me")
  end
--Help section
--------------------------------
--In battle
if startmenu == "1" then
    repeat
	    clean()
	    creature()
	    print("A "..monster.." is attacking!")
	    print("Move, Attack, or Potion?")
	    battle = read()
	    if battle == "Potion" then
		    var2 = 10
		    print("Heal Complete!")
		    print("HP: "..var2)
		    print(monster.." attacks!")
		    var2 = var2-1
		    print("HP: "..var2)
	    elseif battle == "Attack" then
		    var3 = var3-1
		    print("Did 1 damage to "..monster)
		    var2 = var2-1
		    print(monster.." did 1 damage to you.")
	    elseif battle == "Move" then
		    clean()
		    creature()
		    i = i+1
	    end
    until var4 == 100
end
remiX #10
Posted 18 October 2012 - 06:21 AM
In the battle code

if startmenu == "1" then
 repeat
  clean()
  creature()
  print("A "..monster.." is attacking!")
  print("Move, Attack, or Potion?")
  battle = string.lower(read())
  if battle == "potion" then
   var2 = 10
   print("Heal Complete!")
   print("HP: "..var2)
   print(monster.." attacks!")
   var2 = var2-1
   print("HP: "..var2)
   sleep(3)
  elseif battle == "attack" then
   var3 = var3-1
   print("Did 1 damage to "..monster)
   var2 = var2-1
   print(monster.." did 1 damage to you.")
   sleep(3)
  elseif battle == "move" then
   clean()
   creature()
   i = i+1
  else
   print("Unknown action.")
  end
 until var4 == 100
end

You need sleep(x) timers or else it will instantly reset.

I made the input into lower case so then it will not be case sensitive.
ChunLing #11
Posted 18 October 2012 - 06:29 AM
You should really try using indexed tables rather than long lists of elseif statements.

Note that sIdEkIcK_ added an else statement, so that you will know if you failed to match any of your elseif conditions. This is probably what was happening before. Two questions, what is 'i' supposed to do and how does 'var4' reach 400?
Zoroark173 #12
Posted 18 October 2012 - 06:43 AM
In the battle code

if startmenu == "1" then
repeat
  clean()
  creature()
  print("A "..monster.." is attacking!")
  print("Move, Attack, or Potion?")
  battle = string.lower(read())
  if battle == "potion" then
   var2 = 10
   print("Heal Complete!")
   print("HP: "..var2)
   print(monster.." attacks!")
   var2 = var2-1
   print("HP: "..var2)
   sleep(3)
  elseif battle == "attack" then
   var3 = var3-1
   print("Did 1 damage to "..monster)
   var2 = var2-1
   print(monster.." did 1 damage to you.")
   sleep(3)
  elseif battle == "move" then
   clean()
   creature()
   i = i+1
  else
   print("Unknown action.")
  end
until var4 == 100
end

You need sleep(x) timers or else it will instantly reset.

I made the input into lower case so then it will not be case sensitive.
Thanks, but no I have a problem where if I type in the command, it will work fine, but then it just restart the game, but I think I know how to fix it, thanks!
EDIT- Yup, just had to move the start of the repeat down a few lines! Thanks!

You should really try using indexed tables rather than long lists of elseif statements.

Note that sIdEkIcK_ added an else statement, so that you will know if you failed to match any of your elseif conditions. This is probably what was happening before. Two questions, what is 'i' supposed to do and how does 'var4' reach 400?

I have no clue what i is, I just had it put there when trying to fix the problem, and var4 never reaches 400, so the game will just keep going on.
And how do I use an indexed table?
ChunLing #13
Posted 18 October 2012 - 07:21 AM
indexed_table = {potion = function()
var2 = 10
print("Heal Complete!")
print("HP: "..var2)
print(monster.." attacks!")
var2 = var2-1
print("HP: "..var2)
end,
attack = function()
var3 = var3-1
print("Did 1 damage to "..monster)
var2 = var2-1
print(monster.." did 1 damage to you.")
end,
move = function()
clean()
creature()
i = i+1
end}
Then you just say
if indexed_table[battle] then indexed_table[battle]() else print("Unknown action.") end
Meaning, if there is an entry in indexed_table with an index matching the value of battle, then execute that function.

For a list of three possible actions it isn't a big savings, but for ten or so different possible actions it makes a big difference. It also makes your code much easier to maintain and upgrade, you just add functions to the table, as many as you want. Add a hundred, add a thousand, the program will work just fine.
Zoroark173 #14
Posted 18 October 2012 - 07:28 AM
Thanks, but I would like to keep it how it is. If you would like the full program to play, I just posted it!
http://www.computercraft.info/forums2/index.php?/topic/5159-zoroark-and-co/
remiX #15
Posted 18 October 2012 - 09:16 AM
Naice ;3