0
这是我第二篇关于我正在研究的“代码挑战”的文章。井字游戏逻辑不起作用
这里是用Ruby编写的我井字游戏代码:
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
[email protected]['1']
elsif (@grid['4'] == @grid['5'] && @grid['4'] == @grid['6'])
end_game=true
[email protected]['4']
elsif (@grid['7'] == @grid['8'] && @grid['7'] == @grid['9'])
end_game=true
[email protected]['7']
elsif (@grid['1'] == @grid['4'] && @grid['1'] == @grid['7'])
end_game=true
[email protected]['1']
elsif (@grid['2'] == @grid['5'] && @grid['2'] == @grid['8'])
end_game=true
[email protected]['2']
elsif (@grid['3'] == @grid['6'] && @grid['3'] == @grid['9'])
end_game=true
[email protected]['3']
elsif (@grid['1'] == @grid['5'] && @grid['1'] == @grid['9'])
end_game=true
[email protected]['1']
elsif (@grid['7'] == @grid['5'] && @grid['7'] == @grid['3'])
end_game=true
[email protected]['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
将返回错误,因此当玩家获得组合胜利时,游戏不会停止。
SOLUTION
我发现了什么,以我的程序做正确检查,如果有一个赢家还是不行。 下面是纠正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
[email protected]['1']
elsif ((@grid['4'] == @grid['5'] && @grid['4'] == @grid['6']) && (@grid['4'] != ' ' && @grid['5'] != ' ' && @grid['6'] != ' '))
end_game=true
[email protected]['4']
elsif ((@grid['7'] == @grid['8'] && @grid['7'] == @grid['9']) && (@grid['7'] != ' ' && @grid['8'] != ' ' && @grid['9'] != ' '))
end_game=true
[email protected]['7']
elsif ((@grid['1'] == @grid['4'] && @grid['1'] == @grid['7']) && (@grid['1'] != ' ' && @grid['4'] != ' ' && @grid['7'] != ' '))
end_game=true
[email protected]['1']
elsif ((@grid['2'] == @grid['5'] && @grid['2'] == @grid['8']) && (@grid['2'] != ' ' && @grid['5'] != ' ' && @grid['8'] != ' '))
end_game=true
[email protected]['2']
elsif ((@grid['3'] == @grid['6'] && @grid['3'] == @grid['9']) && (@grid['3'] != ' ' && @grid['6'] != ' ' && @grid['9'] != ' '))
end_game=true
[email protected]['3']
elsif ((@grid['1'] == @grid['5'] && @grid['1'] == @grid['9']) && (@grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' '))
end_game=true
[email protected]['1']
elsif ((@grid['7'] == @grid['5'] && @grid['7'] == @grid['3']) && (@grid['7'] != ' ' && @grid['5'] != ' ' && @grid['3'] != ' '))
end_game=true
[email protected]['7']
else
end_game=false
end
我在为'game_checker'寻找一个好的解决方案时遇到了一些麻烦,你有什么建议吗? – Cyzanfar 2014-10-08 18:43:02
找到正确的方法。在我的问题中查看** SOLUTION **部分:) – Cyzanfar 2014-10-13 17:53:46