rock_paper_scissors 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7c9fc79727f2a7a8508367d369aef6f41a8f72a
4
- data.tar.gz: 7df58eeb5813a39859800ffe7d569fc527240706
3
+ metadata.gz: bdd4f1b4e8b71c60258e0003f63ce43fae68abbc
4
+ data.tar.gz: 941c9cd64aebe2a34d89935ba56cb5758ac9605e
5
5
  SHA512:
6
- metadata.gz: 6ec79c9ef54f12070ba961fb5620836e363d7518b7cf72391d2cdfaf2b117ae90b350c3c860828b9791c0c8a52cca82188876c89932ac47e06af22ad4a6eea29
7
- data.tar.gz: 51ce0a7c93ac4e95e7b2d1d8454bf232402b811a004a12e3f70d9c000b2bf43a258d34110e6f332064e47200d571c28654257ecbece5bce9cef8966fb87339b1
6
+ metadata.gz: 62dfb37f7937669288b29e91845534d21e92debb0ba2e513e9a783666a55cedcceff97f1223195638efb6aeebdbd9900bbac2e0ce343ae666b04caa13ad8cfe8
7
+ data.tar.gz: 55688fe3b36b9f04865bec6c89c507f74fe998bd798184a1e58763e66a17e4be1404e4c9f1b4c8c1de788ecdaa523985b3e597466014293f9c5f65ff9d1fe013
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ delete.rb
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Settle any argument with an epic Rock Paper Scissors battle. This gem allows you to run a 1 or 2 player rock paper scissors game from your command line.
4
4
 
5
+ *NEW* Rock-paper-scissors-lizard-Spock mode now available too! Test your prowess Sheldon-style. Don't know what I'm talking about? Check out the video...
6
+
7
+ <a href="http://www.youtube.com/watch?feature=player_embedded&v=cSLeBKT7-sM
8
+ " target="_blank"><img src="http://img.youtube.com/vi/cSLeBKT7-sM/0.jpg"
9
+ alt="IMAGE ALT TEXT HERE" width="240" height="180" border="10" /></a>
10
+
5
11
  ## Installation
6
12
 
7
13
  Add this line to your application's Gemfile:
@@ -26,7 +32,10 @@ Or install it yourself as:
26
32
  2. Then, set a variable equal to a new instance of Game:
27
33
  `game = RockPaperScissors::Game.new`
28
34
 
29
- 3. Finally, start the game with `game.play` (or whichever variable you set in step 2).
35
+ 3. The game will prompt you for play mode (1 for rock-paper-scissors, 2 for rock-paper-scissors-lizard-spock) and number of players (1 or 2).
36
+
37
+ 4. Finally, start the game with `game.play` (or whichever variable you set in step 2).
38
+
30
39
 
31
40
  ## Contributing
32
41
 
@@ -1,5 +1,6 @@
1
1
  require_relative "rock_paper_scissors/version"
2
2
  require_relative "rock_paper_scissors/game_manager"
3
+ require_relative "rock_paper_scissors/board"
3
4
  require_relative "rock_paper_scissors/player"
4
5
  require_relative "rock_paper_scissors/human"
5
6
  require_relative "rock_paper_scissors/computer"
@@ -0,0 +1,65 @@
1
+ module RockPaperScissors
2
+ class Board
3
+ attr_reader :plays, :player_moves
4
+
5
+ @@wins = [
6
+ {pair: ['r', 's'], phrase: 'Rock crushes Scissors.'},
7
+ {pair: ['r', 'l'], phrase: 'Rock crushes Lizard.'},
8
+ {pair: ['p', 'r'], phrase: 'Paper covers Rock.'},
9
+ {pair: ['p', 'sp'], phrase: 'Paper disproves Spock.'},
10
+ {pair: ['s', 'p'], phrase: 'Scissors cut Paper.'},
11
+ {pair: ['s', 'l'], phrase: 'Scissors decapitate Lizard.'},
12
+ {pair: ['l', 'sp'], phrase: 'Lizard poisons Spock.'},
13
+ {pair: ['l', 'p'], phrase: 'Lizard eats Paper.'},
14
+ {pair: ['sp', 's'], phrase: 'Spock smashes Scissors.'},
15
+ {pair: ['sp', 'r'], phrase: 'Spock vaporizes Rock.'}
16
+ ]
17
+
18
+ def initialize(mode)
19
+ @player_moves = []
20
+
21
+ @plays = {r: 'rock', p: 'paper', s: 'scissors'}
22
+
23
+ if mode == 2
24
+ @plays[:l] = 'lizard'
25
+ @plays[:sp] = 'Spock'
26
+ end
27
+ end
28
+
29
+ def options
30
+ options = []
31
+ @plays.each do |symbol, word|
32
+ options << "#{symbol} = #{word}"
33
+ end
34
+ options.join(', ')
35
+ end
36
+
37
+ def place_move(player_number, move)
38
+ @player_moves[player_number - 1] = move if valid?(move)
39
+ end
40
+
41
+ def winner
42
+ if @@wins.any? { |win| win[:pair] == @player_moves }
43
+ 1
44
+ else
45
+ 2
46
+ end
47
+ end
48
+
49
+ def phrase(moves)
50
+ winning_move = @@wins.select { |win| win[:pair] == moves }.first
51
+ winning_move[:phrase]
52
+ end
53
+
54
+ private
55
+
56
+ def valid?(move)
57
+ if @plays.has_key?(move.to_sym)
58
+ true
59
+ else
60
+ puts Rainbow("\nI'm sorry, I don't recognize that move. Try again...").red
61
+ end
62
+ end
63
+
64
+ end
65
+ end
@@ -2,13 +2,16 @@ module RockPaperScissors
2
2
 
