Skip to content

Commit

Permalink
Finish board functionality and tests, start game function
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrobertso committed Apr 28, 2019
1 parent 2310c28 commit 58dd20b
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 1 deletion.
43 changes: 43 additions & 0 deletions lib/tic_tac_toe/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,48 @@ def initialize(input)
def default_grid
Array.new(3) {Array.new(3) {Cell.new}}
end

def get_cell(x, y)
grid[y][x]
end

def set_cell(x, y, value)
get_cell(x,y).value = value
end

def game_over
return :winner if winner?
return :draw if draw?
false
end

def draw?
grid.flatten.map {|cell| cell.value}.none_empty?
end

def winning_positions
grid + # rows
grid.transpose + # columns
diagonals # two diagonals
end

def diagonals
[
[get_cell(0,0), get_cell(1,1), get_cell(2,2)],
[get_cell(0,2), get_cell(1,1), get_cell(2,0)]
]
end

def winner?
winning_positions.each do |winning_position|
next if winning_position_values(winning_position).all_empty?
return true if winning_position_values(winning_position).all_same?
end
false
end

def winning_position_values(winning_position)
winning_position.map {|cell| cell.value}
end
end
end
17 changes: 17 additions & 0 deletions lib/tic_tac_toe/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Array
def all_empty?
self.all? {|element| element.to_s.empty?}
end

def all_same?
self.all? {|element| element == self[0]}
end

def any_empty?
self.any? {|element| element.to_s.empty?}
end

def none_empty?
!any_empty?
end
end
10 changes: 10 additions & 0 deletions lib/tic_tac_toe/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module TicTacToe
class Game
attr_reader :players, :board, :current_player, :other_player
def initialize(players, board = Board.new)
@players = players
@board = board
@current_player, @other_player = players.shuffle
end
end
end
96 changes: 95 additions & 1 deletion spec/board_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,100 @@ module TicTacToe
expect(board.grid).to eq "blah"
end
end


context "#get_cell" do
it "returns the cell based on the (x,y) coordinate" do
grid = [["","",""], ["","","something"], ["","",""]]
board = Board.new(grid: grid)
expect(board.get_cell(2,1)).to eq "something"
end
end

context "#set_cell" do
it "updates the value of a cell at an (x,y) coordinate" do
Cat = Struct.new(:value)
grid = [[Cat.new("cool"), "",""], ["","",""], ["","",""]]
board = Board.new(grid: grid)
board.set_cell(0,0,"meow")
expect(board.get_cell(0,0).value).to eq "meow"
end
end

TestCell=Struct.new(:value)
let(:x_cell) { TestCell.new("X")}
let(:y_cell) {TestCell.new("Y")}
let(:empty) {TestCell.new}

context "#game_over" do
it "returns :winner if winner? is true" do
board = Board.new
board.stub(:winner?) {true}
expect(board.game_over).to eq :winner
end

it "returns :draw if winner? is false and draw? is true" do
board = Board.new
board.stub(:winner?) {false}
board.stub(:draw?) {true}
expect(board.game_over).to eq :draw
end

it "returns false if winner? is false and draw? is false" do
board = Board.new
board.stub(:winner?) {false}
board.stub(:draw?) {false}
expect(board.game_over).to be_false
end

it "returns :winner when row has objects with values that are all the same" do
grid = [
[x_cell,x_cell,x_cell],
[y_cell,y_cell,y_cell],
[y_cell, y_cell, empty]
]
board=Board.new(grid: grid)
expect(board.game_over).to eq :winner
end

it "returns :winner when column has objects with values that are all the same" do
grid = [
[x_cell, x_cell, empty],
[y_cell, x_cell, y_cell],
[y_cell, x_cell, empty]
]
board = Board.new(grid: grid)
expect(board.game_over).to eq :winner
end

it "returns :winner when diagonal has objects with values that are all the same" do
grid = [
[x_cell, empty, empty],
[y_cell, x_cell, empty],
[y_cell, x_cell, x_cell]
]
board = Board.new(grid: grid)
expect(board.game_over).to eq :winner
end

it "returns draw when all spaces on the board have been taken" do
grid = [
[x_cell, y_cell, x_cell],
[y_cell, x_cell, y_cell],
[y_cell, x_cell, y_cell]
]
board = Board.new(grid: grid)
expect(board.game_over).to eq :draw
end

it "returns false when there is no winner or draw" do
grid = [
[x_cell, empty, empty],
[y_cell, empty, empty],
[x_cell, empty, empty]
]
board = Board.new(grid: grid)
expect(board.game_over).to be_false
end
end
end
end
59 changes: 59 additions & 0 deletions spec/core_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "spec_helper"

describe Array do
context "all_empty?" do
it "returns true if all elements of the array are empty" do
expect(["","",""].all_empty?).to be_true
end

it "returns false if some of the array elements are not empty" do
expect(["1","","", object.new, :a]).to be_false
end

it "returns true for an empty Array" do
expect([].all_empty?).to be_true
end
end

context "all_same?" do
it "returns true if all elements of the array are the same" do
expect(["a","a","a"].all_same?).to be_true
end

it "returns false if some of the array elements are different" do
expect(["a",:end,123].all_same?).to be_false
end

it "returns true for an empty array" do
expect([].all_same?).to be_true
end
end

context "any_empty" do
it "returns true for an array with any empty elements" do
expect(["a", "", 123, :end].any_empty?).to be_true
end

it "returns false for a full array" do
expect(["a",123,:end].any_empty?).to be_false
end

it "returns true for an empty array" do
expect([].any_empty?).to be_true
end
end

context "none_empty" do
it "returns true for an array with no empty elements" do
expect(["a", 123, :end].none_empty?).to be_true
end

it "returns false for an array with some empty elements" do
expect(["a", "", 1234, :end].none_empty?).to be_false
end

it "returns false for an empty array" do
expect([].none_empty?).to be_false
end
end
end
Empty file added spec/game_spec.rb
Empty file.

0 comments on commit 58dd20b

Please sign in to comment.