我用ruby写了Tictatcoe,但电脑的性能不好
基本上,计算机的移动方法很简单。我只想让它选择一个开放的立场,并采取它。它注定是愚蠢的。这并不意味着要熟练地演奏 问题是,这是出于某种不合理的原因。每次我移动,电脑都会随机移动。。有时是1,有时是3或6。只是奇怪的行为。我不明白为什么 请注意,这里没有提到的任何方法都在gameart.rb文件中,并且都是艺术,例如,我用ruby写了Tictatcoe,但电脑的性能不好,ruby,Ruby,基本上,计算机的移动方法很简单。我只想让它选择一个开放的立场,并采取它。它注定是愚蠢的。这并不意味着要熟练地演奏 问题是,这是出于某种不合理的原因。每次我移动,电脑都会随机移动。。有时是1,有时是3或6。只是奇怪的行为。我不明白为什么 请注意,这里没有提到的任何方法都在gameart.rb文件中,并且都是艺术,例如,you\u win只是艺术,没有任何逻辑 以下是所有代码: class TicTacToe require 'colorize' require_relative 'lib/
you\u win
只是艺术,没有任何逻辑
以下是所有代码:
class TicTacToe
require 'colorize'
require_relative 'lib/gameart.rb'
attr_reader :home_screen_ninja_art
WINS = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 5, 9], [3, 5, 7], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
@@scoreboard = {player: 0, computer: 0, tie: 0}
def initialize name, mark
@name, @gameover, @game_memory, @winner = name, false, ['_','_','_','_','_','_','_','_','_'], ''
mark == 'X' ? (@player, @computer = 'X', 'O') : (@player, @computer = 'O', 'X')
end
def menu i
return render_board if i == 99
return about if i == 100
return how_to_play if i == 200
return scoreboard if i == 300
return goodbye if i == 400
end
def action move
return menu(move) if move > 10
current_caller = caller[0] =~ /computer_make_move/ ? @computer : @player
i = move-1
if @game_memory[i] == '_'
render_board(i, current_caller)
else
try_again_art
end
end
def computer_make_move
action(current_board_positions[2].sample+1) # just pick any random open space for now
end
def render_board input=nil, caller=nil
if @gameover == false && !input.nil?
@game_memory[input] = caller
verify_game_state
end
if @gameover == true
game_over_art
case
when @winner == 'TIE' then you_tied
when @winner == @player then you_win
when @winner != @player then you_lose
end
end
board = @game_memory.map {|ps| ps == 'X' ? ps.colorize(:blue) : ps.colorize(:green) }.map {|ps| ps.gsub(/_/, " ")}
show_board board
end
def current_board_positions
x_positions, o_positions = [], []
@game_memory.select.with_index do |v,i|
x_positions << i+1 if v == 'X'
o_positions << i+1 if v == 'O'
end
open_positions = (1..9).to_a - (x_positions + o_positions)
[x_positions, o_positions, open_positions]
end
def verify_game_state
x, o, open = current_board_positions
find_winner = ->side { WINS.map {|win| side.combination(3).to_a.map {|set| set == win }.include? true } }
case
when (find_winner.(x).include? true)
@gameover=true
@winner='X'
when (find_winner.(o).include? true)
@gameover=true
@winner='O'
when !@game_memory.include?('_')
@gameover=true
@winner='TIE'
else
computer_make_move
end
end
end
在方法
中,验证游戏状态
,在每次渲染棋盘
后调用该方法时,不存在人员移动的情况。它只有赢家或平手,或计算机移动
,因此任何计算机都不应移动
所以,每当游戏还没结束的时候,电脑就会采取行动,直到结束,正如我所见。我想你已经缩小了范围。。我肯定它在那个区域,但我不确定我是否完全理解你说的话。我认为在
验证游戏状态下
你根本不应该打电话给计算机。它应该检查一下是否游戏结束了。现在它总是叫它,直到游戏结束。哇,我刚刚意识到我的错误。。。我现在明白我所做的了。谢谢。@f.instance\u variable\u get:@gameover
:1。您没有使@f
成为其成员的类,因此应该删除@
。2.为什么不仅仅是@f.gameover
,或者更好的是,@f.gameover?
遵循Ruby约定。
# Start a game
def start_new_game name, mark
@f = TicTacToe.new(name, mark)
puts @f.action 99
loop do
if @f.instance_variable_get :@gameover
puts 'Ready to play again?'.colorize(:white).on_red
print ' y|yes '.colorize(:green)
print ' n|no '.colorize(:red)
valid = false
until valid
startover = gets.chomp
valid = true if startover =~ /y|n|yes|no/i
end
if startover =~ /y|yes/i
start_new_game name, mark
else
@f.goodbye
end
else
print "* "
puts "Make your move #{name}:".colorize(:white).on_red
end
move = gets.chomp
puts @f.action move.to_i
end
end
valid = false
until valid
TicTacToe.home_screen_ninja_art
puts 'What is your name?'.colorize(:white).on_red
name = gets.chomp
valid = true if name.length > 1
end
valid = false
until valid
TicTacToe.home_screen_ninja_art
puts 'Pick your mark?'.colorize(:white).on_red
print ' X '.colorize(:blue)
print ' O '.colorize(:green)
puts ''
mark = gets.chomp.upcase
if mark =~ /X|O/i
valid = true
end
end
start_new_game name, mark