首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有AI的Ruby Mastermind游戏项目

带有AI的Ruby Mastermind游戏项目
EN

Code Review用户
提问于 2020-08-19 00:14:15
回答 1查看 239关注 0票数 2

我是自学的,对编码非常陌生,并在Ruby中创建了一个Mastermind游戏。任何一般性的反馈或建议都将不胜感激。游戏现在是完全功能,并有一个简单的人工智能。在开始时,玩家选择他们想要选择的角色(密码破解者还是代码制造者)。

干杯

https://repl.it/repls/WeirdFrankButtons

编辑:固定链接

代码语言:javascript
复制
class Game

  def initialize
    puts "---------------------------------"
    puts "Welcome to Mastermind"
    puts "The goal is to either create a 4 digit code (Code maker role) containing numbers ranging from 1 through 6 or to guess a code (Codebreaker role) created by the computer within 12 turns to win."
    puts "After each guess you will be given an accuracy score indicating how close you were to guessing the code correctly."
    puts "The letter \"H\" indicates one of the numbers you guessed is in the correct position. The letter \"h\" indicates you guessed a correct number but it is NOT in the correct position"
    puts "----------------------------------"
    @game_over = false
    @turn = 1
    until @comp_guess_mode === "Y" || @comp_guess_mode === "N"
      print "Is the computer the code breaker? Y/N"
      @comp_guess_mode = gets.chomp.upcase
    end
    game_mode
    turn_sequence
  end

  def game_mode
    if @comp_guess_mode == "Y"
      human_code_generator
    else
      code_generator
    end
  end

  def code_generator
    @code = Array.new(4) {rand(1..6)}
  end

  def human_code_generator
    @code = ""
    puts "Please enter a 4 digit code"
    until @code.length == 4
      @code = gets.chomp.each_char.map(&:to_i)
    end
  end
# computer_guesser method that tests if the computer's guess matches the human's
# by iterating through the array, if a direct match ('H') is found it will keep that number in the next guess
  def computer_guesser
    @updated_comp_guess = [" "," "," "," "]
    if @turn == 1
      @guess = Array.new(4) {rand(1..6)}
    else
      i = 0
      while i <4
        if @guess[i] == @code[i]
          @updated_comp_guess[i] = @guess[i]
          i+=1
        else
          i +=1
        end
      end
    end
      @guess = Array.new(4) {rand(1..6)}
      @updated_comp_guess.each_with_index do |value, idx|
        if value != " "
          @guess[idx] = value
        end
      end
      puts "Guess: #{@guess.join}"
  end


  def codebreaker_guess
    @guess = []
    until @guess.length == 4
      puts "Enter your 4 digit guess"
      @guess = gets.chomp.each_char.map(&:to_i)
      puts "Guess: #{@guess.join}"
      if @guess.length != 4
        print "Your guess was not 4 digits long, please guess again \n"
      end
    end
  end

  def turn_display
    puts "-------------------------"
    puts "It's turn number: #{@turn}" 
  end

  #Repeats the following guess/check sequence for 12 turns
  # or until the code and guess are matched
  def turn_sequence
    while @turn <13 && @game_over == false
      turn_display
      if @comp_guess_mode == "Y"
        computer_guesser
      else
        codebreaker_guess
      end       
      guess_checker
      @turn += 1
      victory_check
    end
  end

  def guess_checker
    @guess_accuracy = []
    @i=0
    @h_counter = 0
    while @i<4
      if @guess[@i] == @code[@i]
        @guess_accuracy.push("H")
        @h_counter += 1
        @i+=1
      else
        @i+=1
      end
    end
    if @i == 4
      i = 0
      compare_array = @code.clone
      while i < 4
        if compare_array.include?(@guess[i]) 
          compare_array[(compare_array.index(@guess[i]))]= " "
          @guess_accuracy.push("h")
          i+=1
        else
          i+=1
       end
      end
    @guess_accuracy.pop(@h_counter)
    puts "Guess accuracy: #{@guess_accuracy.join}"
    end
  end


  def victory_check
    if @guess[0..3] == @code[0..3]
      puts "Code was guessed correctly, it's #{@code}, codebreaker wins"
      @game_over = true
    elsif @turn == 13 && @game_over == false
      puts "Code was not guessed correctly, code maker wins"
      @game_over = true
    end
  end

end

game = Game.new

```
代码语言:javascript
复制
EN

回答 1

Code Review用户

发布于 2020-08-19 11:44:15

代码1

代码语言:javascript
复制
while i <4
    if @guess[i] == @code[i]
      @updated_comp_guess[i] = @guess[i]
      i+=1
    else
      i +=1
    end
  end

ifelse中,您都要将i增加1,这可以缩短。

代码语言:javascript
复制
while i <4
    if @guess[i] == @code[i]
      @updated_comp_guess[i] = @guess[i]
    end
    i += 1
  end

guess_checkercompare_array中类似

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/248114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档