Mastermind Minigame for RPGVXA?

Spoiler: Mastermind hard 640 x 480
Code:
#==============================================================================
#  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 = true              # Show a black background
    Game_Music = false                    # Plays a different BGM during game
    Game_BGM = ""              # BGM played during the game
    Win_Sound = ""            # Sound played when game is won
    Win_Sound_Type = ""                 # Type of sound Win_Sound is (ME/SE)
    Loss_Sound = ""          # Sound played when game is lost
    Loss_Sound_Type = ""                # 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
        SceneManager.call(Scene_Mastermind)
    end
end

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

#~ class Scene_Map
#~ #--------------------------------------------------------------------------
#~ # * Execute Screen Switch
#~ #--------------------------------------------------------------------------
#~ alias kylos_mm_update_scene_change scene_change
#~ def kylos_mm_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, line_height, "Wins:")
        self.contents.font.color = normal_color
        self.contents.draw_text(4, 0, 90, line_height, $game_system.wins, 2)
        self.contents.font.color = system_color
        self.contents.draw_text(4, 0, 90, line_height*2.6, "Losses:")
        self.contents.font.color = normal_color
        self.contents.draw_text(4, 0, 90, line_height*2.6, $game_system.losses, 2)
    end
end

class Window_MastermindCommand < Window_Command
    def initialize(x, y)
        super(x, y)
    end
   
    def process_handling
        
    end
   
    def make_command_list
        s1 = "Play Normal Game?"
        s2 = "Play Hard Game?"
        s3 = "Quit"
        add_command(s1, :s1)
        add_command(s2, :s2)
        add_command(s3, :s3)
    end
   
end


class Window_MastermindCommand_Color < Window_Command
    def initialize(x, y)
        super(x, y)
    end
   
    def make_command_list
        s1 = "Red"
        s2 = "Orange"
        s3 = "Yellow"
        s4 = "Green"
        s5 = "Blue"
        s6 = "Purple"
        add_command(s1, :s1a)
        add_command(s2, :s2a)
        add_command(s3, :s3a)
        add_command(s4, :s4)
        add_command(s5, :s5)
        add_command(s6, :s6)
    end
   
    def process_handling
        
    end
   
    def visible_line_number
        return 6
    end
   
end


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

class Scene_Mastermind < Scene_MenuBase
    #--------------------------------------------------------------------------
    # * 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
        @playe_vocab = "Play Normal Game?"
        @playh_vocab = "Play Hard Game?"
        @cancel_vocab = "Quit"
        create_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, 640, 480))
        @board_sprite.bitmap = Cache.picture("mm_board")
        @board_sprite.x = 50
        @board_sprite.y = 32
        @board_sprite.z = 1
        @answer_sprite = Sprite.new
        @answer_sprite.bitmap = Cache.picture("mm_answer")
        @answer_sprite.x = 260
        @answer_sprite.y = 5
        @answer_sprite.z = 10
        @hard_sprite = Sprite.new(Viewport.new(0, 0, 640, 480))
        @hard_sprite.bitmap = Cache.picture("mm_hard")
        @hard_sprite.x = 50
        @hard_sprite.y = 32
        @hard_sprite.z = 2
        if Mastermind_Options::Side == true
            @side_sprite = Sprite.new
            @side_sprite.bitmap = Cache.picture("mm_side")
            @side_sprite.x = 420 + (146 - @side_sprite.width) / 2
            @side_sprite.y = (480 - @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_command_window
        @board_sprite.dispose
        @answer_sprite.dispose
        @hard_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 tests
        # p 1
    # end
    def easy_terminate
        @hard_sprite.dispose
    end
   
    def update
        super
        #update_menu_background if Mastermind_Options::Black_Background == false
        @winloss.update if Mastermind_Options::Win_Lose == true
        if @game_mode == "start"
            if Input.press?(:C)
                case @command_window.index
                when 0
                    Sound.play_ok
                    close_command_window
                    cover_answer
                    easy_terminate
                when 1
                    Sound.play_ok
                    close_command_window
                    cover_answer
                when 2
                    Sound.play_cancel
                    SceneManager.return
                end
            end
        elsif @game_mode == "color"
            if Input.trigger?(:C)
                if @guess_index != 4
                    Sound.play_ok
                    @go = @command_window_2.index
                    @guess[@guess_index] = @go
                    @color_sprite[@color_index] = Sprite.new
                    @color_sprite[@color_index].bitmap=Cache.picture("mm_color"[email hidden]_s)
                    @color_sprite[@color_index].x = 278 + (@guess_index * 35)
                    @color_sprite[@color_index].y = 390 - (@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
            if Input.trigger?(:B)
                if @guess_index == 0
                    Sound.play_cancel
                    close_command_window_2
                    @playe_vocab = "Continue"
                    @playh_vocab = "Continue"
                    @cancel_vocab = "Quit"
                    @command_window.index = 0
                    @command_window.dispose
                    create_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
        elsif @game_mode == "quit"
            if Input.press?(:C)
                case @command_window.index
                when 0
                    Sound.play_ok
                    close_command_window
                    open_command_window_2
                    @game_mode = "color"
                when 1
                    Sound.play_ok
                    close_command_window
                    open_command_window_2
                    @game_mode = "color"
                when 2
                    Sound.play_cancel
                    SceneManager.return
                end
            end
        elsif @game_mode == "win" or @game_mode == "lose"
            if @command_window.openness == 255
                if Input.trigger?(:C)
                    case @command_window.index
                    when 0
                        Sound.play_ok
                        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
                        easy_terminate
                    when 1
                        Sound.play_ok
                        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
                        easy_terminate
                        @hard_sprite = Sprite.new(Viewport.new(0, 0, 640, 480))
                        @hard_sprite.bitmap = Cache.picture("mm_hard")
                        @hard_sprite.x = 50
                        @hard_sprite.y = 32
                        @hard_sprite.z = 2
                    when 2
                        Sound.play_cancel
                        SceneManager.return
                    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
      if @command_window.index == 1
        @round_index = 2
      else
        @round_index = 0
      end
        @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 = 50 - (@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 = 278 + (i * 35)
            @color_sprite[i].y = 60
            @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 = 50 - (@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 == 5
        @playe_vocab = "Try Harder"
        @playh_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 = 234
                    @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                when 1
                    @peg_sprite[@peg_index].x = 248
                    @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                when 2
                    @peg_sprite[@peg_index].x = 234
                    @peg_sprite[@peg_index].y = 402 - (@round_index * 40)
                when 3
                    @peg_sprite[@peg_index].x = 248
                    @peg_sprite[@peg_index].y = 402 - (@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 = 234
                                @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                            when 1
                                @peg_sprite[@peg_index].x = 248
                                @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                            when 2
                                @peg_sprite[@peg_index].x = 234
                                @peg_sprite[@peg_index].y = 402 - (@round_index * 40)
                            when 3
                                @peg_sprite[@peg_index].x = 248
                                @peg_sprite[@peg_index].y = 402 - (@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
        @command_window = Window_MastermindCommand.new(0, 0)
        @command_window.x = (Graphics.width - @command_window.width) / 2
        @command_window.y = (Graphics.height - @command_window.height) / 2
        @command_window.height = @command_window.fitting_height(3)
        @command_window.refresh
        @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
        @command_window.refresh
        @command_window.active = true
    end
    #--------------------------------------------------------------------------
    # * Close Command Window
    #--------------------------------------------------------------------------
    def close_command_window
        @command_window.close
        @command_window.refresh
        @command_window.active = false
    end
    #--------------------------------------------------------------------------
    # * Create Command Window Two
    #--------------------------------------------------------------------------
    def create_command_window_2
        @command_window_2 = Window_MastermindCommand_Color.new(0, 0)
        @command_window_2.x = (200 - @command_window_2.width) / 2
        @command_window_2.height = @command_window_2.fitting_height(6)
        if Mastermind_Options::Win_Lose == false
            @command_window_2.y = (Graphics.height - @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
        @command_window_2.active = true
        @command_window_2.refresh
    end
    #--------------------------------------------------------------------------
    # * Close Command Window Two
    #--------------------------------------------------------------------------
    def close_command_window_2
        @command_window_2.close
        @command_window_2.active = false
        @command_window_2.refresh
    end
end
Spoiler: Mastermind hard 640 x 480 no repeat color
Code:
#==============================================================================
#  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 = true              # Show a black background
    Game_Music = false                    # Plays a different BGM during game
    Game_BGM = ""              # BGM played during the game
    Win_Sound = ""            # Sound played when game is won
    Win_Sound_Type = ""                 # Type of sound Win_Sound is (ME/SE)
    Loss_Sound = ""          # Sound played when game is lost
    Loss_Sound_Type = ""                # 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
        SceneManager.call(Scene_Mastermind)
    end
end

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

#~ class Scene_Map
#~ #--------------------------------------------------------------------------
#~ # * Execute Screen Switch
#~ #--------------------------------------------------------------------------
#~ alias kylos_mm_update_scene_change scene_change
#~ def kylos_mm_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, line_height, "Wins:")
        self.contents.font.color = normal_color
        self.contents.draw_text(4, 0, 90, line_height, $game_system.wins, 2)
        self.contents.font.color = system_color
        self.contents.draw_text(4, 0, 90, line_height*2.6, "Losses:")
        self.contents.font.color = normal_color
        self.contents.draw_text(4, 0, 90, line_height*2.6, $game_system.losses, 2)
    end
end

class Window_MastermindCommand < Window_Command
    def initialize(x, y)
        super(x, y)
    end
   
    def process_handling
        
    end
   
    def make_command_list
        s1 = "Play Normal Game?"
        s2 = "Play Hard Game?"
        s3 = "Quit"
        add_command(s1, :s1)
        add_command(s2, :s2)
        add_command(s3, :s3)
    end
   
end


class Window_MastermindCommand_Color < Window_Command
    def initialize(x, y)
        super(x, y)
    end
   
    def make_command_list
        s1 = "Red"
        s2 = "Orange"
        s3 = "Yellow"
        s4 = "Green"
        s5 = "Blue"
        s6 = "Purple"
        add_command(s1, :s1a)
        add_command(s2, :s2a)
        add_command(s3, :s3a)
        add_command(s4, :s4)
        add_command(s5, :s5)
        add_command(s6, :s6)
    end
   
    def process_handling
        
    end
   
    def visible_line_number
        return 6
    end
   
end


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

class Scene_Mastermind < Scene_MenuBase
    #--------------------------------------------------------------------------
    # * 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
        @playe_vocab = "Play Normal Game?"
        @playh_vocab = "Play Hard Game?"
        @cancel_vocab = "Quit"
        create_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, 640, 480))
        @board_sprite.bitmap = Cache.picture("mm_board")
        @board_sprite.x = 50
        @board_sprite.y = 32
        @board_sprite.z = 1
        @answer_sprite = Sprite.new
        @answer_sprite.bitmap = Cache.picture("mm_answer")
        @answer_sprite.x = 260
        @answer_sprite.y = 5
        @answer_sprite.z = 10
        @hard_sprite = Sprite.new(Viewport.new(0, 0, 640, 480))
        @hard_sprite.bitmap = Cache.picture("mm_hard")
        @hard_sprite.x = 50
        @hard_sprite.y = 32
        @hard_sprite.z = 2
        if Mastermind_Options::Side == true
            @side_sprite = Sprite.new
            @side_sprite.bitmap = Cache.picture("mm_side")
            @side_sprite.x = 420 + (146 - @side_sprite.width) / 2
            @side_sprite.y = (480 - @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_command_window
        @board_sprite.dispose
        @answer_sprite.dispose
        @hard_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 tests
#~         p 1
#~     end
    def easy_terminate
        @hard_sprite.dispose
    end
   
    def update
        super
        #update_menu_background if Mastermind_Options::Black_Background == false
        @winloss.update if Mastermind_Options::Win_Lose == true
        if @game_mode == "start"
            if Input.press?(:C)
                case @command_window.index
                when 0
                    Sound.play_ok
                    close_command_window
                    cover_answer
                    easy_terminate
                when 1
                    Sound.play_ok
                    close_command_window
                    cover_answer
                when 2
                    Sound.play_cancel
                    SceneManager.return
                end
            end
        elsif @game_mode == "color"
            if Input.trigger?(:C)
                if @guess_index != 4
                    Sound.play_ok
                    @go = @command_window_2.index
                    @guess[@guess_index] = @go
                    @color_sprite[@color_index] = Sprite.new
                    @color_sprite[@color_index].bitmap=Cache.picture("mm_color"[email hidden]_s)
                    @color_sprite[@color_index].x = 278 + (@guess_index * 35)
                    @color_sprite[@color_index].y = 390 - (@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
            if Input.trigger?(:B)
                if @guess_index == 0
                    Sound.play_cancel
                    close_command_window_2
                    @playe_vocab = "Continue"
                    @playh_vocab = "Continue"
                    @cancel_vocab = "Quit"
                    @command_window.index = 0
                    @command_window.dispose
                    create_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
        elsif @game_mode == "quit"
            if Input.press?(:C)
                case @command_window.index
                when 0
                    Sound.play_ok
                    close_command_window
                    open_command_window_2
                    @game_mode = "color"
                when 1
                    Sound.play_ok
                    close_command_window
                    open_command_window_2
                    @game_mode = "color"
                when 2
                    Sound.play_cancel
                    SceneManager.return
                end
            end
        elsif @game_mode == "win" or @game_mode == "lose"
            if @command_window.openness == 255
                if Input.trigger?(:C)
                    case @command_window.index
                    when 0
                        Sound.play_ok
                        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
                        easy_terminate
                    when 1
                        Sound.play_ok
                        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
                        easy_terminate
                        @hard_sprite = Sprite.new(Viewport.new(0, 0, 640, 480))
                        @hard_sprite.bitmap = Cache.picture("mm_hard")
                        @hard_sprite.x = 50
                        @hard_sprite.y = 32
                        @hard_sprite.z = 2
                    when 2
                        Sound.play_cancel
                        SceneManager.return
                    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
        @used_colors = [0, 1, 2, 3, 4] # modded to add single color use
      if @command_window.index == 1
        @round_index = 2
      else
        @round_index = 0
      end
        @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 = 50 - (@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 = 278 + (i * 35)
            @color_sprite[i].y = 60
            @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 = 50 - (@answer_sprite.y)
            Sound.play_cursor
            wait(2)
        end
        for i in 0...4
            @answer[i] = @used_colors[rand(@used_colors.size)] # modded to add single color use
            @used_colors.delete(@answer[i]) # modded to add single color use
            @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 == 5
        @playe_vocab = "Try Harder"
        @playh_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 = 234
                    @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                when 1
                    @peg_sprite[@peg_index].x = 248
                    @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                when 2
                    @peg_sprite[@peg_index].x = 234
                    @peg_sprite[@peg_index].y = 402 - (@round_index * 40)
                when 3
                    @peg_sprite[@peg_index].x = 248
                    @peg_sprite[@peg_index].y = 402 - (@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 = 234
                                @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                            when 1
                                @peg_sprite[@peg_index].x = 248
                                @peg_sprite[@peg_index].y = 388 - (@round_index * 40)
                            when 2
                                @peg_sprite[@peg_index].x = 234
                                @peg_sprite[@peg_index].y = 402 - (@round_index * 40)
                            when 3
                                @peg_sprite[@peg_index].x = 248
                                @peg_sprite[@peg_index].y = 402 - (@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
        @command_window = Window_MastermindCommand.new(0, 0)
        @command_window.x = (Graphics.width - @command_window.width) / 2
        @command_window.y = (Graphics.height - @command_window.height) / 2
        @command_window.height = @command_window.fitting_height(3)
        @command_window.refresh
        @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
        @command_window.refresh
        @command_window.active = true
    end
    #--------------------------------------------------------------------------
    # * Close Command Window
    #--------------------------------------------------------------------------
    def close_command_window
        @command_window.close
        @command_window.refresh
        @command_window.active = false
    end
    #--------------------------------------------------------------------------
    # * Create Command Window Two
    #--------------------------------------------------------------------------
    def create_command_window_2
        @command_window_2 = Window_MastermindCommand_Color.new(0, 0)
        @command_window_2.x = (200 - @command_window_2.width) / 2
        @command_window_2.height = @command_window_2.fitting_height(6)
        if Mastermind_Options::Win_Lose == false
            @command_window_2.y = (Graphics.height - @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
        @command_window_2.active = true
        @command_window_2.refresh
    end
    #--------------------------------------------------------------------------
    # * Close Command Window Two
    #--------------------------------------------------------------------------
    def close_command_window_2
        @command_window_2.close
        @command_window_2.active = false
        @command_window_2.refresh
    end
end

Original thread: https://forums.rpgmakerweb.com/threads/mastermind-minigame-for-rpgvxa.77443/