Skip to content

Week 4 homework #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4573e5b
Update questions.txt
metroretro Oct 9, 2014
e2e4d69
Update strings_and_rspec_spec.rb
metroretro Oct 9, 2014
ea05488
Update strings_and_rspec_spec.rb
metroretro Oct 10, 2014
91ee300
week 1 homework completed
metroretro Oct 10, 2014
b7158e1
Merge branch 'master' of https://github.com/metroretro/RubyFall2014
metroretro Oct 16, 2014
95778a6
reset week1 homework questions to earlier version
metroretro Oct 17, 2014
635d540
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2014
metroretro Oct 17, 2014
8541281
Week 3 homework and exercises
metroretro Oct 23, 2014
7fbda61
completed homework 3
metroretro Oct 23, 2014
4b2e145
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2014
metroretro Oct 24, 2014
544be88
week 4 homework exercise and questions complete
metroretro Oct 31, 2014
9bbae5e
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2014
metroretro Oct 31, 2014
4e21e7b
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2014
metroretro Nov 7, 2014
2e912a0
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2014
metroretro Nov 14, 2014
b1428a8
pirate speak implemented
metroretro Nov 20, 2014
680ed0a
Week 7 files
metroretro Dec 6, 2014
48523f9
Merge branch 'master' of https://github.com/UWE-Ruby/RubyFall2014
metroretro Dec 6, 2014
b75d2d5
Mastermind in progress
metroretro Dec 7, 2014
83f0938
Tic Tac Toe partially complete
metroretro Dec 8, 2014
5960318
Tic Tac Toe 3 tests remaining
metroretro Dec 9, 2014
17e713d
MAstermine gem complete-ish
metroretro Dec 9, 2014
6e599f5
TicTacToe complete except for solution and draw features
metroretro Dec 11, 2014
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
77 changes: 77 additions & 0 deletions Mastermind/mastermind.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Mastermind

COLORS = ['Red', 'Blue', 'Green', 'Yellow', 'Periwinkle', 'Egg Shell', 'Chartreuse']

def initialize
@guess = []
#randomly choose 4 colors from COLORS
@secret_code = COLORS.shuffle[0,4]
@game_over = false
#@play_again = "Y"

end

def play_game
10.times do |i|
if @game_over == true
play_again
else
#get guess from user
make_guess
#compare guess to code
compare_guess
#print out number of guess left
if @game_over == false
puts "You have #{9 - i} guesses left."
end
end
end
#out of attempts - game over
puts "Game over! The correct code was #{@secret_code.join(', ')}."
play_again
end


def make_guess
#prompt user for input
puts "Enter 4 colors, separated by commas. Your choices are: \n #{COLORS.join(', ')}"
@guess = gets.chomp.split(',')
@guess
end

def compare_guess
#current_guess = @guess.each_with_index { |c, i| "#{i} #{c}" }

if exact_match?
puts "Congratulations! You cracked the code."
@game_over = true
#find the matching colors
else
partial_match = []
@guess.each do |c|
if @secret_code.include?(c)
partial_match << c
end
end
#tell the user which colors they had in the wrong spots
puts "You had #{partial_match.length} out of 4 colors correct"
end
partial_match
end

def exact_match?
@guess == @secret_code
end

def play_again
puts "Play again? Enter 'y' or 'n'"
@play_again = gets.chomp
if @play_again == "y"
@game_over = false
play_game
else
puts "Thanks for playing!"
end
end

end
124 changes: 124 additions & 0 deletions TicTacToe/features/step_definitions/tic-tac-toe-steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
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|
expect(@game.welcome_player).to eq arg1
end

Then /^randomly chooses who goes first$/ do
expect([@game.player, "Computer"]).to include @game.current_player
end

Then /^who is X and who is O$/ do
expect(TicTacToe::SYMBOLS).to 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
expect(@game.current_player).to eq "Renee"
end

Given /^the computer knows my name is Renee$/ do
expect(@game.player).to 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.get_player_move
end

Given /^it is the computers turn$/ do
@game = TicTacToe.new(:computer, :O)
expect(@game.current_player).to 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
expect(open_spots).to include(@com_move)
end

Given /^the computer is playing X$/ do
expect(@game.computer_symbol).to eq :X
end

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

Given /^I am playing X$/ do
@game = TicTacToe.new(:player, :X)
expect(@game.player_symbol).to eq :X
end

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

When /^"(.*?)" is not taken$/ do |arg1|
expect(@old_pos).to 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(:computer, :X)
@game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X
end

Then /^I am declared the winner$/ do
@game.determine_winner
expect(@game.player_won?).to be_true
end

Then /^the game ends$/ do
expect(@game.over?).to 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.spots_open?.should be_false
end

Then /^the game is declared a draw$/ do
expect(@game.draw?).to be_true
end

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

Then /^computer should ask me for another position "(.*?)"$/ do |arg1|
@game.board[arg1.to_sym] = ' '
@game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1)
@game.player_move.should eq arg1.to_sym
end
57 changes: 57 additions & 0 deletions TicTacToe/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 "Renee's Move:"
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
Loading