Queen_Of_Diamonds 0.3-mswin32 → 0.4-mswin32

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.
data/README CHANGED
@@ -1,6 +1,6 @@
1
- Queen Of Diamonds v0.3
1
+ Queen Of Diamonds v0.4
2
2
  Dirk Meijer
3
- 17th April 2006
3
+ 22nd April 2006
4
4
 
5
5
  Queen Of Diamonds is released under:
6
6
  GNU General Public License (GPL) version 3.
@@ -11,11 +11,11 @@ Queen Of Diamonds uses the gosu library v0.62
11
11
  Queen Of Diamonds is a library that makes it easy
12
12
  to create Windows-like card games.
13
13
 
14
- ./samples/blackjack.rb is an example of a cardgame
15
- without graphics (at the moment).
16
-
17
- ./samples/pwnage.rb is an example that does use
14
+ ./samples/blackjack.rb is an example that uses
18
15
  graphics.
19
16
 
17
+ ./samples/blackjack_no_graphics.rb is an example of a cardgame
18
+ without graphics.
19
+
20
20
  Enjoy it!
21
21
  Dirk.
@@ -1,7 +1,7 @@
1
1
  =begin
2
- name: Queen of Diamonds v0.3
2
+ name: Queen of Diamonds v0.4
3
3
  author: Dirk Meijer
4
- date: April 17th 2006
4
+ date: April 22nd 2006
5
5
  description: The graphic part of the app, almost ready!
6
6
  =end
7
7
 
@@ -47,6 +47,7 @@ class Table < Gosu::Window
47
47
  card.zoom_y = 1
48
48
  card.angle = 1
49
49
  card.movements=[]
50
+ card.table=self
50
51
  end
51
52
  true
52
53
  end
@@ -55,12 +56,25 @@ end
55
56
 
56
57
 
57
58
  class Card
58
- attr_accessor(:x, :y, :zoom_x, :zoom_y, :z, :angle, :front, :back, :side, :movements)
59
+ attr_accessor(:x, :y, :zoom_x, :zoom_y, :z, :angle, :front, :back, :side, :movements, :table)
60
+ attr_reader(:showing)
59
61
 
60
62
  def draw
61
63
  @side.draw_rot(@x, @y, @z, @angle, 0.5, 0.5, @zoom_x, @zoom_y)
62
64
  end
63
65
 
66
+ def show
67
+ return false if @showing and @table.visible_cards.include?(self)
68
+ @table.visible_cards<< self
69
+ @showing=true
70
+ end
71
+
72
+ def hide
73
+ return false if !@showing and !@table.visible_cards.include?(self)
74
+ @table.visible_cards.delete(self)
75
+ @showing=false
76
+ end
77
+
64
78
  def action
65
79
  @movements.each do |move|
66
80
  @movements -= [move] if move.step
@@ -83,6 +97,7 @@ end
83
97
 
84
98
 
85
99
  class Slide
100
+ attr_reader :steps, :steps_done
86
101
  def initialize(card, dest_x, dest_y, steps)
87
102
  @card=card
88
103
  @step_x=(dest_x-@card.x)/steps.to_f
@@ -109,6 +124,7 @@ end
109
124
 
110
125
 
111
126
  class Rotation
127
+ attr_reader :steps, :steps_done
112
128
  def initialize(card, dest_angle, steps)
113
129
  @card=card
114
130
  @step_angle=(dest_angle-@card.angle)/steps.to_f
@@ -131,6 +147,7 @@ end
131
147
 
132
148
 
133
149
  class Flip
150
+ attr_reader :steps, :steps_done
134
151
  def initialize(card, x_or_y, steps)
135
152
  @card=card
136
153
  case x_or_y
@@ -1,7 +1,7 @@
1
1
  =begin
2
- name: Queen of Diamonds v0.3
2
+ name: Queen of Diamonds v0.4
3
3
  author: Dirk Meijer
4
- date: April 17th 2006
4
+ date: April 22nd 2006
5
5
  description: The basis of the app, contains the classes.
6
6
  =end
7
7
 
data/samples/blackjack.rb CHANGED
@@ -1,16 +1,13 @@
1
1
  =begin
2
- name: BlackJack v0.2
2
+ name: BlackJack v0.3
3
3
  author: Dirk Meijer
4
- date: January 31st 2006
4
+ date: April 22nd 2006
5
5
  description: A simple BlackJack game using the Queen of Diamonds library,
6
- it uses a simple terminal window, until I have finished the
7
- graphic part. I'm probably about half-way through.
6
+ now with graphics! Enjoy!
8
7
  =end
9
8
 
10
9
  require "Queen_Of_Diamonds"
11
10
 
12
- #addition to the Card class, an ace can only be degraded once,
13
- #otherwise certain situations would keep a player (or dealer) from losing
14
11
  class Card
15
12
  attr_accessor :ace_degraded
16
13
  end
@@ -19,20 +16,20 @@ class Hand
19
16
  def calculate_score
20
17
  score=0
21
18
  each do |card|
22
- if card.value == :A #if it's an ace
23
- score += 11 #add 11 points
19
+ if card.value == :A
20
+ score += 11
24
21
  score -= 10 if card.ace_degraded
