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