prisoners-dilemma 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,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ prisoners-dilemma (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ minitest (3.3.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ minitest
16
+ prisoners-dilemma!
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ prisoners-dilemma
2
+ =================
3
+
4
+ A Ruby Simulator for the Prisoners' Dilemma
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/prisoners-dilemma'
4
+
5
+ Game.new.play
@@ -0,0 +1,15 @@
1
+ class GreenPlayer
2
+ attr_accessor :log
3
+
4
+ def initialize
5
+ @log = []
6
+ end
7
+
8
+ def cooperate?
9
+ if @log.empty? # 1st round
10
+ true
11
+ else
12
+ @log.last[0] ? true : false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ class RedPlayer
2
+ attr_accessor :log
3
+
4
+ def initialize
5
+ @log = []
6
+ end
7
+
8
+ def cooperate?
9
+ [true, true, false].sample
10
+ end
11
+ end
@@ -0,0 +1,47 @@
1
+ module ScoreRound
2
+ def both_cooperate
3
+ @score[0] += 3
4
+ @score[1] += 3
5
+
6
+ round = [true, true, @score]
7
+
8
+ @red.log << round
9
+ @green.log << round
10
+
11
+ puts "\tBoth Cooperate"
12
+ end
13
+
14
+ def neither_cooperate
15
+ @score[0] += 1
16
+ @score[1] += 1
17
+
18
+ round = [false, false, @score]
19
+
20
+ @red.log << round
21
+ @green.log << round
22
+
23
+ puts "\tNeither Cooperate"
24
+ end
25
+
26
+ def red_screws_green
27
+ @score[0] += 5
28
+
29
+ round = [false, true, @score]
30
+
31
+ @red.log << round
32
+ @green.log << round
33
+
34
+ puts "\tRed Screws Green"
35
+ end
36
+
37
+ def green_screws_red
38
+ @score[1] += 5
39
+
40
+ round = [true, false, @score]
41
+
42
+ @red.log << round
43
+ @green.log << round
44
+
45
+ puts "\tGreen Screws Red"
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ class Game
2
+ def self.version
3
+ '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,56 @@
1
+ require_relative 'prisoners-dilemma/players/red'
2
+ require_relative 'prisoners-dilemma/players/green'
3
+ require_relative 'prisoners-dilemma/score-round'
4
+ require_relative 'prisoners-dilemma/version'
5
+
6
+ class Game
7
+ include ScoreRound
8
+
9
+ attr_reader :score
10
+
11
+ def initialize
12
+ @score = [0, 0]
13
+
14
+ @red = RedPlayer.new
15
+ @green = GreenPlayer.new
16
+ end
17
+
18
+ def play
19
+ 1.upto 10_000 do |round_number|
20
+ announce round_number
21
+
22
+ score_round
23
+ end
24
+
25
+ score_game
26
+ end
27
+
28
+ private
29
+
30
+ def announce round_number
31
+ puts "\nRound #{round_number}:\n"
32
+ end
33
+
34
+ def score_round
35
+ red = @red.cooperate?
36
+ green = @green.cooperate?
37
+
38
+ if red && green
39
+ both_cooperate
40
+ elsif !red && !green
41
+ neither_cooperate
42
+ elsif green
43
+ red_screws_green
44
+ elsif red
45
+ green_screws_red
46
+ end
47
+ end
48
+
49
+ def score_game
50
+ puts "\nFinal Earnings:"
51
+ puts "\t$#{@score[0].to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse} Red"
52
+ puts "\t$#{@score[1].to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse} Green"
53
+ puts "\t---"
54
+ puts "\t$#{(@score[0] + @score[1]).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse}"
55
+ end
56
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/prisoners-dilemma/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Shannon']
6
+ gem.email = ['shannonskipper@gmail.com']
7
+ gem.description = %q{Ruby Simulation of the "Prisoners' Dilemma"}
8
+ gem.summary = %q{A Ruby Gem that simulates the Prisoners' Dilemma.}
9
+ gem.homepage = 'https://github.com/Havenwood/persist'
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/})
14
+ gem.name = 'prisoners-dilemma'
15
+ gem.require_paths = ['lib']
16
+ gem.version = Game.version
17
+
18
+ gem.add_development_dependency 'minitest'
19
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prisoners-dilemma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Shannon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
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'
30
+ description: Ruby Simulation of the "Prisoners' Dilemma"
31
+ email:
32
+ - shannonskipper@gmail.com
33
+ executables:
34
+ - prisoners-dilemma
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - README.md
42
+ - bin/prisoners-dilemma
43
+ - lib/prisoners-dilemma.rb
44
+ - lib/prisoners-dilemma/players/green.rb
45
+ - lib/prisoners-dilemma/players/red.rb
46
+ - lib/prisoners-dilemma/score-round.rb
47
+ - lib/prisoners-dilemma/version.rb
48
+ - prisoners-dilemma.gemspec
49
+ homepage: https://github.com/Havenwood/persist
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.24
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A Ruby Gem that simulates the Prisoners' Dilemma.
73
+ test_files: []
74
+ has_rdoc: