-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwar.rb
118 lines (94 loc) · 2.47 KB
/
war.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
# reinterpreted from BASIC by stephan.com
class War
class Card
class CardError < StandardError; end
SUITS = %i[spades hearts clubs diamonds].freeze
PIPS = %i[ace deuce trey four five six seven eight nine ten jack king queen].freeze
CARDS = SUITS.product(PIPS).freeze
VALUES = PIPS.zip(1..13).to_h.freeze
attr_reader :value
def initialize(suit, pip)
@suit = suit
@pip = pip
raise CardError, 'invalid suit' unless SUITS.include? @suit
raise CardError, 'invalid pip' unless PIPS.include? @pip
@value = VALUES[pip]
end
def <=>(other)
@value <=> other.value
end
def >(other)
@value > other.value
end
def <(other)
@value < other.value
end
def to_s
"the #{@pip} of #{@suit}"
end
def self.shuffle
CARDS.map { |suit, pip| new(suit, pip) }.shuffle
end
end
def initialize
@your_score = 0
@computer_score = 0
@your_deck = Card.shuffle
@computer_deck = Card.shuffle
end
def play
intro
loop do
puts "\nYou: #{@your_score} Computer: #{@computer_score}"
round @your_deck.shift, @computer_deck.shift
break if empty?
puts 'Do you want to continue?'
break unless yesno
end
outro
end
private
def round(your_card, computer_card)
puts "You: #{your_card} vs Computer: #{computer_card}"
return puts 'Tie. No score change.' if your_card == computer_card
if computer_card > your_card
puts "Computer wins with #{computer_card}"
@computer_score += 1
else
puts "You win with #{your_card}"
@your_score += 1
end
end
def yesno
loop do
wants = gets.strip
return true if wants.downcase == 'yes'
return false if wants.downcase == 'no'
puts 'Yes or no, please.'
end
end
def intro
puts 'War'.center(80)
puts 'stephan.com'.center(80)
puts
puts 'This is the card game of war.'
puts 'Do you want directions'
directions if yesno
end
def directions
puts 'The computer gives you and it a \'card\'. The higher card'
puts '(numerically) wins. The game ends when you choose not to'
puts 'continue or when you have finished the pack.'
puts
end
def outro
puts "We've run out of cards" if empty?
puts "Final score:\nYou: #{@your_score}\nComputer: #{@computer_score}"
puts 'Thanks for playing!'
end
def empty?
@your_deck.empty? || @computer_deck.empty?
end
end
War.new.play