25
- elsif (2..10).include?(card.value) #if it's a 2 upto a 10
26
- score += card.value #add that value
27
- else #if it's something else
28
- score += 10 #add 10 points
22
+ elsif (2..10).include?(card.value)
23
+ score += card.value
24
+ else
25
+ score += 10
29
26
  end
30
27
  end
31
- if score>21 #if your hand is more than 21
32
- each do |card| #each card
33
- if card.value == :A and card.ace_degraded != true #if an ace hasn't been degraded yet
34
- score-=10 if score>21 #turn 11 points from ace into 1 point if your score still > 21
35
- card.ace_degraded = true #tell the ace that it has been degraded
28
+ if score>21
29
+ each do |card|
30
+ if card.value == :A and card.ace_degraded != true
31
+ score-=10 if score>21
32
+ card.ace_degraded = true
36
33
  end
37
34
  end
38
35
  end
@@ -41,187 +38,430 @@ class Hand
41
38
  end
42
39
 
43
40
 
44
- #the BlackJack class, which holds all the methods required to play blackjack
45
- class BlackJack
46
- attr_reader :player_score, :dealer_score, :player_hand, :dealer_hand, :winner, :money, :bet
47
-
48
- def initialize(money=50)
49
- @money=money
41
+
42
+ class BlackJackTable < Table
43
+ attr_accessor :status, :next_status
44
+ def initialize(*args)
45
+ super(*args)
46
+ @title=Gosu::Font.new(self,"Times New Roman",50)
47
+ @instruction=Gosu::Font.new(self,"Century Gothic",20)
48
+ @money=500
49
+ @status=:reshuffle
50
50
  end
51
51
 
52
-
53
- #the play methods, call this method to start a game
54
- def play
55
- loop do
56
- @deck = Deck.new.shuffle #create a new (and shuffled) deck
57
- @player_score=0 #your score is nullified
58
- @dealer_score=0
59
- @turn=0
60
- puts "---------------------------\n Money: #{@money}\n How much money would you like to bet?"
61
- bet=gets.to_i
62
- until (1..@money).include?(bet)
63
- puts "---------------------------\n Money: #{@money}\n How much money would you like to bet?"
64
- bet=gets.to_i
65
- end
66
- @bet=bet
67
- @money-=@bet
68
- @player_hand=@deck.stock(2).sort #you get two cards
69
- @player_score=@player_hand.calculate_score
70
- puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
71
- if @player_score==21 #if your score = 21
72
- puts " Blackjack!" #tell player he has a blackjack
73
- @winner=:player #player wins
74
- @money+=(@bet*1.5).to_i
75
- next #tell game to end
52
+ def draw
53
+ @background.draw(0,0,0)
54
+ @title.draw_rel(@text1,320,165,0,0.5,0.5,1,1,0xff000000)
55
+ @title.draw_rel("BlackJack v0.3",320,215,0,0.5,0.5,1,1,0xffffff00)
56
+ @title.draw_rel(@text2,320,265,0,0.5,0.5,1,1,0xff000000)
57
+ @title.draw_rel("Money: #{@money}",120,370,0,0.5,0.5,0.5,0.5,0xff000000)
58
+ @title.draw_rel("Bet: #{@bet}",120,400,0,0.5,0.5,0.5,0.5,0xff000000)
59
+ @title.draw_rel("Score: #{@player_score}",120,430,0,0.5,0.5,0.5,0.5,0xff000000)
60
+ @title.draw_rel("Score: #{@dealer_score}",520,75,0,0.5,0.5,0.5,0.5,0xff000000)
61
+ @visible_cards.reverse.each do |card|
62
+ card.draw
63
+ end
64
+ case @status
65
+ when :choose
66
+ @instruction.draw_rel("Hit - Spacebar",140,60,0,0.5,0.5)
67
+ @instruction.draw_rel("Stand - Escape",140,80,0,0.5,0.5)
68
+ y=100
69
+ if @player_hand.size==2 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet
70
+ @instruction.draw_rel("Double - Home",140,y,0,0.5,0.5)
71
+ y=120
76
72
  end
77
- option= :choose #the first option is choose
78
- loop do #until the game exits
79
- option=__send__(option) #each method will return a symbol that is a method name, which will be evaluated, when the game is supposed to end, this will be :exit
80
- break if option == :play
73
+ if @player_hand.size==2 and @player_hand[0].value == @player_hand[1].value and @money>=@bet
74
+ @instruction.draw_rel("Split - End",140,y,0,0.5,0.5)
75
+ y+=20
81
76
  end
