Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Sean Sehr's Tic Tac Toe final #134

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

Then /^the computer prints "(.*?)"$/ do |arg1|
@game.should_receive(:puts).with(arg1)
@game.indicate_palyer_turn
@game.indicate_player_turn
end

Then /^waits for my input of "(.*?)"$/ do |arg1|
Expand Down
142 changes: 142 additions & 0 deletions week7/homework/features/step_definitions/tic-tac-toe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
class TicTacToe
attr_accessor :board, :player
attr_reader :player_symbol, :computer_symbol
SYMBOLS = [:X, :O]

def initialize current_player = (rand() > 0.5 ? :computer : :player), symbol = SYMBOLS.sample
@player = "Player"
@computers_turn = current_player == :computer ? true : false
@player_symbol = symbol.to_sym
@computer_symbol = @player_symbol == SYMBOLS[0] ? SYMBOLS[1] : SYMBOLS[0];
@game_over = false
@player_won, @computer_won = false, false

@wins = [
[:A1,:A2,:A3],
[:B1,:B2,:B3],
[:C1,:C2,:C3],

[:A1,:B1,:C1],
[:A2,:B2,:C2],
[:A3,:B3,:C3],

[:A1,:B2,:C3],
[:C1,:B2,:A3]
]

@board = {
:A1 => " ", :A2 => " ", :A3 => " ",
:B1 => " ", :B2 => " ", :B3 => " ",
:C1 => " ", :C2 => " ", :C3 => " "
}
end

def welcome_player
"Welcome #{@player}"
end

def current_player
@computers_turn ? "Computer" : @player
end

def computer_move
spot = computer_find_spot
@board[spot] = @computer_symbol
@computers_turn = false
spot
end

def computer_find_spot
open_spots.sample
end

def indicate_player_turn
print "#{@player}'s Move:"
end

def get_player_move i = gets
indicate_player_turn
i.chomp.upcase.to_sym
end

def player_move
spot = get_player_move.to_sym
r = spot
if @board[spot] != " "
r = spot_taken
else
@board[spot] = @player_symbol
@computers_turn = true
end
r
end

def spot_taken
print "spot already taken:"
player_move
end

def open_spots
spots = []
@board.each{|k, v| spots << k if v == " " }
spots
end

def determine_winner
@wins.each do |win|
if (@board[win[0]] == @board[win[1]] && @board[win[1]] == @board[win[2]]) && @board[win[0]] != " "
@game_over = true
if @board[win[0]] == @player_symbol
@player_won = true
else
@computer_won = true
end
end
end
end

def spots_open?
r = false
@board.each do |k, v|
if v == " "
r = true
end
end
r
end

def computer_won?
determine_winner
@computer_won
end

def player_won?
determine_winner
@player_won
end

def over?
!spots_open? || @game_over ? true : false
end

def draw?
r = false
if over? && player_won? == false && computer_won? == false
r = true
end
r
end

def current_state
<<-eos

a b c"

1 #{@board[:A1].to_s} | #{@board[:B1].to_s} | #{@board[:C1].to_s}
--- --- ---
2 #{@board[:A2].to_s} | #{@board[:B2].to_s} | #{@board[:C2].to_s}
--- --- ---
3 #{@board[:A3].to_s} | #{@board[:B3].to_s} | #{@board[:C3].to_s}
eos
end
end