注册会员 登录
Project1 返回首页

喵呜喵5的树洞 https://rpg.blue/?291206 [收藏] [复制] [分享] [RSS]

日志

【脚本】猜数字解答器

热度 5已有 1281 次阅读2014-6-4 23:12 |个人分类:脚本

=begin
===============================================================================
  猜数字解答器 By喵呜喵5
===============================================================================

【说明】

  以wiki中看到的暴力破解猜数字算法为原型写的猜数字解答器
  
  在事件中的脚本指令里输入
    
    caishuzidaan
    
  然后按照提示分别输入对应的结果,即可在7步以内算出猜数字游戏的答案
  
  计算开始的几个数字需要花费大量的时间,建议在测试模式下打开控制台来查看解答进度
  
=end
$m5script = {} if $m5script.nil?
$m5script["M5_Mini_Num_Ans"] = true
module M5_Mini_Num
#==============================================================================
# 设定部分
#==============================================================================
  
  NUMBER = [1,2,3,4,5,6,7,8,9]  #[0,1,2,3,4,5,6,7,8,9]
  
  #组成答案的数字,我自己的猜数字脚本中答案里是不会出现0的,  
  #填入0以后,这个解答器同样支持答案中包含0的猜数字游戏。
  #如果已经确定答案中不包含某个数字时,也可以把上面对应的那个数字删掉。
  #数字越多,解答器计算答案的时间越长。
  
  INDEX = true #false
  
  #是否储存计算数据?true为建立
  #在游戏目录下储存已经计算过的数据,
  #下次计算相同内容时便会读取对应的数据,大大加快了计算速度
  #【如果因此造成最终结果出错,请关闭这个功能】
  #【更换新版脚本后请删除旧版脚本储存的所有数据(M5MiniNum文件夹)】
  
