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

Week 7 Homework #103

Open
wants to merge 10 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
131 changes: 131 additions & 0 deletions Final/features/step_definitions/tic-tac-toe-steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
require 'rspec/mocks/standalone'
require 'rspec/expectations'

Given /^I start a new Tic\-Tac\-Toe game$/ do
@game = TicTacToe.new
end

When /^I enter my name (\w+)$/ do |name|
@game.player = name
end

Then /^the computer welcomes me to the game with "(.*?)"$/ do |arg1|
@game.welcome_player.should eq arg1
end

Then /^randomly chooses who goes first$/ do
[:human, :computer].should include @game.current_player
end

Then /^who is X and who is O$/ do
TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol
end

Given /^I have a started Tic\-Tac\-Toe game$/ do
@game = TicTacToe.new(:player)
@game.player = "Renee"
end

Given /^it is my turn$/ do
@game.assign_symbols(0)
@game.current_player.should eq :human

end

Given /^the computer knows my name is Renee$/ do
@game.player.should eq "Renee"
end

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

Then /^waits for my input of "(.*?)"$/ do |arg1|
@game.should_receive(:gets).and_return(arg1)
@game.player_move
end

Given /^it is the computer's turn$/ do
@game = TicTacToe.new(:computer)
@game.assign_symbols(1)
@game.current_player.should eq :computer
end

Then /^the computer randomly chooses an open position for its move$/ do
open_spots = @game.open_spots
@com_move = @game.computer_move
open_spots.should include(@game.board.key(:X))
end

Given /^the computer is playing X$/ do
@game.computer_symbol.should eq :X
end

Then /^the board should have an X on it$/ do
@game.board.values.should include :X
end

Given /^I am playing X$/ do
@game = TicTacToe.new(:human)
@game.assign_symbols(0)
@game.player_symbol.should eq :X
end

When /^I enter a position "(.*?)" on the board$/ do |arg1|
@old_pos = @game.board[arg1.downcase.to_sym]
@game.should_receive(:get_player_move).and_return(arg1)
@game.player_move.should eq arg1
end

When /^"(.*?)" is not taken$/ do |arg1|
@old_pos.should eq " "
end

Then /^it is now the computer's turn$/ do
@game.current_player.should eq :computer
end

When /^there are three X's in a row$/ do
@game = TicTacToe.new(:human)
@game.assign_symbols(0)
@game.board[:c1] = @game.board[:b2] = @game.board[:a3] = :X
end

Then /^I am declared the winner$/ do
@game.determine_winner
@game.player_won?.should be_true
@game.draw?.should be_false
@game.computer_won?.should be_false
end
Then /^the game ends$/ do
@game.over?.should be_true
end

Given /^there are not three symbols in a row$/ do
@game.board = {
:a1 => :X, :a2 => :O, :a3 => :X,
:b1 => :X, :b2 => :O, :b3 => :X,
:c1 => :O, :c2 => :X, :c3 => :O
}
@game.determine_winner
end

When /^there are no open spaces left on the board$/ do
@game.open_spots.empty?.should be_true
end

Then /^the game is declared a draw$/ do
@game.draw?.should be_true
end

When /^"(.*?)" is taken$/ do |arg1|
@game.board[arg1] = :O
@taken_spot = arg1
end

Then /^computer should ask me for another position "(.*?)"$/ do |arg1|
@game.board[arg1] = ' '
@game.should_receive(:get_player_move).twice

end
163 changes: 163 additions & 0 deletions Final/features/step_definitions/tic-tac-toe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@


class TicTacToe

attr_accessor :player, :board
attr_reader :wins


SYMBOLS = [:X, :O]

def initialize(player = "PUNY HUMAN")
@board = {
:a1 => " ", :a2 => " ", :a3 => " ",
:b1 => " ", :b2 => " ", :b3 => " ",
:c1 => " ", :c2 => " ", :c3 => " "
}

@wins = [
[:a1, :b1, :c1],
[:a2, :b2, :c2],
[:a3, :b3, :c3],
[:a1, :a2, :a3],
[:b1, :b2, :b3],
[:c1, :c2, :c3],
[:a1, :b2, :c3],
[:a3, :b2, :c1]
]

@roll = rand(2)
@player = player
assign_symbols
open_spots
current_state
end


def welcome_player
"Welcome #{@player}"
end

def assign_symbols(roll = @roll)
if roll == 0
@competitors = {:human => :X, :computer => :O}
else
@competitors = {:human => :O, :computer => :X}
end
end


