Last active
December 31, 2019 08:37
-
-
Save HKGx/7429fdfb8eecdb155356148e8078163d to your computer and use it in GitHub Desktop.
Rock paper scissors made for CGI 2019.
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
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!") |
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
how to play
if you want to quit type down "exit"
proof of playability: