fork download
  1. class TicTacToe
  2. def initialize
  3. @board = Array.new(9){|i| "#{i}: "}
  4. puts "Welcome to Tic Tac Toe!"
  5. show_board
  6. @turn = 1
  7. choose_spot
  8. end
  9.  
  10. def show_board
  11. puts "\n\n"
  12. puts "Player 1 may begin. Please place your mark using the numbers provided on the board."
  13. puts "\n\n"
  14. puts ">>>>>>>>>>>>>>>"
  15. puts "#{@board[0]}|#{@board[1]}|#{@board[2]}|\n\n"
  16. puts "#{@board[3]}|#{@board[4]}|#{@board[5]}|\n\n"
  17. puts "#{@board[6]}|#{@board[7]}|#{@board[8]}|"
  18. puts "<<<<<<<<<<<<<<<"
  19. puts "\n\n"
  20. end
  21.  
  22. def choose_spot
  23. @player = @turn.odd? ? "1" : "2"
  24. @board_piece = @turn.odd? ? "X" : "O"
  25. @turn += 1;
  26. puts "Player #{@player}, (#{@board_piece}). Your turn. Please type a number to put your piece on the board."
  27. @input = gets.chomp.to_i
  28. check_valid_spot
  29. end
  30.  
  31. def check_valid_spot
  32. loop do
  33. board.each do |i|
  34. if @input.include?(0..8)
  35. if i.include?(" ")
  36. i.replace(@board_piece)
  37. else
  38. puts "Sorry there's already a piece there! Please try again."
  39. end
  40. else
  41. puts "Sorry that isn't a valid number! Please look at the board and try again."
  42. end
  43. end
  44. end
  45. end
  46. end
  47.  
  48. game = TicTacToe.new
Runtime error #stdin #stdout #stderr 0.05s 9776KB
stdin
Standard input is empty
stdout
Welcome to Tic Tac Toe!


Player 1 may begin. Please place your mark using the numbers provided on the board.


>>>>>>>>>>>>>>>
0:  |1:  |2:  |

3:  |4:  |5:  |

6:  |7:  |8:  |
<<<<<<<<<<<<<<<


Player 1, (X). Your turn. Please type a number to put your piece on the board.
stderr
prog.rb:27:in `choose_spot': undefined method `chomp' for nil:NilClass (NoMethodError)
	from prog.rb:7:in `initialize'
	from prog.rb:48:in `new'
	from prog.rb:48:in `<main>'