#==============================================================================
# 脚本部分
#==============================================================================
end
class Window_M5_Mini_Num5 < Window_Base
  include M5_Mini_Num
  def initialize
    super(0, 2*Graphics.height/5, Graphics.width,Graphics.height/5)    
    creat_ansewr_list
    @number = @ansewr_list[0]
    msgbox "出现异常!请检查设置的数据!" unless @number
    @handler = {}
    @index = 0
    @input = [0,0]
    @history = []
    create_contents
    refresh
  end
  def creat_ansewr_list
    @ansewr_list = []    
    array = NUMBER.clone
    array.uniq!
    array.each do |value1|
      number1 = value1      
      array.each do |value2|
        next if value2 == number1
        number2 = value2
        array.each do |value3|
          next if value3 == number1
          next if value3 == number2
          number3 = value3
          array.each do |value4|
            next if value4 == number1
            next if value4 == number2
            next if value4 == number3
            number4 = value4
            @ansewr_list.push([number1,number2,number3,number4])
          end
        end
      end
    end
    @ansewr_list.uniq!
  end
  def cursor_move
    @index = (@index + 1) % 2
  end  
  def update
    super
    return unless active
    process_cursor_move
    process_digit_change
    process_handling
    update_cursor
  end
  def process_cursor_move
    last_index = @index
    cursor_move if Input.repeat?(:RIGHT) || Input.repeat?(:LEFT)    
    Sound.play_cursor if @index != last_index
  end
  def process_digit_change
    if Input.repeat?(:UP) || Input.repeat?(:DOWN)
      Sound.play_cursor      
      if Input.repeat?(:UP)
        @input[@index] = (@input[@index] + 1) % 5 
        @input[@index] = (@input[@index] + 1) % 5 until normal_input?
      else
        @input[@index] = (@input[@index] - 1) % 5
        @input[@index] = (@input[@index] - 1) % 5 until normal_input?
      end      
      refresh
    end
  end
  def normal_input?    
    return (@input[0] + @input[1]) < 5
  end
  def process_handling
    return process_ok     if Input.trigger?(:C)
    return process_cancel if Input.trigger?(:B)
    return process_exit   if Input.trigger?(:A)
  end
  def process_exit
    call_handler(:return)
  end
  def process_cancel    
    Sound.play_cancel
    creat_ansewr_list
    @number = @ansewr_list[0]
    @index = 0
    @input = [0,0]
    @history = []
    refresh
  end
  def item_rect(index)
    Rect.new(@hint1_width + @hint2_width*(index-1)-2,0, 
    text_size("口").width*1.5, line_height)
  end
  def refresh
    contents.clear
    result = ""
    @number.size.times {|i| result +=  "#{@number[i]}" }    
    hint_text = "输入数字#{result}后: 完全正确    个,数字正确"
    @hint1_width = text_size(hint_text).width
    @hint2_width = text_size("    个,数字正确").width
    draw_text(0,0,self.width,line_height,hint_text + "    个")
    change_color(crisis_color)
    2.times do |i|
      rect = item_rect(i)
      text = @input[i]
      draw_text(rect, text, 1)
    end
    y = self.height - standard_padding*1.5 - line_height
    make_font_smaller
    change_color(normal_color)
    draw_text(0,y,self.width-line_height - standard_padding,line_height,
      "Z确定,X重新计算,SHIFT退出", 2)
    reset_font_settings
  end
  def update_cursor
    cursor_rect.set(item_rect(@index))
  end
  def set_handler(symbol, method)
    @handler[symbol] = method
  end
  def call_handler(symbol)
    @handler[symbol].call
  end  
  def process_ok
    Sound.play_ok
    test_result
    @input = [0,0]
    @index = 0
    refresh
  end
  def test_result
    load_list    
    if @ansewr_list.size == 1
      show_data = @ansewr_list[0].clone
      show_data.reverse!
      result = 0
      4.times {|i| result +=  show_data[i] * 10 ** (i) }
      result = "0" + result.to_s if show_data[3] == 0
      msgbox "此次猜数字的答案为#{result}"
      call_handler(:return)
      return
    end
    if @ansewr_list.size < 1      
      msgbox "不存在这样的数字!"
      call_handler(:return)
      return
    end
    @history.push(@number)
    i = 0
    loop do
      @number = @ansewr_list[(i += 1)]
      break unless @history.include?(@number)
    end
  end
  def calculate_data
    temp_ansewr = @ansewr_list.clone
    temp_ansewr.each_with_index do |answer,index|
      result2 = result3 = 0
      4.times {|i| result3 += 1 if answer.include?(@number[i])}
      4.times {|i| result2 += 1 if answer[i] == @number[i]}
      result3 -= result2
      @ansewr_list.delete(answer) if (result2 != @input[0] || result3 != @input[1])
      p "Calculating...#{(index.to_f/temp_ansewr.size*100).to_i}%"
    end
    p "Finish!"
  end
  def load_list    
    if M5_Mini_Num::INDEX && @history.empty?
      index = load_data("M5MiniNum/Index.rvdata2") if !Dir.glob("M5MiniNum/Index.rvdata2").empty?      
      number_list = NUMBER.clone
      number_list.uniq!
      number_list.sort!
      type_name = ""
      number_list.each {|v| type_name += "#{v}"}
      input = "#{@input[0]}#{@input[1]}"      
      index = {} unless index
      if index[type_name]
        filename = index[type_name][input]
      else
        filename = nil
      end
      if filename and !Dir.glob(filename).empty?
        @ansewr_list = load_data(filename)
        p "Finish!"
      else
        calculate_data
        Dir.mkdir("M5MiniNum") if !File.directory?("M5MiniNum")
        filename = "M5MiniNum/#{Time.now.strftime("%Y%j%H%M%S")}.rvdata2"
        index[type_name] = {} unless index[type_name]
        index[type_name][input] = filename
        save_data(@ansewr_list, filename)
        save_data(index, "M5MiniNum/Index.rvdata2")
      end
    else
      calculate_data
    end
  end
end
class Scene_M5_Mini_Num_Ans < Scene_Base
  def start
    super
    create_command_window
  end
  def create_command_window
    @command_window = Window_M5_Mini_Num5.new
    @command_window.set_handler(:return,   method(:return_scene))    
  end
end
class Game_Interpreter
  def caishuzidaan
    SceneManager.call(Scene_M5_Mini_Num_Ans)    
  end
end
3

鸡蛋

鲜花

刚表态过的朋友 (3 人)

评论 (0 个评论)

facelist doodle 涂鸦笔

您需要登录后才可以评论 登录 | 注册会员

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-29 06:59

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

返回顶部