Advertisement
leequangson

[RMXP] Blackjack

Jun 10th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 49.44 KB | None | 0 0
  1. module BlackJack
  2.   #--------------------------------------------------------------------------
  3.   # * Window_Skin
  4.   #--------------------------------------------------------------------------
  5.   Window_Skin = 'BlackJack'
  6.   #--------------------------------------------------------------------------
  7.   # * Win SE
  8.   #--------------------------------------------------------------------------
  9.   Win = RPG::AudioFile.new('006-System06')
  10.   #--------------------------------------------------------------------------
  11.   # * Lose SE
  12.   #--------------------------------------------------------------------------
  13.   Lose = RPG::AudioFile.new()
  14.   #--------------------------------------------------------------------------
  15.   # * Push SE
  16.   #--------------------------------------------------------------------------
  17.   Push = RPG::AudioFile.new()
  18.   #--------------------------------------------------------------------------
  19.   # * Draw SE
  20.   #--------------------------------------------------------------------------
  21.   Draw = RPG::AudioFile.new('Dealing')
  22.   #--------------------------------------------------------------------------
  23.   # * Flip SE
  24.   #--------------------------------------------------------------------------
  25.   Flip = RPG::AudioFile.new('Flipping')
  26.   #--------------------------------------------------------------------------
  27.   # * Shuffle SE
  28.   #--------------------------------------------------------------------------
  29.   Shuffle = RPG::AudioFile.new('Shuffle')
  30. end
  31.  
  32. class Game_Deck
  33.   #--------------------------------------------------------------------------
  34.   # * Constants
  35.   #--------------------------------------------------------------------------
  36.   Suits = %w( spades hearts diamonds clubs )
  37.   Values = 'A','2','3','4','5','6','7','8','9','10','J','Q','K'
  38.   #--------------------------------------------------------------------------
  39.   # * Object Initialization
  40.   #--------------------------------------------------------------------------
  41.   def initialize(num_decks = 1)
  42.     # Setup Cards
  43.     @cards = []
  44.     # Setup Deck
  45.     @deck = []
  46.     # Setup Discarded
  47.     @discard = []
  48.     # Setup In Play
  49.     @in_play = []
  50.     # Setup Number of Decks
  51.     @num_decks = num_decks
  52.     # Setup Cards
  53.     setup_cards
  54.     # Shuffle
  55.     shuffle
  56.   end
  57.   #--------------------------------------------------------------------------
  58.   # * Setup Cards
  59.   #--------------------------------------------------------------------------
  60.   def setup_cards
  61.     # Run Through number of decks times
  62.     @num_decks.times do
  63.       # Run Through Each Suit
  64.       Suits.each do |suit|
  65.         # Run Through each value
  66.         Values.each do |number|
  67.           # Setup Card
  68.           card = Game_Card.new(suit, number)
  69.           # Push into cards array
  70.           @deck << card
  71.         end
  72.       end
  73.     end
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # * Shuffle
  77.   #--------------------------------------------------------------------------
  78.   def shuffle
  79.     # Setup local variable cards
  80.     cards = []
  81.     # Clear Discarded
  82.     @discard.clear
  83.     # Setup Cards to Deck
  84.     @cards = [@deck].flatten!
  85.     # Delete Cards already out
  86.     delete_in_use_cards
  87.     # Run through until cards are empty
  88.     until @cards.empty?
  89.       # Push a Random Card and Delete
  90.       cards << @cards.delete_at(rand(@cards.size))
  91.     end
  92.     # Set Cards to Cards
  93.     @cards = cards
  94.   end
  95.   #--------------------------------------------------------------------------
  96.   # * Draw
  97.   #--------------------------------------------------------------------------
  98.   def draw(number = 1)
  99.     # Return if number is 1
  100.     if number == 1
  101.       # Shuffle if empty?
  102.       shuffle if @cards.empty?
  103.       # Get Card
  104.       card = @cards.shift
  105.       # Add Card to in play
  106.       @in_play << card
  107.       # Return card
  108.       return card
  109.     end
  110.     # Setup Array
  111.     drawn = []
  112.     # Run Through number of times
  113.     number.times do
  114.       # Shuffle if empty?
  115.       shuffle if @cards.empty?
  116.       # Break if cards are still empty (all in play)
  117.       break if @cards.empty?
  118.       # Get Card
  119.       card = @cards.shift
  120.       # Add Card to in play
  121.       @in_play << card
  122.       # Add to Drawn
  123.       drawn << card
  124.     end
  125.     # Return Drawn Cards
  126.     return drawn
  127.   end
  128.   #--------------------------------------------------------------------------
  129.   # * Discard In Play
  130.   #--------------------------------------------------------------------------
  131.   def discard
  132.     # Add all in play cards to discard
  133.     @discard += @in_play
  134.     # Clear in play
  135.     @in_play.clear
  136.   end
  137.   #--------------------------------------------------------------------------
  138.   # * Number
  139.   #--------------------------------------------------------------------------
  140.   def number(*strings)
  141.     # Setup Local Variable
  142.     number = 0
  143.     # Run Through each card
  144.     @cards.each do |card|
  145.       # Run Through Each String Sent to this method
  146.       strings.each do |string|
  147.         # If A Suit
  148.         if Suits.include?(string)
  149.           # Increase By 1 if suits are equal
  150.           number += 1 if card.suit == string
  151.         # If A Value
  152.         elsif Values.include?(string)
  153.           # Increase By 1 if values are equal
  154.           number += 1 if card.value == string
  155.         end
  156.       end
  157.     end
  158.     return number
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # * Size
  162.   #--------------------------------------------------------------------------
  163.   def size
  164.     return @cards.size
  165.   end
  166.   #--------------------------------------------------------------------------
  167.   # * Delete Cards In Use
  168.   #--------------------------------------------------------------------------
  169.   private
  170.   def delete_in_use_cards
  171.     # Return if empty cards
  172.     return if @in_play.empty?
  173.     # Run Through each in play card and Delete Card from list
  174.     @in_play.each {|card| @cards.delete(card)}
  175.     # Clear In Play
  176.     @in_play.clear
  177.   end
  178. end
  179.  
  180. class Game_Card
  181.   #--------------------------------------------------------------------------
  182.   # * Public Instance Variables
  183.   #--------------------------------------------------------------------------
  184.   attr_reader :number
  185.   attr_reader :suit
  186.   attr_reader :value
  187.   attr_accessor :flipped
  188.   #--------------------------------------------------------------------------
  189.   # * Object Initialization
  190.   #--------------------------------------------------------------------------
  191.   def initialize(suit, value, face10 = false, ace11 = false)
  192.     @suit = suit
  193.     @value = value
  194.     @face10 = face10
  195.     @ace11 = ace11
  196.     @flipped = true
  197.     setup_number
  198.   end
  199.   #--------------------------------------------------------------------------
  200.   # * Ace?
  201.   #--------------------------------------------------------------------------
  202.   def ace?
  203.     return @value == 'A'
  204.   end
  205.   #--------------------------------------------------------------------------
  206.   # * King?
  207.   #--------------------------------------------------------------------------
  208.   def king?
  209.     return @value == 'K'
  210.   end
  211.   #--------------------------------------------------------------------------
  212.   # * Queen?
  213.   #--------------------------------------------------------------------------
  214.   def queen?
  215.     return @value == 'Q'
  216.   end
  217.   #--------------------------------------------------------------------------
  218.   # * Jack?
  219.   #--------------------------------------------------------------------------
  220.   def jack?
  221.     return @value == 'J'
  222.   end
  223.   #--------------------------------------------------------------------------
  224.   # * Face?
  225.   #--------------------------------------------------------------------------
  226.   def face?
  227.     return ['J','Q','K'].include?(@value)
  228.   end
  229.   #--------------------------------------------------------------------------
  230.   # * Ten?
  231.   #--------------------------------------------------------------------------
  232.   def ten?
  233.     return @value == '10' || (self.face? and @face10)
  234.   end
  235.   #--------------------------------------------------------------------------
  236.   # * To String (Returns card's value)
  237.   #--------------------------------------------------------------------------
  238.   def to_s
  239.     return self.value
  240.   end
  241.   #--------------------------------------------------------------------------
  242.   # * Setup Number (Private)
  243.   #--------------------------------------------------------------------------
  244.   private
  245.   def setup_number
  246.     case @value
  247.     when 'A'
  248.       @number = @ace11 ? 11 : 1
  249.     when 'J'
  250.       @number = @face10 ? 10 : 11
  251.     when 'Q'
  252.       @number = @face10 ? 10 : 12
  253.     when 'K'
  254.       @number = @face10 ? 10 : 13
  255.     else
  256.       @number = @value.to_i
  257.     end
  258.   end
  259. end
  260.  
  261. class Game_CardHand
  262.   #--------------------------------------------------------------------------
  263.   # * Object Initialization
  264.   #--------------------------------------------------------------------------
  265.   def initialize
  266.     # Setup Cards
  267.     @cards = []
  268.   end
  269.   #--------------------------------------------------------------------------
  270.   # * Each (Runs Through Each Card in Hand)
  271.   #--------------------------------------------------------------------------
  272.   def each
  273.     @cards.each {|card| yield(card)}
  274.   end
  275.   #--------------------------------------------------------------------------
  276.   # * Cards
  277.   #--------------------------------------------------------------------------
  278.   def cards
  279.     return @cards.size
  280.   end
  281.   #--------------------------------------------------------------------------
  282.   # * Draw
  283.   #--------------------------------------------------------------------------
  284.   def draw(*cards)
  285.     @cards.push(*cards)
  286.   end
  287.   #--------------------------------------------------------------------------
  288.   # * Discard
  289.   #--------------------------------------------------------------------------
  290.   def discard
  291.     @cards.clear
  292.   end
  293.   #--------------------------------------------------------------------------
  294.   # * To String (ex. A234K)
  295.   #--------------------------------------------------------------------------
  296.   def to_s
  297.     string = ''
  298.     @cards.each {|card| string += card.to_s}
  299.     return string
  300.   end
  301. end
  302.  
  303. module RPG
  304. module Cache
  305.   #--------------------------------------------------------------------------
  306.   # * Card
  307.   #--------------------------------------------------------------------------
  308.   def self.card(suit, number)
  309.     style = $game_system.card_style
  310.     self.load_bitmap("Graphics/Cards/#{style}/", "#{suit}_#{number}")
  311.   end
  312.   #--------------------------------------------------------------------------
  313.   # * Card Back
  314.   #--------------------------------------------------------------------------
  315.   def self.card_back(style)
  316.     self.load_bitmap("Graphics/Cards/Backs/", style)
  317.   end
  318. end
  319. end
  320.  
  321. class Sprite_Card < Sprite
  322.   #--------------------------------------------------------------------------
  323.   # * Object Initialization
  324.   #--------------------------------------------------------------------------
  325.   def initialize(card, flipped = true)
  326.     super(nil)
  327.     # Setup Suit and Value
  328.     @suit, @value = card.suit, card.value
  329.     # Setup Flipped Flag
  330.     @flipped = flipped
  331.     # Setup Bitmap
  332.     if @flipped
  333.       # Get Flipped Card
  334.       self.bitmap = RPG::Cache.card(@suit, @value)
  335.     else
  336.       # Get Card Back
  337.       self.bitmap = RPG::Cache.card_back($game_system.card_back_style)
  338.     end
  339.   end
  340.   #--------------------------------------------------------------------------
  341.   # * Flipped
  342.   #--------------------------------------------------------------------------
  343.   def flipped=(bool)
  344.     # Set Flipped Flag
  345.     @flipped = bool
  346.     # Setup Bitmap
  347.     if @flipped
  348.       # Get Flipped Card
  349.       self.bitmap = RPG::Cache.card(@suit, @value)
  350.     else
  351.       # Get Card Back
  352.       self.bitmap = RPG::Cache.card_back($game_system.card_back_style)
  353.     end
  354.   end
  355. end
  356.  
  357. class Game_System
  358.   #--------------------------------------------------------------------------
  359.   # * Public Instance Variables
  360.   #--------------------------------------------------------------------------
  361.   attr_accessor :card_style
  362.   attr_accessor :card_back_style
  363.   attr_accessor :blackjack_min_bet
  364.   attr_accessor :blackjack_max_bet
  365.   attr_accessor :num_decks
  366.   attr_accessor :dealer_ai
  367.   attr_accessor :dealer_risk
  368.   attr_accessor :dealer_min
  369.   attr_accessor :blackjack_audio
  370.   #--------------------------------------------------------------------------
  371.   # * Initialize
  372.   #--------------------------------------------------------------------------
  373.   alias blackjack_initialize initialize
  374.   def initialize
  375.     # The Usual
  376.     blackjack_initialize
  377.     # Setup Card Style
  378.     @card_style = 'Normal'
  379.     # Setup Card Back Style
  380.     @card_back_style = 'DragonKid'
  381.     # Setup Minimum Bet
  382.     @blackjack_min_bet = 100
  383.     # Setup Maximum Bet
  384.     @blackjack_max_bet = 1000
  385.     # Setup Number of Decks
  386.     @num_decks = 1
  387.     # Setup Dealer AI (0: hit on 16 stand on 17 1: hits when possible)
  388.     @dealer_ai = 1
  389.     # Only with @dealer_ai = 1 How Much of a risk the dealer takes
  390.     @dealer_risk = 20
  391.     # Only with @dealer_ai = 1 Dealer will hit if value is less than this
  392.     @dealer_min = 16
  393.     # BGM Used file, volume, pitch
  394.     @blackjack_audio = RPG::AudioFile.new('')
  395.   end
  396. end
  397.  
  398. class Scene_BlackJack
  399.   #--------------------------------------------------------------------------
  400.   # * Main Processing
  401.   #--------------------------------------------------------------------------
  402.   def main
  403.     # Create Background
  404.     @background = Window_Base.new(0,0,640,480)
  405.     # Set Window Skin
  406.     @background.windowskin = RPG::Cache.windowskin(BlackJack::Window_Skin)
  407.     # Set Z
  408.     @background.z = 0
  409.     # Create Betting Window
  410.     @window_bet = Window_BlackJackBet.new
  411.     # Create Hit/Stand/Double/Split Command Window
  412.     @window_command = Window_BlackJackCommand.new
  413.     # Set to Invisible
  414.     @window_command.visible = false
  415.     # Create Dealer Information Window
  416.     @window_dealer = Window_PlayerInfo.new('Dealer\'s Hand')
  417.     # Setup Position
  418.     @window_dealer.x, @window_dealer.y = 32, 32
  419.     # Set To Invisible
  420.     @window_dealer.visible = false
  421.     # Setup PLayer's Information Window
  422.     @window_player = Window_PlayerInfo.new('Player\'s Hand')
  423.     # Setup Position
  424.     @window_player.x, @window_player.y = 32, 320
  425.     # Set To Invisible
  426.     @window_player.visible = false
  427.     # Create Result Window
  428.     @window_result = Window_BlackJackResult.new
  429.     # Create Deck
  430.     @deck = Game_BlackJackDeck.new($game_system.num_decks)
  431.     # Create Player Hand
  432.     @player_hand = Game_BlackJackHand.new
  433.     # Create Player Sprites
  434.     @player_sprites = []
  435.     # Create Dealer Hand
  436.     @dealer_hand = Game_BlackJackHand.new
  437.     # Create Dealer Sprites
  438.     @dealer_sprites = []
  439.     # Save Map BGM
  440.     @map_bgm = $game_system.playing_bgm
  441.     # Play BlackJack BGM
  442.     $game_system.bgm_play($game_system.blackjack_audio)
  443.     # Create Phase Variable
  444.     @phase = 0
  445.     # Create Wait Count
  446.     @wait_count = 0
  447.     # Start Betting
  448.     start_bet
  449.     # Execute transition
  450.     Graphics.transition
  451.     # Main loop
  452.     loop do
  453.       # Update game screen
  454.       Graphics.update
  455.       # Update input information
  456.       Input.update
  457.       # Frame update
  458.       update
  459.       # Abort loop if screen is changed
  460.       if $scene != self
  461.         break
  462.       end
  463.     end
  464.     # Prepare for transition
  465.     Graphics.freeze
  466.     # Dispose
  467.     dispose
  468.     # Play Map BGM
  469.     $game_system.bgm_play(@map_bgm) if @map_bgm != nil
  470.   end
  471.   #--------------------------------------------------------------------------
  472.   # * Dispose
  473.   #--------------------------------------------------------------------------
  474.   def dispose
  475.     # Dispose Objects
  476.     @background.dispose
  477.     @window_player.dispose
  478.     @window_dealer.dispose
  479.     @window_command.dispose
  480.     @window_bet.dispose
  481.     @window_result.dispose
  482.   end
  483.   #--------------------------------------------------------------------------
  484.   # * Frame Update
  485.   #--------------------------------------------------------------------------
  486.   def update
  487.     # Update Command Window
  488.     @window_command.update
  489.     # Update Player Sprites and Dealer Sprites
  490.     @player_sprites.each {|sprite| sprite.update}
  491.     @dealer_sprites.each {|sprite| sprite.update}
  492.     # Return if any movement
  493.     (@player_sprites + @dealer_sprites).each {|sprite| return if sprite.moving?}
  494.     # If waiting
  495.     if @wait_count > 0
  496.       # Decrease wait count
  497.       @wait_count -= 1
  498.       return
  499.     end
  500.     # Branch by phase
  501.     case @phase
  502.     when 1
  503.       # Update Bet
  504.       update_bet
  505.     when 2
  506.       # Update Drawing
  507.       update_draw
  508.     when 3
  509.       # Update Selection
  510.       update_selection
  511.     when 4
  512.       # Update Cpu
  513.       update_cpu
  514.     when 5
  515.       # Update Check
  516.       update_check
  517.     end
  518.   end
  519.   #--------------------------------------------------------------------------
  520.   # * Start Betting Phase
  521.   #--------------------------------------------------------------------------
  522.   def start_bet
  523.     # Set Phase
  524.     @phase = 1
  525.     # Set Info Windows to Invisible
  526.     @window_dealer.visible = false
  527.     @window_player.visible = false
  528.     # Set Result Window Invisible
  529.     @window_result.visible = false
  530.     # Set Window Bet Index
  531.     @window_bet.index = 0
  532.     # Set Window Bet Active
  533.     @window_bet.active = true
  534.   end
  535.   #--------------------------------------------------------------------------
  536.   # * Update Bet
  537.   #--------------------------------------------------------------------------
  538.   def update_bet
  539.     # Update Window Bet
  540.     @window_bet.update
  541.     # If B button was pressed
  542.     if Input.trigger?(Input::B)
  543.       # Play cancel SE
  544.       $game_system.se_play($data_system.cancel_se)
  545.       # Return to Map
  546.       $scene = Scene_Map.new
  547.       # Return
  548.       return
  549.     end
  550.     # If C button was pressed
  551.     if Input.trigger?(Input::C)
  552.       # Get Bet Amount
  553.       @amount = @window_bet.number
  554.       # If not enough Money
  555.       if $game_party.gold < @amount
  556.         # Play buzzer SE
  557.         $game_system.se_play($data_system.buzzer_se)
  558.         # Return
  559.         return
  560.       end
  561.       # Play Decision SE
  562.       $game_system.se_play($data_system.decision_se)
  563.       # Lose Gold
  564.       $game_party.lose_gold(@amount)
  565.       # Refresh
  566.       @window_bet.refresh
  567.       # Start Draw Phase
  568.       start_draw
  569.     end
  570.   end
  571.   #--------------------------------------------------------------------------
  572.   # * Start Draw Phase
  573.   #--------------------------------------------------------------------------
  574.   def start_draw
  575.     # Set Phase to 2
  576.     @phase = 2
  577.     # Set Window Bet Index
  578.     @window_bet.index = -1
  579.     # Set Window Bet Inactive
  580.     @window_bet.active = false
  581.     # Draw Two Cards Player
  582.     player_cards = @deck.draw(2)
  583.     # Add to Hand
  584.     @player_hand.draw(*player_cards)
  585.     # Draw Two Cards Dealer
  586.     dealer_cards = @deck.draw(2)
  587.     # Add to Hand
  588.     @dealer_hand.draw(*dealer_cards)
  589.     # Create Sprites for player drawn cards
  590.     player_cards.each {|card| @player_sprites << Sprite_BlackJackCard.new(card)}
  591.     # Create Sprites for dealer drawn cards
  592.     dealer_cards.each_with_index do |card, index|
  593.       # Set Flipped Flag
  594.       card.flipped = index != 0
  595.       # Create Sprite
  596.       @dealer_sprites << Sprite_BlackJackCard.new(card, index != 0)
  597.     end
  598.     # Setup Draw Index
  599.     @draw_index = 0
  600.   end
  601.   #--------------------------------------------------------------------------
  602.   # * Update Drawing
  603.   #--------------------------------------------------------------------------
  604.   def update_draw
  605.     # If All Cards Drawn
  606.     if @draw_index == 4
  607.       # Start Selection
  608.       start_selection
  609.       # Return
  610.       return
  611.     end
  612.     # Play Draw Se
  613.     $game_system.se_play(BlackJack::Draw)
  614.     # Branch By Draw Index
  615.     case @draw_index
  616.     when 0
  617.       # Move Sprite
  618.       @player_sprites[0].move(240, 352, 10)
  619.     when 1
  620.       # Move Sprite
  621.       @player_sprites[1].move(320, 352, 10)
  622.     when 2
  623.       # Move Sprite
  624.       @dealer_sprites[0].move(240, 32, 5)
  625.     when 3
  626.       # Move Sprite
  627.       @dealer_sprites[1].move(320, 32, 5)
  628.     end
  629.     # Increment Draw Index
  630.     @draw_index += 1
  631.   end
  632.   #--------------------------------------------------------------------------
  633.   # * Start Selection Phase
  634.   #--------------------------------------------------------------------------
  635.   def start_selection
  636.     # Set Phase to 3
  637.     @phase = 3
  638.     # Set CPU Flag
  639.     @start_cpu = false
  640.     # Set Info Windows to Visible
  641.     @window_dealer.visible = true
  642.     @window_player.visible = true
  643.     # Set Hand Totals
  644.     @window_dealer.hand = @dealer_hand.total
  645.     @window_player.hand = @player_hand.total
  646.     # If Player has Black Jack
  647.     if @player_hand.blackjack?
  648.       # Just Start CPU Phase
  649.       start_cpu
  650.       # Return
  651.       return
  652.     end
  653.     # Set Command Window To Visible
  654.     @window_command.visible = true
  655.     @window_command.active = true
  656.     @window_command.index = 0
  657.     # Setup Command Window
  658.     setup_command_window
  659.   end
  660.   #--------------------------------------------------------------------------
  661.   # * Setup Command Window
  662.   #--------------------------------------------------------------------------
  663.   def setup_command_window
  664.     # Setup Original Commands
  665.     @window_command.commands = %w( Hit Stand )
  666.     # If Can Use Double
  667.     if @player_hand.double?
  668.       # Push Double onto Commands
  669.       @window_command.commands += ['Double']
  670.       # If not enough money
  671.       if $game_party.gold < @amount
  672.         # Disable Double
  673.         @window_command.disable_item('Double')
  674.       end
  675.     end
  676.   end
  677.   #--------------------------------------------------------------------------
  678.   # * Update Selection
  679.   #--------------------------------------------------------------------------
  680.   def update_selection
  681.     # If C button was pressed
  682.     if Input.trigger?(Input::C)
  683.       # Branch by command
  684.       case @window_command.command
  685.       when 'Hit'
  686.         update_hit
  687.       when 'Stand'
  688.         update_stand
  689.       when 'Double'
  690.         update_double
  691.       end
  692.     end
  693.   end
  694.   #--------------------------------------------------------------------------
  695.   # * Update Hit
  696.   #--------------------------------------------------------------------------
  697.   def update_hit
  698.     # Play Decision SE
  699.     $game_system.se_play($data_system.decision_se)
  700.     # Draw Card Player
  701.     player_draw
  702.     # If Player Bust
  703.     if @player_hand.bust?
  704.       # Start CPU
  705.       start_cpu
  706.       # Return
  707.       return
  708.     end
  709.     # Setup Command Window
  710.     setup_command_window
  711.   end
  712.   #--------------------------------------------------------------------------
  713.   # * Update Stand
  714.   #--------------------------------------------------------------------------
  715.   def update_stand
  716.     # Play Decision SE
  717.     $game_system.se_play($data_system.decision_se)
  718.     # Start CPU
  719.     start_cpu
  720.   end
  721.   #--------------------------------------------------------------------------
  722.   # * Update Double
  723.   #--------------------------------------------------------------------------
  724.   def update_double
  725.     # If Gold is less than amount
  726.     if $game_party.gold < @amount
  727.       # Play buzzer SE
  728.       $game_system.se_play($data_system.buzzer_se)
  729.       # Return
  730.       return
  731.     end
  732.     # Play Decision SE
  733.     $game_system.se_play($data_system.decision_se)
  734.     # Subtract from gold
  735.     $game_party.lose_gold(@amount)
  736.     # Refresh
  737.     @window_bet.refresh
  738.     # Add to Amount
  739.     @amount *= 2
  740.     # Draw Card Player
  741.     player_draw
  742.     # Start CPU
  743.     start_cpu
  744.   end
  745.   #--------------------------------------------------------------------------
  746.   # * Player Draw Card
  747.   #--------------------------------------------------------------------------
  748.   def player_draw
  749.     # Draw Card Player
  750.     player_card = @deck.draw
  751.     # Add to Hand
  752.     @player_hand.draw(player_card)
  753.     # Create Sprites for drawn card
  754.     card_sprite = Sprite_BlackJackCard.new(player_card)
  755.     # Get X and Y
  756.     x, y = 320 + 16 * (@player_sprites.size - 1), 352
  757.     # Push to Player Sprites
  758.     @player_sprites << card_sprite
  759.     # Move Card
  760.     card_sprite.move(x, y, 10)
  761.     # Play Draw Se
  762.     $game_system.se_play(BlackJack::Draw)
  763.     # Refresh Player Hand
  764.     @window_player.hand = @player_hand.total
  765.   end
  766.   #--------------------------------------------------------------------------
  767.   # * Start CPU
  768.   #--------------------------------------------------------------------------
  769.   def start_cpu
  770.     # Set Phase to 4
  771.     @phase = 4
  772.     # Set Command Window To Invisible
  773.     @window_command.visible = false
  774.     @window_command.active = false
  775.     @window_command.index = -1
  776.     # Setup Wait Count
  777.     @wait_count = 30
  778.   end
  779.   #--------------------------------------------------------------------------
  780.   # * Run AI
  781.   #--------------------------------------------------------------------------
  782.   def run_ai
  783.     # If total is 21 or greater than player hand or player has blackjack or
  784.     # player busted
  785.     if (@dealer_hand.total == 21 or @dealer_hand.total > @player_hand.total or
  786.       @player_hand.blackjack? or @player_hand.bust?)
  787.       # Start Checking Phase (Dealer Stands)
  788.       start_check
  789.       # Return
  790.       return
  791.     end
  792.     # Branch By AI Settings
  793.     case $game_system.dealer_ai
  794.     when 0 # Hit 16 Stand 17
  795.       # If Less than or equal to 16 Draw Card Else Stand
  796.       @dealer_hand.total <= 16 ? cpu_draw : start_check
  797.     when 1 # Hit When Advantageous
  798.       # Get Total (All Aces are 1)
  799.       amount = 21 - @dealer_hand.total(true)
  800.       # If Amount greater than or equal to 10
  801.       if amount >= 10
  802.         # Always Hit
  803.         cpu_draw
  804.         # Return
  805.         return
  806.       end
  807.       # Create Values
  808.       values = ('2'..amount.to_s).to_a << 'A'
  809.       # Get Number of Cards then won't make the dealer bust
  810.       number = @deck.number(*values)
  811.       # Get Size of Deck
  812.       size = @deck.size
  813.       # Get Chance of Not Busting
  814.       chance = number * 100 / size
  815.       # If chance is less than the riskiness or If value is less than minimum
  816.       if (100 - chance <= $game_system.dealer_risk ||
  817.           @dealer_hand.total <= $game_system.dealer_min)
  818.         # Hit Again
  819.         cpu_draw
  820.         # Return
  821.         return
  822.       end
  823.       # Else Dealer Stands
  824.       start_check
  825.     end
  826.   end
  827.   #--------------------------------------------------------------------------
  828.   # * CPU Draw Card
  829.   #--------------------------------------------------------------------------
  830.   def cpu_draw
  831.     # Draw Card Dealer
  832.     dealer_card = @deck.draw
  833.     # Add to Hand
  834.     @dealer_hand.draw(dealer_card)
  835.     # Create Sprites for drawn card
  836.     card_sprite = Sprite_BlackJackCard.new(dealer_card)
  837.     # Get X and Y
  838.     x, y = 320 + 16 * (@dealer_sprites.size - 1), 32
  839.     # Push to Player Sprites
  840.     @dealer_sprites << card_sprite
  841.     # Move Card
  842.     card_sprite.move(x, y, 5)
  843.     # Play Draw Se
  844.     $game_system.se_play(BlackJack::Draw)
  845.     # Refresh Dealer Hand
  846.     @window_dealer.hand = @dealer_hand.total
  847.     # Setup Wait Count
  848.     @wait_count = 30
  849.   end
  850.   #--------------------------------------------------------------------------
  851.   # * Update CPU
  852.   #--------------------------------------------------------------------------
  853.   def update_cpu
  854.     # Flip over all secret cards
  855.     @dealer_hand.each {|card| card.flipped = true}
  856.     @dealer_sprites.each {|sprite| sprite.flipped = true}
  857.     # Refresh Dealer Hand
  858.     @window_dealer.hand = @dealer_hand.total
  859.     run_ai
  860.   end
  861.   #--------------------------------------------------------------------------
  862.   # * Start Checking
  863.   #--------------------------------------------------------------------------
  864.   def start_check
  865.     # Set Phase to 5
  866.     @phase = 5
  867.     # Get Totals
  868.     player, dealer = @player_hand.value, @dealer_hand.value
  869.     # Set Text
  870.     @window_result.player = "Player: #{player}"
  871.     @window_result.dealer = "Dealer: #{dealer}"
  872.     # Make Result Window Visible
  873.     @window_result.visible = true
  874.     # If Player Busted
  875.     if @player_hand.bust?
  876.       # Lose
  877.       check_lose
  878.       # Return
  879.       return
  880.     end
  881.     # If dealer has Blackjack and player does not
  882.     if @dealer_hand.blackjack? and not @player_hand.blackjack?
  883.       # Lose
  884.       check_lose
  885.       # Return
  886.       return
  887.     # If player has Blackjack and dealer does not
  888.     elsif @player_hand.blackjack? and not @dealer_hand.blackjack?
  889.       # Win
  890.       check_win
  891.       # Return
  892.       return
  893.     end
  894.     # If Player Hand is better or dealer bust
  895.     if @player_hand.total > @dealer_hand.total || @dealer_hand.bust?
  896.       # Win
  897.       check_win
  898.       # Return
  899.       return
  900.     # Push
  901.     elsif @player_hand.total == @dealer_hand.total
  902.       # Push
  903.       check_push
  904.       # Return
  905.       return
  906.     # Lose
  907.     else
  908.       # Lose
  909.       check_lose
  910.       # Return
  911.       return
  912.     end
  913.   end
  914.   #--------------------------------------------------------------------------
  915.   # * Update Checking
  916.   #--------------------------------------------------------------------------
  917.   def update_check
  918.     # Skip if C is not triggered
  919.     return if not Input.trigger?(Input::C)
  920.     # Play Decision SE
  921.     $game_system.se_play($data_system.decision_se)
  922.     # Discard Player Hand
  923.     @player_hand.discard
  924.     # Run Through and Dispose Each
  925.     @player_sprites.each {|sprite| sprite.dispose}
  926.     # Clear
  927.     @player_sprites.clear
  928.     # Discard Dealer Hand
  929.     @dealer_hand.discard
  930.     # Run Through and Dispose Each
  931.     @dealer_sprites.each {|sprite| sprite.dispose}
  932.     # Discard In Play Cards
  933.     @deck.discard
  934.     # Clear
  935.     @dealer_sprites.clear    
  936.     # Start Bet
  937.     start_bet
  938.   end
  939.   #--------------------------------------------------------------------------
  940.   # * Win
  941.   #--------------------------------------------------------------------------
  942.   def check_win
  943.     # Get Winnings
  944.     winnings = (@amount * (@player_hand.blackjack? ? 3 : 2)).to_i
  945.     # Set Winnings
  946.     @window_result.winnings = winnings
  947.     # Refresh Result Window
  948.     @window_result.refresh
  949.     # Play Win SE
  950.     $game_system.se_play(BlackJack::Win)
  951.     # Add to Gold
  952.     $game_party.gain_gold(winnings)
  953.     # Refresh Bet Window
  954.     @window_bet.refresh
  955.   end
  956.   #--------------------------------------------------------------------------
  957.   # * Lose
  958.   #--------------------------------------------------------------------------
  959.   def check_lose
  960.     # Set Winnings
  961.     @window_result.winnings = -@amount
  962.     # Refresh Result Window
  963.     @window_result.refresh
  964.     # Play Lose SE
  965.     $game_system.se_play(BlackJack::Lose)
  966.   end
  967.   #--------------------------------------------------------------------------
  968.   # * Push
  969.   #--------------------------------------------------------------------------
  970.   def check_push
  971.     # Set Winnings
  972.     @window_result.winnings = 0
  973.     # Refresh Result Window
  974.     @window_result.refresh
  975.     # Give Back Bet
  976.     $game_party.gain_gold(@amount)
  977.     # Play Push Se
  978.     $game_system.se_play(BlackJack::Push)
  979.     # Refresh Bet Window
  980.     @window_bet.refresh
  981.   end
  982. end
  983.  
  984. class Window_BlackJackBet < Window_Base
  985.   #--------------------------------------------------------------------------
  986.   # * Public Instance Variables
  987.   #--------------------------------------------------------------------------
  988.   attr_accessor :index
  989.   #--------------------------------------------------------------------------
  990.   # * Object Initialization
  991.   #--------------------------------------------------------------------------
  992.   def initialize
  993.     # Call Window_Base#initialize
  994.     super(448, 32, 160, 96)
  995.     # Setup Contents
  996.     self.contents = Bitmap.new(width - 32, height - 32)
  997.     # Setup Cursor Width
  998.     @cursor_width = self.contents.text_size('0').width + 8
  999.     # Setup Opacity
  1000.     self.opacity = 0
  1001.     # Get Number of Digits from Max Bet
  1002.     @digits_max = Math.log10($game_system.blackjack_max_bet).to_i + 1
  1003.     # Setup Minimum and Maximum
  1004.     @minimum = $game_system.blackjack_min_bet
  1005.     @maximum = $game_system.blackjack_max_bet
  1006.     # Setup Number
  1007.     @number = @minimum
  1008.     # Setup Index
  1009.     @index = -1
  1010.     # Refresh
  1011.     refresh
  1012.     # Update Cursor Rectangle
  1013.     update_cursor_rect
  1014.   end
  1015.   #--------------------------------------------------------------------------
  1016.   # * Set Cursor Position
  1017.   #--------------------------------------------------------------------------
  1018.   def index=(index)
  1019.     # Set Instance Variable
  1020.     @index = index
  1021.     # Update cursor rectangle
  1022.     update_cursor_rect
  1023.   end
  1024.   #--------------------------------------------------------------------------
  1025.   # * Get Number
  1026.   #--------------------------------------------------------------------------
  1027.   def number
  1028.     return @number
  1029.   end
  1030.   #--------------------------------------------------------------------------
  1031.   # * Set Number
  1032.   #     number : new number
  1033.   #--------------------------------------------------------------------------
  1034.   def number=(number)
  1035.     # Set Number
  1036.     @number = [[number, 0].max, 10 ** @digits_max - 1].min
  1037.     # Restrict to [min, max] bet
  1038.     @number = [[@number, @minimum].max, @maximum].min
  1039.     # Refresh
  1040.     refresh
  1041.   end
  1042.   #--------------------------------------------------------------------------
  1043.   # * Cursor Rectangle Update
  1044.   #--------------------------------------------------------------------------
  1045.   def update_cursor_rect
  1046.     # If cursor position is less than 0
  1047.     if @index < 0
  1048.       # Empty Cursor Rect
  1049.       self.cursor_rect.empty
  1050.       # Return
  1051.       return
  1052.     end
  1053.     # Get X
  1054.     x = width - 32 - (@index + 1) * @cursor_width
  1055.     # Set Cursor Rect
  1056.     self.cursor_rect.set(x, 0, @cursor_width, 32)
  1057.   end
  1058.   #--------------------------------------------------------------------------
  1059.   # * Frame Update
  1060.   #--------------------------------------------------------------------------
  1061.   def update
  1062.     super
  1063.     # If up or down directional button was pressed
  1064.     if Input.repeat?(Input::UP) or Input.repeat?(Input::DOWN)
  1065.       # Play Cursor SE
  1066.       $game_system.se_play($data_system.cursor_se)
  1067.       # Get current place number
  1068.       place = 10 ** @index
  1069.       # If up add place, if down substract place
  1070.       @number += Input.repeat?(Input::UP) ? place : -place
  1071.       # Restrict to [min, max] bet
  1072.       @number = [[@number, @minimum].max, @maximum].min
  1073.       # Refresh
  1074.       refresh
  1075.     end
  1076.     # Return if number of digits is one
  1077.     return if @digits_max == 1
  1078.     # Cursor Right
  1079.     if Input.repeat?(Input::RIGHT)
  1080.       # Play Cursor
  1081.       $game_system.se_play($data_system.cursor_se)
  1082.       # Decrement Index
  1083.       @index = (@index - 1) % @digits_max
  1084.     end
  1085.     # Cursor Left
  1086.     if Input.repeat?(Input::LEFT)
  1087.       # Play Cursor
  1088.       $game_system.se_play($data_system.cursor_se)
  1089.       # Increment Index
  1090.       @index = (@index + 1) % @digits_max
  1091.     end
  1092.     # Update Cursor Rect
  1093.     update_cursor_rect
  1094.   end
  1095.   #--------------------------------------------------------------------------
  1096.   # * Refresh
  1097.   #--------------------------------------------------------------------------
  1098.   def refresh
  1099.     # Clear
  1100.     self.contents.clear
  1101.     # Set Font Color
  1102.     self.contents.font.color = normal_color
  1103.     # Draw Bet
  1104.     self.contents.draw_text(0, 0, 120, 32, 'Bet:')
  1105.     # Format Print
  1106.     s = sprintf("%0*d", @digits_max, @number).reverse
  1107.     # Run Through Number of Digits Times
  1108.     @digits_max.times do |i|
  1109.       # Get X
  1110.       x = width - 32 - (i + 1) * @cursor_width + 4
  1111.       # Get Rect
  1112.       rect = Rect.new(x, 0, @cursor_width, 32)
  1113.       # Draw Digit Text
  1114.       self.contents.draw_text(rect, s[i,1])
  1115.     end
  1116.     # Draw Gold Text
  1117.     self.contents.draw_text(0, 32, 120, 32, $data_system.words.gold)
  1118.     # Draw Gold
  1119.     self.contents.draw_text(0, 32, width-32, 32, $game_party.gold.to_s, 2)
  1120.   end
  1121. end
  1122.  
  1123. class Window_PlayerInfo < Window_Base
  1124.   #--------------------------------------------------------------------------
  1125.   # * Object Initialization
  1126.   #--------------------------------------------------------------------------
  1127.   def initialize(text)
  1128.     # Call Window_Base#initialize
  1129.     super(0, 0, 160, 96)
  1130.     # Setup Contents
  1131.     self.contents = Bitmap.new(width - 32, height - 32)
  1132.     # Set instanve Variable
  1133.     @text = text
  1134.     # Setup Hand Count
  1135.     @hand = nil
  1136.     # Setup Opacity
  1137.     self.opacity = 0
  1138.     # Refresh
  1139.     refresh
  1140.   end
  1141.   #--------------------------------------------------------------------------
  1142.   # * Set Hand
  1143.   #--------------------------------------------------------------------------
  1144.   def hand=(value)
  1145.     # Return if same
  1146.     return if @hand == value
  1147.     # Set Hand
  1148.     @hand = value
  1149.     # Refresh
  1150.     refresh
  1151.   end
  1152.   #--------------------------------------------------------------------------
  1153.   # * Refresh
  1154.   #--------------------------------------------------------------------------
  1155.   def refresh
  1156.     self.contents.clear
  1157.     self.contents.font.color = system_color
  1158.     self.contents.draw_text(4, 0, 128, 32, @text)
  1159.     self.contents.font.color = normal_color
  1160.     self.contents.draw_text(4, 32, 124, 32, @hand.to_s, 2)
  1161.   end
  1162. end
  1163.  
  1164. class Window_BlackJackResult < Window_Base
  1165.   #--------------------------------------------------------------------------
  1166.   # * Object Initialization
  1167.   #--------------------------------------------------------------------------
  1168.   attr_accessor :player
  1169.   attr_accessor :dealer
  1170.   attr_accessor :winnings
  1171.   #--------------------------------------------------------------------------
  1172.   # * Object Initialization
  1173.   #--------------------------------------------------------------------------
  1174.   def initialize
  1175.     # Call Window_Base#initialize
  1176.     super(176, 192, 288, 96)
  1177.     # Setup Contents
  1178.     self.contents = Bitmap.new(width - 32, height - 32)
  1179.     # Set Player and Dealer Text
  1180.     @player, @dealer = nil
  1181.     # Set Winnings
  1182.     @winnings = 0
  1183.     # Set Visibility
  1184.     self.visible = false
  1185.   end
  1186.   #--------------------------------------------------------------------------
  1187.   # * Refresh
  1188.   #--------------------------------------------------------------------------
  1189.   def refresh
  1190.     # Get Text Size
  1191.     size = (contents.text_size(@player+@dealer).width / 32.0).ceil * 32
  1192.     # Dispose Contents
  1193.     self.contents.dispose
  1194.     # Setup New Width
  1195.     self.width = size + 64
  1196.     # Setup X
  1197.     self.x = (640 - width) / 2
  1198.     # Setup Contents
  1199.     self.contents = Bitmap.new(width - 32, height - 32)
  1200.     # Clear
  1201.     self.contents.clear
  1202.     # Set to Normal Color
  1203.     self.contents.font.color = normal_color
  1204.     # Draw Player Text
  1205.     self.contents.draw_text(0, 0, width/2-16, 32, @player, 1)
  1206.     # Draw Dealer Text
  1207.     self.contents.draw_text(width/2-16, 0, width/2-16, 32, @dealer, 1)
  1208.     # Get Winnings Text
  1209.     if @winnings == 0
  1210.       # Draw Text
  1211.       self.contents.draw_text(0, 32, width-32, 32, 'Push', 2)
  1212.     else
  1213.       # Get Text
  1214.       text = (@winnings > 0 ? 'Won ' : 'Lost ') + "#{@winnings.abs} "
  1215.       # Get Gold Text Size
  1216.       cx = contents.text_size($data_system.words.gold).width
  1217.       # Draw Text
  1218.       self.contents.draw_text(0, 32, width-32-cx, 32, text, 2)
  1219.       # Set to System Color
  1220.       self.contents.font.color = system_color
  1221.       # Draw Gold Text
  1222.       self.contents.draw_text(0, 32, width-32, 32, $data_system.words.gold, 2)
  1223.     end
  1224.   end
  1225. end
  1226.  
  1227. class Window_BlackJackCommand < Window_Selectable
  1228.   #--------------------------------------------------------------------------
  1229.   # * Public Instance Variables
  1230.   #--------------------------------------------------------------------------
  1231.   attr_reader :commands
  1232.   #--------------------------------------------------------------------------
  1233.   # * Object Initialization
  1234.   #--------------------------------------------------------------------------
  1235.   def initialize
  1236.     # Compute window height from command quantity
  1237.     super(448, 320, 160, 96)
  1238.     # Get Item Max
  1239.     @item_max = 2
  1240.     # Setup Original Commands
  1241.     @commands = %w( Hit Stand )
  1242.     # Create Contents
  1243.     self.contents = Bitmap.new(width - 32, @item_max * 32)
  1244.     # Refresh
  1245.     refresh
  1246.     # Set Index
  1247.     self.index = -1
  1248.     # Set Activity
  1249.     self.active = false
  1250.     # Set Opacity
  1251.     self.opacity = 0
  1252.   end
  1253.   #--------------------------------------------------------------------------
  1254.   # * Get Current Command
  1255.   #--------------------------------------------------------------------------
  1256.   def command
  1257.     return @commands[self.index]
  1258.   end
  1259.   #--------------------------------------------------------------------------
  1260.   # * Set Commands
  1261.   #--------------------------------------------------------------------------
  1262.   def commands=(commands)
  1263.     # Return if same commands
  1264.     return if @commands == commands
  1265.     # If Sizes are not the same
  1266.     if @commands.size != commands.size
  1267.       # Dispose
  1268.       self.contents.dispose
  1269.       # Get Item Max
  1270.       @item_max = commands.size
  1271.       # Setup Height
  1272.       self.height = @item_max * 32 + 32
  1273.       # Setup Contents
  1274.       self.contents = Bitmap.new(width - 32, @item_max * 32)
  1275.     end
  1276.     # Setup Commands
  1277.     @commands = commands
  1278.     # Refresh
  1279.     refresh
  1280.   end
  1281.   #--------------------------------------------------------------------------
  1282.   # * Refresh
  1283.   #--------------------------------------------------------------------------
  1284.   def refresh
  1285.     # Clear
  1286.     self.contents.clear
  1287.     # Draw Items
  1288.     @item_max.times {|i| draw_item(i)}
  1289.   end
  1290.   #--------------------------------------------------------------------------
  1291.   # * Draw Item
  1292.   #     index : item number
  1293.   #--------------------------------------------------------------------------
  1294.   def draw_item(index, color = normal_color)
  1295.     # Set Color
  1296.     self.contents.font.color = color
  1297.     # Setup Rect
  1298.     rect = Rect.new(4, 32 * index, self.contents.width - 8, 32)
  1299.     # Clear At Rect
  1300.     self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
  1301.     # Draw At Rect
  1302.     self.contents.draw_text(rect, @commands[index])
  1303.   end
  1304.   #--------------------------------------------------------------------------
  1305.   # * Disable Item
  1306.   #     item : String or Integer
  1307.   #--------------------------------------------------------------------------
  1308.   def disable_item(item)
  1309.     # Get Index
  1310.     index = item.is_a?(Numeric) ? item : @commands.index(item)
  1311.     # Draw in disabled color
  1312.     draw_item(index, disabled_color)
  1313.   end
  1314. end
  1315.  
  1316. class Game_BlackJackDeck < Game_Deck
  1317.   #--------------------------------------------------------------------------
  1318.   # * Initialize
  1319.   #--------------------------------------------------------------------------
  1320.   def initialize(flag = true)
  1321.     # Setup Instance Variable Flag
  1322.     @flag = flag
  1323.     # Call Game_Deck#initialize
  1324.     super
  1325.   end
  1326.   #--------------------------------------------------------------------------
  1327.   # * Setup Cards
  1328.   #--------------------------------------------------------------------------
  1329.   def setup_cards
  1330.     # Run Through number of decks times
  1331.     @num_decks.times do
  1332.       # Run Through Each Suit
  1333.       Suits.each do |suit|
  1334.         # Run Through each value
  1335.         Values.each do |number|
  1336.           # Setup Card
  1337.           card = Game_Card.new(suit, number, true, true)
  1338.           # Push into cards array
  1339.           @deck << card
  1340.         end
  1341.       end
  1342.     end
  1343.   end
  1344.   #--------------------------------------------------------------------------
  1345.   # * Shuffle
  1346.   #--------------------------------------------------------------------------
  1347.   def shuffle
  1348.     # Play Shuffle SE
  1349.     $game_system.se_play(BlackJack::Shuffle) if not @flag
  1350.     # Setup Flag
  1351.     @flag = false if @flag
  1352.     # Call Game_Deck#shuffle
  1353.     super
  1354.   end
  1355. end
  1356.  
  1357. class Game_BlackJackHand < Game_CardHand
  1358.   #--------------------------------------------------------------------------
  1359.   # * Bust?
  1360.   #--------------------------------------------------------------------------
  1361.   def bust?
  1362.     return self.total > 21
  1363.   end
  1364.   #--------------------------------------------------------------------------
  1365.   # * Black Jack
  1366.   #--------------------------------------------------------------------------
  1367.   def blackjack?
  1368.     return ((@cards[0].ace? && @cards[1].ten?) || (@cards[1].ace? &&
  1369.              @cards[0].ten?)) && @cards.size == 2
  1370.   end
  1371.   #--------------------------------------------------------------------------
  1372.   # * Double?
  1373.   #--------------------------------------------------------------------------
  1374.   def double?
  1375.     return @cards.size == 2
  1376.   end
  1377.   #--------------------------------------------------------------------------
  1378.   # * Split
  1379.   #--------------------------------------------------------------------------
  1380.   def split?
  1381.     return @cards[0].value == @cards[1].value && @cards.size == 2
  1382.   end
  1383.   #--------------------------------------------------------------------------
  1384.   # * Aces
  1385.   #--------------------------------------------------------------------------
  1386.   def aces
  1387.     num = 0
  1388.     @cards.each {|card| num += 1 if card.ace?}
  1389.     return num
  1390.   end
  1391.   #--------------------------------------------------------------------------
  1392.   # * Total
  1393.   #--------------------------------------------------------------------------
  1394.   def total(ace1 = false)
  1395.     # Setup Number
  1396.     num = 0
  1397.     # Add Up
  1398.     @cards.each {|card| num += card.number if card.flipped}
  1399.     # Decrease ace value to 1 if over 21
  1400.     self.aces.times { num -= 10 if num > 21 || ace1 }
  1401.     # Return total
  1402.     return num
  1403.   end
  1404.   #--------------------------------------------------------------------------
  1405.   # * Value
  1406.   #--------------------------------------------------------------------------
  1407.   def value
  1408.     return blackjack? ? 'BJ' : bust? ? 'Bust' : total.to_s
  1409.   end
  1410. end
  1411.  
  1412. class Numeric
  1413.   #--------------------------------------------------------------------------
  1414.   # * Sign
  1415.   #--------------------------------------------------------------------------
  1416.   def sign
  1417.     return 0 if self.zero?
  1418.     return (self / self.abs).to_i
  1419.   end
  1420. end
  1421.  
  1422. class Sprite
  1423.   #--------------------------------------------------------------------------
  1424.   # * Move the sprite
  1425.   #   x     : x coordinate of the destination point
  1426.   #   y     : y coordinate of the destination point
  1427.   #   speed : Speed of movement
  1428.   #--------------------------------------------------------------------------
  1429.   def move(x, y, speed = 1)
  1430.     # Set Destination Points speed and move count set moving flag
  1431.     @destination_x = x
  1432.     @destination_y = y
  1433.     @move_speed = speed
  1434.     @moving = true
  1435.     # Get the distance + (negative if to left or up positive if down or right)
  1436.     @distance_x = (@destination_x - self.x).to_f
  1437.     @distance_y = (@destination_y - self.y).to_f
  1438.     # Get slant distance (hypotenuse of the triangle (xf,yi) (xi,yf) and (xf,yf))
  1439.     @distance = Math.sqrt(@distance_x ** 2 + @distance_y ** 2)
  1440.     # Calculate angle of movement which is later used to determine direction
  1441.     # If X distance is 0 (Prevent Infinity Error)
  1442.     if @distance_x == 0
  1443.       # The Angle is sign(distance_y) * - π / 2 (90° or 270°)
  1444.       @angle = @distance_y.sign * Math::PI / 2
  1445.     # If Y distance is 0 (Prevent Incorrect Direction for later)
  1446.     elsif @distance_y == 0
  1447.       # The Angle is sign(distance_x) - 1 * π / 2 (0° or 180°)
  1448.       @angle = (@distance_x.sign - 1) * Math::PI / 2
  1449.     else
  1450.       # The Angle is the Arctangent of @distance_y / @distance_x (slope)
  1451.       # Returns [-π,π]
  1452.       @angle = Math.atan2(@distance_y, @distance_x.to_f)
  1453.     end
  1454.     # Convert the angle to degrees
  1455.     @angle *= 180 / Math::PI
  1456.   end
  1457.   #--------------------------------------------------------------------------
  1458.   # * Update Move
  1459.   #--------------------------------------------------------------------------
  1460.   if @moving_sprite_update.nil?
  1461.     alias moving_sprite_update update
  1462.     @moving_sprite_update = true
  1463.   end
  1464.   def update
  1465.     moving_sprite_update
  1466.     update_move
  1467.   end
  1468.   #--------------------------------------------------------------------------
  1469.   # * Moving?
  1470.   #--------------------------------------------------------------------------
  1471.   def moving?
  1472.     return @moving
  1473.   end
  1474.   #--------------------------------------------------------------------------
  1475.   # * Update Move
  1476.   #--------------------------------------------------------------------------
  1477.   def update_move
  1478.     # If not moving
  1479.     if (self.x == @destination_x and self.y == @destination_y) or not @moving
  1480.       @moving = false
  1481.       return
  1482.     end
  1483.     # move increase x = the cosine of the arctangent of the dist x over dist y
  1484.     # move increase x = cos(arctan(disty/distx)) simplified by trigonometry
  1485.     # to distance_x / slant_distance, the sprite moves (move speed)
  1486.     # along the slanted line (if it is slanted)
  1487.     movinc_x = @move_speed * @distance_x.abs / @distance
  1488.     # same reasoning with y increase except it is the sine of the arctangent
  1489.     # move increase y = sin(arctan(disty/distx)) simplified by trigonometry
  1490.     # to distance_y / slant_distance
  1491.     movinc_y = @move_speed * @distance_y.abs / @distance
  1492.     # Move the sign of the distance left + move increase or the remaining distance
  1493.     # left if it will go past that point
  1494.     if @move_speed != 0
  1495.       # Get distance remaining
  1496.       remain_x = (@destination_x - self.x).abs
  1497.       remain_y = (@destination_y - self.y).abs
  1498.       self.x += (@destination_x - self.x).sign * [movinc_x.ceil, remain_x].min
  1499.       self.y += (@destination_y - self.y).sign * [movinc_y.ceil, remain_y].min
  1500.     end
  1501.     # If Destination Reached stop moving
  1502.     if self.x == @destination_x and self.y == @destination_y
  1503.       @moving = false
  1504.       return
  1505.     end
  1506.   end
  1507. end
  1508.  
  1509. class Sprite_BlackJackCard < Sprite_Card
  1510.   #--------------------------------------------------------------------------
  1511.   # * Object Initialization
  1512.   #--------------------------------------------------------------------------
  1513.   def initialize(card, flipped = true)
  1514.     super(card, flipped)
  1515.     # Setup Y
  1516.     self.y = -bitmap.height
  1517.     # Setup X
  1518.     self.x = 320 + bitmap.width
  1519.   end
  1520. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement