Skip to content

Instantly share code, notes, and snippets.

@quackhouse
Created October 4, 2013 13:06
Show Gist options
  • Save quackhouse/6825601 to your computer and use it in GitHub Desktop.
Save quackhouse/6825601 to your computer and use it in GitHub Desktop.
Ruby & Sinatra based Rock/Paper/Scissors game with options for multi- or single-player games.
def game(input1, input2)
if input1 == input2
outcome = "It's a tie!"
elsif input1 == "rock"
if input2 == "paper"
outcome = "Paper covers rock. Player 1 loses!"
elsif input2 == "scissors"
outcome = "Rock smashes scissors. Player 1 wins!"
end
elsif input1 == "paper"
if input2 == "scissors"
outcome = "Scissors cuts paper. Player 1 loses!"
elsif input2 == "rock"
outcome = "Paper covers rock. Player 1 wins!"
end
elsif input1 == "scissors"
if input2 == "rock"
outcome = "Rock smashes scissors. Player 1 loses!"
elsif input2 == "paper"
outcome = "Scissors cut paper. Player 1 winss!"
end
end
return outcome
end
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="css/style.css" rel="stylesheet">
<title>Rock Paper Scissors!</title>
</head>
<body>
<section class="container">
<nav>
<ul>
<li><a href="/single_player">Play Single-Player Game</a></li>
<li><a href="/multi_player">Play Multi-Player Game</a></li>
</ul>
</nav>
<div class="content">
<%= yield %>
</div>
</section>
</body>
</html>
require 'sinatra'
require 'sinatra/reloader' if development?
require_relative 'game'
play_options = ["rock", "paper", "scissors"]
get '/single_player' do
erb :single_player
end
get '/result' do
@second_choice = params[:choice2]
if @second_choice == nil
@user_choice = params[:choice]
if @user_choice != nil
@user_choice.downcase!
end
@second_choice = play_options.sample
@player2 = "The computer"
else
@user_choice = $user_choice
@second_choice.downcase!
@player2 = "Player 2"
end
@outcome = game(@user_choice, @second_choice)
if @outcome == nil
@outcome = "You chose a fake thing. You suck."
end
erb :result
end
get '/multi_player' do
erb :multi_player
end
get '/multi_player2' do
$user_choice = params[:choice]
erb :multi_player2
end
<form action="/multi_player2">
Player 1: <input class="input" type="text" placeholder="rock, paper or scissors?" name="choice"><br><br><input class="button" type="submit" value="Go!">
</form>
<form action="/result">
Player 2: <input class="input" type="text" placeholder="rock, paper or scissors?" name="choice2">
<br><br><input class="button" type="submit" value="Go!">
</form>
<p>Player 1 chose <%= @user_choice %>.</p>
<p><%= @player2 %> chose <%= @second_choice %>.</p>
<p><%= @outcome %></p>
<form action="/result">
<input class="input" type="text" placeholder="rock, paper or scissors?" name="choice">
<br>
<p>
<input class="button" type="submit" value="Go!"></p>
</form>
* {
box-sizing: border-box;
}
body {
background: url('http://d1535dk28ea235.cloudfront.net/preset_64/rock-paper-scissors1.png');
background-size: cover;
background-repeat: no-repeat;
font-family: 'helvetica', sans-serif;
font-size: 50px;
color: white;
position: relative;
}
form {
position: relative;
top: 50px;
}
.input {
background-color: rgba(0, 191, 255, 0.8);
color: white;
font-weight: bold;
font-size: 50px;
width: 700px;
height: 100px;
border: 5px solid white;
border-radius: 20px;
}
.button {
background-color: rgba(0, 191, 255, 0.8);
color: white;
border-radius: 20px;
border: 5px solid white;
height: 100px;
width: 100px;
}
nav {
background-color: rgba(0, 0, 0, 0.9);
max-width: 960px;
text-align: center;
margin: 0 auto;
border-radius: 20px;
}
nav li {
display: inline;
margin: 10px;
padding: 10px;
font-size: 30px;
}
a {
color: white;
text-decoration: none;
}
.content {
background-color: rgba(0, 0, 0, 0.9);
width: 960px;
height: 440px;
text-align: center;
margin: 0 auto;
position: relative;
border-radius: 20px;
}
.content p {
padding-top: 20px;
padding-bottom: 20px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment