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.
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 1.8.7
6
+
7
+ branches:
8
+ only: master
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ #ruby=1.9.3
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  # Specify your gem's dependencies in war.gemspec
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
- $ gem install war_simulator
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
- WarSimulator.run
25
+ ```ruby
26
+ WarSimulator.run
27
+ ```
20
28
 
21
29
  Then the number of rounds will be outputted:
22
30
 
23
- => 234
31
+ ```irb
32
+ => 234
33
+ ```
24
34
 
25
35
  ## Todo
26
36
 
@@ -1,16 +1,22 @@
1
1
  module WarSimulator
2
2
  class Game
3
3
 
4
- attr_accessor :p1_hand, :p2_hand, :deck
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
- war_pile << @p1_hand.shift
30
- war_pile << @p2_hand.shift
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
- @p1_hand.flatten!
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
@@ -3,9 +3,5 @@ module WarSimulator
3
3
 
4
4
  attr_accessor :hand
5
5
 
6
- def intialize
7
- @hand = []
8
- end
9
-
10
6
  end
11
7
  end
@@ -1,3 +1,3 @@
1
1
  module WarSimulator
2
- VERSION = "0.0.2"
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,3 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  require 'rspec'
2
5
  require 'war_simulator'
3
6
 
@@ -6,4 +6,12 @@ describe WarSimulator do
6
6
  subject.should respond_to :run
7
7
  end
8
8
 
9
+ it 'should return integer' do
10
+ subject.run.should be_an Integer
11
+ end
12
+
13
+ it 'should be positive' do
14
+ subject.run.should > 0
15
+ end
16
+
9
17
  end
@@ -15,5 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = WarSimulator::VERSION
17
17
 
18
+ gem.add_development_dependency 'rake'
18
19
  gem.add_development_dependency 'rspec', '~> 2.11'
20
+ gem.add_development_dependency 'simplecov', '~> 0.6'
19
21
  end
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.2
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-05 00:00:00.000000000 Z
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: