Created
March 24, 2016 03:39
-
-
Save dillonkearns/b8c609f9023474727127 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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