gambit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. data/AUTHORS +2 -0
  2. data/CHANGELOG +7 -0
  3. data/COPYING +340 -0
  4. data/INSTALL +23 -0
  5. data/LICENSE +7 -0
  6. data/README +25 -0
  7. data/Rakefile +87 -0
  8. data/TODO +6 -0
  9. data/examples/bird_wars/game.rb +434 -0
  10. data/examples/bird_wars/html/chest.rhtml +37 -0
  11. data/examples/bird_wars/html/combat.rhtml +38 -0
  12. data/examples/bird_wars/html/fight.rhtml +71 -0
  13. data/examples/bird_wars/html/fridge.rhtml +31 -0
  14. data/examples/bird_wars/html/help.rhtml +30 -0
  15. data/examples/bird_wars/html/house.rhtml +36 -0
  16. data/examples/bird_wars/html/images/albatross.jpg +0 -0
  17. data/examples/bird_wars/html/images/alka_seltzer_small.png +0 -0
  18. data/examples/bird_wars/html/images/bazooka_small.png +0 -0
  19. data/examples/bird_wars/html/images/bb_gun_small.png +0 -0
  20. data/examples/bird_wars/html/images/cat_small.png +0 -0
  21. data/examples/bird_wars/html/images/combat.png +0 -0
  22. data/examples/bird_wars/html/images/crossbow_small.png +0 -0
  23. data/examples/bird_wars/html/images/cuckoo.jpg +0 -0
  24. data/examples/bird_wars/html/images/fridge.png +0 -0
  25. data/examples/bird_wars/html/images/gull.jpg +0 -0
  26. data/examples/bird_wars/html/images/house.jpg +0 -0
  27. data/examples/bird_wars/html/images/items.png +0 -0
  28. data/examples/bird_wars/html/images/not_found.png +0 -0
  29. data/examples/bird_wars/html/images/note.jpg +0 -0
  30. data/examples/bird_wars/html/images/oil_small.png +0 -0
  31. data/examples/bird_wars/html/images/one_stone_small.png +0 -0
  32. data/examples/bird_wars/html/images/owl.jpg +0 -0
  33. data/examples/bird_wars/html/images/poisoned_seeds_small.png +0 -0
  34. data/examples/bird_wars/html/images/rice_small.png +0 -0
  35. data/examples/bird_wars/html/images/shotgun_small.png +0 -0
  36. data/examples/bird_wars/html/images/slingshot_small.png +0 -0
  37. data/examples/bird_wars/html/images/soda_rings_small.png +0 -0
  38. data/examples/bird_wars/html/images/spear_small.png +0 -0
  39. data/examples/bird_wars/html/images/stork.jpg +0 -0
  40. data/examples/bird_wars/html/images/weapons.png +0 -0
  41. data/examples/bird_wars/html/images/woodpecker.jpg +0 -0
  42. data/examples/bird_wars/html/images/you.jpg +0 -0
  43. data/examples/bird_wars/html/index.rhtml +13 -0
  44. data/examples/bird_wars/html/weapons.rhtml +35 -0
  45. data/examples/cli/blackjack.rb +86 -0
  46. data/examples/cli/go.rb +93 -0
  47. data/examples/cli/space_trader2/planet_data +12280 -0
  48. data/examples/cli/space_trader2/space_trader2.rb +248 -0
  49. data/examples/cli/space_trader2/world.rb +127 -0
  50. data/examples/cli/spacetrader.rb +262 -0
  51. data/examples/cli/yahtzee.rb +161 -0
  52. data/examples/galactic_courier/Rakefile +9 -0
  53. data/examples/galactic_courier/bin/galactic_courier.rb +44 -0
  54. data/examples/galactic_courier/html/console.rhtml +15 -0
  55. data/examples/galactic_courier/html/images/barren.jpg +0 -0
  56. data/examples/galactic_courier/html/images/industrial.jpg +0 -0
  57. data/examples/galactic_courier/html/images/jungle.jpg +0 -0
  58. data/examples/galactic_courier/html/images/oceanic.jpg +0 -0
  59. data/examples/galactic_courier/html/images/tundra.jpg +0 -0
  60. data/examples/galactic_courier/html/images/volcanic.jpg +0 -0
  61. data/examples/galactic_courier/lib/galaxy.rb +64 -0
  62. data/examples/galactic_courier/lib/planet.rb +30 -0
  63. data/examples/galactic_courier/lib/sector.rb +84 -0
  64. data/examples/galactic_courier/lib/station.rb +18 -0
  65. data/examples/galactic_courier/test/tc_galaxy.rb +24 -0
  66. data/examples/galactic_courier/test/tc_planet.rb +22 -0
  67. data/examples/galactic_courier/test/tc_sector.rb +55 -0
  68. data/examples/galactic_courier/test/ts_all.rb +12 -0
  69. data/examples/gambit_mail/html/compose.rhtml +39 -0
  70. data/examples/gambit_mail/html/index.rhtml +3 -0
  71. data/examples/gambit_mail/html/mailbox.rhtml +47 -0
  72. data/examples/gambit_mail/html/message.rhtml +34 -0
  73. data/examples/gambit_mail/mail.rb +75 -0
  74. data/lib/gambit.rb +10 -0
  75. data/lib/gambit/server.rb +269 -0
  76. data/lib/gambit/server/gambiterror.rb +28 -0
  77. data/lib/gambit/server/game.rb +185 -0
  78. data/lib/gambit/server/game/eventform.rb +174 -0
  79. data/lib/gambit/server/message.rb +85 -0
  80. data/lib/gambit/server/player.rb +40 -0
  81. data/lib/gambit/tools.rb +13 -0
  82. data/lib/gambit/tools/board.rb +275 -0
  83. data/lib/gambit/tools/cards.rb +11 -0
  84. data/lib/gambit/tools/cards/card.rb +158 -0
  85. data/lib/gambit/tools/cards/deck.rb +99 -0
  86. data/lib/gambit/tools/cards/hand.rb +86 -0
  87. data/lib/gambit/tools/cards/pile.rb +107 -0
  88. data/lib/gambit/tools/currency.rb +148 -0
  89. data/lib/gambit/tools/dice.rb +219 -0
  90. data/lib/gambit/tools/movehistory.rb +164 -0
  91. data/lib/gambit/tools/scorecard.rb +333 -0
  92. data/lib/gambit/viewable.rb +71 -0
  93. data/setup.rb +1360 -0
  94. data/test/tc_board.rb +167 -0
  95. data/test/tc_cards.rb +182 -0
  96. data/test/tc_currency.rb +99 -0
  97. data/test/tc_dice.rb +83 -0
  98. data/test/tc_error.rb +34 -0
  99. data/test/tc_event.rb +367 -0
  100. data/test/tc_game.rb +29 -0
  101. data/test/tc_message.rb +35 -0
  102. data/test/tc_movehistory.rb +38 -0
  103. data/test/tc_player.rb +60 -0
  104. data/test/tc_scorecard.rb +112 -0
  105. data/test/tc_views.rb +353 -0
  106. data/test/test_game.rb +19 -0
  107. data/test/test_view.rhtml +2 -0
  108. data/test/ts_all.rb +12 -0
  109. data/test/ts_server.rb +14 -0
  110. data/test/ts_tools.rb +14 -0
  111. metadata +183 -0