3
3
  class Computer < Player
4
4
 
5
- def initialize
5
+ def initialize(board, player_number)
6
+ super(board, player_number)
6
7
  @name = "Skynet"
7
8
  end
8
9
 
9
10
  def select_move
10
- @move = VALID_ANSWERS.shuffle.first
11
+ move = @board.plays.keys.sample.to_s
12
+ @board.place_move(@player_number, move)
11
13
  end
14
+
12
15
  end
13
16
 
14
17
  end
@@ -2,15 +2,16 @@ require 'rainbow'
2
2
  module RockPaperScissors
3
3
 
4
4
  class Game
5
- @@move_lookup = {'r' => 'rock', 'p' => 'paper', 's' => 'scissors'}
6
5
 
7
- def initialize mode="AI"
8
- puts "\n"
9
- puts Rainbow("*********************************************").bg(:yellow).black
10
- puts Rainbow("** Welcome to Rock, Paper, Scissors! **").bg(:yellow).black
11
- puts Rainbow("*********************************************").bg(:yellow).black
6
+ def initialize
7
+ welcome_message
8
+
9
+ # Instantiate board based on game mode
12
10
  mode = select_mode
13
- initialize_players(mode)
11
+ @board = Board.new(mode)
12
+
13
+ num_players = select_player_mode
14
+ initialize_players(num_players)
14
15
 
15
16
  puts "\n(Remember to begin play by entering game.play)"
16
17
  end
@@ -28,41 +29,58 @@ module RockPaperScissors
28
29
 
29
30
  private
30
31
 
31
- def winner
32
- puts "\n\n"
33
- if @player1.move == @player2.move
34
- puts Rainbow("It's a draw! Let's try again!").red
35
- elsif player1_win?
36
- puts Rainbow("#{@player1.name} WINS with #{@@move_lookup[@player1.move].upcase} beating #{@player2.name}'s #{@@move_lookup[@player2.move].upcase}!").green
37
- true
38
- else
39
- puts Rainbow("#{@player2.name} WINS with #{@@move_lookup[@player2.move].upcase} beating #{@player1.name}'s #{@@move_lookup[@player1.move].upcase}.").red
40
- true
32
+ # Choose regular mode or rock-paper-scissors-lizard-spock mode
33
+ def select_mode
34
+ loop do
35
+ puts "\nType 1 for regular mode or 2 for rock-paper-scissors-lizard-spock mode."
36
+ print " > "
37
+ mode = gets.chomp.to_i
38
+ if valid_mode?(mode)
39
+ return mode
40
+ end
41
41
  end
42
42
  end
43
43
 
44
- def select_mode
45
- puts "\nType 1 for 1 player or 2 for 2 players"
46
- print " > "
47
- gets.chomp.to_i
44
+ # Choose 1 or 2 player mode
45
+ def select_player_mode
46
+ loop do
47
+ puts "\nType 1 for 1 player or 2 for 2 players"
48
+ print " > "
49
+ num_players = gets.chomp.to_i
50
+ if valid_mode?(num_players)
51
+ return num_players
52
+ end
53
+ end
48
54
  end
49
55
 
50
- def initialize_players(mode)
51
- @player1 = Human.new
56
+ # Instantiate players based on player mode
57
+ def initialize_players(num_players)
58
+ @player1 = Human.new(@board, 1)
59
+ if num_players == 1
60
+ @player2 = Computer.new(@board, 2)
61
+ else
62
+ @player2 = Human.new(@board, 2)
63
+ end
64
+ end
52
65
 
53
- if mode == 1
54
- @player2 = Computer.new
55
- elsif mode == 2
56
- @player2 = Human.new
66
+ def winner
67
+ puts "\n\n"
68
+ if @board.player_moves[0] == @board.player_moves[1]
69
+ puts Rainbow("It's a draw! Let's try again!").red
57
70
  else
58
- puts "I'm sorry I didn't recognize your input. Try again..."
59
- initialize_players(select_mode)
71
+ if @board.winner == 1
72
+ print_results(@player1.name, @board.player_moves, 'green')
73
+ else
74
+ print_results(@player2.name, @board.player_moves.reverse, 'red')
75
+ end
76
+ true
60
77
  end