def current_player
if open_spots.count.even? == false
return @competitors.key(:X)
else
return @competitors.key(:O)
end
end

def player_symbol
@competitors[:human]
end


def computer_symbol
@competitors[:computer]
end

def open_spots
r = @board.find_all {|i| i[1] == " "}
r.flatten.delete_if{|i| i == " "}
end

def players_board
p = @board.find_all {|i| i[1] == player_symbol}
p.flatten.delete_if{|i| i == player_symbol}
end

def computers_board
c = @board.find_all {|i| i[1] == computer_symbol}
c.flatten.delete_if{|i| i == computer_symbol}
end


def computer_move
compmove = open_spots.sample
@board[compmove] = computer_symbol
end

def check_move(playmove)
if open_spots.include?(playmove) == true
true
else
false
end
end

def get_player_move
move = gets.chomp.downcase.to_sym
end

def player_move
playmove = get_player_move
if check_move(playmove) == true
@board[playmove] = player_symbol
return playmove
else
puts "invalid move"
return playmove
player_move
end
end

def current_state
"#{@board[:a1]}|#{@board[:a2]}|#{@board[:a3]}\n-----
\n#{@board[:b1]}|#{@board[:b2]}|#{@board[:b3]}\n-----
\n#{@board[:c1]}|#{@board[:c2]}|#{@board[:c3]}\n#{'#'*10}"
end

def indicate_player_turn
"Your move, #{@player}: "
end




def player_won?
won = @wins.map{|i| i&players_board}
won.any? {|x| x.length == 3}
end

def computer_won?
won = @wins.map{|i| i&computers_board}
won.any? {|x| x.length == 3}
end

def draw?
if open_spots.length == 0
true
else
false
end
end

def over?
if determine_winner == true
true
else
false
end
end

def determine_winner
if player_won? == true
true
elsif computer_won? == true
true
elsif draw? == true
true
else
false
end
end

end
57 changes: 57 additions & 0 deletions Final/features/tic-tac-toe.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Feature: Tic-Tac-Toe Game
As a game player I like tic-tac-toe
In order to up my skills
I would like to play agaist the computer

Scenario: Begin Game
Given I start a new Tic-Tac-Toe game
When I enter my name Renee
Then the computer welcomes me to the game with "Welcome Renee"
And randomly chooses who goes first
And who is X and who is O

Scenario: My Turn
Given I have a started Tic-Tac-Toe game
And it is my turn
And the computer knows my name is Renee
Then the computer prints "Your move, Renee: "
And waits for my input of "B2"

Scenario: Computer's Turn
Given I have a started Tic-Tac-Toe game
And it is the computer's turn
And the computer is playing X
Then the computer randomly chooses an open position for its move
And the board should have an X on it

Scenario: Making Moves
Given I have a started Tic-Tac-Toe game
And it is my turn
And I am playing X
When I enter a position "A1" on the board
And "A1" is not taken
Then the board should have an X on it
And it is now the computer's turn

Scenario: Making Bad Moves
Given I have a started Tic-Tac-Toe game
And it is my turn
And I am playing X
When I enter a position "A1" on the board
And "A1" is taken
Then computer should ask me for another position "B2"
And it is now the computer's turn

Scenario: Winning the Game
Given I have a started Tic-Tac-Toe game
And I am playing X
When there are three X's in a row
Then I am declared the winner
And the game ends

Scenario: Game is a draw
Given I have a started Tic-Tac-Toe game
And there are not three symbols in a row
When there are no open spaces left on the board
Then the game is declared a draw
And the game ends
22 changes: 22 additions & 0 deletions Final/play_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require './features/step_definitions/tic-tac-toe.rb'

@game = TicTacToe.new
puts "What is your name?"
@game.player = gets.chomp
puts @game.welcome_player

until @game.over?
case @game.current_player
when :computer
@game.computer_move
when :human
@game.indicate_player_turn
@game.player_move
end
puts @game.current_state
@game.determine_winner
end

puts "You Won!" if @game.player_won?
puts "I Won!" if @game.computer_won?
puts "DRAW!" if @game.draw?
Binary file added Programming Ruby 1.9 & 2.0, 4th edition.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module hiya

def hello
puts yield
end
end
6 changes: 6 additions & 0 deletions exceptions2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require_relative "exceptons"

include hiya

hello

1 change: 1 addition & 0 deletions friendly_error
Submodule friendly_error added at 7ea5f1
Loading