2015-09-02 41 views
-2

我有以下的井字棋遊戲(我是小白,請忽略類的設計,遊戲作品,這是我所關心的現在。)如何從保存的yaml文件繼續tic-tac-toe遊戲?

#a tic tac toe game 
class TicTacToe 
    require "yaml" 
    attr_accessor :player1, :player2 
    #crates playes and a game board to play tic tac toe 
    def initialize() 
     @player1 = Player.new("Player One", "x") 
     @player2 = Player.new("Player Two", "o") 
     @game_board = Board.new 
    end 

    #prints the board 
    def print_board 
     @game_board.board.each_with_index do |row, index| 
      puts "#{row.join(" | ")}" 
      puts "---------" unless index == 2 
     end 
     puts 
    end 

    #determines whose move it is 
    def move 
     if @turn % 2 == 1 
      player_one_turn 
     else 
      player_two_turn 
     end 
     @turn += 1 
    end 

    def valid_move?(row, col) 
     if @game_board.board[row][col] == " " 
      return true 
     else 
      return false 
     end 
    end 

    #player ones turn 
    def player_one_turn 
     print_board 
     puts "#{@player1.name} it's your turn:" 
     puts "Enter a row (0-2)" 
     row = gets.chomp.to_i 
     puts "Enter a column (0-2)" 
     col = gets.chomp.to_i 
     if valid_move?(row, col) 
      @game_board.board[row][col] = @player1.shape 
     else 
      puts "There's already a shape at that position." 
      player_one_turn 
     end 

     if win?(@player1.shape) 
      winner(@player1.name) 
      @winner = true 
     end 
    end 

    #player two's turn 
    def player_two_turn 
     print_board 
     puts "#{@player2.name} it's your turn:" 
     puts "Enter a row (0-2)" 
     row = gets.chomp.to_i 
     puts "Enter a column (0-2)" 
     col = gets.chomp.to_i 
     if valid_move?(row, col) 
      @game_board.board[row][col] = @player2.shape 
     else 
      puts "There's already a shape at that position." 
      player_two_turn 
     end 

     if win?(@player2.shape) 
      winner(@player2.name) 
      @winner = true 
     end 
    end 

    def win?(shape) 
     if (@game_board.board[0][0] == shape) && (@game_board.board[0][1] == shape) && (@game_board.board[0][2] == shape) 
      return true 
     elsif (@game_board.board[1][0] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[1][2] == shape) 
      return true 
     elsif (@game_board.board[2][0] == shape) && (@game_board.board[2][1] == shape) && (@game_board.board[2][2] == shape) 
      return true 
     elsif (@game_board.board[0][0] == shape) && (@game_board.board[1][0] == shape) && (@game_board.board[2][0] == shape) 
      return true 
     elsif (@game_board.board[0][1] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[2][1] == shape) 
      return true 
     elsif (@game_board.board[0][2] == shape) && (@game_board.board[1][2] == shape) && (@game_board.board[2][2] == shape) 
      return true 
     elsif (@game_board.board[0][0] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[2][2] == shape) 
      return true 
     elsif (@game_board.board[0][2] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[2][0] == shape) 
      return true 
     else 
      return false 
     end 
    end 

    def draw? 
     if @turn > 9 
      print_board 
      puts "The game ended in a draw. :)" 
      @winner = true 
      return true 
     end 
     false 
    end 

    def winner(winner_name) 
     puts "#{winner_name}, YOU WIN!!!" 
    end 

    def play 
     @turn = 1 
     @winner = false 

     until @winner 
      move unless draw? 
      save 
     end 
    end 


    #a class that generates an empty board 
    class Board 
    attr_accessor :board 
     def initialize 
      @board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] 
     end 
    end 

    #a class that assigns creates plaers and assigns them a shape "x" or "o" 
    class Player 
     attr_accessor :name, :shape 
     def initialize(name, shape) 
      @name = name 
      @shape = shape 
     end 
    end 

    def save 
     yaml = YAML::dump(self) 
     File.open("save.txt", "w"){|file| file.write(yaml)} 
    end 

    def self.load 
     file = File.read("save.txt") 
     YAML::load_file(file) 
    end 
end 

game = TicTacToe.new 
game.play 

我想開始玩遊戲,在遊戲中間退出程序,然後在我打電話給TicTacToe.load後再回來完成。但是,當我現在這樣做時,YAML文件被加載,但程序不會恢復到它應該的位置。

有人可以告訴我,如果有辦法做我想做的事嗎?

+0

您已經顯示了很多代碼,包括板的'.save'和'.load'方法定義(關鍵的是,您沒有任何存儲其他遊戲狀態的東西,比如輪到誰了,成爲你失蹤的線索)。但是,我無法在代碼中看到實際使用這些方法的任何地方,或者您如何嘗試退出,保存,然後加載並重新啓動遊戲。你需要展示這些代碼 - 即使它只是在IRB中嘗試一些東西 - 因爲否則就是任何人都在猜錯什麼是錯的。 –

回答

0

我在區域上認爲,像YAML::load(self)這樣的操作會自動加載我通過某種魔術引用的文件的保存狀態。但是,我已經瞭解到,我在我的「play」函數中設計我的類和依賴項的方式不允許我加載之前的狀態(如果我的文件)。

加載YAML文件時,必須將文件加載到變量,然後手動將對象值分配給類的值。這樣,變量的當前狀態幾乎被手動分配給類的實例變量。例如,我可以這樣做:file = YAML.load("file_name"),然後分配變量值,如:@board = file.board。如果我之前知道這一點,我會設計我的類具有較少的依賴關係,以便它可以更清潔和更方便的方式加載。