我知道TicTacToe的问题已经发布过,但在查看了其他帖子和很多在线研究后,我仍然感到难过。我真的很感激一些帮助。
总的来说,我无法弄清楚为什么合适的获胜者没有出现,有时候nil似乎被赋予等于" X"或者" O"。我也很难搞清楚猫的游戏,但我想我已经接近了。
以下是我的所有代码,如果有任何其他信息可以帮助您找到解决方案,请与我们联系:
class TicTacToe
def initialize
row1 = [nil,nil,nil]
row2 = [nil,nil,nil]
row3 = [nil,nil,nil]
@row1 = row1
@row2 = row2
@row3 = row3
end
def show_board
print @row1
puts
print @row2
puts
print @row3
puts
end
def prompt
print " >>> "
end
def turn
puts "Pick row #"
print prompt
row_choice = gets.chomp
@row_choice = row_choice
puts "Pick column #"
print prompt
column_choice = gets.chomp
@column_choice = column_choice
end
def winner
print "checking for winner..."
if @row1[0] == @row1[1] && @row1[2]
puts "We have a winner1!"
replay()
elsif @row2[0] == @row2[1] && @row2[2]
puts "We have a winner2!"
replay()
elsif @row3[0] == @row3[1] && @row3[2]
puts "We have a winner3!"
replay()
elsif @row1[0] == @row2[1] && @row3[2]
puts "We have a winner4!"
replay()
elsif @row3[0] == @row2[1] && @row1[2]
puts "We have a winner5!"
replay()
elsif @row1[0] == @row2[0] && @row3[0]
puts "We have a winner6!"
replay()
elsif @row1[1] == @row2[1] && @row3[1]
puts "We have a winner7!"
replay()
elsif @row1[2] == @row2[2] && @row3[2]
puts "We have a winner8!"
replay()
#elsif @row1 && @row2 && @row3 != nil
# puts "Catsgame - no one wins!"
# replay()
else
puts "nope"
end
end
def replay
puts "---------------------"
puts "Would you like to play again? YES or NO"
select = gets.chomp
if select.downcase == "yes"
@row1 = [nil,nil,nil]
@row2 = [nil,nil,nil]
@row3 = [nil,nil,nil]
player1()
else
Process.exit(0)
end
end
def player1
puts "---------------------"
puts "It is Player 1's turn"
turn()
if @row_choice == 1.to_s
if @row1[@column_choice.to_i - 1] == "O"
puts
puts "Space is taken by Player 2"
player1()
elsif @row1[@column_choice.to_i - 1] == "X"
puts
puts "You are already in this space"
player1()
else
@row1[@column_choice.to_i - 1] = "X"
end
elsif @row_choice == 2.to_s
if @row2[@column_choice.to_i - 1] == "O"
puts
puts "Space is taken by Player 2"
player1()
elsif @row2[@column_choice.to_i - 1] == "X"
puts
puts "You are already in this space"
player1()
else
@row2[@column_choice.to_i - 1] = "X"
end
elsif @row_choice == 3.to_s
if @row3[@column_choice.to_i - 1] == "O"
puts
puts "Space is taken by Player 2"
player1()
elsif @row3[@column_choice.to_i - 1] == "X"
puts
puts "You are already in this space"
player1()
else
@row3[@column_choice.to_i - 1] = "X"
end
else
puts
puts "Pick 1, 2, or 3"
player1()
end
show_board()
winner()
player2()
end
def player2
puts "---------------------"
puts "It is Player 2's turn"
turn()
if @row_choice == 1.to_s
if @row1[@column_choice.to_i - 1] == "X"
puts
puts "Space is taken by Player 1"
player2()
elsif @row1[@column_choice.to_i - 1] == "O"
puts
puts "You are already in this space"
player2()
else
@row1[@column_choice.to_i - 1] = "O"
end
elsif @row_choice == 2.to_s
if @row2[@column_choice.to_i - 1] == "X"
puts
puts "Space is taken by Player 1"
player2()
elsif @row2[@column_choice.to_i - 1] == "O"
puts
puts "You are already in this space"
player2()
else
@row2[@column_choice.to_i - 1] = "O"
end
elsif @row_choice == 3.to_s
if @row3[@column_choice.to_i - 1] == "X"
puts
puts "Space is taken by Player 1"
player2()
elsif @row3[@column_choice.to_i - 1] == "O"
puts
puts "You are already in this space"
player2()
else
@row3[@column_choice.to_i - 1] = "O"
end
else
puts
puts "Pick 1, 2, or 3"
player2()
end
show_board()
winner()
player1()
end
end
puts "The game begins!"
new_game = TicTacToe.new()
new_game.show_board()
new_game.player1()
谢谢!
答案 0 :(得分:0)
你想要的是
if @row1[0] == @row1[1] && @row[0] == @row1[2]
puts "We have a winner1!"
replay()
elsif @row2[0] == @row2[1] && @row2[0] == @row2[2]
puts "We have a winner2!"
replay()
等等。 现在,您正在检查第一个和第二个项目是否相等,第三个项目是否存在。你想要的是检查第二个和第三个项目与第一个项目是否相等。
答案 1 :(得分:0)
实际上,您最好检查获胜者是谁 -
if @row1[0] == @row1[1] && @ro1[0] == @row1[2] && @row1[0] == "X"
puts "Player 1 Wins!"
replay()
elsif @row1[0] == @row1[1] && @row1[0] == @row1[2] && @row1[0] == "O"
puts "Player 2 Wins!"
replay()
.....
您还希望将重播功能置于获胜者功能之上。