61
78
  end
62
79
 
63
- def player1_win?
64
- winners = [['r', 's'], ['p', 'r'], ['s', 'p']]
65
- winners.include? [@player1.move, @player2.move]
80
+ def print_results(player_name, moves, color)
81
+ phrase = @board.phrase(moves)
82
+ results = "#{player_name} WINS!!! #{phrase}"
83
+ color == 'green' ? puts(Rainbow(results).green) : puts(Rainbow(results).red)
66
84
  end
67
85
 
68
86
  def quit?
@@ -70,5 +88,16 @@ module RockPaperScissors
70
88
  gets.chomp.downcase != 'y'
71
89
  end
72
90
 
91
+ def valid_mode?(mode)
92
+ mode == 1 || mode == 2 ? true : puts("I'm sorry, I don't recognize that option. Try again...")
93
+ end
94
+
95
+ def welcome_message
96
+ puts "\n"
97
+ puts Rainbow("*********************************************").bg(:yellow).black
98
+ puts Rainbow("** Welcome to Rock, Paper, Scissors! **").bg(:yellow).black
99
+ puts Rainbow("*********************************************").bg(:yellow).black
100
+ end
101
+
73
102
  end
74
103
  end
@@ -4,29 +4,40 @@ module RockPaperScissors
4
4
 
5
5
  class Human < Player
6
6
 
7
- def initialize
7
+ def initialize(board, player_number)
8
+ super(board, player_number)
8
9
  @name = get_name
9
10
  end
10
11
 
11
12
  def select_move
12
13
  loop do
13
- puts "\nInput is HIDDEN! Options: r = rock, p = paper, s = scissors"
14
- print "Enter move (#{@name}) > "
15
- @move = STDIN.noecho(&:gets).chomp
16
- break if valid?(move)
14
+ move = ask_move
15
+ if valid_format?(move)
16
+ break if @board.place_move(@player_number, move)
17
+ end
17
18
  end
18
19
  end
19
20
 
20
21
  private
21
22
 
23
+ def ask_move
24
+ puts "\nInput is HIDDEN! #{@board.options}"
25
+ print "Enter move (#{@name}) > "
26
+ STDIN.noecho(&:gets).chomp
27
+ end
28
+
22
29
  def get_name
23
30
  puts "Welcome, new player! What's your name?"
24
31
  print " > "
25
32
  gets.chomp.capitalize
26
33
  end
27
34
 
28
- def valid? move
29
- VALID_ANSWERS.include?(move) ? true : puts("I'm sorry, that's not a valid move. Try again...")
35
+ def valid_format? move
36
+ if move.length <= 2
37
+ true
38
+ else
39
+ puts("Unrecognized input. Please enter 1-2 letters based on the play options...")
40
+ end
30
41
  end
31
42
 
32
43
  end
@@ -1,6 +1,11 @@
1
1
  module RockPaperScissors
2
2
  class Player
3
- attr_accessor :move, :name
4
- VALID_ANSWERS = ['r', 'p', 's']
3
+ attr_accessor :name
4
+
5
+ def initialize(board, player_number = 1)
6
+ @board = board
7
+ @player_number = player_number
8
+ end
9
+
5
10
  end
6
11
  end
@@ -1,3 +1,3 @@
1
1
  module RockPaperScissors
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["siakaramalegos@gmail.com"]
11
11
 
12
12
  spec.summary = %q{Settle any argument with a Rock Paper Scissors battle.}
13
- spec.description = %q{Settle any argument with a Rock Paper Scissors battle. To install, run gem install rock_paper_scissors.}
13
+ spec.description = %q{Settle any argument with a 1 or 2 player Rock Paper Scissors battle. Also includes rock-paper-scissors-lizard-spock mode! To install, run gem install rock_paper_scissors. See github repo for more information.}
14
14
  spec.homepage = "https://github.com/siakaramalegos/rock_paper_scissors"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rock_paper_scissors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sia Karamalegos
@@ -52,8 +52,9 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
- description: Settle any argument with a Rock Paper Scissors battle. To install, run
56
- gem install rock_paper_scissors.
55
+ description: Settle any argument with a 1 or 2 player Rock Paper Scissors battle. Also
56
+ includes rock-paper-scissors-lizard-spock mode! To install, run gem install rock_paper_scissors.
57
+ See github repo for more information.
57
58
  email:
58
59
  - siakaramalegos@gmail.com
59
60
  executables: []
@@ -72,6 +73,7 @@ files:
72
73
  - examples/README.md
73
74
  - examples/basic.rb
74
75
  - lib/rock_paper_scissors.rb
76
+ - lib/rock_paper_scissors/board.rb
75
77
  - lib/rock_paper_scissors/computer.rb
76
78
  - lib/rock_paper_scissors/game_manager.rb
77
79
  - lib/rock_paper_scissors/human.rb