Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'
gem 'rspec'
gem 'pry'
gem 'faker'
38 changes: 38 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
diff-lcs (1.2.5)
faker (1.4.3)
i18n (~> 0.5)
i18n (0.7.0)
method_source (0.8.2)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rspec (3.2.0)
rspec-core (~> 3.2.0)
rspec-expectations (~> 3.2.0)
rspec-mocks (~> 3.2.0)
rspec-core (3.2.3)
rspec-support (~> 3.2.0)
rspec-expectations (3.2.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-mocks (3.2.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.2.0)
rspec-support (3.2.2)
slop (3.6.0)

PLATFORMS
ruby

DEPENDENCIES
faker
pry
rspec

BUNDLED WITH
1.10.3
49 changes: 49 additions & 0 deletions lib/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative 'view'
require_relative 'model'

class TTTController

def run!
board = TTT.new
view = TTTView.new board

view.print_title

#Make todoList methods like RESTful endpoints (new/edit/update/delete)
#Think Backbone Model & View

loop do
view.print_menu
case view.fetch_user_input.upcase
when "S"
board.start_game
view.print_help_board
while not board.is_solved? do
view.print_board
input = view.fetch_user_input
case input
when 1..9
validMove = board.place_mark input
if not validMove
view.print_invalid_move
end
when "Q"
puts "BYE!"
exit
else
view.print_help_board
end
end
view.print_board
view.print_solved
when "Q"
puts "We're done"
exit
else
view.print_error_message
end
end
end
end

TTTController.new.run!
95 changes: 95 additions & 0 deletions lib/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require 'pry'
class TTT
attr_reader :player1turn, :board, :solved

def initialize
@player1turn = false
@board = Array.new 10, "-"
@solved = false
end

def place_mark location
case
when board[location-1] != '-' then return false
end
case @player1turn
when true
mark = "X"
else
mark = "O"
end
@board[location-1] = mark
@player1turn = ! @player1turn
true

end

def start_game
@player1turn = true
@board = Array.new 10, "-"
end

def is_solved?
check_horizontals or check_verticals or check_diagonals
end


def check_horizontals #TEST THIS
for start in 1..4
startIndex = start * 3 - 3
mark1 = board[startIndex]
case
when mark1 != board[startIndex+1]
false
when mark1 != board[startIndex+2]
false
when mark1 == "-"
false
else
return true
end
end
return false
end

def check_verticals
for start in 1..4
startIndex = start -1
mark1 = board[startIndex]
#binding.pry
case
when mark1 != board[startIndex+3]
false
when mark1 != board[startIndex+6]
false
when mark1 == "-"
false
else
return true
end
end
return false
end

def check_diagonals
startIndex = 0
mark1 = board[startIndex]
case
when mark1 != board[startIndex+4] then false
when mark1 != board[startIndex+8] then false
when mark1 == "-" then false
else
return true
end
startIndex = 2
mark1 = board[startIndex]
case
when mark1 != board[startIndex+2] then false
when mark1 != board[startIndex+4] then false
when mark1 == "-" then false
else
return true
end
return false
end
end
94 changes: 94 additions & 0 deletions lib/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class TTTView
attr_reader :game

def initialize newGame=TTT.new
@game = newGame
end

def print_title
title = <<TITLE

********** || **********
* Tic Tac Toe *
********** || **********

TITLE
puts title
end

def print_menu
menu = <<MENU
Prepare yourself for
the ultimate tic-tac-toe
experience
(S)tart a new game
(Q)uit

MENU
puts menu
end

def print_invalid_move
puts "ERROR... Space already filled"
end

def print_solved
case game.player1turn
when true
puts "Player 0 Wins!"
when false
puts "Player X Wins!"
end
end

def fetch_user_input(question=nil)
puts question if question
print "> "
output = gets.chomp
if output.to_i != 0
output.to_i
else
output.upcase
end
end

def print_error_message
puts "Type what's in the ()"
end

def print_help_board
help_board = <<help_board
-----------------------------------
Input the location that you want to place a mark
1\#2\#3
\#\#\#\#\#
4\#5\#6
\#\#\#\#\#
7\#8\#9
To bring this up again type 'h'
-----------------------------------
help_board
puts help_board
help_board
end

def print_board
line = ""
linenum = 1
lm1 = @game.board[(3 * linenum)-3]
lm2 = @game.board[(3 * linenum)-2]
lm3 = @game.board[(3 * linenum)-1]
line += "#{lm1}\##{lm2}\##{lm3}"
for linenum in 2..3
lm1 = @game.board[(3 * linenum)-3]
lm2 = @game.board[(3 * linenum)-2]
lm3 = @game.board[(3 * linenum)-1]
line += "\n\#\#\#\#\#"
line += "\n#{lm1}\##{lm2}\##{lm3}"
end
puts line
line
puts " -----------------------------------"
end

end
101 changes: 101 additions & 0 deletions spec/model_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'rspec'
require 'model'
require 'pry'

describe 'TTT' do

let(:game) { TTT.new}

it '.initialize' do
expected = Array.new(10, "-")
expect(game.board.eql? expected).to be true
end

it '.place_mark' do
expected = Array.new(10, "-")
expected[1] = "O"
game.place_mark 1
#binding.pry
expect(game.board.eql? expected ).to be true
end

it '.start_game' do
game.start_game
expect(game.player1turn == true). to be true
expected = Array.new(10, "-")
expect(game.board.eql? expected).to be true
end

it '.check_horizontals123' do
game.place_mark 1
game.place_mark 4
game.place_mark 2
game.place_mark 5
game.place_mark 3
expect(game.check_horizontals).to be true
end

it '.check_horizontals12' do
game.place_mark 1
game.place_mark 4
game.place_mark 2
game.place_mark 5
expect(game.check_horizontals).to be false
end

it '.check_horizontals789' do
game.place_mark 7
game.place_mark 4
game.place_mark 8
game.place_mark 5
game.place_mark 9
#binding.pry
expect(game.check_horizontals).to be true
end

it '.check_verticals147' do
game.place_mark 1
game.place_mark 5
game.place_mark 4
game.place_mark 6
game.place_mark 7
#binding.pry
expect(game.check_verticals).to be true
end

it '.check_verticals369' do
game.place_mark 3
game.place_mark 5
game.place_mark 6
game.place_mark 4
game.place_mark 9
expect(game.check_verticals).to be true
end

it '.check_diagonals159' do
game.place_mark 1
game.place_mark 2
game.place_mark 5
game.place_mark 3
game.place_mark 9
expect(game.check_diagonals).to be true
end

it '.check_diagonals357' do
game.place_mark 3
game.place_mark 2
game.place_mark 5
game.place_mark 1
game.place_mark 7
expect(game.check_diagonals).to be true
end

it '.check_diagonalsfail' do
game.place_mark 1
game.place_mark 2
game.place_mark 3
game.place_mark 4
game.place_mark 5
expect(game.check_diagonals).to be false
end
end
Loading