82
- end
83
- end
84
-
85
- private
86
- #choose gives you four options, only the first two currently have effect
87
- def choose
88
- @turn+=1
89
- puts "---------------------------\n Hit/Stand#{"/Double" if @turn==1 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet}#{"/Split" if @turn==1 and @player_hand[0].value == @player_hand[1].value and @money>=@bet}#{"/Surrender" if @turn==1}?" #show options
90
- g=gets #get string
91
- if g=~/^h/i #if string starts with 'h'
92
- return :hit #tell program to hit
93
- elsif g=~/^st/i #if string starts with 'st'
94
- return :dealer_move #tell program your done
95
- elsif g=~/^d/i and @turn==1 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet #if string starts with 'd'
96
- return :double #double
97
- elsif g=~/^sp/i and @turn==1 and @player_hand[0].value == @player_hand[1].value and @money>=@bet #if string starts with 'sp'
98
- return :split #split cards
99
- elsif g=~/^su/i and @turn==1
100
- return :surrender
101
- else #if no option was chosen
102
- @turn-=1
103
- return :choose #choose again
104
- end
105
- end
106
-
107
-
108
- #you get another card
109
- def hit
110
- card=@deck.stock(1) #pop a card from the deck
111
- (@player_hand+=card).sort! #add the popped card to hand
112
- @player_score=@player_hand.calculate_score
113
- puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
114
- if @player_score>21 #if the player's score > 21
115
- puts " Bust!" #tell the player that
116
- @winner=:dealer #dealer wins
117
- return :stop #tell game to end
118
- end
119
- if @player_hand.size==5 #if player has 5 cards
120
- puts " Five cards!" #tell player
121
- @winner=:player #player wins
122
- return :stop #tell game to end
123
- end
124
- return :choose #if the game hasn't ended, tell the game you can choose again
125
- end
126
-
127
-
128
- #the dealers turn
129
- def dealer_move
130
- @dealer_score=0 #dealer score is nullified
131
- @dealer_hand=@deck.stock(2).sort #deal 2 cards
132
- @dealer_score=@dealer_hand.calculate_score
133
- puts "---------------------------\n Dealer's hand:",@dealer_hand," Score: #{@dealer_score}" #show dealer score
134
- if @dealer_score==21 #if blackjack
135
- puts " Blackjack!" #tell player
136
- @winner=:dealer #dealer wins
137
- return :stop #end the game
138
- end
139
- while @dealer_score<@player_score #dealer will continue to hit himself, until his score is no longer less than player score
140
- `pause` #game will pause, so the player can see what's going on
141
- card=@deck.stock(1) #pop a card
142
- @dealer_hand+=card #add the card to dealer hand
143
- @dealer_score=@dealer_hand.calculate_score
144
- puts "---------------------------\n Dealer's hand:",@dealer_hand," Score: #{@dealer_score}" #show dealers score
145
- if @dealer_score>21 #if dealer score exceeds 21
146
- puts " Bust!" #tell player
147
- @winner=:player #player wins
148
- return :stop #end the game
149
- end
150
- if @dealer_hand.size==5 #if dealer holds 5 cards
151
- puts " Five cards!" #tell player
152
- @winner=:dealer #dealer wins
153
- return :stop #end game
77
+ if @player_hand.size==2
78
+ @instruction.draw_rel("Surrender - Delete",140,y,0,0.5,0.5)
154
79
  end
155
- end
156
- @winner=:dealer #when dealer score equal or is more than player score, dealer wins
157
- return :stop #end game
158
- end
159
-
160
-
161
- def double
162
- @money-=@bet
163
- @bet*=2
164
- if @player_hand[0].value==:A
165
- @player_score-=10
166
- @player_hand[0].ace_degraded=true
167
- elsif @player_hand[1].value==:A
168
- @player_score-=10
169
- @player_hand[1].ace_degraded=true
170
- end
171
- card=@deck.stock(1) #pop a card from the deck
172
- (@player_hand+=card).sort! #add the popped card to hand
173
- @player_score=@player_hand.calculate_score
174
- puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
175
- if @player_score>21 #if the player's score > 21
176
- puts " Bust!" #tell the player that
177
- @winner=:dealer #dealer wins
178
- return :stop #tell game to end
179
- end
180
- return :dealer_move
181
- end
182
-
183
-
184
- def split
185
- @money-=@bet
186
- hand1=@player_hand.stock(1)
187
- hand2=@player_hand.stock(1)
188
- [hand1,hand2].each do |hand|
189
- @player_hand=hand
190
- (@player_hand+=@deck.stock(1)).sort! #you get two cards
191
- @player_score=@player_hand.calculate_score
192
- puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
193
- option= :choose #the first option is choose
194
- loop do #until the game exits
195
- option=__send__(option) #each method will return a symbol that is a method name, which will be evaluated, when the game is supposed to end, this will be :exit
196
- break if option == :play
80
+ when :split1
81
+ @instruction.draw_rel("Hit - Spacebar",140,60,0,0.5,0.5)
82
+ @instruction.draw_rel("Stand - Escape",140,80,0,0.5,0.5)
83
+ y=100
84
+ if @player_hand.size==2 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet
85
+ @instruction.draw_rel("Double - Home",140,y,0,0.5,0.5)
86
+ y=120
87
+ end
88
+ if @player_hand.size==2
89
+ @instruction.draw_rel("Surrender - Delete",140,y,0,0.5,0.5)
90
+ end
91
+ when :split2
92
+ @instruction.draw_rel("Hit - Spacebar",140,60,0,0.5,0.5)
93
+ @instruction.draw_rel("Stand - Escape",140,80,0,0.5,0.5)
94
+ y=100
95
+ if @player_hand.size==2 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet
96
+ @instruction.draw_rel("Double - Home",140,y,0,0.5,0.5)
97
+ y=120
197
98
  end
