这是我关于这个“代码挑战”的第二篇文章。
这是我用Ruby编写的Tic Tac Toe游戏的代码:
class Morpion
def initialize
create_grid
get_player
show_grid
end
def get_player
puts "Let play some Tic Tac Toe"
puts ""
@player1 ='X'
@player2='O'
puts ""
puts "Where would you like to move? (check out the grid below and type any number 1-9 to place your symbol): "
puts " 1 | 2 | 3 "
puts "---+---+---"
puts " 4 | 5 | 6 "
puts "---+---+---"
puts " 7 | 8 | 9 "
end
def create_grid
@grid = {
'1' => ' ',
'2' => ' ',
'3' => ' ',
'4' => ' ',
'5' => ' ',
'6' => ' ',
'7' => ' ',
'8' => ' ',
'9' => ' '
}
end
def show_grid
puts ""
puts "#{@grid['1']}|#{@grid['2']}|#{@grid['3']}"
puts "-----"
puts "#{@grid['4']}|#{@grid['5']}|#{@grid['6']}"
puts "-----"
puts "#{@grid['7']}|#{@grid['8']}|#{@grid['9']}"
puts ""
end
def play
number_turns=1
while number_turns < 10
number_turns.odd? ? player_turn(@player1) : player_turn(@player2)
# game_checker
if game_checker
break
end
number_turns+=1
end
end
def player_turn(player)
puts player == 'X' ? "It's X's turn!" : "It's O's turn!"
puts ""
cell = gets.chomp
unless @grid.keys.include?(cell) #check if the user entered a number corresponding to the grid
puts ""
puts "it has to be a number from 1 to 9"
player_turn(player)
end
if @grid[cell] == ' ' #check if cell in grid is empty for user input
@grid[cell] = player
else
puts ""
puts "That cell is occupied. Choose again!"
player_turn(player)
end
show_grid
end
def game_checker
end_game = false
if @grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' '
if (@grid['1'] == @grid['2'] && @grid['1'] == @grid['3'])
end_game=true
victory=@grid['1']
elsif (@grid['4'] == @grid['5'] && @grid['4'] == @grid['6'])
end_game=true
victory=@grid['4']
elsif (@grid['7'] == @grid['8'] && @grid['7'] == @grid['9'])
end_game=true
victory=@grid['7']
elsif (@grid['1'] == @grid['4'] && @grid['1'] == @grid['7'])
end_game=true
victory=@grid['1']
elsif (@grid['2'] == @grid['5'] && @grid['2'] == @grid['8'])
end_game=true
victory=@grid['2']
elsif (@grid['3'] == @grid['6'] && @grid['3'] == @grid['9'])
end_game=true
victory=@grid['3']
elsif (@grid['1'] == @grid['5'] && @grid['1'] == @grid['9'])
end_game=true
victory=@grid['1']
elsif (@grid['7'] == @grid['5'] && @grid['7'] == @grid['3'])
end_game=true
victory=@grid['7']
else
end_game = false
end
end
if end_game
puts "the victory of this game is #{victory}"
return true
end
end
end
m=Morpion.new
m.play
所以在我的game_checker
方法中,我给程序提供了一些逻辑,因此它知道游戏何时结束以及谁是胜利者:end_game = true
。
问题是当我玩游戏时,只有对角线单元@grid['1] == @grid['5] && @grid['1] == @grid['9']
返回true并且游戏结束。对于所有其他可能的获胜组合,游戏end_game = false
返回false,因此当玩家获得获胜组合时游戏不会停止。
解
我发现如何为我的程序正确检查是否有胜利者。
以下是更正的game_checker
方法:
def game_checker # checking all possible winning combination
end_game = false
# condition is: whether cells are equal to each other and not empty!
if ((@grid['1'] == @grid['2'] && @grid['1'] == @grid['3']) && (@grid['1'] != ' ' && @grid['2'] != ' ' && @grid['3'] != ' '))
end_game=true
victory=@grid['1']
elsif ((@grid['4'] == @grid['5'] && @grid['4'] == @grid['6']) && (@grid['4'] != ' ' && @grid['5'] != ' ' && @grid['6'] != ' '))
end_game=true
victory=@grid['4']
elsif ((@grid['7'] == @grid['8'] && @grid['7'] == @grid['9']) && (@grid['7'] != ' ' && @grid['8'] != ' ' && @grid['9'] != ' '))
end_game=true
victory=@grid['7']
elsif ((@grid['1'] == @grid['4'] && @grid['1'] == @grid['7']) && (@grid['1'] != ' ' && @grid['4'] != ' ' && @grid['7'] != ' '))
end_game=true
victory=@grid['1']
elsif ((@grid['2'] == @grid['5'] && @grid['2'] == @grid['8']) && (@grid['2'] != ' ' && @grid['5'] != ' ' && @grid['8'] != ' '))
end_game=true
victory=@grid['2']
elsif ((@grid['3'] == @grid['6'] && @grid['3'] == @grid['9']) && (@grid['3'] != ' ' && @grid['6'] != ' ' && @grid['9'] != ' '))
end_game=true
victory=@grid['3']
elsif ((@grid['1'] == @grid['5'] && @grid['1'] == @grid['9']) && (@grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' '))
end_game=true
victory=@grid['1']
elsif ((@grid['7'] == @grid['5'] && @grid['7'] == @grid['3']) && (@grid['7'] != ' ' && @grid['5'] != ' ' && @grid['3'] != ' '))
end_game=true
victory=@grid['7']
else
end_game=false
end
答案 0 :(得分:2)
以下似乎不合适:
if @grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' '
在这里,您需要检查是否填写了整个左上 - 右下对角线,但if
语句涵盖所有您的胜利检查案例。除非对角线完全填满,否则不会检查它们。
当然,您确实希望检查您检查的每一行单元格是否都是空白的,因此您需要在{{1}中完成更多工作。功能。