Vendily

blackjack

Jul 27th, 2018
155
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BLACKJACK_MAX_BET=1000
  2. BLACKJACK_MIN_BET=100
  3. BLACKJACK_DEALER_AI=1
  4. BLACKJACK_DEALER_RISK=20
  5. BLACKJACK_DEALER_HIT=16
  6.  
  7. class Game_Deck
  8. #--------------------------------------------------------------------------
  9. # * Constants
  10. #--------------------------------------------------------------------------
  11. SUITS=["spades", "hearts", "diamonds", "clubs"]
  12. VALUES=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
  13. #--------------------------------------------------------------------------
  14. # * Object Initialization
  15. #--------------------------------------------------------------------------
  16. def initialize(num_decks = 1)
  17. # Setup Cards
  18. @cards = []
  19. # Setup Deck
  20. @deck = []
  21. # Setup Discarded
  22. @discard = []
  23. # Setup In Play
  24. @in_play = []
  25. # Setup Number of Decks
  26. @num_decks = num_decks
  27. # Setup Cards
  28. setup_cards
  29. # Shuffle
  30. shuffle
  31. end
  32. #--------------------------------------------------------------------------
  33. # * Setup Cards
  34. #--------------------------------------------------------------------------
  35. def setup_cards
  36. # Run Through number of decks times
  37. @num_decks.times do
  38. # Run Through Each Suit
  39. SUITS.each do |suit|
  40. # Run Through each value
  41. VALUES.each do |number|
  42. # Setup Card
  43. card = Game_Card.new(suit, number)
  44. # Push into cards array
  45. @deck.push(card)
  46. end
  47. end
  48. end
  49. end
  50. #--------------------------------------------------------------------------
  51. # * Shuffle
  52. #--------------------------------------------------------------------------
  53. def shuffle
  54. # Setup local variable cards
  55. cards = []
  56. # Clear Discarded
  57. @discard.clear
  58. # Setup Cards to Deck
  59. @cards = [@deck].flatten!
  60. # Delete Cards already out
  61. delete_in_use_cards
  62. # Run through until cards are empty
  63. until @cards.empty?
  64. # Push a Random Card and Delete
  65. cards.push(@cards.delete_at(rand(@cards.size)))
  66. end
  67. # Set Cards to Cards
  68. @cards = cards
  69. end
  70. #--------------------------------------------------------------------------
  71. # * Draw
  72. #--------------------------------------------------------------------------
  73. def draw(number = 1)
  74. # Return if number is 1
  75. if number == 1
  76. # Shuffle if empty?
  77. shuffle if @cards.empty?
  78. # Get Card
  79. card = @cards.shift
  80. # Add Card to in play
  81. @in_play.push(card)
  82. # Return card
  83. return card
  84. end
  85. # Setup Array
  86. drawn = []
  87. # Run Through number of times
  88. number.times do
  89. # Shuffle if empty?
  90. shuffle if @cards.empty?
  91. # Break if cards are still empty (all in play)
  92. break if @cards.empty?
  93. # Get Card
  94. card = @cards.shift
  95. # Add Card to in play
  96. @in_play.push(card)
  97. # Add to Drawn
  98. drawn.push(card)
  99. end
  100. # Return Drawn Cards
  101. return drawn
  102. end
  103. #--------------------------------------------------------------------------
  104. # * Discard In Play
  105. #--------------------------------------------------------------------------
  106. def discard
  107. # Add all in play cards to discard
  108. @discard += @in_play
  109. # Clear in play
  110. @in_play.clear
  111. end
  112. #--------------------------------------------------------------------------
  113. # * Number
  114. #--------------------------------------------------------------------------
  115. def number(*strings)
  116. # Setup Local Variable
  117. number = 0
  118. # Run Through each card
  119. @cards.each do |card|
  120. # Run Through Each String Sent to this method
  121. strings.each do |string|
  122. # If A Suit
  123. if SUITS.include?(string)
  124. # Increase By 1 if suits are equal
  125. number += 1 if card.suit == string
  126. # If A Value
  127. elsif VALUES.include?(string)
  128. # Increase By 1 if values are equal
  129. number += 1 if card.value == string
  130. end
  131. end
  132. end
  133. return number
  134. end
  135. #--------------------------------------------------------------------------
  136. # * Size
  137. #--------------------------------------------------------------------------
  138. def size
  139. return @cards.size
  140. end
  141. #--------------------------------------------------------------------------
  142. # * Delete Cards In Use
  143. #--------------------------------------------------------------------------
  144. def delete_in_use_cards
  145. # Return if empty cards
  146. return if @in_play.empty?
  147. # Run Through each in play card and Delete Card from list
  148. @in_play.each {|card| @cards.delete(card)}
  149. # Clear In Play
  150. @in_play.clear
  151. end
  152. end
  153.  
  154. class Game_Card
  155. #--------------------------------------------------------------------------
  156. # * Public Instance Variables
  157. #--------------------------------------------------------------------------
  158. attr_reader :number
  159. attr_reader :suit
  160. attr_reader :value
  161. #--------------------------------------------------------------------------
  162. # * Object Initialization
  163. #--------------------------------------------------------------------------
  164. def initialize(suit, value, face10 = false, ace11 = false)
  165. @suit = suit
  166. @value = value
  167. @face10 = face10
  168. @ace11 = ace11
  169. @flipped = true
  170. setup_number
  171. end
  172. #--------------------------------------------------------------------------
  173. # * Ace?
  174. #--------------------------------------------------------------------------
  175. def ace?
  176. return @value == 'A'
  177. end
  178. #--------------------------------------------------------------------------
  179. # * King?
  180. #--------------------------------------------------------------------------
  181. def king?
  182. return @value == 'K'
  183. end
  184. #--------------------------------------------------------------------------
  185. # * Queen?
  186. #--------------------------------------------------------------------------
  187. def queen?
  188. return @value == 'Q'
  189. end
  190. #--------------------------------------------------------------------------
  191. # * Jack?
  192. #--------------------------------------------------------------------------
  193. def jack?
  194. return @value == 'J'
  195. end
  196. #--------------------------------------------------------------------------
  197. # * Face?
  198. #--------------------------------------------------------------------------
  199. def face?
  200. return ['J','Q','K'].include?(@value)
  201. end
  202. #--------------------------------------------------------------------------
  203. # * Ten?
  204. #--------------------------------------------------------------------------
  205. def ten?
  206. return @value == '10' || (self.face? and @face10)
  207. end
  208.  
  209. def flipped
  210. return @flipped
  211. end
  212.  
  213. def flipped=(value)
  214. @flipped=value
  215. end
  216. #--------------------------------------------------------------------------
  217. # * To String (Returns card's value)
  218. #--------------------------------------------------------------------------
  219. def to_s
  220. return self.value
  221. end
  222. #--------------------------------------------------------------------------
  223. # * Setup Number (Private)
  224. #--------------------------------------------------------------------------
  225. def setup_number
  226. case @value
  227. when 'A'
  228. @number = @ace11 ? 11 : 1
  229. when 'J'
  230. @number = @face10 ? 10 : 11
  231. when 'Q'
  232. @number = @face10 ? 10 : 12
  233. when 'K'
  234. @number = @face10 ? 10 : 13
  235. else
  236. @number = @value.to_i
  237. end
  238. end
  239.  
  240. def self.createBack
  241. return _INTL("Graphics/Pictures/Cards/Backs/Pokemon")
  242. end
  243.  
  244. def createBitmap
  245. if !@flipped
  246. return Game_Card.createBack
  247. end
  248. return sprintf("Graphics/Pictures/Cards/Normal/%s_%s",@suit,@value)
  249. end
  250. end
  251.  
  252. class Game_CardHand
  253. #--------------------------------------------------------------------------
  254. # * Object Initialization
  255. #--------------------------------------------------------------------------
  256. def initialize
  257. # Setup Cards
  258. @cards = []
  259. end
  260. #--------------------------------------------------------------------------
  261. # * Each (Runs Through Each Card in Hand)
  262. #--------------------------------------------------------------------------
  263. def each
  264. @cards.each {|card| yield(card)}
  265. end
  266. #--------------------------------------------------------------------------
  267. # * Cards
  268. #--------------------------------------------------------------------------
  269. def cards
  270. return @cards.size
  271. end
  272. #--------------------------------------------------------------------------
  273. # * Draw
  274. #--------------------------------------------------------------------------
  275. def draw(*args)
  276. args.flatten!
  277. @cards.push(*args)
  278. @cards.each_with_index do |card, index|
  279. # Set Flipped Flag
  280. card.flipped = index != 0
  281. end
  282. end
  283. #--------------------------------------------------------------------------
  284. # * Discard
  285. #--------------------------------------------------------------------------
  286. def discard
  287. @cards.clear
  288. end
  289. #--------------------------------------------------------------------------
  290. # * To String (ex. A234K)
  291. #--------------------------------------------------------------------------
  292. def to_s
  293. string = ''
  294. @cards.each {|card| string += card.to_s}
  295. return string
  296. end
  297. end
  298.  
  299. class Game_BlackJackHand < Game_CardHand
  300. #--------------------------------------------------------------------------
  301. # * Bust?
  302. #--------------------------------------------------------------------------
  303. def bust?
  304. return self.total > 21
  305. end
  306. #--------------------------------------------------------------------------
  307. # * Black Jack
  308. #--------------------------------------------------------------------------
  309. def blackjack?
  310. return ((@cards[0].ace? && @cards[1].ten?) || (@cards[1].ace? &&
  311. @cards[0].ten?)) && @cards.size == 2
  312. end
  313. #--------------------------------------------------------------------------
  314. # * Double?
  315. #--------------------------------------------------------------------------
  316. def double?
  317. return @cards.size == 2
  318. end
  319. #--------------------------------------------------------------------------
  320. # * Split
  321. #--------------------------------------------------------------------------
  322. def split?
  323. return @cards[0].value == @cards[1].value && @cards.size == 2
  324. end
  325. #--------------------------------------------------------------------------
  326. # * Aces
  327. #--------------------------------------------------------------------------
  328. def aces
  329. num = 0
  330. @cards.each {|card| num += 1 if card.ace?}
  331. return num
  332. end
  333. #--------------------------------------------------------------------------
  334. # * Total
  335. #--------------------------------------------------------------------------
  336. def total(ace1 = false, countflipped=false)
  337. # Setup Number
  338. num = 0
  339. # Add Up
  340. each {|card| num += card.number if card.flipped or countflipped}
  341. # Decrease ace value to 1 if over 21
  342. self.aces.times { num -= 10 if num > 21 || ace1 }
  343. # Return total
  344. return num
  345. end
  346. #--------------------------------------------------------------------------
  347. # * Value
  348. #--------------------------------------------------------------------------
  349. def value
  350. return blackjack? ? 'BJ' : bust? ? 'Bust' : total.to_s
  351. end
  352. end
  353.  
  354. class Blackjack_Scene
  355. def update
  356. pbUpdateSpriteHash(@sprites)
  357. pbDrawTable
  358. end
  359.  
  360. def pbStart
  361. # Create sprite hash
  362. @sprites={}
  363. @deck=Game_Deck.new
  364. @playerhand=Game_BlackJackHand.new
  365. @dealerhand=Game_BlackJackHand.new
  366. @bet=0
  367. @phase=0 # 0 = Bet, 1 = Game, 2 = End
  368. @viewport=Viewport.new(0,0,Graphics.width,Graphics.height)
  369. @viewport.z=99999
  370. addBackgroundPlane(@sprites,"bg","Cards/background",@viewport)
  371. @sprites["overlay"]=BitmapSprite.new(Graphics.width,Graphics.height,@viewport)
  372. pbSetSystemFont(@sprites["overlay"].bitmap)
  373. pbDrawTable
  374. pbFadeInAndShow(@sprites) { update }
  375. end
  376.  
  377. def pbReset
  378. if Kernel.pbConfirmMessage(_INTL("Would you like to play again?"))
  379. @deck=Game_Deck.new
  380. @playerhand=Game_BlackJackHand.new
  381. @dealerhand=Game_BlackJackHand.new
  382. @bet=0
  383. @phase=0 # 0 = Bet, 1 = Game
  384. else
  385. @phase=2
  386. end
  387. end
  388.  
  389. def pbDrawTable
  390. overlay=@sprites["overlay"].bitmap
  391. overlay.clear
  392. baseColor=Color.new(248,248,248)
  393. shadowColor=Color.new(104,104,104)
  394. textPositions=[
  395. [_INTL("Dealer's Hand"),38,58,0,baseColor,shadowColor],
  396. [_INTL("{1}",(@dealerhand.value rescue "N/A")),72,88,1,baseColor,shadowColor],
  397. [_INTL("{1}'s Hand",$Trainer.name),38,174,0,baseColor,shadowColor],
  398. [_INTL("{1}",(@playerhand.value rescue "N/A")),72,204,1,baseColor,shadowColor],
  399. [_INTL("BET: {1}",@bet),342,58,0,baseColor,shadowColor],
  400. [_INTL("COINS: {1}",$PokemonGlobal.coins),324,88,0,baseColor,shadowColor]
  401. ]
  402. pbDrawTextPositions(overlay,textPositions)
  403. imagePositions=[]
  404. x=170
  405. for card in @playerhand
  406. imagePositions.push([card.createBitmap,x,174,0,0,-1,-1])
  407. if @playerhand.cards<=3
  408. x+=82
  409. elsif @playerhand.cards<=7
  410. x+=36
  411. else
  412. x+=12
  413. end
  414. end
  415. x=170
  416. for card in @dealerhand
  417. imagePositions.push([card.createBitmap,x,80,0,0,-1,-1])
  418. if @dealerhand.cards<=3
  419. x+=82
  420. elsif @dealerhand.cards<=7
  421. x+=36
  422. else
  423. x+=12
  424. end
  425. end
  426. pbDrawImagePositions(overlay,imagePositions)
  427. end
  428.  
  429. def pbMain
  430. loop do
  431. Graphics.update
  432. Input.update
  433. self.update
  434. case @phase
  435. when 0
  436. params=ChooseNumberParams.new
  437. params.setRange(BLACKJACK_MIN_BET,BLACKJACK_MAX_BET)
  438. params.setInitialValue(BLACKJACK_MIN_BET)
  439. params.setCancelValue(0)
  440. qty=Kernel.pbMessageChooseNumber(
  441. _INTL("How much are you betting?"),params
  442. )
  443. if qty>0
  444. if $PokemonGlobal.coins>qty
  445. @phase=1
  446. @bet=qty
  447. $PokemonGlobal.coins-=@bet
  448. @playerhand.draw(@deck.draw(2))
  449. @playerhand.each{|card| card.flipped=true}
  450. @dealerhand.draw(@deck.draw(2))
  451. else
  452. Kernel.pbMessage(_INTL("You don't have enough to bet that."))
  453. end
  454. else
  455. Kernel.pbMessage(_INTL("Have a good day."))
  456. break
  457. end
  458. when 1
  459. commands=[_INTL("Hit"),_INTL("Stay")]
  460. commands.push(_INTL("Double")) if @playerhand.double? && !($PokemonGlobal.coins < @bet)
  461. action=Kernel.pbShowCommands(nil,commands)
  462. case action
  463. when 0
  464. action_hit
  465. when 1
  466. action_stay
  467. when 2
  468. action_double
  469. end
  470. when 2
  471. break
  472. end
  473. end
  474. end
  475.  
  476. def action_hit
  477. player_draw
  478. if @playerhand.bust?
  479. start_cpu
  480. return
  481. end
  482. end
  483.  
  484. def action_stay
  485. start_cpu
  486. end
  487.  
  488. def action_double
  489. if $PokemonGlobal.coins < @bet
  490. return
  491. end
  492. $PokemonGlobal.coins-=@bet
  493. @bet*=2
  494. player_draw
  495. start_cpu
  496. end
  497.  
  498. def start_cpu
  499. if (@dealerhand.total(false,true) == 21 or @dealerhand.total(false,true) > @playerhand.total or
  500. @playerhand.blackjack? or @playerhand.bust?)
  501. # Start Checking Phase (Dealer Stands)
  502. start_check
  503. # Return
  504. return
  505. end
  506. case BLACKJACK_DEALER_AI
  507. when 0 # Hit 16 Stand 17
  508. # If Less than or equal to 16 Draw Card Else Stand
  509. @dealerhand.total(false,false) <= 16 ? @dealerhand.draw(@deck.draw) : start_check
  510. when 1 # Hit When Advantageous
  511. # Get Total (All Aces are 1)
  512. amount = 21 - @dealerhand.total(true,true)
  513. # If Amount greater than or equal to 10
  514. if amount >= 10
  515. # Always Hit
  516. @dealerhand.draw(@deck.draw)
  517. # Return
  518. return
  519. end
  520. # Create Values
  521. values = ('2'..amount.to_s).to_a << 'A'
  522. # Get Number of Cards then won't make the dealer bust
  523. number = @deck.number(*values)
  524. # Get Size of Deck
  525. size = @deck.size
  526. # Get Chance of Not Busting
  527. if size != 0
  528. chance = number * 100 / size
  529. else
  530. chance = 0
  531. end
  532. # If chance is less than the riskiness or If value is less than minimum
  533. if (100 - chance <= BLACKJACK_DEALER_RISK ||
  534. @dealerhand.total <= BLACKJACK_DEALER_HIT)
  535. # Hit Again
  536. @dealerhand.draw(@deck.draw)
  537. # Return
  538. return
  539. end
  540. # Else Dealer Stands
  541. start_check
  542. end
  543. end
  544.  
  545. def player_draw
  546. @playerhand.draw(@deck.draw)
  547. @playerhand.each{|card| card.flipped=true}
  548. end
  549.  
  550. def start_check
  551. self.update
  552. player, dealer = @playerhand.value, @dealerhand.value
  553. if @playerhand.bust?
  554. # Lose
  555. check_lose
  556. pbReset
  557. return
  558. end
  559. if @dealerhand.blackjack? and not @playerhand.blackjack?
  560. # Lose
  561. check_lose
  562. # Return
  563. pbReset
  564. return
  565. elsif @playerhand.blackjack? and not @dealerhand.blackjack?
  566. # Win
  567. check_win
  568. # Return
  569. pbReset
  570. return
  571. end
  572. if @playerhand.total > @dealerhand.total || @dealerhand.bust?
  573. # Win
  574. check_win
  575. # Return
  576. pbReset
  577. return
  578. # Push
  579. elsif @playerhand.total == @dealerhand.total
  580. # Push
  581. check_push
  582. # Return
  583. pbReset
  584. return
  585. # Lose
  586. else
  587. # Lose
  588. check_lose
  589. # Return
  590. pbReset
  591. return
  592. end
  593. end
  594.  
  595. def check_lose
  596. Kernel.pbMessage(_INTL("\\me[Voltorb Flip Game Over]Oh no! You lost {1} Coins!\\wtnp[50]",@amount))
  597. end
  598.  
  599. def check_win
  600. winnings = (@bet * (@playerhand.blackjack? ? 2.5 : 2)).to_i
  601. Kernel.pbMessage(_INTL("\\me[Voltorb Flip Win]Congratulations! You won {1} Coins!\\wtnp[40]",winnings))
  602. $PokemonGlobal.coins+=winnings
  603. end
  604.  
  605. def check_push
  606. Kernel.pbMessage(_INTL("\\me[013-Gag02]It's a draw. You got a refund!\\wtnp[50]"))
  607. end
  608.  
  609. def pbEnd
  610. pbFadeOutAndHide(@sprites) { update }
  611. pbDisposeSpriteHash(@sprites)
  612. @viewport.dispose
  613. end
  614.  
  615. end
  616. class Blackjack_Screen
  617. def initialize(scene)
  618. @scene=scene
  619. end
  620.  
  621. def pbStartScreen
  622. @scene.pbStart
  623. @scene.pbMain
  624. @scene.pbEnd
  625. end
  626. end
  627.  
  628.  
  629. def pbBlackJack
  630. if hasConst?(PBItems,:COINCASE) && $PokemonBag.pbQuantity(:COINCASE)<=0
  631. Kernel.pbMessage(_INTL("You can't play unless you have a Coin Case."))
  632. elsif $PokemonGlobal.coins==MAXCOINS
  633. Kernel.pbMessage(_INTL("Your Coin Case is full!"))
  634. else
  635. scene=Blackjack_Scene.new
  636. screen=Blackjack_Screen.new(scene)
  637. screen.pbStartScreen
  638. end
  639. end
RAW Paste Data