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,167 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_board.rb
4
+ #
5
+ # Created by Gregory Brown on 2005-05-29.
6
+ # Copyright 2005 smtose.org. All rights reserved.
7
+
8
+ require "test/unit"
9
+ require "gambit/tools/board"
10
+
11
+ class TestBoard < Test::Unit::TestCase
12
+ def test_bounds
13
+ board = Gambit::Tools::Board.new(10,15)
14
+ assert_equal(10,board.cols)
15
+ assert_equal(15,board.rows)
16
+ end
17
+
18
+ def test_parse_location
19
+ board = Gambit::Tools::Board.new(5,10)
20
+ assert_equal([1,3],board.parse_location("b4"))
21
+ assert_equal([1,3],board.parse_location(1,3))
22
+ end
23
+
24
+ def test_to_chess
25
+ board = Gambit::Tools::Board.new(3,3)
26
+ assert_equal("b4",board.to_chess(1,3))
27
+ end
28
+
29
+ def test_get
30
+ board = Gambit::Tools::Board.new(5,10)
31
+ board["a4"] = "Object1"
32
+ board[2,4] = "Object2"
33
+ assert_equal("Object1",board["a4"])
34
+ assert_equal("Object2",board[2,4])
35
+ end
36
+
37
+ def test_fill
38
+ board = Gambit::Tools::Board.new(2,2,[])
39
+ board["a1"] << "something"
40
+ assert(board["b2"].empty?)
41
+ assert(board["b1"].empty?)
42
+ assert(board["a2"].empty?)
43
+ end
44
+
45
+ def test_valid
46
+ board = Gambit::Tools::Board.new(5,10)
47
+ assert(!board.valid?(-1,3))
48
+ assert(!board.valid?(3,-1))
49
+ assert(!board.valid?(-1,-1))
50
+ assert(!board.valid?(5,2))
51
+ assert(!board.valid?(2,10))
52
+ assert(!board.valid?("f6"))
53
+ assert(!board.valid?("b11"))
54
+ end
55
+
56
+ def test_neighboring_locations
57
+ board = Gambit::Tools::Board.new(3,3)
58
+ actual_neighbors = ["b1","a2", "b2"].sort
59
+ assert_equal(actual_neighbors,
60
+ board.neighboring_locations("a1"))
61
+
62
+ board.include_diagonals = false
63
+ assert_equal(actual_neighbors - ["b2"],
64
+ board.neighboring_locations("a1"))
65
+ end
66
+
67
+ def test_neighbors
68
+ board = Gambit::Tools::Board.new(3,3)
69
+ board["b1"] = "Sample1"
70
+ board["b2"] = "Sample2"
71
+ board["a2"] = "Sample3"
72
+ assert_equal(["Sample1","Sample2","Sample3"].sort,
73
+ board.neighbors("a1").sort)
74
+ board.include_diagonals = false
75
+ assert_equal(["Sample1","Sample3"].sort,
76
+ board.neighbors("a1").sort)
77
+ end
78
+
79
+ def test_move
80
+ board = Gambit::Tools::Board.new(7,3)
81
+ board["b1"] = "Cat"
82
+ board.move("b1","c3")
83
+ assert_equal("Cat",board["c3"])
84
+ assert_equal(nil,board["b1"])
85
+ end
86
+
87
+ def test_each_location
88
+ board = Gambit::Tools::Board.new(3,3)
89
+ expected = %w[a1 b1 c1 a2 b2 c2 a3 b3 c3]
90
+ board.each_location do |loc|
91
+ assert_equal(expected.shift, loc)
92
+ end
93
+ expected = %w[a3 b3 c3 a2 b2 c2 a1 b1 c1]
94
+ board.each_location(true) do |loc|
95
+ assert_equal(expected.shift, loc)
96
+ end
97
+ end
98
+
99
+ def test_each
100
+ board = Gambit::Tools::Board.new(2,2)
101
+ board["a1"] = "Cat"
102
+ board["b1"] = "Bat"
103
+ board["a2"] = "Fat"
104
+ board["b2"] = "Mat"
105
+ expected = %w[Cat Bat Fat Mat]
106
+ board.each do |item|
107
+ assert_equal(expected.shift, item)
108
+ end
109
+ expected = %w[Fat Mat Cat Bat]
110
+ board.each(true) do |item|
111
+ assert_equal(expected.shift, item)
112
+ end
113
+ end
114
+
115
+ def test_each_with_location
116
+ board = Gambit::Tools::Board.new(2,2)
117
+ board["a1"] = "Cat"
118
+ board["b1"] = "Bat"
119
+ board["a2"] = "Fat"
120
+ board["b2"] = "Mat"
121
+ expected_items = %w[Cat Bat Fat Mat]
122
+ expected_loc = %w[a1 b1 a2 b2]
123
+ board.each_with_location do |item,loc|
124
+ assert_equal(expected_items.shift,item)
125
+ assert_equal(expected_loc.shift,loc)
126
+ end
127
+
128
+ expected_items = %w[Fat Mat Cat Bat]
129
+ expected_loc = %w[a2 b2 a1 b1]
130
+ board.each_with_location(true) do |item,loc|
131
+ assert_equal(expected_items.shift,item)
132
+ assert_equal(expected_loc.shift,loc)
133
+ end
134
+ end
135
+
136
+ def test_each_row
137
+ board = Gambit::Tools::Board.new(2,2)
138
+ board["a1"] = "Tree"
139
+ board["b1"] = "Fee"
140
+ board["a2"] = "Bee"
141
+ board["b2"] = "Lee"
142
+ expected = [ %w[Bee Lee],%w[Tree Fee] ]
143
+ board.each_row do |row|
144
+ assert_equal(expected.shift, row)
145
+ end
146
+ expected = [ %w[Tree Fee],%w[Bee Lee] ]
147
+ board.each_row(false) do |row|
148
+ assert_equal(expected.shift, row)
149
+ end
150
+ end
151
+
152
+ def test_each_col
153
+ board = Gambit::Tools::Board.new(2,2)
154
+ board["a1"] = "James"
155
+ board["b1"] = "A"
156
+ board["a2"] = "Is"
157
+ board["b2"] = "Bastard"
158
+ expected = [ %w[James Is], %w[A Bastard] ]
159
+ board.each_col do |col|
160
+ assert_equal(expected.shift, col)
161
+ end
162
+ expected = [ %w[A Bastard], %w[James Is] ]
163
+ board.each_col(true) do |col|
164
+ assert_equal(expected.shift, col)
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,182 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_cards.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-05-30.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ require "test/unit"
9
+
10
+ require "gambit/tools/cards"
11
+
12
+ class TestCards < Test::Unit::TestCase
13
+ class MyCard < Gambit::Tools::Cards::Card
14
+ def initialize( value )
15
+ @value = value
16
+ end
17
+
18
+ attr_reader :value
19
+
20
+ def <=>( other )
21
+ @value <=> other.value
22
+ end
23
+ end
24
+
25
+ def test_card
26
+ assert_not_nil(card1 = Gambit::Tools::Cards::Card.new)
27
+ assert_not_nil(card2 = Gambit::Tools::Cards::Card.new)
28
+ assert_raise(NotImplementedError) { card1 <=> card2 }
29
+ assert_raise(NotImplementedError) { card1 < card2 }
30
+
31
+ assert_not_nil(card1 = MyCard.new(1))
32
+ assert_not_nil(card2 = MyCard.new(10))
33
+ assert_equal(-1, card1 <=> card2)
34
+ assert_equal(true, card1 < card2)
35
+
36
+ assert_equal( Gambit::Tools::Cards::StandardCard.new(:joker, :red),
37
+ Gambit::Tools::Cards::StandardCard.new(:joker, :red) )
38
+ end
39
+
40
+ def test_standard_card
41
+ deck = Array.new
42
+ Gambit::Tools::Cards::StandardCard.use_jokers = false
43
+ Gambit::Tools::Cards::StandardCard.value_order =
44
+ [:ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king]
45
+ Gambit::Tools::Cards::StandardCard.each { |card| deck << card }
46
+ names = Array.new
47
+ %w{Clubs Diamonds Hearts Spades}.each do |suit|
48
+ %w{Ace 2 3 4 5 6 7 8 9 10 Jack Queen King}.each do |value|
49
+ names << "The #{value} of #{suit}"
50
+ end
51
+ end
52
+ deck.each { |card| assert_equal(names.shift, card.name) }
53
+
54
+ %w{Clubs Diamonds Hearts Spades}.each do |suit|
55
+ %w{Ace 2 3 4 5 6 7 8 9 10 Jack Queen King}.each do |value|
56
+ names << "The #{value} of #{suit}"
57
+ end
58
+ end
59
+ deck = deck.sort_by { rand }
60
+ deck.sort!
61
+ deck.each { |card| assert_equal(names.shift, card.name) }
62
+ end
63
+
64
+ def test_jokers
65
+ deck = Array.new
66
+ Gambit::Tools::Cards::StandardCard.use_jokers = true
67
+ Gambit::Tools::Cards::StandardCard.each { |card| deck << card }
68
+ names = Array.new
69
+ %w{Clubs Diamonds Hearts Spades}.each do |suit|
70
+ %w{Ace 2 3 4 5 6 7 8 9 10 Jack Queen King}.each do |value|
71
+ names << "The #{value} of #{suit}"
72
+ end
73
+ end
74
+ names << "The Red Joker" << "The Black Joker"
75
+ deck.each { |card| assert_equal(names.shift, card.name) }
76
+
77
+ Gambit::Tools::Cards::StandardCard.jokers_are_high = false
78
+ Gambit::Tools::Cards::StandardCard.value_order =
79
+ [2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king, :ace]
80
+ names << "The Red Joker" << "The Black Joker"
81
+ %w{Clubs Diamonds Hearts Spades}.each do |suit|
82
+ %w{2 3 4 5 6 7 8 9 10 Jack Queen King Ace}.each do |value|
83
+ names << "The #{value} of #{suit}"
84
+ end
85
+ end
86
+ deck = deck.sort_by { rand }
87
+ deck.sort!
88
+ deck.each { |card| assert_equal(names.shift, card.name) }
89
+ end
90
+
91
+ def test_deck
92
+ Gambit::Tools::Cards::StandardCard.use_jokers = false
93
+ Gambit::Tools::Cards::StandardCard.value_order =
94
+ [:ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king]
95
+ deck = Gambit::Tools::Cards::Deck.new
96
+ assert_equal(52, deck.count)
97
+ names = Array.new
98
+ %w{Clubs Diamonds Hearts Spades}.each do |suit|
99
+ %w{Ace 2 3 4 5 6 7 8 9 10 Jack Queen King}.each do |value|
100
+ names << "The #{value} of #{suit}"
101
+ end
102
+ end
103
+ deck.each { |card| assert_equal(names.shift, card.name) }
104
+
105
+ ordered = deck.map { |card| card.name }
106
+ shuffled = deck.shuffle!.map { |card| card.name }
107
+ assert_not_equal( ordered, shuffled,
108
+ "Testing Shuffle: " +
109
+ "Could fail under insane statistical odds!" )
110
+
111
+ sorted = deck.sort!.map { |card| card.name }
112
+ ordered.each { |name| assert_equal(sorted.shift, name) }
113
+
114
+ cut = deck.cut!(10).map { |card| card.name }
115
+ assert_equal(52, deck.count)
116
+ assert_not_equal(ordered, cut)
117
+ 10.times { ordered << ordered.shift }
118
+ deck.each { |card| assert_equal(ordered.shift, card.name) }
119
+
120
+ card = deck.draw
121
+ assert_equal("The Jack of Clubs", card.name)
122
+ assert_equal(51, deck.count)
123
+ deck.replace(card)
124
+ assert_equal(52, deck.count)
125
+ card = deck.draw
126
+ assert_equal("The Queen of Clubs", card.name)
127
+ assert_equal(51, deck.count)
128
+ deck << card
129
+ assert_equal(52, deck.count)
130
+
131
+ deck.sort!
132
+ hand1 = Array.new
133
+ hand2 = Array.new
134
+ hand3 = Array.new
135
+ deck.deal(hand1, hand2, 2)
136
+ assert_equal( ["The Ace of Clubs", "The 3 of Clubs"],
137
+ hand1.map { |card| card.name } )
138
+ assert_equal( ["The 2 of Clubs", "The 4 of Clubs"],
139
+ hand2.map { |card| card.name } )
140
+ assert_equal(48, deck.count)
141
+ deck.deal(hand3)
142
+ assert_equal(0, deck.count)
143
+ deck.deal(hand3)
144
+ assert_equal(48, hand3.size)
145
+ end
146
+
147
+ def test_hand
148
+ assert_not_nil(hand = Gambit::Tools::Cards::Hand.new)
149
+ assert_same( hand,
150
+ hand << Gambit::Tools::Cards::StandardCard.new(:ace, :spades) )
151
+ assert_same( hand,
152
+ hand << Gambit::Tools::Cards::StandardCard.new(:joker, :red) )
153
+ assert_same( hand,
154
+ hand << Gambit::Tools::Cards::StandardCard.new(3, :clubs) )
155
+ assert_equal("The Ace of Spades", hand[0].name)
156
+ assert_same(hand, hand.sort!)
157
+ assert_equal("The 3 of Clubs", hand[0].name)
158
+
159
+ assert_equal("The Red Joker", hand.discard(-1).name)
160
+ assert_equal(2, hand.count)
161
+
162
+ names = ["The 3 of Clubs", "The Ace of Spades"]
163
+ hand.each { |card| assert_equal(names.shift, card.name) }
164
+ end
165
+
166
+ def test_pile
167
+ assert_not_nil(pile = Gambit::Tools::Cards::Pile.new)
168
+ assert_same( pile,
169
+ pile << Gambit::Tools::Cards::StandardCard.new(:ace, :spades) )
170
+ assert_same( pile,
171
+ pile << Gambit::Tools::Cards::StandardCard.new(:joker, :red) )
172
+ assert_same( pile,
173
+ pile << Gambit::Tools::Cards::StandardCard.new(3, :clubs) )
174
+
175
+ assert_equal("The 3 of Clubs", pile[0].name)
176
+ assert_equal("The Ace of Spades", pile[-1].name)
177
+ assert_equal("The 3 of Clubs", pile.remove.name)
178
+
179
+ assert_equal( "The Red Joker",
180
+ pile.find { |card| card.value == :joker }.name )
181
+ end
182
+ end
@@ -0,0 +1,99 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ #tc_currency.rb
4
+ #
5
+ # Created by Gregory Brown on 2005-05-30
6
+ # Copyright 2005 smtose.org. All rights reserved
7
+
8
+ require "test/unit"
9
+ require "gambit/tools/currency"
10
+
11
+ class TestCurrency < Test::Unit::TestCase
12
+ include Gambit::Tools
13
+
14
+ def test_amount_of
15
+ holdings = Hash.new()
16
+ holdings[:gold] = 50
17
+ holdings[:silver] = 50
18
+ holdings[:copper] = 25
19
+
20
+ currency_hold = Currency.new(holdings)
21
+
22
+ assert_equal(50, currency_hold[:gold])
23
+ assert_equal(50, currency_hold[:silver])
24
+ assert_equal(25, currency_hold[:copper])
25
+ end
26
+
27
+ def test_withdraw
28
+ holdings = Hash.new(0)
29
+ holdings[:gold] = 50
30
+ holdings[:silver] = 75
31
+ holdings[:copper] = 25
32
+
33
+ currency_hold = Currency.new(holdings)
34
+
35
+ Currency[:gold] = 1.00
36
+ Currency[:silver] = 0.50
37
+ Currency[:copper] = 0.25
38
+
39
+ assert_equal(25, currency_hold.withdraw(25,:gold))
40
+ assert_equal(25, currency_hold[:gold])
41
+ assert_equal(50, currency_hold.withdraw(25,:gold,:silver))
42
+ assert_equal(0, currency_hold[:gold])
43
+ assert_equal(100, currency_hold.withdraw(50,:silver,:copper))
44
+ end
45
+
46
+ def test_deposit
47
+ currency_hold = Currency.new()
48
+
49
+ Currency[:dollars] = 1
50
+
51
+ assert_equal(100,currency_hold.deposit(100,:dollars))
52
+ assert_equal(100,currency_hold[:dollars])
53
+ end
54
+
55
+ def test_relative_value
56
+ Currency[:dollars] = 1
57
+ Currency[:nickels] = 0.05
58
+ assert_equal(20,Currency.relative_value(:dollars,:nickels))
59
+ end
60
+
61
+ def test_add
62
+ Currency[:gold] = 1
63
+ Currency[:silver] = 0.5
64
+ Currency[:copper] = 2
65
+
66
+ currency_hold = Currency.new({:gold => 100, :silver => 50})
67
+ currency_hold2 = Currency.new({:gold => 25, :copper => 30})
68
+ currency_hold += currency_hold2
69
+ assert_equal( [125, 50, 30], [ currency_hold[:gold],
70
+ currency_hold[:silver],
71
+ currency_hold[:copper] ] )
72
+ end
73
+
74
+ def test_subtract
75
+ Currency[:gold] = 1
76
+ Currency[:silver] = 0.5
77
+ Currency[:copper] = 2
78
+
79
+ currency_hold = Currency.new({:gold => 100, :silver => 50})
80
+ currency_hold2 = Currency.new({:gold => 50, :silver => 10})
81
+ currency_hold -= currency_hold2
82
+ assert_equal( [50,40], [ currency_hold[:gold],
83
+ currency_hold[:silver] ] )
84
+ end
85
+
86
+ def test_each
87
+ Currency[:gold] = 1
88
+ Currency[:silver] = 2
89
+ Currency[:copper] = 5
90
+
91
+ data = { :gold => 500, :silver => 200, :copper => 50 }
92
+
93
+ currency_hold = Currency.new(data)
94
+
95
+ currency_hold.each do |resource, amount|
96
+ assert_equal(data[resource],amount)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,83 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_dice.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-02-12.
6
+ # Copyright 2005 Gray Productions. All rights reserved.
7
+
8
+ require "test/unit"
9
+ require "gambit/tools/dice"
10
+
11
+ class TestDice < Test::Unit::TestCase
12
+ def test_rolls
13
+ assert_not_nil(roll = Gambit::Tools::Dice.new(6))
14
+ assert((1..6).to_a.include?(roll.sum))
15
+
16
+ assert_not_nil(roll = Gambit::Tools::Dice.new(3, 6))
17
+ assert(roll.sum >= 3)
18
+ assert(roll.sum <= 18)
19
+ roll.each do |die|
20
+ assert(die >= 1)
21
+ assert(die <= 6)
22
+ end
23
+
24
+ assert_not_nil(roll = Gambit::Tools::Dice.new(2, 100))
25
+ assert((2..200).to_a.include?(roll.sum))
26
+ assert_not_nil(roll = Gambit::Tools::Dice.new(5, 4))
27
+ assert((5..20).to_a.include?(roll.sum))
28
+ end
29
+
30
+ def test_reroll
31
+ assert_not_nil(yahtzee = Gambit::Tools::Dice.new(5, 6))
32
+
33
+ save = yahtzee[0]
34
+ others = [ ]
35
+ yahtzee.each_with_index { |die, i| others << i if die != save }
36
+ count = yahtzee.sum(save)
37
+
38
+ assert_equal(save * (5 - others.size), count)
39
+ assert_equal((5 - others.size), yahtzee.count(save))
40
+ assert_same(yahtzee, yahtzee.reroll(*others))
41
+ assert(yahtzee.sum(save) >= count)
42
+
43
+ count = yahtzee.sum(save)
44
+
45
+ assert_same(yahtzee, yahtzee.reroll { |die| die != save })
46
+ assert(yahtzee.sum(save) >= count)
47
+ end
48
+
49
+ def test_success
50
+ assert_not_nil(roll = Gambit::Tools::Dice.new(7, 10))
51
+
52
+ assert_equal( roll.sum(7) / 7 + roll.sum(8) / 8 +
53
+ roll.sum(9) / 9 + roll.sum(10) / 10, roll.success(7) )
54
+ assert_equal(roll.sum(1), roll.fail(1))
55
+ end
56
+
57
+ def test_matches
58
+ assert_not_nil(roll = Gambit::Tools::Dice.new)
59
+
60
+ roll.instance_eval { @roll = [2, 2, 5, 5, 5] }
61
+ assert(roll.matches?(*%w{x x x y y}))
62
+
63
+ roll.instance_eval { @roll = [2, 5, 3, 2, 4] }
64
+ assert(roll.matches?(1, 2, 3, 4))
65
+ end
66
+
67
+ def test_top_level
68
+ assert_not_nil(roll = 3.d(6))
69
+ assert_instance_of(Gambit::Tools::Dice, roll)
70
+ assert(roll.sum >= 3)
71
+ assert(roll.sum <= 18)
72
+
73
+ assert_not_nil(total = 3.d(6) + 5)
74
+ assert_instance_of(Fixnum, total)
75
+ assert(total >= 8)
76
+ assert(total <= 23)
77
+
78
+ assert_not_nil(total = 5 + 3.d(6))
79
+ assert_instance_of(Fixnum, total)
80
+ assert(total >= 8)
81
+ assert(total <= 23)
82
+ end
83
+ end