Skip to content
Permalink
Browse files

Turn TicTacToe::Game into a class

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 2579626bd73fc7ad9e7d0a87419d5ecab2aacdda
Showing with 13 additions and 10 deletions.
  1. +2 −1 app.rb
  2. +11 −9 lib/tictactoe/game.rb
3 app.rb
@@ -1,3 +1,4 @@
require_relative "lib/tictactoe"

TicTacToe::Game.play
game = TicTacToe::Game.new
game.play
@@ -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

0 comments on commit 2579626

Please sign in to comment.
You can’t perform that action at this time.