Skip to content

Commit

Permalink
Turn TicTacToe::Game into a class
Browse files Browse the repository at this point in the history
I am going to want to refactor this code and add extra functions, but
currently it looks like most of that would require passing around board
and player objects everywhere.  Pulling these attributes up into a constructor
method and defining a reader gives us the flexibility to access them across the
whole object without having to pass them to the helper functions we're about to
create.
  • Loading branch information
practicingruby committed Dec 3, 2010
1 parent 5af9694 commit 2579626
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion app.rb
@@ -1,3 +1,4 @@
require_relative "lib/tictactoe"

TicTacToe::Game.play
game = TicTacToe::Game.new
game.play
20 changes: 11 additions & 9 deletions lib/tictactoe/game.rb
@@ -1,17 +1,19 @@
module TicTacToe
module Game
extend self
class Game

def play
board = [[nil,nil,nil],
[nil,nil,nil],
[nil,nil,nil]]
def initialize
@board = [[nil,nil,nil],
[nil,nil,nil],
[nil,nil,nil]]

left_diagonal = [[0,0],[1,1],[2,2]]
right_diagonal = [[2,0],[1,1],[0,2]]
@players = [:X, :O].cycle
end

attr_reader :board, :players

players = [:X, :O].cycle
def play
left_diagonal = [[0,0],[1,1],[2,2]]
right_diagonal = [[2,0],[1,1],[0,2]]

current_player = players.next

Expand Down

0 comments on commit 2579626

Please sign in to comment.