Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
84
Task/24-game/Nanoquery/24-game.nanoquery
Normal file
84
Task/24-game/Nanoquery/24-game.nanoquery
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import Nanoquery.Lang
|
||||
import Nanoquery.Util
|
||||
|
||||
// a function to get the digits from an arithmetic expression
|
||||
def extract_digits(input)
|
||||
input = str(input)
|
||||
digits = {}
|
||||
|
||||
loc = 0
|
||||
digit = ""
|
||||
while (loc < len(input))
|
||||
if input[loc] in "0123456789"
|
||||
digit += input[loc]
|
||||
else
|
||||
if len(digit) > 0
|
||||
digits.append(int(digit))
|
||||
digit = ""
|
||||
end
|
||||
end
|
||||
loc += 1
|
||||
end
|
||||
// check if there's a final digit
|
||||
if len(digit) > 0
|
||||
digits.append(int(digit))
|
||||
end
|
||||
|
||||
return digits
|
||||
end
|
||||
|
||||
// a function to duplicate a digit list
|
||||
def dup(list)
|
||||
nlist = {}
|
||||
|
||||
for n in list
|
||||
nlist.append(new(Integer, n))
|
||||
end
|
||||
|
||||
return nlist
|
||||
end
|
||||
|
||||
// generate four random digits and output them
|
||||
random = new(Random)
|
||||
randDigits = {}
|
||||
for i in range(1, 4)
|
||||
randDigits.append(random.getInt(8) + 1)
|
||||
end
|
||||
println "Your digits are: " + randDigits + "\n"
|
||||
|
||||
// get expressions from the user until a valid one is found
|
||||
won = false
|
||||
while !won
|
||||
print "> "
|
||||
expr = input()
|
||||
tempDigits = dup(randDigits)
|
||||
|
||||
// check for invalid digits in the expression
|
||||
invalid = false
|
||||
digits = extract_digits(expr)
|
||||
for (i = 0) (!invalid and (i < len(digits))) (i += 1)
|
||||
if not digits[i] in tempDigits
|
||||
invalid = true
|
||||
println "Invalid digit " + digits[i]
|
||||
else
|
||||
tempDigits.remove(tempDigits)
|
||||
end
|
||||
end
|
||||
|
||||
// if there were no invalid digits, check if the expression
|
||||
// evaluates to 24
|
||||
if !invalid
|
||||
a = null
|
||||
try
|
||||
exec("a = " + expr)
|
||||
catch
|
||||
println "Invalid expression " + expr
|
||||
end
|
||||
println a
|
||||
|
||||
if a = 24
|
||||
println "Success!"
|
||||
won = true
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue