war_simulator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in war.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Phil Bottomley
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # War simulator
2
+
3
+ Simulator for the War card game. Gives a summary of the game
4
+ including the number of rounds that are needed before a player
5
+ wins.
6
+
7
+ For more info and how to play see the
8
+ [Wikipedia page](http://en.wikipedia.org/wiki/War_(card_game)).
9
+
10
+ ## Installation
11
+
12
+ Install it:
13
+
14
+ $ gem install war_simulator
15
+
16
+ ## Usage
17
+
18
+ To run a new simulaton:
19
+
20
+ WarSimulator.run
21
+
22
+ Then the number of rounds will be outputted:
23
+
24
+ => 234
25
+
26
+ ## Todo
27
+
28
+ - Improve on specs
29
+ - Add ability to use certain range of cards (eg. Ace to 10)
30
+ - Account for more than two players
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,16 @@
1
+ require "war_simulator/version"
2
+ require "war_simulator/card"
3
+ require "war_simulator/deck"
4
+ require "war_simulator/player"
5
+ require "war_simulator/game"
6
+
7
+ module WarSimulator
8
+ class <<
9
+
10
+ def self.run
11
+ game = Game.new
12
+ game.simulate
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module WarSimulator
2
+ class Card
3
+
4
+ SUITS = ['S', 'C', 'H', 'D']
5
+ NUMBERS = (2..14).to_a
6
+
7
+ attr_accessor :suit, :number
8
+
9
+ def initialize(suit, number)
10
+ number = number.to_i
11
+ if valid?(suit, number)
12
+ @suit = suit
13
+ @number = number
14
+ else
15
+ raise 'Card not valid'
16
+ end
17
+ end
18
+
19
+ def valid?(suit, number)
20
+ SUITS.include?(suit) && NUMBERS.include?(number)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ module WarSimulator
2
+ class Deck
3
+
4
+ def self.generate_deck
5
+ deck = []
6
+
7
+ Card::SUITS.each do |suit|
8
+ Card::NUMBERS.each do |number|
9
+ deck << Card.new(suit, number)
10
+ end
11
+ end
12
+
13
+ return deck
14
+ end
15
+
16
+ attr_accessor :cards
17
+
18
+ def initialize
19
+ @cards = self.class.generate_deck
20
+ shuffle
21
+ end
22
+
23
+ def shuffle
24
+ @cards = @cards.sort_by{rand}
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,59 @@
1
+ module WarSimulator
2
+ class Game
3
+
4
+ attr_accessor :player_1, :player_2, :deck
5
+
6
+ def initialize
7
+ @deck = Deck.new
8
+ @p1_hand = Player.new.hand
9
+ @p2_hand = Player.new.hand
10
+ deal
11
+ end
12
+
13
+ def deal
14
+ cut = @deck.cards.each_slice(@deck.cards.count / 2).to_a
15
+ @deck.cards = nil
16
+
17
+ @p1_hand = cut[0]
18
+ @p2_hand = cut[1]
19
+ end
20
+
21
+ def simulate
22
+ battles = 0
23
+ war_pile = []
24
+
25
+ until player_has_no_cards?
26
+ battles += 1
27
+
28
+ if @p1_hand.first.number == @p2_hand.first.number
29
+ war_pile << @p1_hand.shift
30
+ war_pile << @p2_hand.shift
31
+
32
+ elsif @p1_hand.first.number > @p2_hand.first.number
33
+ @p1_hand << @p2_hand.shift
34
+ @p1_hand << @p1_hand.shift
35
+ @p1_hand << war_pile
36
+ war_pile = []
37
+
38
+ else
39
+ @p2_hand << @p1_hand.shift
40
+ @p2_hand << @p2_hand.shift
41
+ @p2_hand << war_pile
42
+ war_pile = []
43
+
44
+ end
45
+
46
+ @p1_hand.flatten!
47
+ @p2_hand.flatten!
48
+ war_pile.flatten!
49
+ end
50
+
51
+ battles
52
+ end
53
+
54
+ def player_has_no_cards?
55
+ @p1_hand.count == 0 || @p2_hand.count == 0
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,11 @@
1
+ module WarSimulator
2
+ class Player
3
+
4
+ attr_accessor :hand
5
+
6
+ def intialize
7
+ @hand = []
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module WarSimulator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/war_simulator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Phil Bottomley"]
6
+ gem.email = ["philbottomley1@gmail.com"]
7
+ gem.description = %q{Simulator for the War card game.}
8
+ gem.summary = %q{Simulator for the War card game that outputs the number of rounds until a player wins.}
9
+ gem.homepage = "https://github.com/philbott/war_simulator"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "war_simulator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = WarSimulator::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: war_simulator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Phil Bottomley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simulator for the War card game.
15
+ email:
16
+ - philbottomley1@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/war_simulator.rb
27
+ - lib/war_simulator/card.rb
28
+ - lib/war_simulator/deck.rb
29
+ - lib/war_simulator/game.rb
30
+ - lib/war_simulator/player.rb
31
+ - lib/war_simulator/version.rb
32
+ - war_simulator.gemspec
33
+ homepage: https://github.com/philbott/war_simulator
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Simulator for the War card game that outputs the number of rounds until a
57
+ player wins.
58
+ test_files: []
59
+ has_rdoc: