Skip to content

Commit

Permalink
updated migration and started working on the current_player and play …
Browse files Browse the repository at this point in the history
…methods, they are early drafts that need tests
  • Loading branch information
zassmin committed Apr 18, 2013
1 parent ac6e6bc commit 3c059da
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
32 changes: 26 additions & 6 deletions app/models/game.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
class Game < ActiveRecord::Base
serialize :board

# This line tells Rails which attributes of the model are accessible, i.e.,
# which attributes can be modified automatically by outside users
# (such as users submitting requests with web browsers).
attr_accessible :board, :current_player, :status, :player_o, :player_x,
attr_accessible :board, :current_player, :status, :player_o, :player_x,

def initialize
super
self.board = Array.new(3).map{[nil, nil, nil]}
self.board = Array.new(3).map{[nil, nil, nil]}
end

# This could be refactored to use @player_o and @player_x
def update_board(player, row, column)
# hmm...unless might work better here, I'm wondering how it know the board[row][column] is not empty...
if board[row][column]
raise ArgumentError, "This spot is not empty."
else
Expand All @@ -20,8 +21,29 @@ def update_board(player, row, column)
end
end

def current_player(turn)
if turn.even?
'x'
else
'o'
end
end

def play
# will call on update board
# will call on current player

(0..8).each do |num|
update_board(current_player(num), board[row], board[row][column])
# how does it know to I
end

end

# This might not actually be necessary. In the views, we can draw a permanent grid
# and insert the value of each cell into it.
# and insert the value of each cell into it.
# Would be helpful to have so people can play before they get to the views
# ...might have this under a helper module
def display_board
"#{display_line(0)}" +
"- - -\n" +
Expand All @@ -44,9 +66,7 @@ def display_line(row)
end

# TODO
# Make sure changes to board are saved in database
# Only create default values for the board in the db OR initialize it in the model, not both!
# Validate that space in board is empty before allowing anything to be inserted into the board
# Play method
# winner?
# current_player (maybe - is it necessary?)
Expand Down
4 changes: 2 additions & 2 deletions db/migrate/20130406222125_create_games.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class CreateGames < ActiveRecord::Migration
def change
create_table :games do |t|
t.text :board, {:default => Array.new(3).map{[]}}
t.string :player_o, {:default => 'o'}
t.string :player_x, {:default => 'x'}
t.string :player_o
t.string :player_x
t.string :current_player
t.string :status, {:default => 'in_progress'}

Expand Down
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

create_table "games", :force => true do |t|
t.text "board", :default => "'---\n- []\n- []\n- []\n'"
t.string "player_o", :default => "o"
t.string "player_x", :default => "x"
t.string "player_o"
t.string "player_x"
t.string "current_player"
t.string "status", :default => "in_progress"
t.datetime "created_at", :null => false
Expand Down
28 changes: 24 additions & 4 deletions spec/models/game_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,36 @@
@test_game.update_board('x', 0, 0)
lambda { @test_game.update_board('x', 0, 0) }.should raise_error(ArgumentError)
end
end

it "should be saved to the database" do
game = @test_game.update_board('x', 2, 1)
saved_game = Game.all.find { |g| g.id == 2 }
@test_game.update_board('x', 2, 1)
saved_game = Game.all.find { |g| g.id == 1 }
saved_game.display_element(2, 1).should == 'x'

# TODO explain the error in our documentation

end
end

describe "set_player" do
it "should set first player to 'x' " do
@test_game.set_player.should == 'x'
end

it "should set next player to 'o' " do
@test_game.set_player.should == 'o'
end
end

# describe "play" do
# xit "first player should be 'x' if board is empty"
# # game = Game.new
# # game.current_player.should == 'x'
# # end

# xit "should set next player after current player"
# end

describe "display_element" do
it "should display @board element" do
@test_game.update_board('x', 0, 1)
Expand Down Expand Up @@ -75,7 +95,7 @@
" | | \n" +
"- - -\n" +
" | | \n"
end
end

it "should display player marks on the board" do
@test_game.update_board('x', 0, 0)
Expand Down

0 comments on commit 3c059da

Please sign in to comment.