@@ -0,0 +1,161 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # yahtzee.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-05-29.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "..", "lib"))
9
+ require "gambit/tools/dice"
10
+ require "gambit/tools/scorecard"
11
+
12
+ yahtzee = Gambit::Tools::Scorecard.new("Player")
13
+ yahtzee.use_category("Ones", 0, 6)
14
+ yahtzee.use_category("Twos", [0, 2, 4, 6, 8, 10])
15
+ yahtzee.use_category("Threes", [ 0, 3, 6, 9, 12, 15 ])
16
+ yahtzee.use_category("Fours", [ 0, 4, 8, 12, 16, 20 ])
17
+ yahtzee.use_category("Fives", [ 0, 5, 10, 15, 20, 25 ])
18
+ yahtzee.use_category("Sixes", [ 0, 6, 12, 18, 24, 30 ])
19
+
20
+ yahtzee.set_total("Bonus", *%w{Ones Twos Threes Fours Fives Sixes}) do |n|
21
+ if n >= 63 then 35 else 0 end
22
+ end
23
+ yahtzee.set_total("Upper", *%w{Ones Twos Threes Fours Fives Sixes Bonus})
24
+
25
+ yahtzee.use_category("Three of a Kind", 0, 30)
26
+ yahtzee.use_category("Four of a Kind", 0, 30)
27
+ yahtzee.use_category("Full House", [0, 25])
28
+ yahtzee.use_category("Small Straight", [0, 30])
29
+ yahtzee.use_category("Large Straight", [0, 40])
30
+ yahtzee.use_category("Yahtzee") { |n| n == 0 or n == 50 or (n - 50) % 100 == 0 }
31
+ yahtzee.use_category("Chance", 0, 36)
32
+ yahtzee.require_categories
33
+
34
+ yahtzee.set_total( "Lower", "Three of a Kind", "Four of a Kind", "Full House",
35
+ "Small Straight", "Large Straight", "Yahtzee",
36
+ "Chance" )
37
+ yahtzee.set_total("Overall", *%w{Upper Lower})
38
+ yahtzee.set_win("Overall")
39
+
40
+ def format( score )
41
+ count = 0
42
+ score.gsub!(/^-/, " -")
43
+ score.gsub!(/^( *\w+)/) do
44
+ p $1
45
+ if %w{Player Bonus Upper Lower Overall}.include?($1.strip)
46
+ "%2s %s" % ["", $1]
47
+ else
48
+ "%2d: %s" % [(count += 1), $1]
49
+ end
50
+ end
51
+ end
52
+
53
+ # Game.
54
+ puts "\nWelcome to Yahtzee!"
55
+ scratches = (1..13).to_a
56
+ 13.times do
57
+ # Rolling...
58
+ roll = Gambit::Tools::Dice.new(5, 6)
59
+ rolls = 2
60
+ while rolls > 0
61
+ puts "\nYou rolled #{roll.view(:gambit_text)}."
62
+ print "Action: (c)heck score, (s)core, (q)uit or #s to reroll? "
63
+ choice = STDIN.gets.chomp
64
+ case choice
65
+ when /^c/i
66
+ puts "\nScore:\n#{format(yahtzee.view(:gambit_text))}"
67
+ when /^s/i
68
+ break
69
+ when /^q/i
70
+ exit
71
+ else
72
+ begin
73
+ pips = choice.gsub(/\s+/, "").split(//).map do |n|
74
+ Integer(n)
75
+ end
76
+ indices = [ ]
77
+ roll.each_with_index do |die, i|
78
+ if found = pips.index(die)
79
+ indices << i
80
+ pips[found] = -1
81
+ end
82
+ end
83
+ roll.reroll(*indices)
84
+ rolls -= 1
85
+ rescue
86
+ puts "Error: That not a valid reroll."
87
+ end
88
+ end
89
+ end
90
+
91
+ # Scoring...
92
+ loop do
93
+ if roll.matches?(*%w{x x x x x}) and
94
+ yahtzee.total("Player", "Yahtzee") == 50
95
+ bonus_yahtzees += 100
96
+
97
+ if scratches.include?(roll[0])
98
+ yahtzee.score("Player", roll.sum(roll[0]), categories[roll[0]])
99
+ scratches.delete(roll[0])
100
+ puts "Bonus Yahtzee scored in #{categories[roll[0]]}."
101
+ break
102
+ end
103
+
104
+ puts "Bonus Yahtzee! 100 points added. " +
105
+ "Score in lower section as a wild-card."
106
+ bonus_yahtzee = true
107
+ else
108
+ bonus_yahtzee = false
109
+ end
110
+
111
+ print "\nScore:\n#{format(yahtzee.view(:gambit_text))}\n" +
112
+ "Where would you like to count your #{roll.view(:gambit_text)} " +
113
+ "(# of category)? "
114
+ begin
115
+ categories = %w{ Ones Twos Threes Fours Fives Sixes
116
+ Three\ of\ a\ Kind Four\ of\ a\ Kind
117
+ Full\ House Small\ Straight Large\ Straight
118
+ Yahtzee Chance }
119
+ choice = Integer(STDIN.gets.chomp)
120
+ raise "Already scored." unless scratches.include?(choice)
121
+ case choice
122
+ when 1..6
123
+ yahtzee.score( "Player", roll.sum(choice),
124
+ categories[choice - 1] )
125
+ when 7
126
+ if roll.matches?(*%w{x x x}) or bonus_yahtzee
127
+ yahtzee.score("Player", roll.sum, categories[choice - 1])
128
+ end
129
+ when 8
130
+ if roll.matches?(*%w{x x x x}) or bonus_yahtzee
131
+ yahtzee.score("Player", roll.sum, categories[choice - 1])
132
+ end
133
+ when 9
134
+ if roll.matches?(*%w{x x x y y}) or bonus_yahtzee
135
+ yahtzee.score("Player", 25, categories[choice - 1])
136
+ end
137
+ when 10
138
+ if roll.matches?(1, 2, 3, 4) or bonus_yahtzee
139
+ yahtzee.score("Player", 30, categories[choice - 1])
140
+ end
141
+ when 11
142
+ if roll.matches?(1, 2, 3, 4, 5) or bonus_yahtzee
143
+ yahtzee.score("Player", 40, categories[choice - 1])
144
+ end
145
+ when 12
146
+ if roll.matches?(*%w{x x x x x})
147
+ yahtzee.score("Player", 50, categories[choice - 1])
148
+ end
149
+ when 13
150
+ yahtzee.score("Player", roll.sum, categories[choice - 1])
151
+ end
152
+ scratches.delete(choice)
153
+ break
154
+ rescue
155
+ puts "Error: Invalid category choice."
156
+ end
157
+ end
158
+ end
159
+
160
+ print "\nFinal Score:\n#{format(yahtzee.view(:gambit_text))}\n" +
161
+ "Thanks for playing.\n\n"
@@ -0,0 +1,9 @@
1
+ require "rake/testtask"
2
+
3
+ task :default => [:test]
4
+
5
+ Rake::TestTask.new do |test|
6
+ test.libs << "test"
7
+ test.test_files = [ "test/ts_all.rb" ]
8
+ test.verbose = true
9
+ end
@@ -0,0 +1,44 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # galactic_courier.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-06-06.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ $dir = File.expand_path(File.dirname(__FILE__))
9
+ $:.unshift(File.join($dir, "../../../lib"), File.join($dir, "../lib"))
10
+
11
+ require "galaxy"
12
+ require "sector"
13
+ require "planet"
14
+ require "station"
15
+
16
+ require "gambit"
17
+
18
+ class GalacticCourier < Gambit::Server::Game
19
+ def initialize( )
20
+ super
21
+
22
+ @galaxy = Galaxy.random(10_000)
23
+ @where = nil
24
+ end
25
+
26
+ def do_configure( player, details = Hash.new )
27
+ super
28
+ "console.rhtml"
29
+ end
30
+
31
+ def do_event( player, event, details = Hash.new )
32
+ @where = @galaxy[0] if @where.nil?
33
+ super
34
+ end
35
+
36
+ def do_login( player, details = Hash.new )
37
+ "console.rhtml"
38
+ end
39
+
40
+ def do_warp( player, details = Hash.new )
41
+ @where = @galaxy[details["sector"].to_i - 1]
42
+ "console.rhtml"
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml">
4
+ <head>
5
+ <title><%= if @where.is_a? Sector
6
+ "Arriving in Sector #{@where.name}..."
7
+ else
8
+ "Landing on Planet #{@where.name}..."
9
+ end %></title>
10
+ </head><body>
11
+ <h1><%= @where.class.to_s.capitalize %> <%= @where.name %>:</h1>
12
+ <%= @where.view(:html).gsub(/class="sector">(.+?)</) do
13
+ ">" + event($1, "warp", "sector" => $1) + "<"
14
+ end %>
15
+ </body></html>
@@ -0,0 +1,64 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # galaxy.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-06-06.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ require "sector"
9
+
10
+ class Galaxy
11
+ def self.random( size )
12
+ galaxy = new
13
+
14
+ worm_holes = size / 100
15
+ sectors = [Sector.random]
16
+ sectors.first.name = sectors.size
17
+ link_queue = [sectors.first]
18
+ size -= 1
19
+ until size.zero?
20
+ if link_queue.empty?
21
+ link_queue << sectors.find { |sec| sec.links.size < 5 }
22
+ end
23
+ from = link_queue.shift
24
+ link_count = from.links.size
25
+
26
+ create_count = if size < 5 - link_count
27
+ size
28
+ else
29
+ rand(6 - link_count)
30
+ end
31
+ create_count.times do |num|
32
+ to = Sector.random
33
+ sectors << to
34
+ to.name = sectors.size
35
+ link_queue << to
36
+ from.link(to)
37
+
38
+ size -= 1
39
+ end
40
+ end
41
+ worm_holes.times do
42
+ from = sectors.find { |sec| sec.links.size == 1 } or break
43
+ to = sectors.find do |sec|
44
+ sec.links.size < 5 and not from.links.include?(sec)
45
+ end or break
46
+ from.link(to, true)
47
+ end
48
+ galaxy.instance_eval { @sectors = sectors }
49
+
50
+ galaxy
51
+ end
52
+
53
+ def initialize( )
54
+ @sectors = Hash.new
55
+ end
56
+
57
+ def []( sector )
58
+ @sectors[sector]
59
+ end
60
+
61
+ def size( )
62
+ @sectors.size
63
+ end
64
+ end
@@ -0,0 +1,30 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # planet.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-06-06.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ require "gambit/viewable"
9
+
10
+ class Planet
11
+ include Gambit::Viewable
12
+ register_view(:html, <<-END_HTML.gsub(/^t/, ""))
13
+ <div class="sidebar">
14
+ <img src="<%= @type %>.jpg" width="300" height="300" />
15
+ </div>
16
+ END_HTML
17
+
18
+ def self.random( )
19
+ new( [ :volcanic, :tundra, :oceanic,
20
+ :jungle, :industrial, :barren ][rand(6)] )
21
+ end
22
+
23
+ def initialize( type )
24
+ @name = "Unknown"
25
+ @type = type
26
+ end
27
+
28
+ attr_accessor :name
29
+ attr_reader :type
30
+ end
@@ -0,0 +1,84 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # sector.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-06-06.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ require "planet"
9
+ require "station"
10
+
11
+ class Sector
12
+ include Gambit::Viewable
13
+ register_view(:html, <<-END_HTML.gsub(/^\t/, ""))
14
+ <div class="content">
15
+ <ul>
16
+ % @links.each do |l|
17
+ <li class="sector"><%= l.name %></li>
18
+ % end
19
+ </ul>
20
+ </div>
21
+ END_HTML
22
+
23
+ def self.random( )
24
+ sector = new
25
+
26
+ rand(6).times { sector << Planet.random }
27
+ sector.build_station(Station.random) if rand(2) == 1
28
+ sector.instance_eval do
29
+ @feature = [:none, :nebula, :asteroids, :black_hole][rand(4)]
30
+ end
31
+
32
+ sector
33
+ end
34
+
35
+ def initialize( feature = :none )
36
+ @name = "Unknown"
37
+
38
+ @planets = Array.new
39
+ @station = nil
40
+ @feature = feature
41
+
42
+ @links = Array.new
43
+ end
44
+
45
+ attr_accessor :name
46
+ attr_reader :feature, :links
47
+
48
+ def <<( planet )
49
+ if @planets.size < 5
50
+ @planets << planet
51
+ self
52
+ else
53
+ raise RuntimeError, "Sector is full."
54
+ end
55
+ end
56
+ alias_method :create_planet, :<<
57
+
58
+ def []( index )
59
+ @planets[index]
60
+ end
61
+
62
+ def build_station( station )
63
+ if @station.nil?
64
+ @station = station
65
+ else
66
+ raise RuntimeError, "This sector already has a space station."
67
+ end
68
+ end
69
+
70
+ include Enumerable
71
+
72
+ def each( &block )
73
+ @planets.each(&block)
74
+ end
75
+
76
+ def link( other, one_way = false )
77
+ @links << other
78
+ other.link(self, true) unless one_way
79
+ end
80
+
81
+ def size( )
82
+ @planets.size
83
+ end
84
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # station.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-06-06.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ class Station
9
+ def self.random( )
10
+ new
11
+ end
12
+
13
+ def initialize( )
14
+
15
+ end
16
+
17
+
18
+ end
@@ -0,0 +1,24 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_galaxy.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-06-06.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ require "test/unit"
9
+
10
+ require "galaxy"
11
+ require "sector"
12
+
13
+ class TestGalaxy < Test::Unit::TestCase
14
+ def test_creation
15
+ assert_not_nil(galaxy = Galaxy.new)
16
+ assert_not_nil(galaxy.instance_eval { @sectors[1001] = Sector.new })
17
+ assert_instance_of(Sector, galaxy[1001])
18
+
19
+ assert_not_nil(galaxy = Galaxy.random(10_000))
20
+ assert_instance_of(Galaxy, galaxy)
21
+ assert_equal(10_000, galaxy.size)
22
+ assert_instance_of(Sector, galaxy[rand(10_000)])
23
+ end
24
+ end