Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module BlackJack
- #--------------------------------------------------------------------------
- # * Window_Skin
- #--------------------------------------------------------------------------
- Window_Skin = 'BlackJack'
- #--------------------------------------------------------------------------
- # * Win SE
- #--------------------------------------------------------------------------
- Win = RPG::AudioFile.new('006-System06')
- #--------------------------------------------------------------------------
- # * Lose SE
- #--------------------------------------------------------------------------
- Lose = RPG::AudioFile.new()
- #--------------------------------------------------------------------------
- # * Push SE
- #--------------------------------------------------------------------------
- Push = RPG::AudioFile.new()
- #--------------------------------------------------------------------------
- # * Draw SE
- #--------------------------------------------------------------------------
- Draw = RPG::AudioFile.new('Dealing')
- #--------------------------------------------------------------------------
- # * Flip SE
- #--------------------------------------------------------------------------
- Flip = RPG::AudioFile.new('Flipping')
- #--------------------------------------------------------------------------
- # * Shuffle SE
- #--------------------------------------------------------------------------
- Shuffle = RPG::AudioFile.new('Shuffle')
- end
- class Game_Deck
- #--------------------------------------------------------------------------
- # * Constants
- #--------------------------------------------------------------------------
- Suits = %w( spades hearts diamonds clubs )
- Values = 'A','2','3','4','5','6','7','8','9','10','J','Q','K'
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize(num_decks = 1)
- # Setup Cards
- @cards = []
- # Setup Deck
- @deck = []
- # Setup Discarded
- @discard = []
- # Setup In Play
- @in_play = []
- # Setup Number of Decks
- @num_decks = num_decks
- # Setup Cards
- setup_cards
- # Shuffle
- shuffle
- end
- #--------------------------------------------------------------------------
- # * Setup Cards
- #--------------------------------------------------------------------------
- def setup_cards
- # Run Through number of decks times
- @num_decks.times do
- # Run Through Each Suit
- Suits.each do |suit|
- # Run Through each value
- Values.each do |number|
- # Setup Card
- card = Game_Card.new(suit, number)
- # Push into cards array
- @deck << card
- end
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Shuffle
- #--------------------------------------------------------------------------
- def shuffle
- # Setup local variable cards
- cards = []
- # Clear Discarded
- @discard.clear
- # Setup Cards to Deck
- @cards = [@deck].flatten!
- # Delete Cards already out
- delete_in_use_cards
- # Run through until cards are empty
- until @cards.empty?
- # Push a Random Card and Delete
- cards << @cards.delete_at(rand(@cards.size))
- end
- # Set Cards to Cards
- @cards = cards
- end
- #--------------------------------------------------------------------------
- # * Draw
- #--------------------------------------------------------------------------
- def draw(number = 1)
- # Return if number is 1
- if number == 1
- # Shuffle if empty?
- shuffle if @cards.empty?
- # Get Card
- card = @cards.shift
- # Add Card to in play
- @in_play << card
- # Return card
- return card
- end
- # Setup Array
- drawn = []
- # Run Through number of times
- number.times do
- # Shuffle if empty?
- shuffle if @cards.empty?
- # Break if cards are still empty (all in play)
- break if @cards.empty?
- # Get Card
- card = @cards.shift
- # Add Card to in play
- @in_play << card
- # Add to Drawn
- drawn << card
- end
- # Return Drawn Cards
- return drawn
- end
- #--------------------------------------------------------------------------
- # * Discard In Play
- #--------------------------------------------------------------------------
- def discard
- # Add all in play cards to discard
- @discard += @in_play
- # Clear in play
- @in_play.clear
- end
- #--------------------------------------------------------------------------
- # * Number
- #--------------------------------------------------------------------------
- def number(*strings)
- # Setup Local Variable
- number = 0
- # Run Through each card
- @cards.each do |card|
- # Run Through Each String Sent to this method
- strings.each do |string|
- # If A Suit
- if Suits.include?(string)
- # Increase By 1 if suits are equal
- number += 1 if card.suit == string
- # If A Value
- elsif Values.include?(string)
- # Increase By 1 if values are equal
- number += 1 if card.value == string
- end
- end
- end
- return number
- end
- #--------------------------------------------------------------------------
- # * Size
- #--------------------------------------------------------------------------
- def size
- return @cards.size
- end
- #--------------------------------------------------------------------------
- # * Delete Cards In Use
- #--------------------------------------------------------------------------
- private
- def delete_in_use_cards
- # Return if empty cards
- return if @in_play.empty?
- # Run Through each in play card and Delete Card from list
- @in_play.each {|card| @cards.delete(card)}
- # Clear In Play
- @in_play.clear
- end
- end
- class Game_Card
- #--------------------------------------------------------------------------
- # * Public Instance Variables
- #--------------------------------------------------------------------------
- attr_reader :number
- attr_reader :suit
- attr_reader :value
- attr_accessor :flipped
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize(suit, value, face10 = false, ace11 = false)
- @suit = suit
- @value = value
- @face10 = face10
- @ace11 = ace11
- @flipped = true
- setup_number
- end
- #--------------------------------------------------------------------------
- # * Ace?
- #--------------------------------------------------------------------------
- def ace?
- return @value == 'A'
- end
- #--------------------------------------------------------------------------
- # * King?
- #--------------------------------------------------------------------------
- def king?
- return @value == 'K'
- end
- #--------------------------------------------------------------------------
- # * Queen?
- #--------------------------------------------------------------------------
- def queen?
- return @value == 'Q'
- end
- #--------------------------------------------------------------------------
- # * Jack?
- #--------------------------------------------------------------------------
- def jack?
- return @value == 'J'
- end
- #--------------------------------------------------------------------------
- # * Face?
- #--------------------------------------------------------------------------
- def face?
- return ['J','Q','K'].include?(@value)
- end
- #--------------------------------------------------------------------------
- # * Ten?
- #--------------------------------------------------------------------------
- def ten?
- return @value == '10' || (self.face? and @face10)
- end
- #--------------------------------------------------------------------------
- # * To String (Returns card's value)
- #--------------------------------------------------------------------------
- def to_s
- return self.value
- end
- #--------------------------------------------------------------------------
- # * Setup Number (Private)
- #--------------------------------------------------------------------------
- private
- def setup_number
- case @value
- when 'A'
- @number = @ace11 ? 11 : 1
- when 'J'
- @number = @face10 ? 10 : 11
- when 'Q'
- @number = @face10 ? 10 : 12
- when 'K'
- @number = @face10 ? 10 : 13
- else
- @number = @value.to_i
- end
- end
- end
- class Game_CardHand
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize
- # Setup Cards
- @cards = []
- end
- #--------------------------------------------------------------------------
- # * Each (Runs Through Each Card in Hand)
- #--------------------------------------------------------------------------
- def each
- @cards.each {|card| yield(card)}
- end
- #--------------------------------------------------------------------------
- # * Cards
- #--------------------------------------------------------------------------
- def cards
- return @cards.size
- end
- #--------------------------------------------------------------------------
- # * Draw
- #--------------------------------------------------------------------------
- def draw(*cards)
- @cards.push(*cards)
- end
- #--------------------------------------------------------------------------
- # * Discard
- #--------------------------------------------------------------------------
- def discard
- @cards.clear
- end
- #--------------------------------------------------------------------------
- # * To String (ex. A234K)
- #--------------------------------------------------------------------------
- def to_s
- string = ''
- @cards.each {|card| string += card.to_s}
- return string
- end
- end
- module RPG
- module Cache
- #--------------------------------------------------------------------------
- # * Card
- #--------------------------------------------------------------------------
- def self.card(suit, number)
- style = $game_system.card_style
- self.load_bitmap("Graphics/Cards/#{style}/", "#{suit}_#{number}")
- end
- #--------------------------------------------------------------------------
- # * Card Back
- #--------------------------------------------------------------------------
- def self.card_back(style)
- self.load_bitmap("Graphics/Cards/Backs/", style)
- end
- end
- end
- class Sprite_Card < Sprite
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize(card, flipped = true)
- super(nil)
- # Setup Suit and Value
- @suit, @value = card.suit, card.value
- # Setup Flipped Flag
- @flipped = flipped
- # Setup Bitmap
- if @flipped
- # Get Flipped Card
- self.bitmap = RPG::Cache.card(@suit, @value)
- else
- # Get Card Back
- self.bitmap = RPG::Cache.card_back($game_system.card_back_style)
- end
- end
- #--------------------------------------------------------------------------
- # * Flipped
- #--------------------------------------------------------------------------
- def flipped=(bool)
- # Set Flipped Flag
- @flipped = bool
- # Setup Bitmap
- if @flipped
- # Get Flipped Card
- self.bitmap = RPG::Cache.card(@suit, @value)
- else
- # Get Card Back
- self.bitmap = RPG::Cache.card_back($game_system.card_back_style)
- end
- end
- end
- class Game_System
- #--------------------------------------------------------------------------
- # * Public Instance Variables
- #--------------------------------------------------------------------------
- attr_accessor :card_style
- attr_accessor :card_back_style
- attr_accessor :blackjack_min_bet
- attr_accessor :blackjack_max_bet
- attr_accessor :num_decks
- attr_accessor :dealer_ai
- attr_accessor :dealer_risk
- attr_accessor :dealer_min
- attr_accessor :blackjack_audio
- #--------------------------------------------------------------------------
- # * Initialize
- #--------------------------------------------------------------------------
- alias blackjack_initialize initialize
- def initialize
- # The Usual
- blackjack_initialize
- # Setup Card Style
- @card_style = 'Normal'
- # Setup Card Back Style
- @card_back_style = 'DragonKid'
- # Setup Minimum Bet
- @blackjack_min_bet = 100
- # Setup Maximum Bet
- @blackjack_max_bet = 1000
- # Setup Number of Decks
- @num_decks = 1
- # Setup Dealer AI (0: hit on 16 stand on 17 1: hits when possible)
- @dealer_ai = 1
- # Only with @dealer_ai = 1 How Much of a risk the dealer takes
- @dealer_risk = 20
- # Only with @dealer_ai = 1 Dealer will hit if value is less than this
- @dealer_min = 16
- # BGM Used file, volume, pitch
- @blackjack_audio = RPG::AudioFile.new('')
- end
- end
- class Scene_BlackJack
- #--------------------------------------------------------------------------
- # * Main Processing
- #--------------------------------------------------------------------------
- def main
- # Create Background
- @background = Window_Base.new(0,0,640,480)
- # Set Window Skin
- @background.windowskin = RPG::Cache.windowskin(BlackJack::Window_Skin)
- # Set Z
- @background.z = 0
- # Create Betting Window
- @window_bet = Window_BlackJackBet.new
- # Create Hit/Stand/Double/Split Command Window
- @window_command = Window_BlackJackCommand.new
- # Set to Invisible
- @window_command.visible = false
- # Create Dealer Information Window
- @window_dealer = Window_PlayerInfo.new('Dealer\'s Hand')
- # Setup Position
- @window_dealer.x, @window_dealer.y = 32, 32
- # Set To Invisible
- @window_dealer.visible = false
- # Setup PLayer's Information Window
- @window_player = Window_PlayerInfo.new('Player\'s Hand')
- # Setup Position
- @window_player.x, @window_player.y = 32, 320
- # Set To Invisible
- @window_player.visible = false
- # Create Result Window
- @window_result = Window_BlackJackResult.new
- # Create Deck
- @deck = Game_BlackJackDeck.new($game_system.num_decks)
- # Create Player Hand
- @player_hand = Game_BlackJackHand.new
- # Create Player Sprites
- @player_sprites = []
- # Create Dealer Hand
- @dealer_hand = Game_BlackJackHand.new
- # Create Dealer Sprites
- @dealer_sprites = []
- # Save Map BGM
- @map_bgm = $game_system.playing_bgm
- # Play BlackJack BGM
- $game_system.bgm_play($game_system.blackjack_audio)
- # Create Phase Variable
- @phase = 0
- # Create Wait Count
- @wait_count = 0
- # Start Betting
- start_bet
- # Execute transition
- Graphics.transition
- # Main loop
- loop do
- # Update game screen
- Graphics.update
- # Update input information
- Input.update
- # Frame update
- update
- # Abort loop if screen is changed
- if $scene != self
- break
- end
- end
- # Prepare for transition
- Graphics.freeze
- # Dispose
- dispose
- # Play Map BGM
- $game_system.bgm_play(@map_bgm) if @map_bgm != nil
- end
- #--------------------------------------------------------------------------
- # * Dispose
- #--------------------------------------------------------------------------
- def dispose
- # Dispose Objects
- @background.dispose
- @window_player.dispose
- @window_dealer.dispose
- @window_command.dispose
- @window_bet.dispose
- @window_result.dispose
- end
- #--------------------------------------------------------------------------
- # * Frame Update
- #--------------------------------------------------------------------------
- def update
- # Update Command Window
- @window_command.update
- # Update Player Sprites and Dealer Sprites
- @player_sprites.each {|sprite| sprite.update}
- @dealer_sprites.each {|sprite| sprite.update}
- # Return if any movement
- (@player_sprites + @dealer_sprites).each {|sprite| return if sprite.moving?}
- # If waiting
- if @wait_count > 0
- # Decrease wait count
- @wait_count -= 1
- return
- end
- # Branch by phase
- case @phase
- when 1
- # Update Bet
- update_bet
- when 2
- # Update Drawing
- update_draw
- when 3
- # Update Selection
- update_selection
- when 4
- # Update Cpu
- update_cpu
- when 5
- # Update Check
- update_check
- end
- end
- #--------------------------------------------------------------------------
- # * Start Betting Phase
- #--------------------------------------------------------------------------
- def start_bet
- # Set Phase
- @phase = 1
- # Set Info Windows to Invisible
- @window_dealer.visible = false
- @window_player.visible = false
- # Set Result Window Invisible
- @window_result.visible = false
- # Set Window Bet Index
- @window_bet.index = 0
- # Set Window Bet Active
- @window_bet.active = true
- end
- #--------------------------------------------------------------------------
- # * Update Bet
- #--------------------------------------------------------------------------
- def update_bet
- # Update Window Bet
- @window_bet.update
- # If B button was pressed
- if Input.trigger?(Input::B)
- # Play cancel SE
- $game_system.se_play($data_system.cancel_se)
- # Return to Map
- $scene = Scene_Map.new
- # Return
- return
- end
- # If C button was pressed
- if Input.trigger?(Input::C)
- # Get Bet Amount
- @amount = @window_bet.number
- # If not enough Money
- if $game_party.gold < @amount
- # Play buzzer SE
- $game_system.se_play($data_system.buzzer_se)
- # Return
- return
- end
- # Play Decision SE
- $game_system.se_play($data_system.decision_se)
- # Lose Gold
- $game_party.lose_gold(@amount)
- # Refresh
- @window_bet.refresh
- # Start Draw Phase
- start_draw
- end
- end
- #--------------------------------------------------------------------------
- # * Start Draw Phase
- #--------------------------------------------------------------------------
- def start_draw
- # Set Phase to 2
- @phase = 2
- # Set Window Bet Index
- @window_bet.index = -1
- # Set Window Bet Inactive
- @window_bet.active = false
- # Draw Two Cards Player
- player_cards = @deck.draw(2)
- # Add to Hand
- @player_hand.draw(*player_cards)
- # Draw Two Cards Dealer
- dealer_cards = @deck.draw(2)
- # Add to Hand
- @dealer_hand.draw(*dealer_cards)
- # Create Sprites for player drawn cards
- player_cards.each {|card| @player_sprites << Sprite_BlackJackCard.new(card)}
- # Create Sprites for dealer drawn cards
- dealer_cards.each_with_index do |card, index|
- # Set Flipped Flag
- card.flipped = index != 0
- # Create Sprite
- @dealer_sprites << Sprite_BlackJackCard.new(card, index != 0)
- end
- # Setup Draw Index
- @draw_index = 0
- end
- #--------------------------------------------------------------------------
- # * Update Drawing
- #--------------------------------------------------------------------------
- def update_draw
- # If All Cards Drawn
- if @draw_index == 4
- # Start Selection
- start_selection
- # Return
- return
- end
- # Play Draw Se
- $game_system.se_play(BlackJack::Draw)
- # Branch By Draw Index
- case @draw_index
- when 0
- # Move Sprite
- @player_sprites[0].move(240, 352, 10)
- when 1
- # Move Sprite
- @player_sprites[1].move(320, 352, 10)
- when 2
- # Move Sprite
- @dealer_sprites[0].move(240, 32, 5)
- when 3
- # Move Sprite
- @dealer_sprites[1].move(320, 32, 5)
- end
- # Increment Draw Index
- @draw_index += 1
- end
- #--------------------------------------------------------------------------
- # * Start Selection Phase
- #--------------------------------------------------------------------------
- def start_selection
- # Set Phase to 3
- @phase = 3
- # Set CPU Flag
- @start_cpu = false
- # Set Info Windows to Visible
- @window_dealer.visible = true
- @window_player.visible = true
- # Set Hand Totals
- @window_dealer.hand = @dealer_hand.total
- @window_player.hand = @player_hand.total
- # If Player has Black Jack
- if @player_hand.blackjack?
- # Just Start CPU Phase
- start_cpu
- # Return
- return
- end
- # Set Command Window To Visible
- @window_command.visible = true
- @window_command.active = true
- @window_command.index = 0
- # Setup Command Window
- setup_command_window
- end
- #--------------------------------------------------------------------------
- # * Setup Command Window
- #--------------------------------------------------------------------------
- def setup_command_window
- # Setup Original Commands
- @window_command.commands = %w( Hit Stand )
- # If Can Use Double
- if @player_hand.double?
- # Push Double onto Commands
- @window_command.commands += ['Double']
- # If not enough money
- if $game_party.gold < @amount
- # Disable Double
- @window_command.disable_item('Double')
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Update Selection
- #--------------------------------------------------------------------------
- def update_selection
- # If C button was pressed
- if Input.trigger?(Input::C)
- # Branch by command
- case @window_command.command
- when 'Hit'
- update_hit
- when 'Stand'
- update_stand
- when 'Double'
- update_double
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Update Hit
- #--------------------------------------------------------------------------
- def update_hit
- # Play Decision SE
- $game_system.se_play($data_system.decision_se)
- # Draw Card Player
- player_draw
- # If Player Bust
- if @player_hand.bust?
- # Start CPU
- start_cpu
- # Return
- return
- end
- # Setup Command Window
- setup_command_window
- end
- #--------------------------------------------------------------------------
- # * Update Stand
- #--------------------------------------------------------------------------
- def update_stand
- # Play Decision SE
- $game_system.se_play($data_system.decision_se)
- # Start CPU
- start_cpu
- end
- #--------------------------------------------------------------------------
- # * Update Double
- #--------------------------------------------------------------------------
- def update_double
- # If Gold is less than amount
- if $game_party.gold < @amount
- # Play buzzer SE
- $game_system.se_play($data_system.buzzer_se)
- # Return
- return
- end
- # Play Decision SE
- $game_system.se_play($data_system.decision_se)
- # Subtract from gold
- $game_party.lose_gold(@amount)
- # Refresh
- @window_bet.refresh
- # Add to Amount
- @amount *= 2
- # Draw Card Player
- player_draw
- # Start CPU
- start_cpu
- end
- #--------------------------------------------------------------------------
- # * Player Draw Card
- #--------------------------------------------------------------------------
- def player_draw
- # Draw Card Player
- player_card = @deck.draw
- # Add to Hand
- @player_hand.draw(player_card)
- # Create Sprites for drawn card
- card_sprite = Sprite_BlackJackCard.new(player_card)
- # Get X and Y
- x, y = 320 + 16 * (@player_sprites.size - 1), 352
- # Push to Player Sprites
- @player_sprites << card_sprite
- # Move Card
- card_sprite.move(x, y, 10)
- # Play Draw Se
- $game_system.se_play(BlackJack::Draw)
- # Refresh Player Hand
- @window_player.hand = @player_hand.total
- end
- #--------------------------------------------------------------------------
- # * Start CPU
- #--------------------------------------------------------------------------
- def start_cpu
- # Set Phase to 4
- @phase = 4
- # Set Command Window To Invisible
- @window_command.visible = false
- @window_command.active = false
- @window_command.index = -1
- # Setup Wait Count
- @wait_count = 30
- end
- #--------------------------------------------------------------------------
- # * Run AI
- #--------------------------------------------------------------------------
- def run_ai
- # If total is 21 or greater than player hand or player has blackjack or
- # player busted
- if (@dealer_hand.total == 21 or @dealer_hand.total > @player_hand.total or
- @player_hand.blackjack? or @player_hand.bust?)
- # Start Checking Phase (Dealer Stands)
- start_check
- # Return
- return
- end
- # Branch By AI Settings
- case $game_system.dealer_ai
- when 0 # Hit 16 Stand 17
- # If Less than or equal to 16 Draw Card Else Stand
- @dealer_hand.total <= 16 ? cpu_draw : start_check
- when 1 # Hit When Advantageous
- # Get Total (All Aces are 1)
- amount = 21 - @dealer_hand.total(true)
- # If Amount greater than or equal to 10
- if amount >= 10
- # Always Hit
- cpu_draw
- # Return
- return
- end
- # Create Values
- values = ('2'..amount.to_s).to_a << 'A'
- # Get Number of Cards then won't make the dealer bust
- number = @deck.number(*values)
- # Get Size of Deck
- size = @deck.size
- # Get Chance of Not Busting
- chance = number * 100 / size
- # If chance is less than the riskiness or If value is less than minimum
- if (100 - chance <= $game_system.dealer_risk ||
- @dealer_hand.total <= $game_system.dealer_min)
- # Hit Again
- cpu_draw
- # Return
- return
- end
- # Else Dealer Stands
- start_check
- end
- end
- #--------------------------------------------------------------------------
- # * CPU Draw Card
- #--------------------------------------------------------------------------
- def cpu_draw
- # Draw Card Dealer
- dealer_card = @deck.draw
- # Add to Hand
- @dealer_hand.draw(dealer_card)
- # Create Sprites for drawn card
- card_sprite = Sprite_BlackJackCard.new(dealer_card)
- # Get X and Y
- x, y = 320 + 16 * (@dealer_sprites.size - 1), 32
- # Push to Player Sprites
- @dealer_sprites << card_sprite
- # Move Card
- card_sprite.move(x, y, 5)
- # Play Draw Se
- $game_system.se_play(BlackJack::Draw)
- # Refresh Dealer Hand
- @window_dealer.hand = @dealer_hand.total
- # Setup Wait Count
- @wait_count = 30
- end
- #--------------------------------------------------------------------------
- # * Update CPU
- #--------------------------------------------------------------------------
- def update_cpu
- # Flip over all secret cards
- @dealer_hand.each {|card| card.flipped = true}
- @dealer_sprites.each {|sprite| sprite.flipped = true}
- # Refresh Dealer Hand
- @window_dealer.hand = @dealer_hand.total
- run_ai
- end
- #--------------------------------------------------------------------------
- # * Start Checking
- #--------------------------------------------------------------------------
- def start_check
- # Set Phase to 5
- @phase = 5
- # Get Totals
- player, dealer = @player_hand.value, @dealer_hand.value
- # Set Text
- @window_result.player = "Player: #{player}"
- @window_result.dealer = "Dealer: #{dealer}"
- # Make Result Window Visible
- @window_result.visible = true
- # If Player Busted
- if @player_hand.bust?
- # Lose
- check_lose
- # Return
- return
- end
- # If dealer has Blackjack and player does not
- if @dealer_hand.blackjack? and not @player_hand.blackjack?
- # Lose
- check_lose
- # Return
- return
- # If player has Blackjack and dealer does not
- elsif @player_hand.blackjack? and not @dealer_hand.blackjack?
- # Win
- check_win
- # Return
- return
- end
- # If Player Hand is better or dealer bust
- if @player_hand.total > @dealer_hand.total || @dealer_hand.bust?
- # Win
- check_win
- # Return
- return
- # Push
- elsif @player_hand.total == @dealer_hand.total
- # Push
- check_push
- # Return
- return
- # Lose
- else
- # Lose
- check_lose
- # Return
- return
- end
- end
- #--------------------------------------------------------------------------
- # * Update Checking
- #--------------------------------------------------------------------------
- def update_check
- # Skip if C is not triggered
- return if not Input.trigger?(Input::C)
- # Play Decision SE
- $game_system.se_play($data_system.decision_se)
- # Discard Player Hand
- @player_hand.discard
- # Run Through and Dispose Each
- @player_sprites.each {|sprite| sprite.dispose}
- # Clear
- @player_sprites.clear
- # Discard Dealer Hand
- @dealer_hand.discard
- # Run Through and Dispose Each
- @dealer_sprites.each {|sprite| sprite.dispose}
- # Discard In Play Cards
- @deck.discard
- # Clear
- @dealer_sprites.clear
- # Start Bet
- start_bet
- end
- #--------------------------------------------------------------------------
- # * Win
- #--------------------------------------------------------------------------
- def check_win
- # Get Winnings
- winnings = (@amount * (@player_hand.blackjack? ? 3 : 2)).to_i
- # Set Winnings
- @window_result.winnings = winnings
- # Refresh Result Window
- @window_result.refresh
- # Play Win SE
- $game_system.se_play(BlackJack::Win)
- # Add to Gold
- $game_party.gain_gold(winnings)
- # Refresh Bet Window
- @window_bet.refresh
- end
- #--------------------------------------------------------------------------
- # * Lose
- #--------------------------------------------------------------------------
- def check_lose
- # Set Winnings
- @window_result.winnings = -@amount
- # Refresh Result Window
- @window_result.refresh
- # Play Lose SE
- $game_system.se_play(BlackJack::Lose)
- end
- #--------------------------------------------------------------------------
- # * Push
- #--------------------------------------------------------------------------
- def check_push
- # Set Winnings
- @window_result.winnings = 0
- # Refresh Result Window
- @window_result.refresh
- # Give Back Bet
- $game_party.gain_gold(@amount)
- # Play Push Se
- $game_system.se_play(BlackJack::Push)
- # Refresh Bet Window
- @window_bet.refresh
- end
- end
- class Window_BlackJackBet < Window_Base
- #--------------------------------------------------------------------------
- # * Public Instance Variables
- #--------------------------------------------------------------------------
- attr_accessor :index
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize
- # Call Window_Base#initialize
- super(448, 32, 160, 96)
- # Setup Contents
- self.contents = Bitmap.new(width - 32, height - 32)
- # Setup Cursor Width
- @cursor_width = self.contents.text_size('0').width + 8
- # Setup Opacity
- self.opacity = 0
- # Get Number of Digits from Max Bet
- @digits_max = Math.log10($game_system.blackjack_max_bet).to_i + 1
- # Setup Minimum and Maximum
- @minimum = $game_system.blackjack_min_bet
- @maximum = $game_system.blackjack_max_bet
- # Setup Number
- @number = @minimum
- # Setup Index
- @index = -1
- # Refresh
- refresh
- # Update Cursor Rectangle
- update_cursor_rect
- end
- #--------------------------------------------------------------------------
- # * Set Cursor Position
- #--------------------------------------------------------------------------
- def index=(index)
- # Set Instance Variable
- @index = index
- # Update cursor rectangle
- update_cursor_rect
- end
- #--------------------------------------------------------------------------
- # * Get Number
- #--------------------------------------------------------------------------
- def number
- return @number
- end
- #--------------------------------------------------------------------------
- # * Set Number
- # number : new number
- #--------------------------------------------------------------------------
- def number=(number)
- # Set Number
- @number = [[number, 0].max, 10 ** @digits_max - 1].min
- # Restrict to [min, max] bet
- @number = [[@number, @minimum].max, @maximum].min
- # Refresh
- refresh
- end
- #--------------------------------------------------------------------------
- # * Cursor Rectangle Update
- #--------------------------------------------------------------------------
- def update_cursor_rect
- # If cursor position is less than 0
- if @index < 0
- # Empty Cursor Rect
- self.cursor_rect.empty
- # Return
- return
- end
- # Get X
- x = width - 32 - (@index + 1) * @cursor_width
- # Set Cursor Rect
- self.cursor_rect.set(x, 0, @cursor_width, 32)
- end
- #--------------------------------------------------------------------------
- # * Frame Update
- #--------------------------------------------------------------------------
- def update
- super
- # If up or down directional button was pressed
- if Input.repeat?(Input::UP) or Input.repeat?(Input::DOWN)
- # Play Cursor SE
- $game_system.se_play($data_system.cursor_se)
- # Get current place number
- place = 10 ** @index
- # If up add place, if down substract place
- @number += Input.repeat?(Input::UP) ? place : -place
- # Restrict to [min, max] bet
- @number = [[@number, @minimum].max, @maximum].min
- # Refresh
- refresh
- end
- # Return if number of digits is one
- return if @digits_max == 1
- # Cursor Right
- if Input.repeat?(Input::RIGHT)
- # Play Cursor
- $game_system.se_play($data_system.cursor_se)
- # Decrement Index
- @index = (@index - 1) % @digits_max
- end
- # Cursor Left
- if Input.repeat?(Input::LEFT)
- # Play Cursor
- $game_system.se_play($data_system.cursor_se)
- # Increment Index
- @index = (@index + 1) % @digits_max
- end
- # Update Cursor Rect
- update_cursor_rect
- end
- #--------------------------------------------------------------------------
- # * Refresh
- #--------------------------------------------------------------------------
- def refresh
- # Clear
- self.contents.clear
- # Set Font Color
- self.contents.font.color = normal_color
- # Draw Bet
- self.contents.draw_text(0, 0, 120, 32, 'Bet:')
- # Format Print
- s = sprintf("%0*d", @digits_max, @number).reverse
- # Run Through Number of Digits Times
- @digits_max.times do |i|
- # Get X
- x = width - 32 - (i + 1) * @cursor_width + 4
- # Get Rect
- rect = Rect.new(x, 0, @cursor_width, 32)
- # Draw Digit Text
- self.contents.draw_text(rect, s[i,1])
- end
- # Draw Gold Text
- self.contents.draw_text(0, 32, 120, 32, $data_system.words.gold)
- # Draw Gold
- self.contents.draw_text(0, 32, width-32, 32, $game_party.gold.to_s, 2)
- end
- end
- class Window_PlayerInfo < Window_Base
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize(text)
- # Call Window_Base#initialize
- super(0, 0, 160, 96)
- # Setup Contents
- self.contents = Bitmap.new(width - 32, height - 32)
- # Set instanve Variable
- @text = text
- # Setup Hand Count
- @hand = nil
- # Setup Opacity
- self.opacity = 0
- # Refresh
- refresh
- end
- #--------------------------------------------------------------------------
- # * Set Hand
- #--------------------------------------------------------------------------
- def hand=(value)
- # Return if same
- return if @hand == value
- # Set Hand
- @hand = value
- # Refresh
- refresh
- end
- #--------------------------------------------------------------------------
- # * Refresh
- #--------------------------------------------------------------------------
- def refresh
- self.contents.clear
- self.contents.font.color = system_color
- self.contents.draw_text(4, 0, 128, 32, @text)
- self.contents.font.color = normal_color
- self.contents.draw_text(4, 32, 124, 32, @hand.to_s, 2)
- end
- end
- class Window_BlackJackResult < Window_Base
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- attr_accessor :player
- attr_accessor :dealer
- attr_accessor :winnings
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize
- # Call Window_Base#initialize
- super(176, 192, 288, 96)
- # Setup Contents
- self.contents = Bitmap.new(width - 32, height - 32)
- # Set Player and Dealer Text
- @player, @dealer = nil
- # Set Winnings
- @winnings = 0
- # Set Visibility
- self.visible = false
- end
- #--------------------------------------------------------------------------
- # * Refresh
- #--------------------------------------------------------------------------
- def refresh
- # Get Text Size
- size = (contents.text_size(@player+@dealer).width / 32.0).ceil * 32
- # Dispose Contents
- self.contents.dispose
- # Setup New Width
- self.width = size + 64
- # Setup X
- self.x = (640 - width) / 2
- # Setup Contents
- self.contents = Bitmap.new(width - 32, height - 32)
- # Clear
- self.contents.clear
- # Set to Normal Color
- self.contents.font.color = normal_color
- # Draw Player Text
- self.contents.draw_text(0, 0, width/2-16, 32, @player, 1)
- # Draw Dealer Text
- self.contents.draw_text(width/2-16, 0, width/2-16, 32, @dealer, 1)
- # Get Winnings Text
- if @winnings == 0
- # Draw Text
- self.contents.draw_text(0, 32, width-32, 32, 'Push', 2)
- else
- # Get Text
- text = (@winnings > 0 ? 'Won ' : 'Lost ') + "#{@winnings.abs} "
- # Get Gold Text Size
- cx = contents.text_size($data_system.words.gold).width
- # Draw Text
- self.contents.draw_text(0, 32, width-32-cx, 32, text, 2)
- # Set to System Color
- self.contents.font.color = system_color
- # Draw Gold Text
- self.contents.draw_text(0, 32, width-32, 32, $data_system.words.gold, 2)
- end
- end
- end
- class Window_BlackJackCommand < Window_Selectable
- #--------------------------------------------------------------------------
- # * Public Instance Variables
- #--------------------------------------------------------------------------
- attr_reader :commands
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize
- # Compute window height from command quantity
- super(448, 320, 160, 96)
- # Get Item Max
- @item_max = 2
- # Setup Original Commands
- @commands = %w( Hit Stand )
- # Create Contents
- self.contents = Bitmap.new(width - 32, @item_max * 32)
- # Refresh
- refresh
- # Set Index
- self.index = -1
- # Set Activity
- self.active = false
- # Set Opacity
- self.opacity = 0
- end
- #--------------------------------------------------------------------------
- # * Get Current Command
- #--------------------------------------------------------------------------
- def command
- return @commands[self.index]
- end
- #--------------------------------------------------------------------------
- # * Set Commands
- #--------------------------------------------------------------------------
- def commands=(commands)
- # Return if same commands
- return if @commands == commands
- # If Sizes are not the same
- if @commands.size != commands.size
- # Dispose
- self.contents.dispose
- # Get Item Max
- @item_max = commands.size
- # Setup Height
- self.height = @item_max * 32 + 32
- # Setup Contents
- self.contents = Bitmap.new(width - 32, @item_max * 32)
- end
- # Setup Commands
- @commands = commands
- # Refresh
- refresh
- end
- #--------------------------------------------------------------------------
- # * Refresh
- #--------------------------------------------------------------------------
- def refresh
- # Clear
- self.contents.clear
- # Draw Items
- @item_max.times {|i| draw_item(i)}
- end
- #--------------------------------------------------------------------------
- # * Draw Item
- # index : item number
- #--------------------------------------------------------------------------
- def draw_item(index, color = normal_color)
- # Set Color
- self.contents.font.color = color
- # Setup Rect
- rect = Rect.new(4, 32 * index, self.contents.width - 8, 32)
- # Clear At Rect
- self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
- # Draw At Rect
- self.contents.draw_text(rect, @commands[index])
- end
- #--------------------------------------------------------------------------
- # * Disable Item
- # item : String or Integer
- #--------------------------------------------------------------------------
- def disable_item(item)
- # Get Index
- index = item.is_a?(Numeric) ? item : @commands.index(item)
- # Draw in disabled color
- draw_item(index, disabled_color)
- end
- end
- class Game_BlackJackDeck < Game_Deck
- #--------------------------------------------------------------------------
- # * Initialize
- #--------------------------------------------------------------------------
- def initialize(flag = true)
- # Setup Instance Variable Flag
- @flag = flag
- # Call Game_Deck#initialize
- super
- end
- #--------------------------------------------------------------------------
- # * Setup Cards
- #--------------------------------------------------------------------------
- def setup_cards
- # Run Through number of decks times
- @num_decks.times do
- # Run Through Each Suit
- Suits.each do |suit|
- # Run Through each value
- Values.each do |number|
- # Setup Card
- card = Game_Card.new(suit, number, true, true)
- # Push into cards array
- @deck << card
- end
- end
- end
- end
- #--------------------------------------------------------------------------
- # * Shuffle
- #--------------------------------------------------------------------------
- def shuffle
- # Play Shuffle SE
- $game_system.se_play(BlackJack::Shuffle) if not @flag
- # Setup Flag
- @flag = false if @flag
- # Call Game_Deck#shuffle
- super
- end
- end
- class Game_BlackJackHand < Game_CardHand
- #--------------------------------------------------------------------------
- # * Bust?
- #--------------------------------------------------------------------------
- def bust?
- return self.total > 21
- end
- #--------------------------------------------------------------------------
- # * Black Jack
- #--------------------------------------------------------------------------
- def blackjack?
- return ((@cards[0].ace? && @cards[1].ten?) || (@cards[1].ace? &&
- @cards[0].ten?)) && @cards.size == 2
- end
- #--------------------------------------------------------------------------
- # * Double?
- #--------------------------------------------------------------------------
- def double?
- return @cards.size == 2
- end
- #--------------------------------------------------------------------------
- # * Split
- #--------------------------------------------------------------------------
- def split?
- return @cards[0].value == @cards[1].value && @cards.size == 2
- end
- #--------------------------------------------------------------------------
- # * Aces
- #--------------------------------------------------------------------------
- def aces
- num = 0
- @cards.each {|card| num += 1 if card.ace?}
- return num
- end
- #--------------------------------------------------------------------------
- # * Total
- #--------------------------------------------------------------------------
- def total(ace1 = false)
- # Setup Number
- num = 0
- # Add Up
- @cards.each {|card| num += card.number if card.flipped}
- # Decrease ace value to 1 if over 21
- self.aces.times { num -= 10 if num > 21 || ace1 }
- # Return total
- return num
- end
- #--------------------------------------------------------------------------
- # * Value
- #--------------------------------------------------------------------------
- def value
- return blackjack? ? 'BJ' : bust? ? 'Bust' : total.to_s
- end
- end
- class Numeric
- #--------------------------------------------------------------------------
- # * Sign
- #--------------------------------------------------------------------------
- def sign
- return 0 if self.zero?
- return (self / self.abs).to_i
- end
- end
- class Sprite
- #--------------------------------------------------------------------------
- # * Move the sprite
- # x : x coordinate of the destination point
- # y : y coordinate of the destination point
- # speed : Speed of movement
- #--------------------------------------------------------------------------
- def move(x, y, speed = 1)
- # Set Destination Points speed and move count set moving flag
- @destination_x = x
- @destination_y = y
- @move_speed = speed
- @moving = true
- # Get the distance + (negative if to left or up positive if down or right)
- @distance_x = (@destination_x - self.x).to_f
- @distance_y = (@destination_y - self.y).to_f
- # Get slant distance (hypotenuse of the triangle (xf,yi) (xi,yf) and (xf,yf))
- @distance = Math.sqrt(@distance_x ** 2 + @distance_y ** 2)
- # Calculate angle of movement which is later used to determine direction
- # If X distance is 0 (Prevent Infinity Error)
- if @distance_x == 0
- # The Angle is sign(distance_y) * - π / 2 (90° or 270°)
- @angle = @distance_y.sign * Math::PI / 2
- # If Y distance is 0 (Prevent Incorrect Direction for later)
- elsif @distance_y == 0
- # The Angle is sign(distance_x) - 1 * π / 2 (0° or 180°)
- @angle = (@distance_x.sign - 1) * Math::PI / 2
- else
- # The Angle is the Arctangent of @distance_y / @distance_x (slope)
- # Returns [-π,π]
- @angle = Math.atan2(@distance_y, @distance_x.to_f)
- end
- # Convert the angle to degrees
- @angle *= 180 / Math::PI
- end
- #--------------------------------------------------------------------------
- # * Update Move
- #--------------------------------------------------------------------------
- if @moving_sprite_update.nil?
- alias moving_sprite_update update
- @moving_sprite_update = true
- end
- def update
- moving_sprite_update
- update_move
- end
- #--------------------------------------------------------------------------
- # * Moving?
- #--------------------------------------------------------------------------
- def moving?
- return @moving
- end
- #--------------------------------------------------------------------------
- # * Update Move
- #--------------------------------------------------------------------------
- def update_move
- # If not moving
- if (self.x == @destination_x and self.y == @destination_y) or not @moving
- @moving = false
- return
- end
- # move increase x = the cosine of the arctangent of the dist x over dist y
- # move increase x = cos(arctan(disty/distx)) simplified by trigonometry
- # to distance_x / slant_distance, the sprite moves (move speed)
- # along the slanted line (if it is slanted)
- movinc_x = @move_speed * @distance_x.abs / @distance
- # same reasoning with y increase except it is the sine of the arctangent
- # move increase y = sin(arctan(disty/distx)) simplified by trigonometry
- # to distance_y / slant_distance
- movinc_y = @move_speed * @distance_y.abs / @distance
- # Move the sign of the distance left + move increase or the remaining distance
- # left if it will go past that point
- if @move_speed != 0
- # Get distance remaining
- remain_x = (@destination_x - self.x).abs
- remain_y = (@destination_y - self.y).abs
- self.x += (@destination_x - self.x).sign * [movinc_x.ceil, remain_x].min
- self.y += (@destination_y - self.y).sign * [movinc_y.ceil, remain_y].min
- end
- # If Destination Reached stop moving
- if self.x == @destination_x and self.y == @destination_y
- @moving = false
- return
- end
- end
- end
- class Sprite_BlackJackCard < Sprite_Card
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize(card, flipped = true)
- super(card, flipped)
- # Setup Y
- self.y = -bitmap.height
- # Setup X
- self.x = 320 + bitmap.width
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement