Created
February 5, 2022 19:26
-
-
Save tyleransom/156ce90b39b3b6808126564b1dd0d15e to your computer and use it in GitHub Desktop.
Chutes and Ladders in Julia
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
wait_for_key(prompt) = (print(stdout, prompt); read(stdin, 1); nothing) | |
user_input(prompt) = (print(prompt); p = readline(); p) | |
function spin() | |
wait_for_key("Press enter to spin\n") | |
spun = rand(1:6) | |
println("You spun a ",spun) | |
return spun | |
end | |
function chute_or_ladder!(position::Int64) | |
if in(position,[1 4 9 21 28 36 51 71 80]) | |
println("You climbed up a ladder!\n") | |
wait_for_key("Press enter to continue\n") | |
if position == 1 ; position = 38 ; end | |
if position == 4 ; position = 14 ; end | |
if position == 9 ; position = 31 ; end | |
if position == 21; position = 42 ; end | |
if position == 28; position = 84 ; end | |
if position == 36; position = 44 ; end | |
if position == 51; position = 67 ; end | |
if position == 71; position = 91 ; end | |
if position == 80; position = 100; end | |
end | |
if in(position,[16 48 49 56 62 64 87 93 95 98]) | |
println("You slid down a chute!\n") | |
wait_for_key("Press enter to continue\n") | |
if position == 16; position = 6 ; end | |
if position == 48; position = 26; end | |
if position == 49; position = 11; end | |
if position == 56; position = 53; end | |
if position == 62; position = 19; end | |
if position == 64; position = 60; end | |
if position == 87; position = 24; end | |
if position == 93; position = 73; end | |
if position == 95; position = 75; end | |
if position == 98; position = 78; end | |
end | |
return position | |
end | |
function update_pos(pos::Int64) | |
spun = spin() | |
pos += spun | |
pos = chute_or_ladder!(pos) | |
return pos | |
end | |
function game_setup() | |
np = user_input("How many players will there be?\n") | |
np = parse(Int, np) | |
names = String[] | |
for p = 1:np | |
tn = user_input("What is Player "*string(p)*"'s name?\n") | |
names = vcat(names,tn) | |
end | |
return names,np | |
end | |
function game() | |
names,nplayers = game_setup() | |
pos = zeros(Int64,nplayers) | |
while maximum(pos) < 100 | |
for p = 1:nplayers | |
println("") | |
println("") | |
println("") | |
println("**************************") | |
println(names[p],"'s turn") | |
println("You are currently on number ",pos[p]) | |
pos[p] = update_pos(pos[p]) | |
println("You are now on number ",pos[p]) | |
end | |
end | |
@show pos | |
println(names[argmax(pos)]," wins!") | |
end | |
game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment