gpt4 book ai didi

ruby-on-rails - 如何修复错误 "void value expression"?

转载 作者:数据小太阳 更新时间:2023-10-29 07:14:22 24 4
gpt4 key购买 nike

我写了一个井字游戏程序。我遇到问题的程序部分是出现错误:

tac.rb:63: void value expression
tac.rb:65: void value expression

此错误来 self 的 check_win 方法。

if str == "xxx"
return true and puts "X Wins!"
str == "ooo"
return true and puts "O Wins!"

在我添加“X wins”和“O wins”语句后,它给了我这个错误。

这是我的整个程序:

class Game

def initialize
@board=Array.new
@board[1]="1 __|"
@board[2]="__"
@board[3]="|__"
@board[4]="\n2 __|"
@board[5]="__"
@board[6]="|__"
@board[7]="\n3 |"
@board[8]=" "
@board[9]="| "
@turn="x"
@win_status = false
end

def turn
@turn
end

def show_board
puts " 1 2 3"
@board.each do |i|
print i
end
puts ""
end

def set_turn #switches turns
if @turn == "x"
@turn = "o"
else @turn == "o"
@turn = "x"
end
end

def make_move
puts "Enter x coordinate"
x=gets.to_i
puts "Enter y coordinate"
y=gets.to_i
@board[1]="1 _"+@turn+"|" if y==1 && x==1
@board[2]="_"+@turn if y==2 && x==1
@board[3]="|_"+@turn if y==3 && x==1
@board[4]="\n2 _"+@turn+"|" if y==1 && x==2
@board[5]="_"+@turn if y==2 && x==2
@board[6]="|_"+@turn if y==3 && x==2
@board[7]="\n3 "+@turn+"|" if y==1 && x==3
@board[8]=" "+@turn if y==2 && x==3
@board[9]="|"+@turn+" \n" if y==3 && x==3
end
def win_combo
return [[@board[1][1] + @board[2][1] + @board[3][2]], [@board[4][2] + @board[5][1] + @board[6][2]], [@board[7][1] + @board[8][1] + @board[9][1]],[@board[1][1] + @board[4][2] + @board[7][1]], [@board[2][1] + @board[5][1] + @board[8][1]], [@board[3][2] + @board[6][2] + @board[9][1]], [@board[1][1] + @board[5][1] + @board[9][1]], [@board[3][2] + @board[5][1] + @board[7][1]]]
end

def check_win
#if some row or column or diagonal is "xxx" or "ooo" then set @win_status = true
self.win_combo.each do |arr|
str = arr.join
if str == "xxx"
return true and puts "X Wins!"
str == "ooo"
return true and puts "O Wins!"

end
end
return false
end
end


g = Game.new
while g.check_win != true
g.show_board
g.set_turn
g.make_move
end

最佳答案

您需要一个 elsif 来让 ruby​​ 知道条件发生变化的位置。这是正确的代码:

if str == "xxx"
puts "X Wins!"
return true
elsif str == "ooo"
puts "O Wins!"
return true
end

您还需要结束您的 if block 。

关于ruby-on-rails - 如何修复错误 "void value expression"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20038999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com