Skip to content

Commit

Permalink
generated a game model to the database, created a migration, refactor…
Browse files Browse the repository at this point in the history
…ed board.rb and #board to game.rb #game. Updated spec/model/board_spec.rb to spec/model/game_spec.rb. refactored some tests and #board.
  • Loading branch information
zassmin committed Apr 7, 2013
1 parent 11bcd5e commit 2d2899e
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
47 changes: 47 additions & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Game < ActiveRecord::Base
attr_accessible :board, :player_o, :player_x, :current_player, :status, :created_at

def initialize
@board = [[nil, nil, nil],
[nil, nil, nil],
[nil, nil, nil]]
@player_o = 'o'
@player_x = 'x'
end

def board
@board
end

def assign_player_position(player, row, column)
@board[row][column] = player
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.
def display_board
"#{display_line(0)}" +
"- - -\n" +
"#{display_line(1)}" +
"- - -\n" +
"#{display_line(2)}"
end

def display_element(row, column)
element = @board[row][column]
if element
element
else
' '
end
end

def display_line(row)
"#{display_element(row,0)}|#{display_element(row,1)}|#{display_element(row,2)}\n"
end
end

# board = Game.new
# board.assign_player_position('x', 0, 1)
# board.assign_player_position('o', 0, 2)
# puts board.display_board
15 changes: 15 additions & 0 deletions db/migrate/20130406222125_create_games.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateGames < ActiveRecord::Migration
def change
create_table :games do |t|
t.text :board
t.string :player_o
t.string :player_x
t.string :current_player
t.string :status
t.datetime :created_at
t.datetime :updated_at

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 0) do
ActiveRecord::Schema.define(:version => 20130406222125) do

create_table "games", :force => true do |t|
t.text "board", :default => "[[],[],[]]"
t.string "player_o"
t.string "player_x"
t.string "current_player"
t.string "status", :default => "in_progress"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
77 changes: 77 additions & 0 deletions spec/models/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'game.rb'

describe Game do
before do
@test_game = Game.new
end

it "should be initialized with nil values inside of the array, [[]]" do
@test_game.board.should == [[nil, nil, nil],
[nil, nil, nil],
[nil, nil, nil]]
end

it "should always have a length of 3" do
@test_game.board.length.should == 3
end

it "should always have the array within the array length of 3" do
@test_game.board.each { |array| array.length.should == 3 }
end

describe "assign_player_position" do
it "should establish a position on the board, using 'x' or 'o'" do
to_test = @test_game.assign_player_position('x', 0, 1)
to_test.should == @test_game.board[0][1]
end
end

describe "display_element" do
it "should display @board element" do
mock_player = @test_game.assign_player_position('x', 0, 1)

to_test = @test_game.display_element(0,1)
to_test.should == mock_player
end

it "should display a space if the element is nil" do
@test_game.display_element(0,0).should == ' '
end

it "should display an 'x' or an 'o' if the element is not a space" do
@test_game.assign_player_position('x', 0, 1)
@test_game.display_element(0,1).should == 'x'
end
end

describe "display_line" do
it "should display player marks or a space (if nil) in the designated row" do
@test_game.assign_player_position('x', 0, 2)

to_test = @test_game.display_line(0)
to_test.should == " | |x\n"
end
end

describe "display_board" do
it "should display board" do
@test_game.display_board.should == " | | \n" +
"- - -\n" +
" | | \n" +
"- - -\n" +
" | | \n"
end

it "should display player marks on the board" do
@test_game.assign_player_position('x', 0, 0)
@test_game.assign_player_position('o', 1, 1)
@test_game.assign_player_position('x', 2, 2)

@test_game.display_board.should == "x| | \n" +
"- - -\n" +
" |o| \n" +
"- - -\n" +
" | |x\n"
end
end
end
19 changes: 19 additions & 0 deletions test/fixtures/games.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
board: MyText
player_o: MyString
player_x: MyString
current_player: MyString
status: MyString
created_at: 2013-04-06 15:21:25
updated_at: 2013-04-06 15:21:25

two:
board: MyText
player_o: MyString
player_x: MyString
current_player: MyString
status: MyString
created_at: 2013-04-06 15:21:25
updated_at: 2013-04-06 15:21:25
7 changes: 7 additions & 0 deletions test/unit/game_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class GameTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 2d2899e

Please sign in to comment.