Last active
August 29, 2015 13:57
-
-
Save karai17/9885698 to your computer and use it in GitHub Desktop.
The game starts with 5 bowls. All but the last are filled with 2 beans. The player must select a bowl to empty and distribute forward (looping around back to 1). The next bowl to select must be the last bowl that was distributed to EXCEPT if the last bowl is the actual last bowl (in this example, 5), in which they can choose any bowl again. If t…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function init_new_game(num_bowls) | |
| local bowls = {} | |
| for i=1, num_bowls - 1 do | |
| table.insert(bowls, 2) | |
| end | |
| table.insert(bowls, 0) | |
| return bowls | |
| end | |
| function move_beans(bowls, bowl) | |
| local beans = bowls[bowl] | |
| bowls[bowl] = 0 | |
| while beans > 0 do | |
| bowl = bowl + 1 | |
| if bowl > #bowls then bowl = 1 end | |
| beans = beans - 1 | |
| bowls[bowl] = bowls[bowl] + 1 | |
| end | |
| return bowl | |
| end | |
| function check_win(bowls) | |
| local beans = 0 | |
| for i = 1, #bowls - 1 do | |
| beans = beans + bowls[i] | |
| end | |
| if beans > 0 then | |
| return false | |
| end | |
| return true | |
| end | |
| local win = false | |
| while not win do | |
| local lose = false | |
| local bowls = init_new_game(5) | |
| local path = {} | |
| local next_bowl = #bowls | |
| while not win and not lose do | |
| if next_bowl < #bowls then | |
| if bowls[next_bowl] == 1 then | |
| lose = true | |
| else | |
| table.insert(path, next_bowl) | |
| next_bowl = move_beans(bowls, next_bowl) | |
| end | |
| else | |
| next_bowl = math.random(1, #bowls) | |
| while bowls[next_bowl] == 0 do | |
| next_bowl = math.random(1, #bowls) | |
| end | |
| table.insert(path, next_bowl) | |
| next_bowl = move_beans(bowls, next_bowl) | |
| end | |
| win = check_win(bowls) | |
| if win then | |
| local message = "WIN: " | |
| for k,v in pairs(path) do | |
| message = message .. v | |
| end | |
| print(message) | |
| end | |
| --[[ | |
| if lose then | |
| local message = "LOSE: " | |
| for k,v in pairs(path) do | |
| message = message .. v | |
| end | |
| print(message) | |
| end | |
| --]] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment