forked from zassmin/TicTacToeSkeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generated a game model to the database, created a migration, refactor…
…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
Showing
6 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
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
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 |
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
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 |
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
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
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 |
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
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 |
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
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 |