99
+ if @player_hand.size==2
100
+ @instruction.draw_rel("Surrender - Delete",140,y,0,0.5,0.5)
101
+ end
102
+ when :bet
103
+ @instruction.draw_rel("Place your bet!",140,60,0,0.5,0.5)
104
+ @instruction.draw_rel("Use the arrow keys",140,80,0,0.5,0.5)
105
+ @instruction.draw_rel("to change the amount.",140,100,0,0.5,0.5)
106
+ @instruction.draw_rel("Press Return to confirm.",140,120,0,0.5,0.5)
198
107
  end
199
- :play
200
- end
201
-
202
-
203
- def surrender
204
- @money+=(@bet*0.5).to_i
205
- return :play
206
108
  end
207
109
 
208
-
209
- #what to do when the game ends
210
- def stop
211
- puts "---------------------------\n Player's score: #{@player_score}\n Dealer's score: #{@dealer_score}" #show scores
212
- if @winner==:player #tell player who's won
213
- puts " You win!"
214
- @money+=@bet*2
215
- else
216
- puts " You lose!"
217
- end
218
- if @money>0
219
- return :play
220
- else
221
- puts "---------------------------\n Game Over!\n No money left!\n---------------------------\n Blackjack\n Queen of Diamonds\n Dirk Meijer" #give credit to me
222
- return :exit
110
+ def update
111
+ case @status
112
+ when :bet
113
+ if button_down?(Gosu::Button::KbRight)
114
+ @bet+=1 unless @bet==@money
115
+ elsif button_down?(Gosu::Button::KbLeft)
116
+ @bet-=1 unless @bet==0
117
+ elsif button_down?(Gosu::Button::KbUp)
118
+ @bet+=10 unless @bet+10>@money
119
+ elsif button_down?(Gosu::Button::KbDown)
120
+ @bet-=10 unless @bet-10<0
121
+ elsif button_down?(Gosu::Button::KbReturn) and @bet>0 and @bet <= @money
122
+ @money-=@bet
123
+ @hand[0].z=1
124
+ @hand[1].z=2
125
+ @hand[0].new_slide(295,400,20)
126
+ @hand[1].new_slide(310,400,20)
127
+ @hand[0].new_rotation(0,20)
128
+ @hand[1].new_rotation(0,20)
129
+ @hand[0].new_flip(:y,20)
130
+ @hand[1].new_flip(:y,20)
131
+ @player_hand=@hand.stock(2)
132
+ @next_status=:choose
133
+ @status=:action
134
+ end
135
+
136
+ when :choose
137
+ if @player_score==21 and @player_hand.size==2
138
+ @money+=(@bet*1.5).to_i
139
+ @text1="BlackJack!"
140
+ @text2="You Win!"
141
+ @status=:end
142
+ elsif @player_score>21
143
+ @text1="Bust!"
144
+ @text2="You Lose!"
145
+ @status=:end
146
+ elsif @player_hand.size==5
147
+ @money+=@bet*2
148
+ @text1="5 Cards!"
149
+ @text2="You Win!"
150
+ @status=:end
151
+ end
152
+ if button_down?(Gosu::Button::KbDelete) and @player_hand.size==2
153
+ @money+=@bet/2
154
+ @bet=0
155
+ @status=:end
156
+ elsif button_down?(Gosu::Button::KbSpace)
157
+ @hand[0].z=@player_hand.size+1
158
+ @hand[0].new_slide(310+(15*(@player_hand.size-1)),400,20)
159
+ @hand[0].new_rotation(0,20)
160
+ @hand[0].new_flip(:y,20)
161
+ @player_hand+=@hand.stock(1)
162
+ @next_status=:choose
163
+ @status=:action
164
+ elsif button_down?(Gosu::Button::KbEscape)
165
+ @status=:cpu_start
166
+ elsif button_down?(Gosu::Button::KbHome) and @player_hand.size==2 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet
167
+ @money-=@bet
168
+ @bet*=2
169
+ @hand[0].new_slide(335,400,20)
170
+ @hand[0].new_rotation(90,20)
171
+ @hand[0].new_flip(:y,20)
172
+ @hand[0].z=3
173
+ @player_hand+=@hand.stock(1)
174
+ @next_status=:cpu_start
175
+ @status=:action
176
+ elsif button_down?(Gosu::Button::KbEnd) and @player_hand.size==2 and @player_hand[0].value == @player_hand[1].value and @money>=@bet
177
+ @money-=@bet
178
+ @player_hand[1].new_slide(500,400,20)
179
+ @player_hand[1].new_flip(:x,20)
180
+ @hand[0].z=2
181
+ @hand[0].new_slide(310,400,20)
182
+ @hand[0].new_rotation(0,20)
183
+ @hand[0].new_flip(:y,20)
184
+ @split_hand1=@player_hand.stock(1)
185
+ @split_hand1[0].ace_degraded=false
186
+ @split_hand2=@player_hand.stock(1)
187
+ @split_hand2[0].ace_degraded=false
188
+ @split_hand1+=@hand.stock(1)
189
+ @player_hand=@split_hand1
190
+ @status=:action
191
+ @next_status=:split1
192
+ end
193
+
194
+ when :split1
195
+ if @player_score==21 and @player_hand.size==2
196
+ @money+=(@bet*1.5).to_i
197
+ @text1="BlackJack!"
198
+ @text2="You Win!"
199
+ @status=:split1_end
200
+ elsif @player_score>21
201
+ @text1="Bust!"
202
+ @text2="You Lose!"
203
+ @status=:split1_end
204
+ elsif @player_hand.size==5
205
+ @money+=@bet*2
206
+ @text1="5 Cards!"
207
+ @text2="You Win!"
208
+ @status=:split1_end
209
+ end
210
+ if button_down?(Gosu::Button::KbDelete) and @player_hand.size==2
211
+ @money+=@bet/2
212
+ @bet=0
213
+ @status=:split1_end
214
+ elsif button_down?(Gosu::Button::KbSpace)
215
+ @hand[0].z=@player_hand.size+1
216
+ @hand[0].new_slide(310+(15*(@player_hand.size-1)),400,20)
217
+ @hand[0].new_rotation(0,20)
218
+ @hand[0].new_flip(:y,20)
219
+ @player_hand+=@hand.stock(1)
220
+ @next_status=:split1
221
+ @status=:action
222
+ elsif button_down?(Gosu::Button::KbEscape)
223
+ @status=:split1_cpu
224
+ elsif button_down?(Gosu::Button::KbHome) and @player_hand.size==2 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet
225
+ @money-=@bet
226
+ @bet*=2
227
+ @hand[0].new_slide(335,400,20)
228
+ @hand[0].new_rotation(90,20)
229
+ @hand[0].new_flip(:y,20)
230
+ @hand[0].z=3
231
+ @player_hand+=@hand.stock(1)
232
+ @next_status=:split1_cpu
233
+ @status=:action
234
+ end
235
+
236
+ when :split2
237
+ if @player_score==21 and @player_hand.size==2
238
+ @money+=(@bet*1.5).to_i
239
+ @text1="BlackJack!"
240
+ @text2="You Win!"
241
+ @status=:end
242
+ elsif @player_score>21
243
+ @text1="Bust!"
244
+ @text2="You Lose!"
245
+ @status=:end
246
+ elsif @player_hand.size==5
247
+ @money+=@bet*2
248
+ @text1="5 Cards!"
249
+ @text2="You Win!"
250
+ @status=:end
251
+ end
252
+ if button_down?(Gosu::Button::KbDelete) and @player_hand.size==2
253
+ @money+=@bet/2
254
+ @bet=0
255
+ @status=:end
256
+ elsif button_down?(Gosu::Button::KbSpace)
257
+ @hand[0].z=@player_hand.size+1
258
+ @hand[0].new_slide(310+(15*(@player_hand.size-1)),400,20)
259
+ @hand[0].new_rotation(0,20)
260
+ @hand[0].new_flip(:y,20)
261
+ @player_hand+=@hand.stock(1)
262
+ @next_status=:split2
263
+ @status=:action
264
+ elsif button_down?(Gosu::Button::KbEscape)
265
+ @status=:cpu_start
266
+ elsif button_down?(Gosu::Button::KbHome) and @player_hand.size==2 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet
267
+ @money-=@bet
268
+ @bet*=2
269
+ @hand[0].new_slide(335,400,20)
270
+ @hand[0].new_rotation(90,20)
271
+ @hand[0].new_flip(:y,20)
272
+ @hand[0].z=3
273
+ @player_hand+=@hand.stock(1)
274
+ @next_status=:cpu_start
275
+ @status=:action
276
+ end
277
+
278
+ when :split1_end
279
+ @player_hand.each do |card|
280
+ card.new_slide(500,400,20)
281
+ card.new_flip(:x,20)
282
+ end
283
+ if @dealer_hand
284
+ @dealer_hand.each do |card|
285
+ card.new_slide(500,80,20)
286
+ card.new_flip(:x,20)
287
+ end
288
+ end
289
+ @split_hand1=@player_hand
290
+ @split_dealer_hand=@dealer_hand if @dealer_hand
291
+ @player_hand=@split_hand2
292
+ @player_hand[0].z=1
293
+ @player_hand[0].new_slide(295,400,20)
294
+ @player_hand[0].new_rotation(0,20)
295
+ @player_hand[0].new_flip(:y,20)
296
+ @hand[0].z=2
297
+ @hand[0].new_slide(310,400,20)
298
+ @hand[0].new_rotation(0,20)
299
+ @hand[0].new_flip(:y,20)
300
+ @player_hand+=@hand.stock(1)
301
+ @next_status=:split2
302
+ @status=:action
303
+
304
+ when :split1_cpu
305
+ @hand[0].z=1
306
+ @hand[1].z=2
307
+ @hand[0].new_slide(295,80,20)
308
+ @hand[1].new_slide(310,80,20)
309
+ @hand[0].new_rotation(0,20)
310
+ @hand[1].new_rotation(0,20)
311
+ @hand[0].new_flip(:y,20)
312
+ @hand[1].new_flip(:y,20)
313
+ @dealer_score=0
314
+ @split_dealer_hand=@hand.stock(2)
315
+ @dealer_hand=@split_dealer_hand
316
+ @next_status=:split1_cpu_play
317
+ @status=:action
318
+
319
+ when :split1_cpu_play
320
+ if @dealer_score==21 and @dealer_hand.size==2
321
+ @text1="BlackJack!"
322
+ @text2="You Lose!"
323
+ @status=:split1_end
324
+ elsif @dealer_score>21
325
+ @money+=@bet*2
326
+ @text1="Bust!"
327
+ @text2="You Win!"
328
+ @status=:split1_end
329
+ elsif @dealer_hand.size==5
330
+ @text1="5 Cards!"
331
+ @text2="You Lose!"
332
+ @status=:split1_end
333
+ elsif @dealer_score>=@player_score
334
+ @text2="You Lose!"
335
+ @status=:split1_end
336
+ else
337
+ @hand[0].z=@dealer_hand.size+1
338
+ @hand[0].new_slide(310+(15*(@dealer_hand.size-1)),80,20)
339
+ @hand[0].new_rotation(0,20)
340
+ @hand[0].new_flip(:y,20)
341
+ @dealer_hand+=@hand.stock(1)
342
+ @next_status=:split1_cpu_play
343
+ @status=:action
344
+ end
345
+
346
+ when :action
347
+ stage=0
348
+ @visible_cards.each do |card|
349
+ card.action
350
+ card.movements.each do |m|
351
+ stage+=(m.steps-m.steps_done)
352
+ end
353
+ end
354
+ if stage==0
355
+ @status=@next_status
356
+ @player_score=@player_hand.calculate_score
357
+ @dealer_score=@dealer_hand.calculate_score if @dealer_hand
358
+ end
359
+
360
+ when :cpu_start
361
+ @hand[0].z=1
362
+ @hand[1].z=2
363
+ @hand[0].new_slide(295,80,20)
364
+ @hand[1].new_slide(310,80,20)
365
+ @hand[0].new_rotation(0,20)
366
+ @hand[1].new_rotation(0,20)
367
+ @hand[0].new_flip(:y,20)
368
+ @hand[1].new_flip(:y,20)
369
+ @dealer_score=0
370
+ @dealer_hand=@hand.stock(2)
371
+ @next_status=:cpu_play
372
+ @status=:action
373
+
374
+ when :cpu_play
375
+ if @dealer_score==21 and @dealer_hand.size==2
376
+ @text1="BlackJack!"
377
+ @text2="You Lose!"
378
+ @status=:end
379
+ elsif @dealer_score>21
380
+ @money+=@bet*2
381
+ @text1="Bust!"
382
+ @text2="You Win!"
383
+ @status=:end
384
+ elsif @dealer_hand.size==5
385
+ @text1="5 Cards!"
386
+ @text2="You Lose!"
387
+ @status=:end
388
+ elsif @dealer_score>=@player_score
389
+ @text2="You Lose!"
390
+ @status=:end
391
+ else
392
+ @hand[0].z=@dealer_hand.size+1
393
+ @hand[0].new_slide(310+(15*(@dealer_hand.size-1)),80,20)
394
+ @hand[0].new_rotation(0,20)
395
+ @hand[0].new_flip(:y,20)
396
+ @dealer_hand+=@hand.stock(1)
397
+ @next_status=:cpu_play
398
+ @status=:action
399
+ end
400
+
401
+ when :end
402
+ if @player_hand
403
+ @player_hand.each do |card|
404
+ card.new_slide(50,240,40)
405
+ card.new_rotation(rand(20)-10,40)
406
+ card.new_flip(:x,40)
407
+ @hand<<card
408
+ end
409
+ end
410
+ if @dealer_hand and not @split_dealer_hand
411
+ @dealer_hand.each do |card|
412
+ card.new_slide(50,240,40)
413
+ card.new_rotation(rand(20)-10,40)
414
+ card.new_flip(:x,40)
415
+ @hand<<card
416
+ end
417
+ end
418
+ if @split_hand1
419
+ @split_hand1.each do |card|
420
+ card.new_slide(50,240,40)
421
+ card.new_rotation(rand(20)-10,40)
422
+ @hand<<card
423
+ end
424
+ end
425
+ if @split_dealer_hand
426
+ @split_dealer_hand.each do |card|
427
+ card.new_slide(50,240,40)
428
+ card.new_rotation(rand(20)-10,40)
429
+ @hand<<card
430
+ end
431
+ end
432
+ @hand.shuffle!
433
+ @text2="Game Over!" if @money==0
434
+ @next_status=:reshuffle
435
+ @status=:action
436
+
437
+ when :reshuffle
438
+ @bet=0
439
+ @hand.each do |card|
440
+ card.hide
441
+ card.side=card.back
442
+ card.z=0
443
+ card.x=50
444
+ card.y=240
445
+ card.angle=rand(20)-10
446
+ card.ace_degraded=false
447
+ card.show
448
+ end
449
+ @status=:bet
450
+ @text1=""
451
+ @text2=""
452
+ @player_hand=nil
453
+ @dealer_hand=nil
454
+ @split_hand1=nil
455
+ @split_hand2=nil
456
+ @player_score=nil
457
+ @dealer_score=nil
458
+ self.close if @money==0
223
459
  end
