war_simulator 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +8 -0
- data/Gemfile +1 -0
- data/README.md +13 -3
- data/lib/war_simulator/game.rb +55 -11
- data/lib/war_simulator/player.rb +0 -4
- data/lib/war_simulator/version.rb +1 -1
- data/spec/spec_helper.rb +3 -0
- data/spec/war_simulator_spec.rb +8 -0
- data/war_simulator.gemspec +2 -0
- metadata +41 -3
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
# War simulator
|
2
|
+
[![Build Status](https://secure.travis-ci.org/philbott/war_simulator.png?branch=master)][travis] [![Dependency Status](https://gemnasium.com/philbott/war_simulator.png?travis)][gemnasium]
|
3
|
+
|
4
|
+
[travis]: http://travis-ci.org/philbott/war_simulator
|
5
|
+
[gemnasium]: https://gemnasium.com/philbott/war_simulator
|
2
6
|
|
3
7
|
Simulator for the War card game. Gives a summary of the game
|
4
8
|
including the number of rounds that are needed before a player
|
@@ -10,17 +14,23 @@ For more info and how to play see: http://en.wikipedia.org/wiki/War_(card_game).
|
|
10
14
|
|
11
15
|
Install it:
|
12
16
|
|
13
|
-
|
17
|
+
```bash
|
18
|
+
$ gem install war_simulator
|
19
|
+
```
|
14
20
|
|
15
21
|
## Usage
|
16
22
|
|
17
23
|
To run a new simulaton:
|
18
24
|
|
19
|
-
|
25
|
+
```ruby
|
26
|
+
WarSimulator.run
|
27
|
+
```
|
20
28
|
|
21
29
|
Then the number of rounds will be outputted:
|
22
30
|
|
23
|
-
|
31
|
+
```irb
|
32
|
+
=> 234
|
33
|
+
```
|
24
34
|
|
25
35
|
## Todo
|
26
36
|
|
data/lib/war_simulator/game.rb
CHANGED
@@ -1,16 +1,22 @@
|
|
1
1
|
module WarSimulator
|
2
2
|
class Game
|
3
3
|
|
4
|
-
|
4
|
+
WAR_CARDS = 3
|
5
|
+
|
6
|
+
attr_accessor :p1_hand, :p2_hand, :deck, :war_hold
|
5
7
|
|
6
8
|
def initialize
|
7
9
|
@deck = Deck.new
|
8
10
|
@p1_hand = Player.new.hand
|
9
11
|
@p2_hand = Player.new.hand
|
12
|
+
@war_hold = []
|
13
|
+
@p1_war_cards = []
|
14
|
+
@p2_war_cards = []
|
10
15
|
deal
|
11
16
|
end
|
12
17
|
|
13
18
|
def deal
|
19
|
+
raise 'Cards already delt' if @deck.cards.nil?
|
14
20
|
cut = @deck.cards.each_slice(@deck.cards.count / 2).to_a
|
15
21
|
@deck.cards = nil
|
16
22
|
|
@@ -20,40 +26,78 @@ module WarSimulator
|
|
20
26
|
|
21
27
|
def simulate
|
22
28
|
battles = 0
|
23
|
-
war_pile = []
|
24
29
|
|
25
30
|
until player_has_no_cards?
|
26
31
|
battles += 1
|
27
32
|
|
28
33
|
if @p1_hand.first.number == @p2_hand.first.number
|
29
|
-
|
30
|
-
|
34
|
+
@war_hold << @p1_hand.shift
|
35
|
+
@war_hold << @p2_hand.shift
|
36
|
+
setup_war
|
31
37
|
|
32
38
|
elsif @p1_hand.first.number > @p2_hand.first.number
|
33
39
|
@p1_hand << @p2_hand.shift
|
34
40
|
@p1_hand << @p1_hand.shift
|
35
|
-
@p1_hand << war_pile
|
36
|
-
war_pile = []
|
37
41
|
|
38
42
|
else
|
39
43
|
@p2_hand << @p1_hand.shift
|
40
44
|
@p2_hand << @p2_hand.shift
|
41
|
-
@p2_hand << war_pile
|
42
|
-
war_pile = []
|
43
45
|
|
44
46
|
end
|
45
47
|
|
46
|
-
|
47
|
-
@p2_hand.flatten!
|
48
|
-
war_pile.flatten!
|
48
|
+
flatten_hands
|
49
49
|
end
|
50
50
|
|
51
51
|
battles
|
52
52
|
end
|
53
53
|
|
54
|
+
def setup_war
|
55
|
+
WAR_CARDS.times do
|
56
|
+
@p1_war_cards << @p1_hand.shift unless @p1_hand.empty? and return
|
57
|
+
@p2_war_cards << @p2_hand.shift unless @p2_hand.empty? and return
|
58
|
+
end
|
59
|
+
|
60
|
+
@p1_war_cards.flatten!
|
61
|
+
@p2_war_cards.flatten!
|
62
|
+
|
63
|
+
pick_and_compare
|
64
|
+
end
|
65
|
+
|
66
|
+
def pick_and_compare
|
67
|
+
p1_pick = @p1_war_cards.shuffle.first
|
68
|
+
p2_pick = @p2_war_cards.shuffle.first
|
69
|
+
|
70
|
+
@war_hold << @p1_war_cards
|
71
|
+
@war_hold << @p2_war_cards
|
72
|
+
|
73
|
+
@p1_war_cards = []
|
74
|
+
@p2_war_cards = []
|
75
|
+
|
76
|
+
flatten_hands
|
77
|
+
|
78
|
+
if p1_pick.number == p2_pick.number
|
79
|
+
setup_war
|
80
|
+
|
81
|
+
elsif p1_pick.number > p2_pick.number
|
82
|
+
@p1_hand << @war_hold
|
83
|
+
@war_hold = []
|
84
|
+
|
85
|
+
else
|
86
|
+
@p2_hand << @war_hold
|
87
|
+
@war_hold = []
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
54
92
|
def player_has_no_cards?
|
55
93
|
@p1_hand.count == 0 || @p2_hand.count == 0
|
56
94
|
end
|
57
95
|
|
96
|
+
def flatten_hands
|
97
|
+
@p1_hand.flatten!
|
98
|
+
@p2_hand.flatten!
|
99
|
+
@war_hold.flatten!
|
100
|
+
end
|
101
|
+
|
58
102
|
end
|
59
103
|
end
|
data/lib/war_simulator/player.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/war_simulator_spec.rb
CHANGED
data/war_simulator.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: war_simulator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: rspec
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -27,6 +43,22 @@ dependencies:
|
|
27
43
|
- - ~>
|
28
44
|
- !ruby/object:Gem::Version
|
29
45
|
version: '2.11'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: simplecov
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.6'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.6'
|
30
62
|
description: Simulator for the War card game.
|
31
63
|
email:
|
32
64
|
- philbottomley1@gmail.com
|
@@ -35,6 +67,7 @@ extensions: []
|
|
35
67
|
extra_rdoc_files: []
|
36
68
|
files:
|
37
69
|
- .gitignore
|
70
|
+
- .travis.yml
|
38
71
|
- Gemfile
|
39
72
|
- LICENSE
|
40
73
|
- README.md
|
@@ -64,12 +97,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
97
|
- - ! '>='
|
65
98
|
- !ruby/object:Gem::Version
|
66
99
|
version: '0'
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
hash: -701390445
|
67
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
104
|
none: false
|
69
105
|
requirements:
|
70
106
|
- - ! '>='
|
71
107
|
- !ruby/object:Gem::Version
|
72
108
|
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: -701390445
|
73
112
|
requirements: []
|
74
113
|
rubyforge_project:
|
75
114
|
rubygems_version: 1.8.24
|
@@ -84,4 +123,3 @@ test_files:
|
|
84
123
|
- spec/war_simulator/game_spec.rb
|
85
124
|
- spec/war_simulator/player_spec.rb
|
86
125
|
- spec/war_simulator_spec.rb
|
87
|
-
has_rdoc:
|