RosettaCodeData/Task/Nim-game/MiniScript/nim-game.mini

41 lines
774 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
tokens = 12
print "Nim Game"
print "Starting with " + tokens + " tokens."
print
printRemaining = function()
2026-04-30 12:34:36 -04:00
print tokens + " tokens remaining."
print
2023-07-01 11:58:00 -04:00
end function
playerTurn = function(take)
2026-04-30 12:34:36 -04:00
take = floor(val(take))
if take < 1 or take > 3 then
print "Take must be between 1 and 3."
return false
end if
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
globals.tokens = tokens - take
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
print "Player takes " + take + " tokens."
printRemaining
return true
2023-07-01 11:58:00 -04:00
end function
computerTurn = function()
2026-04-30 12:34:36 -04:00
take = tokens % 4
globals.tokens = tokens - take
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
print "Computer takes " + take + " tokens."
printRemaining
2023-07-01 11:58:00 -04:00
end function
while tokens > 0
2026-04-30 12:34:36 -04:00
if playerTurn(input("How many tokens would you like to take? ")) then
computerTurn
end if
2023-07-01 11:58:00 -04:00
end while
print "Computer wins."