224
460
  end
225
461
  end
226
462
 
227
- BlackJack.new(5000).play #call the game!!
463
+
464
+
465
+ table=BlackJackTable.new(Deck.new.shuffle,640,480,false,20,"BlackJack")
466
+
467
+ table.show
@@ -0,0 +1,227 @@
1
+ =begin
2
+ name: BlackJack v0.2
3
+ author: Dirk Meijer
4
+ date: January 31st 2006
5
+ description: A simple BlackJack game using the Queen of Diamonds library,
6
+ it uses a simple terminal window, until I have finished the
7
+ graphic part. I'm probably about half-way through.
8
+ =end
9
+
10
+ require "Queen_Of_Diamonds_no_graphics"
11
+
12
+ #addition to the Card class, an ace can only be degraded once,
13
+ #otherwise certain situations would keep a player (or dealer) from losing
14
+ class Card
15
+ attr_accessor :ace_degraded
16
+ end
17
+
18
+ class Hand
19
+ def calculate_score
20
+ score=0
21
+ each do |card|
22
+ if card.value == :A #if it's an ace
23
+ score += 11 #add 11 points
24
+ score -= 10 if card.ace_degraded
25
+ elsif (2..10).include?(card.value) #if it's a 2 upto a 10
26
+ score += card.value #add that value
27
+ else #if it's something else
28
+ score += 10 #add 10 points
29
+ end
30
+ end
31
+ if score>21 #if your hand is more than 21
32
+ each do |card| #each card
33
+ if card.value == :A and card.ace_degraded != true #if an ace hasn't been degraded yet
34
+ score-=10 if score>21 #turn 11 points from ace into 1 point if your score still > 21
35
+ card.ace_degraded = true #tell the ace that it has been degraded
36
+ end
37
+ end
38
+ end
39
+ score
40
+ end
41
+ end
42
+
43
+
44
+ #the BlackJack class, which holds all the methods required to play blackjack
45
+ class BlackJack
46
+ attr_reader :player_score, :dealer_score, :player_hand, :dealer_hand, :winner, :money, :bet
47
+
48
+ def initialize(money=50)
49
+ @money=money
50
+ end
51
+
52
+
53
+ #the play methods, call this method to start a game
54
+ def play
55
+ loop do
56
+ @deck = Deck.new.shuffle #create a new (and shuffled) deck
57
+ @player_score=0 #your score is nullified
58
+ @dealer_score=0
59
+ @turn=0
60
+ puts "---------------------------\n Money: #{@money}\n How much money would you like to bet?"
61
+ bet=gets.to_i
62
+ until (1..@money).include?(bet)
63
+ puts "---------------------------\n Money: #{@money}\n How much money would you like to bet?"
64
+ bet=gets.to_i
65
+ end
66
+ @bet=bet
67
+ @money-=@bet
68
+ @player_hand=@deck.stock(2).sort #you get two cards
69
+ @player_score=@player_hand.calculate_score
70
+ puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
71
+ if @player_score==21 #if your score = 21
72
+ puts " Blackjack!" #tell player he has a blackjack
73
+ @winner=:player #player wins
74
+ @money+=(@bet*1.5).to_i
75
+ next #tell game to end
76
+ end
77
+ option= :choose #the first option is choose
78
+ loop do #until the game exits
79
+ option=__send__(option) #each method will return a symbol that is a method name, which will be evaluated, when the game is supposed to end, this will be :exit
80
+ break if option == :play
81
+ end
82
+ end
83
+ end
84
+
85
+ private
86
+ #choose gives you four options, only the first two currently have effect
87
+ def choose
88
+ @turn+=1
89
+ puts "---------------------------\n Hit/Stand#{"/Double" if @turn==1 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet}#{"/Split" if @turn==1 and @player_hand[0].value == @player_hand[1].value and @money>=@bet}#{"/Surrender" if @turn==1}?" #show options
90
+ g=gets #get string
91
+ if g=~/^h/i #if string starts with 'h'
92
+ return :hit #tell program to hit
93
+ elsif g=~/^st/i #if string starts with 'st'
94
+ return :dealer_move #tell program your done
95
+ elsif g=~/^d/i and @turn==1 and ((9..11).include?(@player_score) or ([19,20].include?(@player_score) and (@player_hand[0].value==:A or @player_hand[1].value==:A))) and @money>=@bet #if string starts with 'd'
96
+ return :double #double
97
+ elsif g=~/^sp/i and @turn==1 and @player_hand[0].value == @player_hand[1].value and @money>=@bet #if string starts with 'sp'
98
+ return :split #split cards
99
+ elsif g=~/^su/i and @turn==1
100
+ return :surrender
101
+ else #if no option was chosen
102
+ @turn-=1
103
+ return :choose #choose again
104
+ end
105
+ end
106
+
107
+
108
+ #you get another card
109
+ def hit
110
+ card=@deck.stock(1) #pop a card from the deck
111
+ (@player_hand+=card).sort! #add the popped card to hand
112
+ @player_score=@player_hand.calculate_score
113
+ puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
114
+ if @player_score>21 #if the player's score > 21
115
+ puts " Bust!" #tell the player that
116
+ @winner=:dealer #dealer wins
117
+ return :stop #tell game to end
118
+ end
119
+ if @player_hand.size==5 #if player has 5 cards
120
+ puts " Five cards!" #tell player
121
+ @winner=:player #player wins
122
+ return :stop #tell game to end
123
+ end
124
+ return :choose #if the game hasn't ended, tell the game you can choose again
125
+ end
126
+
127
+
128
+ #the dealers turn
129
+ def dealer_move
130
+ @dealer_score=0 #dealer score is nullified
131
+ @dealer_hand=@deck.stock(2).sort #deal 2 cards
132
+ @dealer_score=@dealer_hand.calculate_score
133
+ puts "---------------------------\n Dealer's hand:",@dealer_hand," Score: #{@dealer_score}" #show dealer score
134
+ if @dealer_score==21 #if blackjack
135
+ puts " Blackjack!" #tell player
136
+ @winner=:dealer #dealer wins
137
+ return :stop #end the game
138
+ end
139
+ while @dealer_score<@player_score #dealer will continue to hit himself, until his score is no longer less than player score
140
+ `pause` #game will pause, so the player can see what's going on
141
+ card=@deck.stock(1) #pop a card
142
+ @dealer_hand+=card #add the card to dealer hand
143
+ @dealer_score=@dealer_hand.calculate_score
144
+ puts "---------------------------\n Dealer's hand:",@dealer_hand," Score: #{@dealer_score}" #show dealers score
145
+ if @dealer_score>21 #if dealer score exceeds 21
146
+ puts " Bust!" #tell player
147
+ @winner=:player #player wins
148
+ return :stop #end the game
149
+ end
150
+ if @dealer_hand.size==5 #if dealer holds 5 cards
151
+ puts " Five cards!" #tell player
152
+ @winner=:dealer #dealer wins
153
+ return :stop #end game
154
+ end
155
+ end
156
+ @winner=:dealer #when dealer score equal or is more than player score, dealer wins
157
+ return :stop #end game
158
+ end
159
+
160
+
161
+ def double
162
+ @money-=@bet
163
+ @bet*=2
164
+ if @player_hand[0].value==:A
165
+ @player_score-=10
166
+ @player_hand[0].ace_degraded=true
167
+ elsif @player_hand[1].value==:A
168
+ @player_score-=10
169
+ @player_hand[1].ace_degraded=true
170
+ end
171
+ card=@deck.stock(1) #pop a card from the deck
172
+ (@player_hand+=card).sort! #add the popped card to hand
173
+ @player_score=@player_hand.calculate_score
174
+ puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
175
+ if @player_score>21 #if the player's score > 21
176
+ puts " Bust!" #tell the player that
177
+ @winner=:dealer #dealer wins
178
+ return :stop #tell game to end
179
+ end
180
+ return :dealer_move
181
+ end
182
+
183
+
184
+ def split
185
+ @money-=@bet
186
+ hand1=@player_hand.stock(1)
187
+ hand2=@player_hand.stock(1)
188
+ [hand1,hand2].each do |hand|
189
+ @player_hand=hand
190
+ (@player_hand+=@deck.stock(1)).sort! #you get two cards
191
+ @player_score=@player_hand.calculate_score
192
+ puts "---------------------------\n Player's hand:",@player_hand," Score: #{@player_score}" #show your score
193
+ option= :choose #the first option is choose
194
+ loop do #until the game exits
195
+ option=__send__(option) #each method will return a symbol that is a method name, which will be evaluated, when the game is supposed to end, this will be :exit
196
+ break if option == :play
197
+ end
198
+ end
199
+ :play
200
+ end
201
+
202
+
203
+ def surrender
204
+ @money+=(@bet*0.5).to_i
205
+ return :play
206
+ end
207
+
208
+
209
+ #what to do when the game ends
210
+ def stop
211
+ puts "---------------------------\n Player's score: #{@player_score}\n Dealer's score: #{@dealer_score}" #show scores
212
+ if @winner==:player #tell player who's won
213
+ puts " You win!"
214
+ @money+=@bet*2
215
+ else
216
+ puts " You lose!"
217
+ end
218
+ if @money>0
219
+ return :play
220
+ else
221
+ puts "---------------------------\n Game Over!\n No money left!\n---------------------------\n Blackjack\n Queen of Diamonds\n Dirk Meijer" #give credit to me
222
+ return :exit
223
+ end
224
+ end
225
+ end
226
+
227
+ BlackJack.new(5000).play #call the game!!
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: Queen_Of_Diamonds
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.3"
7
- date: 2006-04-17 00:00:00 +02:00
6
+ version: "0.4"
7
+ date: 2006-04-22 00:00:00 +02:00
8
8
  summary: A library for creating Windows-like card games.
9
9
  require_paths:
10
10
  - lib
@@ -32,7 +32,6 @@ files:
32
32
  - lib
33
33
  - README
34
34
  - samples
35
- - tests
36
35
  - images/back.png
37
36
  - images/c10.png
38
37
  - images/c2.png
@@ -96,6 +95,7 @@ files:
96
95
  - lib/gosu/msvcr71.dll
97
96
  - lib/gosu/unicows.dll
98
97
  - samples/blackjack.rb
98
+ - samples/blackjack_no_graphics.rb
99
99
  - samples/pwnage.rb
100
100
  test_files: []
101
101