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
Success #stdin #stdout 0.06s 9752KB
stdin
Standard input is empty
stdout
Standard output is empty