Ruby OOP tic tac toe哈希/条件/itiration
试图用Ruby创建一个经典的tic-tac-toe-OOP,但是我的Ruby OOP tic tac toe哈希/条件/itiration,ruby,oop,hash,attributes,attr,Ruby,Oop,Hash,Attributes,Attr,试图用Ruby创建一个经典的tic-tac-toe-OOP,但是我的game\u results()方法遇到了问题 我意识到这并不完全,需要更多的功能,但现在我只是想用用户输入的对象填充我的棋盘,然后输出填充的棋盘和赢家 当我在棋盘上看到我们有一个赢家时,我调用game\u results()方法,它会给我正确的赢家,但每当平局时,我都会收到一个错误 有人对我在操作人员身上犯的错误有什么想法或解决方案吗?顺便说一句,我知道这一切是相当混乱,但我是一个初学者 #require "pry" cla
game\u results()
方法遇到了问题
我意识到这并不完全,需要更多的功能,但现在我只是想用用户输入的对象填充我的棋盘,然后输出填充的棋盘和赢家
当我在棋盘上看到我们有一个赢家时,我调用game\u results()
方法,它会给我正确的赢家,但每当平局时,我都会收到一个错误
有人对我在操作人员身上犯的错误有什么想法或解决方案吗?顺便说一句,我知道这一切是相当混乱,但我是一个初学者
#require "pry"
class Game
attr_reader :turn
def initialize
@turn = 1
@board = { a1: " ", a2: " ", a3: " ", b1: " ", b2: " ", b3: " ", c1: " ", c2: " ", c3: " " }
end
# def start_game
# x = Game.new
# x.player_symbol
# x.player_turn
# x.check_game
# end
def intro
puts "ULTIMATE TIC TAC TOE .. in ruby!\n"
display_board
end
def player_symbol
puts "Player 1, choose your marker. X or O?\n"
i = gets.chomp.upcase
if i == "X"
@p1 = "X"
@p2 = "O"
elsif i == "O"
@p1 = "O"
@p2 = "X"
else
puts "That is not a valid player!"
end
end
def player_turn
if @turn == 1
puts "Player 1 turn."
@turn = 2
player_move(@p1)
else
puts "Player 2 turn."
@turn = 1
player_move(@p2)
end
end
def display_board
# Displays board grid using hash values.
puts " 1 2 3 "
puts "A #{@board[:a1]} | #{@board[:a2]} | #{@board[:a3]} "
puts " ---+---+---"
puts "B #{@board[:b1]} | #{@board[:b2]} | #{@board[:b3]} "
puts " ---+---+---"
puts "C #{@board[:c1]} | #{@board[:c2]} | #{@board[:c3]} "
end
def player_move(n)
# Player picks square to claim.
# Player symbol populates hash.
puts "pick move"
x = gets.chomp.downcase.to_sym
@board[x] = "#{n}" # Directs to player X/O
display_board
end
def game_results
# Determines winner, loser, or tie game
if ((@board[:a1] == @p1) && (@board[:a2] == @p1) && (@board[:a3] == @p1)) ||
((@board[:b1] == @p1) && (@board[:b2] == @p1) && (@board[:b3] == @p1)) ||
((@board[:c1] == @p1) && (@board[:c2] == @p1) && (@board[:c3] == @p1)) ||
((@board[:a1] == @p1) && (@board[:b1] == @p1) && (@board[:c1] == @p1)) ||
((@board[:a2] == @p1) && (@board[:b2] == @p1) && (@board[:c2] == @p1)) ||
((@board[:a3] == @p1) && (@board[:b3] == @p1) && (@board[:c3] == @p1)) ||
((@board[:a1] == @p1) && (@board[:b2] == @p1) && (@board[:c3] == @p1)) ||
((@board[:a3] == @p1) && (@board[:b2] == @p1) && (@board[:c1] == @p1))
puts "Player #{@p1} is the winner!"
elsif ((@board[:a1] == @p2) && (@board[:a2] == @p2) && (@board[:a3] == @p2)) ||
((@board[:b1] == @p2) && (@board[:b2] == @p2) && (@board[:b3] == @p2)) ||
((@board[:c1] == @p2) && (@board[:c2] == @p2) && (@board[:c3] == @p2)) ||
((@board[:a1] == @p2) && (@board[:b1] == @p2) && (@board[:b1] == @p2)) ||
((@board[:a2] == @p2) && (@board[:b2] == @p2) && (@board[:c2] == @p2)) ||
((@board[:a3] == @p2) && (@board[:b3] == @p2) && (@board[:c3] == @p2)) ||
((@board[:a1] == @p2) && (@board[:b2] == @p2) && (@board[:c3] == @p2)) ||
((@board[:a3] == @p2) && (@board[:b2] == @p2) && (@board[:c1] == @p2))
puts "Player #{@p2} is the winner!"
elsif ((@board[:a1] == ("X" || "O")) &&
(@board[:a2] == ("X" || "O")) &&
(@board[:a3] == ("X" || "O")) &&
(@board[:b1] == ("X" || "O")) &&
(@board[:b2] == ("X" || "O")) &&
(@board[:b3] == ("X" || "O")) &&
(@board[:c1] == ("X" || "O")) &&
(@board[:c2] == ("X" || "O")) &&
(@board[:c3] == ("X" || "O")))
# This should represent that empty elements in the hash are now filled not making a winning pair
puts "Tie Game"
else
player_turn
end
end
end
@纳特瓦伦感谢你的否决票和对这篇文章的精彩编辑。贡献的方式。兄弟,我没有投反对票!为了更好的可读性,我进行了编辑,只是为了让你更快地得到答案。哦,抱歉@nathvarun,我想既然你编辑了你,你就投了反对票,因为他们的观点只有5个。希望我能得到一些反馈。没问题,伙计。因为投反对票的人没有留下评论,所以我投了赞成票。我不是一个喜欢红宝石的人,所以我帮不了你。cheers@bigREDcode我敢肯定你被否决了,因为你发布了一大堆乱七八糟的代码,并要求我们仔细研究,以找出错误。退房通常,做一个MCVE,这样你就可以问一个非常好的so问题,这会帮助你自己发现错误,而你根本不需要问!这节省了每个人的时间!