prisoners-dilemma 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +16 -0
- data/README.md +4 -0
- data/bin/prisoners-dilemma +5 -0
- data/lib/prisoners-dilemma/players/green.rb +15 -0
- data/lib/prisoners-dilemma/players/red.rb +11 -0
- data/lib/prisoners-dilemma/score-round.rb +47 -0
- data/lib/prisoners-dilemma/version.rb +5 -0
- data/lib/prisoners-dilemma.rb +56 -0
- data/prisoners-dilemma.gemspec +19 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -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,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:
|