Skip to content

Instantly share code, notes, and snippets.

@HKGx
Last active December 31, 2019 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HKGx/7429fdfb8eecdb155356148e8078163d to your computer and use it in GitHub Desktop.
Save HKGx/7429fdfb8eecdb155356148e8078163d to your computer and use it in GitHub Desktop.
Rock paper scissors made for CGI 2019.
yay_or_nay(v::Bool, y::Function, n::Function) = v ? y : n
p1_score = 0
p2_score = 0
function did_end()
return yay_or_nay(p1_score >= 3, () -> true, () -> yay_or_nay(p2_score >= 3, () -> true, () -> false)())()
end
function get_winner()
return yay_or_nay(did_end(), ()-> yay_or_nay(p1_score >= 3, () -> "You", () -> "Computer")(), () -> throw("game didn't end yet"))()
end
function valid_input(input::String)
return yay_or_nay(input=="rock", () -> true, () -> yay_or_nay(input=="paper", () -> true, () -> yay_or_nay(input=="scissors", () -> true, () -> false)())())()
end
function valid_input_or_exit(input::String)
return yay_or_nay(valid_input(input), () -> true, () -> yay_or_nay(input == "exit", () -> true, () -> false)())()
end
function p2_move()
return rand(["rock", "paper", "scissors"])
end
function exit_not_valid_input(input::String)
return yay_or_nay(valid_input_or_exit(input), () -> yay_or_nay(valid_input(input), () -> false, () -> true)(), () -> false)()
end
function is_draw(p1_choice::String, p2_choice::String)
return yay_or_nay(p1_choice == p2_choice, () -> true, () -> false)()
end
function is_p1_beating_p2(p1_choice:: String, p2_choice::String)
return yay_or_nay(p1_choice == "rock" && p2_choice == "scissors", () -> true, () -> yay_or_nay(p1_choice == "scissors" && p2_choice == "paper", () -> true, ()->yay_or_nay(p1_choice == "paper" && p2_choice=="rock", () -> true, () -> false)())())()
end
function p1_round_win!()::Bool
global p1_score
p1_score += 1
return true
end
function p2_round_win!()::Bool
global p2_score
p2_score += 1
return true
end
function interpret_input(input::String)
p2_input = p2_move()
yay_or_nay(exit_not_valid_input(input), () -> exit(), () -> yay_or_nay(is_draw(input, p2_input), () -> println("You drawed with the $input"), () -> yay_or_nay(is_p1_beating_p2(input, p2_input), () -> yay_or_nay(p1_round_win!(), () -> println("You won the round with the $(input)!"), () -> false)(), () -> yay_or_nay(p2_round_win!(), () -> println("Computer won the round with the $(p2_input)!"), () -> false)())())())()
end
function game()
println("Current score: You: $p1_score; Computer: $p2_score \n")
print("Rock, paper or scissors?\n>")
choice:: String = readline()
yay_or_nay(valid_input_or_exit(choice), ()->interpret_input(choice), () -> println("$choice <- That's invalid, buddy."))()
end
while !did_end()
game()
end
println("$(get_winner()) won the entire game. congrats!")
@HKGx
Copy link
Author

HKGx commented Dec 31, 2019

how to play

  1. Open the game with "julia ./overcomplicated_rock_paper_scissors.jl"
  2. start typing "rock", "paper" or "scissors".
  3. first to three will win

if you want to quit type down "exit"

proof of playability:

asciicast

@HKGx
Copy link
Author

HKGx commented Dec 31, 2019

and yes, the code is intended to be done that way. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment