Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #a tic tac toe game
  2. class TicTacToe
  3. require "yaml"
  4. attr_accessor :player1, :player2
  5. #crates playes and a game board to play tic tac toe
  6. def initialize()
  7. @player1 = Player.new("Player One", "x")
  8. @player2 = Player.new("Player Two", "o")
  9. @game_board = Board.new
  10. end
  11.  
  12. #prints the board
  13. def print_board
  14. @game_board.board.each_with_index do |row, index|
  15. puts "#{row.join(" | ")}"
  16. puts "---------" unless index == 2
  17. end
  18. puts
  19. end
  20.  
  21. #determines whose move it is
  22. def move
  23. if @turn % 2 == 1
  24. player_one_turn
  25. else
  26. player_two_turn
  27. end
  28. @turn += 1
  29. end
  30.  
  31. def valid_move?(row, col)
  32. if @game_board.board[row][col] == " "
  33. return true
  34. else
  35. return false
  36. end
  37. end
  38.  
  39. #player ones turn
  40. def player_one_turn
  41. print_board
  42. puts "#{@player1.name} it's your turn:"
  43. puts "Enter a row (0-2)"
  44. row = gets.chomp.to_i
  45. puts "Enter a column (0-2)"
  46. col = gets.chomp.to_i
  47. if valid_move?(row, col)
  48. @game_board.board[row][col] = @player1.shape
  49. else
  50. puts "There's already a shape at that position."
  51. player_one_turn
  52. end
  53.  
  54. if win?(@player1.shape)
  55. winner(@player1.name)
  56. @winner = true
  57. end
  58. end
  59.  
  60. #player two's turn
  61. def player_two_turn
  62. print_board
  63. puts "#{@player2.name} it's your turn:"
  64. puts "Enter a row (0-2)"
  65. row = gets.chomp.to_i
  66. puts "Enter a column (0-2)"
  67. col = gets.chomp.to_i
  68. if valid_move?(row, col)
  69. @game_board.board[row][col] = @player2.shape
  70. else
  71. puts "There's already a shape at that position."
  72. player_two_turn
  73. end
  74.  
  75. if win?(@player2.shape)
  76. winner(@player2.name)
  77. @winner = true
  78. end
  79. end
  80.  
  81. def win?(shape)
  82. if (@game_board.board[0][0] == shape) && (@game_board.board[0][1] == shape) && (@game_board.board[0][2] == shape)
  83. return true
  84. elsif (@game_board.board[1][0] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[1][2] == shape)
  85. return true
  86. elsif (@game_board.board[2][0] == shape) && (@game_board.board[2][1] == shape) && (@game_board.board[2][2] == shape)
  87. return true
  88. elsif (@game_board.board[0][0] == shape) && (@game_board.board[1][0] == shape) && (@game_board.board[2][0] == shape)
  89. return true
  90. elsif (@game_board.board[0][1] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[2][1] == shape)
  91. return true
  92. elsif (@game_board.board[0][2] == shape) && (@game_board.board[1][2] == shape) && (@game_board.board[2][2] == shape)
  93. return true
  94. elsif (@game_board.board[0][0] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[2][2] == shape)
  95. return true
  96. elsif (@game_board.board[0][2] == shape) && (@game_board.board[1][1] == shape) && (@game_board.board[2][0] == shape)
  97. return true
  98. else
  99. return false
  100. end
  101. end
  102.  
  103. def draw?
  104. if @turn > 9
  105. print_board
  106. puts "The game ended in a draw. :)"
  107. @winner = true
  108. return true
  109. end
  110. false
  111. end
  112.  
  113. def winner(winner_name)
  114. puts "#{winner_name}, YOU WIN!!!"
  115. end
  116.  
  117. def play
  118. @turn = 1
  119. @winner = false
  120.  
  121. until @winner
  122. move unless draw?
  123. save
  124. end
  125. end
  126.  
  127.  
  128. #a class that generates an empty board
  129. class Board
  130. attr_accessor :board
  131. def initialize
  132. @board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
  133. end
  134. end
  135.  
  136. #a class that assigns creates plaers and assigns them a shape "x" or "o"
  137. class Player
  138. attr_accessor :name, :shape
  139. def initialize(name, shape)
  140. @name = name
  141. @shape = shape
  142. end
  143. end
  144.  
  145. def save
  146. yaml = YAML::dump(self)
  147. File.open("save.txt", "w"){|file| file.write(yaml)}
  148. end
  149.  
  150. def self.load
  151. file = File.read("save.txt")
  152. YAML::load_file(file)
  153. end
  154. end
  155.  
  156. game = TicTacToe.new
  157. game.play
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement