2014-10-07 156 views
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 

回答

2

下顯得格格不入:

if @grid['1'] != ' ' && @grid['5'] != ' ' && @grid['9'] != ' ' 

在這裏,我們檢查,看看整個左上角,右下角的對角線是否填寫,但那if聲明涵蓋了全部你的勝利檢查案件。除非完全填入對角線,否則它們都不會被檢查。

當然,您確實希望檢查您檢查的每一行單元格都不是全部空白,所以您還有更多在game_checker函數中工作。

+0

我在爲'game_checker'尋找一個好的解決方案時遇到了一些麻煩,你有什麼建議嗎? – Cyzanfar 2014-10-08 18:43:02

+0

找到正確的方法。在我的問題中查看** SOLUTION **部分:) – Cyzanfar 2014-10-13 17:53:46