The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX => VX Scripts Database => Topic started by: Zylos on March 22, 2011, 04:32:33 AM

Title: Mastermind (VX)
Post by: Zylos on March 22, 2011, 04:32:33 AM
Mastermind (VX)
Version: 1.0
Author: Zylos
Date: March 21, 2011

Description


This script sets up a classic Mastermind minigame, in which the player must try to guess the secret sequence of colors that the opponent has chosen. Each turn, the player selects a four piece sequence using six different colors, and the opponent responds by giving the player black and white pegs accordingly. A black peg means that the player has a right color piece in the right position, while a white peg means that the player has a right color piece but in the wrong position. The game ends when the player either guesses correctly or has failed to figure it out after eight turns.

Features


Screenshots

(https://rmrk.net/proxy.php?request=http%3A%2F%2Fi254.photobucket.com%2Falbums%2Fhh99%2FZylos_2007%2FMastermind.png&hash=6d706be8964cfd39be595a4652f63a3d8cd6434b)

Instructions


Script


NOTE! You must download these graphics to your game's pictures folder.
You can either download them individually right here, download them all in the RAR attached to the bottom of this thread here (http://rmrk.net/index.php?action=dlattach;topic=42143.0;attach=22793), or find them in the demo.

Spoiler for Required Graphics:
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_side.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_color5.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_color4.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_color3.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_color2.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_color1.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_color0.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_board.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_black.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_answer.png
http://i254.photobucket.com/albums/hh99/Zylos_2007/mm_white.png

Code: [Select]
#==============================================================================
#  Mastermind (VX)
#  Version: 1.0
#  Author: Zylos (rmrk.net)
#  Date: March 21, 2011
#------------------------------------------------------------------------------
#  Description:
#
#   This script sets up a classic Mastermind minigame, in which the player must
#   try to guess the secret sequence of colors that the opponent has chosen.
#   Each turn, the player selects a four piece sequence using six different
#   colors, and the opponent responds by giving the player black and white
#   pegs accordingly. A black peg means that the player has a right color piece
#   in the right position, while a white peg means that the player has a right
#   color piece but in the wrong position. The game ends when the player either
#   guesses correctly or has failed to figure it out after eight turns.
#
#------------------------------------------------------------------------------
#  Instructions:
#   
#     - Place this script in the materials section, above Main.
#
#     - Copy the game board and piece graphics to your game's pictures folder.
#       If you do not already have them, they can be found at:
#       http://rmrk.net/index.php/topic,42143.0.html
#
#     - Check the script options below these instructions to tweak the game's
#       appearance to your likings or change the winning and losing sounds.
#
#     - To start the minigame, simply call it with an event by using the
#       script command and entering "call_mastermind" without the quotation
#       marks. It'll automatically initiate the game.
#
#==============================================================================

module Mastermind_Options
  #============================================================================
  # EDITABLE REGION:
  #============================================================================
 
  Win_Lose = true                       # Show the Win Loss Ratio window
  Side = true                           # Show the side decoration graphic
  Black_Background = false              # Show a black background
  Game_Music = false                    # Plays a different BGM during game
  Game_BGM = "Airship.MID"              # BGM played during the game
  Win_Sound = "Applause.OGG"            # Sound played when game is won
  Win_Sound_Type = "SE"                 # Type of sound Win_Sound is (ME/SE)
  Loss_Sound = "Collapse2.OGG"          # Sound played when game is lost
  Loss_Sound_Type = "SE"                # Type of sound Loss_Sound is (ME/SE)

  #============================================================================
  # END EDITABLE REGION
  #============================================================================
end

#==============================================================================
# ** Game_System
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Adding the wins and losses variables to the game here.
#==============================================================================

class Game_System
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :wins                    # number of wins
  attr_accessor :losses                  # number of losses
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias_method :winlossratio, :initialize
  def initialize
    winlossratio
    @wins = 0
    @losses = 0
  end
end

#==============================================================================
# ** Game_Interpreter
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Adding the mastermind command.
#==============================================================================

class Game_Interpreter
  #--------------------------------------------------------------------------
  # * Call Mastermind
  #--------------------------------------------------------------------------
  def call_mastermind
    $game_temp.next_scene = "mastermind"
  end
end

#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  Adding the change to Scene_Mastermind.
#==============================================================================

class Scene_Map
  #--------------------------------------------------------------------------
  # * Execute Screen Switch
  #--------------------------------------------------------------------------
  alias_method :scene_change, :update_scene_change
  def update_scene_change
    return if $game_player.moving?   
    if $game_temp.next_scene == "mastermind"
      call_mastermind
    else
      scene_change
    end
  end
  #--------------------------------------------------------------------------
  # * Switch to Mastermind
  #-------------------------------------------------------------------------- 
  def call_mastermind
    $game_temp.next_scene = nil
    $scene = Scene_Mastermind.new
  end
end

#==============================================================================
# ** Window_MastermindRatio
#------------------------------------------------------------------------------
#  This window displays yours wins versus your losses in Mastermind.
#==============================================================================

class Window_MastermindRatio < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x : window X coordinate
  #     y : window Y coordinate
  #--------------------------------------------------------------------------
  def initialize(x, y)
    super(x, y, 135, 80)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.font.color = system_color
    self.contents.draw_text(4, 0, 90, WLH, "Wins:")
    self.contents.font.color = normal_color
    self.contents.draw_text(4, 0, 90, WLH, $game_system.wins, 2)
    self.contents.font.color = system_color
    self.contents.draw_text(4, 0, 90, WLH*2.6, "Losses:")
    self.contents.font.color = normal_color
    self.contents.draw_text(4, 0, 90, WLH*2.6, $game_system.losses, 2)
  end
end

#==============================================================================
# ** Scene_Mastermind
#------------------------------------------------------------------------------
#  This class performs the Mastermind screen processing.
#==============================================================================

class Scene_Mastermind < Scene_Base
  #--------------------------------------------------------------------------
  # * Start processing
  #--------------------------------------------------------------------------
  def start
    super
    if Mastermind_Options::Game_Music == true
      @last_bgm = RPG::BGM::last
      @last_bgs = RPG::BGS::last
      RPG::BGS.stop
      temp_sound = "Audio/BGM/" + Mastermind_Options::Game_BGM
      Audio.bgm_play(temp_sound, 100, 100)
    end
    @play_vocab = "Play game?"
    @cancel_vocab = "Quit"
    create_menu_background if Mastermind_Options::Black_Background == false
    create_command_window
    create_command_window_2
    if Mastermind_Options::Win_Lose == true
      @winloss = Window_MastermindRatio.new(5.5, 10)
    end
    @board_sprite = Sprite.new(Viewport.new(0, 0, 544, 416))
    @board_sprite.bitmap = Cache.picture("mm_board")
    @board_sprite.x = 0
    @board_sprite.y = 0
    @board_sprite.z = 1
    @answer_sprite = Sprite.new
    @answer_sprite.bitmap = Cache.picture("mm_answer")
    @answer_sprite.x = 212
    @answer_sprite.y = -25
    @answer_sprite.z = 10
    if Mastermind_Options::Side == true
      @side_sprite = Sprite.new
      @side_sprite.bitmap = Cache.picture("mm_side")
      @side_sprite.x = 399 + (146 - @side_sprite.width) / 2
      @side_sprite.y = (416 - @side_sprite.height) / 2
      @side_sprite.z = 1
    end
    @color_sprite = []
    @peg_sprite = []
    @guess = []
    @guess_used = []
    @answer = []
    @answer_used = []
    @color_index = 0
    @peg_index = 0
    @game_mode = "start"
  end
  #--------------------------------------------------------------------------
  # * Post-Start Processing
  #--------------------------------------------------------------------------
  def post_start
    super
    open_command_window
  end
  #--------------------------------------------------------------------------
  # * Wait
  #--------------------------------------------------------------------------
  def wait(duration)
    for i in 0...duration
      Graphics.update
    end
  end
  #--------------------------------------------------------------------------
  # * Pre-termination Processing
  #--------------------------------------------------------------------------
  def pre_terminate
    super
    close_command_window
    close_command_window_2
  end
  #--------------------------------------------------------------------------
  # * Termination Processing
  #--------------------------------------------------------------------------
  def terminate
    super
    dispose_menu_background if Mastermind_Options::Black_Background == false
    dispose_command_window
    @board_sprite.dispose
    @answer_sprite.dispose
    @side_sprite.dispose if Mastermind_Options::Side == true
    @winloss.dispose if Mastermind_Options::Win_Lose == true
    if @color_index != 0
      for i in 0...@color_index
        @color_sprite[i].dispose
      end
      for i in 0...@peg_index
        @peg_sprite[i].dispose
      end
    end
    if Mastermind_Options::Game_Music == true
      @last_bgm.play
      @last_bgs.play
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    update_menu_background if Mastermind_Options::Black_Background == false
    @command_window.update
    @command_window_2.update
    @winloss.update if Mastermind_Options::Win_Lose == true
    if @game_mode == "start"
      if Input.trigger?(Input::C)
        case @command_window.index
        when 0
          Sound.play_decision
          close_command_window
          cover_answer
        when 1
          Sound.play_cancel
          $scene= Scene_Map.new
        end
      end
    elsif @game_mode == "color"
      if Input.trigger?(Input::B)
        if @guess_index == 0
          Sound.play_cancel
          close_command_window_2
          @play_vocab = "Continue"
          @cancel_vocab = "Quit"
          @command_window.index = 0
          @command_window.dispose
          create_command_window
          open_command_window
          open_command_window
          @game_mode = "quit"
        else
          case @guess_index
          when 1
            @guess[0] = -1
          when 2
            @guess[1] = -1
          when 3
            @guess[2] = -1
          end
          @guess_index -= 1
          @color_index -= 1
          Sound.play_cancel
          @color_sprite[@color_index].dispose
        end
      end
      if Input.trigger?(Input::C)
        if @guess_index != 4
          Sound.play_decision
          @go = @command_window_2.index
          @guess[@guess_index] = @go
          @color_sprite[@color_index] = Sprite.new
          @color_sprite[@color_index].bitmap=Cache.picture("mm_color"+@go.to_s)
          @color_sprite[@color_index].x = 228 + (@guess_index * 35)
          @color_sprite[@color_index].y = 360 - (@round_index * 40)
          @color_sprite[@color_index].z = 2
          @guess_index += 1
          @color_index += 1
          if @guess_index == 4
            @game_mode = "peg"
            close_command_window_2
            @command_window.index = 0
            @command_window_2.index = 0
            wait(15)
            check_colors
          end
        end
      end
    elsif @game_mode == "quit"
      if Input.trigger?(Input::C)
        case @command_window.index
        when 0
          Sound.play_decision
          close_command_window
          open_command_window_2
          @game_mode = "color"
        when 1
          Sound.play_cancel
          $scene= Scene_Map.new
        end 
      end
    elsif @game_mode == "win" or @game_mode == "lose"
      if @command_window.openness == 255
        if Input.trigger?(Input::C)
        case @command_window.index
        when 0
          Sound.play_decision
          opac = 250
          close_command_window
          wait(8)
          begin
            opac -= 10
            for i in 0...@color_index
              @color_sprite[i].opacity = opac
            end
            for i in 0...@peg_index
              @peg_sprite[i].opacity = opac
            end
            wait(2)
          end until opac == 0
          for i in 0...@color_index
            @color_sprite[i].dispose
          end
          for i in 0...@peg_index
            @peg_sprite[i].dispose
          end
          wait(10)
          cover_answer
        when 1
          Sound.play_cancel
          $scene= Scene_Map.new
        end
      end
      end
    end 
  end 
  #--------------------------------------------------------------------------
  # * Update Background for Menu Screen
  #--------------------------------------------------------------------------
  def update_menu_background
    super
    @menuback_sprite.tone.set(0, 0, 0, 128)
  end
  #--------------------------------------------------------------------------
  # * Game Start
  #--------------------------------------------------------------------------
  def cover_answer
    @round_index = 0
    @color_index = 0
    @peg_index = 0
    @guess[0] = -1
    @guess[1] = -1
    @guess[2] = -1
    @guess[3] = -1
    @guess_used[0] = false
    @guess_used[1] = false
    @guess_used[2] = false
    @guess_used[3] = false
    @guess_index = 0
    @answer[0] = -1
    @answer[1] = -1
    @answer[2] = -1
    @answer[3] = -1
    @answer_used[0] = false
    @answer_used[1] = false
    @answer_used[2] = false
    @answer_used[3] = false
    @peg_hole = 0
    @j = 0
    @k = 0   
    @distance = 19 - (@answer_sprite.y)
    @iindex = 0
    RPG::ME.stop
    RPG::SE.stop
    for i in 0...4
      Sound.play_cursor
      temp_answer = rand(5)
      @color_sprite[i] = Sprite.new
      @color_sprite[i].bitmap=Cache.picture("mm_color"+temp_answer.to_s)
      @color_sprite[i].x = 228 + (i * 35)
      @color_sprite[i].y = 29
      @color_sprite[i].z = 2
      @color_index += 1
      wait(10)
    end
    while @distance > 0
      @answer_sprite.y += 1
      temp_answer = rand(5)
      @color_sprite[@iindex].bitmap=Cache.picture("mm_color"+temp_answer.to_s)
      @iindex += 1
      if @iindex == 4
        @iindex = 0
      end
      @distance = 19 - (@answer_sprite.y)
      Sound.play_cursor
      wait(2)
    end
    for i in 0...4
      @answer[i] = rand(5)
      @color_sprite[i].bitmap=Cache.picture("mm_color"+@answer[i].to_s)
      @color_sprite[i].z = 2
    end
    open_command_window_2
    @game_mode = "color"
  end
  #--------------------------------------------------------------------------
  # * Game End
  #--------------------------------------------------------------------------
  def uncover_answer
    begin
      @answer_sprite.y -= 1
      wait(2)
    end until @answer_sprite.y == -25
    @play_vocab = "Rematch"
    @cancel_vocab = "Quit"
    @command_window.dispose
    create_command_window
    open_command_window 
  end
  #--------------------------------------------------------------------------
  # * Round End
  #--------------------------------------------------------------------------
  def check_colors
    for i in 0...4
      if @guess[i] == @answer[i]
        @guess_used[i] = true
        @answer_used[i] = true
        @peg_sprite[@peg_index] = Sprite.new
        @peg_sprite[@peg_index].bitmap = Cache.picture("mm_black")
        case @peg_hole
        when 0
          @peg_sprite[@peg_index].x = 184
          @peg_sprite[@peg_index].y = 358 - (@round_index * 40)
        when 1
          @peg_sprite[@peg_index].x = 198
          @peg_sprite[@peg_index].y = 358 - (@round_index * 40)
        when 2
          @peg_sprite[@peg_index].x = 184
          @peg_sprite[@peg_index].y = 372 - (@round_index * 40)
        when 3
          @peg_sprite[@peg_index].x = 198
          @peg_sprite[@peg_index].y = 372 - (@round_index * 40)
        end
        @peg_sprite[@peg_index].z = 2
        @peg_index += 1
        @peg_hole += 1
        if @peg_hole == 4
          @game_mode = "win"
        end
      end
    end
    while @j <= 3
      if @j != @k
        if @guess[@j] == @answer[@k]
          if @guess_used[@j] == false
            if @answer_used[@k] == false
              @guess_used[@j] = true
              @answer_used[@k] = true
              @peg_sprite[@peg_index] = Sprite.new
              @peg_sprite[@peg_index].bitmap = Cache.picture("mm_white")
              case @peg_hole
              when 0
                @peg_sprite[@peg_index].x = 184
                @peg_sprite[@peg_index].y = 358 - (@round_index * 40)
              when 1
                @peg_sprite[@peg_index].x = 198
                @peg_sprite[@peg_index].y = 358 - (@round_index * 40)
              when 2
                @peg_sprite[@peg_index].x = 184
                @peg_sprite[@peg_index].y = 372 - (@round_index * 40)
              when 3
                @peg_sprite[@peg_index].x = 198
                @peg_sprite[@peg_index].y = 372 - (@round_index * 40)
              end
              @peg_sprite[@peg_index].z = 2
              @peg_index += 1
              @peg_hole += 1
            end
          end
        end
      end
      @k += 1
      if @k == 4
        @j += 1
        @k = 0
      end
    end
    @guess[0] = -1
    @guess[1] = -1
    @guess[2] = -1
    @guess[3] = -1
    @guess_used[0] = false
    @guess_used[1] = false
    @guess_used[2] = false
    @guess_used[3] = false
    @guess_index = 0
    @answer_used[0] = false
    @answer_used[1] = false
    @answer_used[2] = false
    @answer_used[3] = false
    @peg_hole = 0
    @j = 0
    @k = 0
    wait(15)
    if @round_index == 7 and @game_mode != "win"
      @game_mode = "lose"
      if Mastermind_Options::Loss_Sound_Type == "SE"
        temp_sound = "Audio/SE/" + Mastermind_Options::Loss_Sound
        Audio.se_play(temp_sound, 100, 100)
      elsif Mastermind_Options::Loss_Sound_Type == "ME"
        temp_sound = "Audio/ME/" + Mastermind_Options::Loss_Sound       
        Audio.me_play(temp_sound, 100, 100)
      end
      $game_system.losses += 1
      @winloss.refresh if Mastermind_Options::Win_Lose == true
      uncover_answer
    elsif @game_mode == "win"
      if Mastermind_Options::Win_Sound_Type == "SE"
        temp_sound = "Audio/SE/" + Mastermind_Options::Win_Sound
        Audio.se_play(temp_sound, 100, 100)
      elsif Mastermind_Options::Win_Sound_Type == "ME"
        temp_sound = "Audio/ME/" + Mastermind_Options::Win_Sound       
        Audio.me_play(temp_sound, 100, 100)
      end
      $game_system.wins += 1
      @winloss.refresh if Mastermind_Options::Win_Lose == true
      uncover_answer
    else
      @round_index += 1
      open_command_window_2
      @game_mode = "color"
    end
  end
  #--------------------------------------------------------------------------
  # * Create Command Window
  #--------------------------------------------------------------------------
  def create_command_window
    s1 = @play_vocab
    s2 = @cancel_vocab
    @command_window = Window_Command.new(142, [s1, s2])
    @command_window.x = (544 - @command_window.width) / 2
    @command_window.y = (416 - @command_window.height) / 2
    @command_window.openness = 0
  end
  #--------------------------------------------------------------------------
  # * Dispose of Command Window
  #--------------------------------------------------------------------------
  def dispose_command_window
    @command_window.dispose
    @command_window_2.dispose
  end
  #--------------------------------------------------------------------------
  # * Open Command Window
  #--------------------------------------------------------------------------
  def open_command_window
    @command_window.open
    begin
      @command_window.update
      Graphics.update
    end until @command_window.openness == 255
    @command_window.active = true
  end
  #--------------------------------------------------------------------------
  # * Close Command Window
  #--------------------------------------------------------------------------
  def close_command_window
    @command_window.close
    begin
      @command_window.update
      Graphics.update
    end until @command_window.openness == 0
    @command_window.active = false
  end
  #--------------------------------------------------------------------------
  # * Create Command Window Two
  #--------------------------------------------------------------------------
  def create_command_window_2
    s1 = "Red"
    s2 = "Orange"
    s3 = "Yellow"
    s4 = "Green"
    s5 = "Blue"
    s6 = "Purple"
    @command_window_2 = Window_Command.new(105, [s1, s2, s3, s4, s5, s6])
    @command_window_2.x = (146 - @command_window_2.width) / 2
    if Mastermind_Options::Win_Lose == false
      @command_window_2.y = (416 - @command_window_2.height) / 2
    else
      @command_window_2.y = 210
    end
    @command_window_2.openness = 0
    @command_window_2.active = false
  end
  #--------------------------------------------------------------------------
  # * Open Command Window Two
  #--------------------------------------------------------------------------
  def open_command_window_2
    @command_window_2.open
    begin
      @command_window_2.update
      Graphics.update
    end until @command_window_2.openness == 255
    @command_window_2.active = true
  end
  #--------------------------------------------------------------------------
  # * Close Command Window Two
  #--------------------------------------------------------------------------
  def close_command_window_2
    @command_window_2.close
    begin
      @command_window_2.update
      Graphics.update
    end until @command_window_2.openness == 0
    @command_window_2.active = false
  end
end

Credit



Demo


See Attached.

Author's Notes


I bet most of you misread this topic title on first glance. Perverts. :V

Also, as you'll see in the demo, you can use this script to let the player earn items based on how many games he wins (or loses). Feel free to use the tutorial in the demo as well, to explain to the player how the game works.
Title: Re: Mastermind (VX)
Post by: modern algebra on March 22, 2011, 12:20:20 PM
That's a pretty cool minigame Zylos. Nice job!
Title: Re: Mastermind (VX)
Post by: Bravo2Kilo on March 23, 2011, 02:08:54 PM
this is really great Zylos
this is the best minigame i have seen and played
it is really fun and it can also be challenging
Title: Re: Mastermind (VX)
Post by: CodeMaster on March 23, 2011, 03:30:29 PM
Looks like a great minigame but is there a way to give a reward or turn on a switch if you win.
Title: Re: Mastermind (VX)
Post by: Zylos on March 23, 2011, 04:58:40 PM
@Codemaster - There certainly is. (https://rmrk.net/proxy.php?request=http%3A%2F%2Fi254.photobucket.com%2Falbums%2Fhh99%2FZylos_2007%2FSmilies%2Fzylos_wiggle.gif&hash=5472339e93805aefa575f992659ab29779a798b8)

The wins and losses are recorded for usage as $game_system.wins and $game_system.losses. So, let's say you want to give out an item after a certain number of wins (we'll call it X for now) like in the demo. After calling the game up, have a conditional branch check if "$game_system.wins >= X" and another conditional branch checking a switch or self-switch inside of it to see if the player has already earned that prize. You then simply hand out the award and turn on the switch, and voila. :3
Title: Re: Mastermind (VX)
Post by: CodeMaster on March 23, 2011, 07:17:39 PM
How do you reset the $game_system.wins or $game_system.losses to zero ???
Title: Re: Mastermind (VX)
Post by: Zylos on March 23, 2011, 07:19:56 PM
You can do that by using an event's script command and just entering "$game_system.wins = 0" or "$game_system.losses = 0".
Title: Re: Mastermind (VX)
Post by: CodeMaster on March 23, 2011, 07:23:58 PM
Thanks Zylos
Title: Re: Mastermind (VX)
Post by: EvilM00s on March 06, 2014, 05:54:34 PM
Going in my new project!