Skip to content

Instantly share code, notes, and snippets.

@dillonkearns
Created March 24, 2016 03:39
Show Gist options
  • Save dillonkearns/b8c609f9023474727127 to your computer and use it in GitHub Desktop.
Save dillonkearns/b8c609f9023474727127 to your computer and use it in GitHub Desktop.
class TicTacToeGame
def initialize
@board = Array.new(3) { Array.new(3) }
end
def to_s
board_string = ""
@board.each do |row|
row.each do |cell|
board_string += cell_to_s(cell)
end
board_string += "\n" # print newline to separate rows
end
board_string
end
def cell_to_s(cell)
if cell.nil?
'-'
else
cell
end
end
def move(col, row, symbol)
@board[row][col] = symbol
puts(self)
end
end
game = TicTacToeGame.new
game.move(1, 1, 'X')
board = Array.new(3) { Array.new(3) }
def print_board(board)
board.each do |row|
row.each do |cell|
print_cell(cell)
end
puts # print newline to separate rows
end
end
def print_cell(cell)
if cell.nil?
print '-'
else
print cell
end
end
def move(board, col, row, symbol)
board[row][col] = symbol
print_board(board)
end
move(board, 1, 1, 'X')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment