PlayRockPaperScissorsGame 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rps.rb +102 -0
  3. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3500ac58a66083f469ba5f7c3c3a9ca8426be87d
4
- data.tar.gz: f5fa0506cd0deac60557ddb18ebb1d39023eb3b8
3
+ metadata.gz: eadc7e18a197f761997ebea753cccb20364a254f
4
+ data.tar.gz: e388f74211e927ea7f7b7a44d1c30e0cd1ba2f75
5
5
  SHA512:
6
- metadata.gz: e00779d6dbb4df8318784ea9791e7125cf6109b398f9ce62f3eb24e67d07bddf3a178ab6c371de6fb0bb7b41a2c425c84d7c7afabc549561ee2e4812796cc8ce
7
- data.tar.gz: f19dc146ba285963931b8a31b4adf4d273ede5f263a4526ae1e9d4c42f5c9d460f1b70b1537d896722d47ecd9505a8da374cd827472420a13cdf7f9726c06f1a
6
+ metadata.gz: 623c37135d4e8ea6c57465087816df822cc7e37265c96701a69f654e3acd423fbdff2d2d06fecb310a70a23c0362a1be8a526d0ef75b682bcfb1fdb4ac2cec21
7
+ data.tar.gz: 301f070e7aff04309444dec661f0d33710017194f0e76195426ceedd626a88dab74fe312ed7d39adb22c0adfa11a755e9542d916f99337aa58c119be6cb00de6
data/lib/rps.rb ADDED
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class Master
4
+
5
+ class RPS
6
+
7
+ module Constants
8
+ NTRY_TO_SYM = {
9
+ 'p' => :PAPER,
10
+ 'r' => :ROCK,
11
+ 's' => :SCISSORS
12
+ };
13
+ VALID_ENTRIES = NTRY_TO_SYM.keys;
14
+ COMPUTER_CHOICES = NTRY_TO_SYM.values;
15
+ WINNERS = [
16
+ [:SCISSORS, :PAPER],
17
+ [:PAPER, :ROCK],
18
+ [:ROCK, :SCISSORS]
19
+ ]; # format: player choice, computer choice
20
+ LOSERS = WINNERS.map { |i,j| [j,i] }; # this will take the original WINNERS array and flip the symbols, thus returning a loss for the user/player
21
+ INIT_STRINGS = [
22
+ "You are about to enter a rock-paper-scissors best of 3 match.",
23
+ "Press the return/enter key to continue...",
24
+ ""
25
+ ];
26
+ end;
27
+
28
+ class RockPaperScissors
29
+ class << self
30
+ def continue(str1, str2, str3)
31
+ puts str1;
32
+ print str2;
33
+ gets;
34
+ puts str3;
35
+ end;
36
+ end;
37
+ continue(Constants::INIT_STRINGS[0], Constants::INIT_STRINGS[1], Constants::INIT_STRINGS[2]);
38
+ def initialize
39
+ @player_score = @computer_score = @ties = 0;
40
+ end;
41
+ def play(winning_score)
42
+ while @player_score < winning_score && @computer_score < winning_score
43
+ puts "Player score: #{@player_score}, " +
44
+ "Computer score: #{@computer_score}, Ties: #{@ties}";
45
+ player = PrivateVars.player_choice;
46
+ computer = Constants::COMPUTER_CHOICES.sample;
47
+ puts "\nPlayer chooses #{player.to_s.downcase}";
48
+ puts "Computer chooses #{computer.to_s.downcase}";
49
+ case PrivateVars.player_outcome [player, computer]
50
+ when :WIN
51
+ puts "#{player.to_s.capitalize} beats #{computer.to_s.downcase}, player wins the round";
52
+ @player_score += 1;
53
+ when :LOSE
54
+ puts "#{computer.to_s.capitalize} beats #{player.to_s.downcase}, computer wins the round";
55
+ @computer_score += 1;
56
+ else
57
+ puts "Tie, choose again";
58
+ @ties += 1;
59
+ end;
60
+ end;
61
+ puts "\nFinal score: player: #{@player_score}, " +
62
+ "computer: #{@computer_score} (ties: #{@ties})";
63
+ case PrivateVars.final_outcome(@player_score, @computer_score)
64
+ when :WIN
65
+ puts "Player wins!";
66
+ when :LOSE
67
+ puts "Computer wins!";
68
+ else
69
+ puts "It's a tie!";
70
+ end;
71
+ puts "";
72
+ gets;
73
+ end;
74
+ private
75
+ module PrivateVars
76
+ class << self
77
+ def player_choice
78
+ loop do
79
+ print "Choose rock (r), paper (p) or scissors (s): ";
80
+ choice = gets.chomp.downcase;
81
+ return Constants::NTRY_TO_SYM[choice] if Constants::NTRY_TO_SYM.key?(choice);
82
+ puts "That entry is invalid. Please re-enter";
83
+ end;
84
+ end;
85
+ def player_outcome(plays)
86
+ return :WIN if Constants::WINNERS.include?(plays);
87
+ return :LOSE if Constants::LOSERS.include?(plays);
88
+ return :TIE if !:WIN | !:LOSE;
89
+ end;
90
+ def final_outcome(pl, co)
91
+ return :WIN if pl > co;
92
+ return :LOSE if pl < co;
93
+ return :TIE if pl = co;
94
+ end;
95
+ end;
96
+ end;
97
+ end;
98
+ end;
99
+ end;
100
+
101
+ Master::RPS::RockPaperScissors.new.play(3); # best of 3
102
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: PlayRockPaperScissorsGame
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - bag3318
@@ -64,6 +64,7 @@ files:
64
64
  - README.md
65
65
  - bin/PlayRockPaperScissorsGame
66
66
  - bin/rps
67
+ - lib/rps.rb
67
68
  homepage: https://rubygems.org/gems/PlayRockPaperScissorsGame/
68
69
  licenses:
69
70
  - MIT