Skip to content
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

Kevin Newland #406

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
89b5bf4
Initial commit
kevin-newland Oct 9, 2024
487cf57
Initial commit
kevin-newland Oct 9, 2024
e495068
adding attr_reader
kevin-newland Oct 9, 2024
126d014
adding pry
kevin-newland Oct 9, 2024
8b27ddf
added comment for future reference
kevin-newland Oct 9, 2024
ffcb881
create and initialize deck class
kevin-newland Oct 9, 2024
1d5de3d
created and initialized deck_spec.rb
kevin-newland Oct 9, 2024
e2a4f57
added rank of card method
kevin-newland Oct 10, 2024
dc8a58a
added can find rank
kevin-newland Oct 10, 2024
ec17b1d
created structure to file
kevin-newland Oct 10, 2024
e541a2e
created structure to spec file
kevin-newland Oct 10, 2024
5516666
finished iteration 1
kevin-newland Oct 11, 2024
51d99d4
finished iteration 1
kevin-newland Oct 11, 2024
f23566e
finsihed and comments wrote
kevin-newland Oct 12, 2024
d678130
finished and comments wrote
kevin-newland Oct 12, 2024
29df0b1
initialized player class
kevin-newland Oct 12, 2024
ef71270
initial commit
kevin-newland Oct 12, 2024
d689000
note added for add card method
kevin-newland Oct 12, 2024
6004e78
player class finished
kevin-newland Oct 12, 2024
13fab40
player test file finished and passed iteration test
kevin-newland Oct 12, 2024
2a03086
comments added
kevin-newland Oct 12, 2024
e2864c2
comments added
kevin-newland Oct 12, 2024
dba5879
initialized
kevin-newland Oct 12, 2024
8c10f2e
initialized
kevin-newland Oct 12, 2024
62be74d
def type created
kevin-newland Oct 12, 2024
f7ad5ad
test has type created
kevin-newland Oct 12, 2024
45bd958
added commas to deck.rb
kevin-newland Oct 14, 2024
d507c90
changes to turn.rb
kevin-newland Oct 14, 2024
af5586b
specfile completed
kevin-newland Oct 14, 2024
752f23d
all tests pass except award spoils
kevin-newland Oct 15, 2024
5d1d0af
changes and notes to class file
kevin-newland Oct 15, 2024
93e2137
War_of_peace_rinner created
kevin-newland Oct 15, 2024
01852ec
changes to winner method to account for all possibilities
kevin-newland Oct 15, 2024
9b39fa6
add changes made to award poils test
kevin-newland Oct 15, 2024
3a9abd6
changes to war_or_peace_runner.rb
kevin-newland Oct 15, 2024
d922d8a
adds game class
kevin-newland Oct 15, 2024
b5e0040
take out pry
kevin-newland Oct 15, 2024
7867869
take out pry
kevin-newland Oct 15, 2024
6947461
adds game_spec_file
kevin-newland Oct 15, 2024
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
11 changes: 11 additions & 0 deletions lib/card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Card
attr_reader :suit,
:value,
:rank

def initialize(suit, value, rank)
@suit = suit
@value = value
@rank = rank
end
end
32 changes: 32 additions & 0 deletions lib/deck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Deck
attr_reader :cards, # allows both reading and modifying to be done to the array of cards from outside the class
:rank, #attribute
:value #attribute

def initialize(cards) # takes an array of cards in the form of an argumantand assigns it to the @cards instance variable
@cards = cards
end

def rank_of_card_at(index)# accesses the card at the given index in @cards and returns the rank of that card
@cards[index].rank #accessing a array of cards and retrieving the rank by using a index
end

def high_ranking_cards #returns an array with only high ranking cards
@cards.select { |card| card.rank >= 11} #filters through @cards array returnig a new array containing only cards whos rank is 11 or greater
end

def percent_high_ranking #this method calculates and returns the percentage of high-ranking cards in the deck, rounded to two decimal places.

high_ranking_cards = @cards.select { |card| card.rank >= 11}
expected_percentage = (high_ranking_cards.count.to_f/ @cards.count.to_f) * 100 # calculates
expected_percentage.round(2)
end

def remove_card
@cards.shift #.shift takes the first element out of the array and returns it
end

def add_card(card)
@cards << card #adds card to array of cards
end
end
8 changes: 8 additions & 0 deletions lib/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Game
attr_reader :player1, :player2

def initialize(player1, player2)
@player1 = player1
@player2 = player2
end
end
14 changes: 14 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Player
attr_accessor :name, #allows reading and modifying to be available to the attributes listed
:deck


def initialize(name, deck)# takes the name of the player and the players deck in form of arguments
@name = name
@deck = deck
end

def has_lost? # ? means boolean
@deck.cards.empty? # checks to see if the cards in the deck are empty(returns true if empty, false if not)
end
end
55 changes: 55 additions & 0 deletions lib/turn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Turn
attr_reader :player1, :player2, :spoils_of_war

def initialize(player1, player2)
@player1 = player1
@player2 = player2
@spoils_of_war = []
end

def type
if @player1.deck.rank_of_card_at(0) != @player2.deck.rank_of_card_at(0)
:basic
elsif @player1.deck.rank_of_card_at(0) == @player2.deck.rank_of_card_at(0) && @player1.deck.rank_of_card_at(2) == @player2.deck.rank_of_card_at(2)
:mutually_assured_destruction
else
:war
end
end

def winner #changed to account for all possibilities
if @player1.deck.rank_of_card_at(0) > @player2.deck.rank_of_card_at(0)
@player1
elsif @player1.deck.rank_of_card_at(0) < @player2.deck.rank_of_card_at(0)
@player2
elsif @player1.deck.rank_of_card_at(0) == @player2.deck.rank_of_card_at(0)
if @player1.deck.rank_of_card_at(0) > @player2.deck.rank_of_card_at(0)
@player1
elsif @player1.deck.rank_of_card_at(2) < @player2.deck.rank_of_card_at(2)
@player2
else
"No Winner"
end
end
end

def pile_cards#can use if, elsif, elsif do not need to end with else
if type == :basic
@spoils_of_war << @player1.deck.cards.shift
@spoils_of_war << @player2.deck.cards.shift
elsif type == :war #each player will send three cards (the top three cards) to the spoils pile
@spoils_of_war << @player1.deck.cards.shift(3)
@spoils_of_war << @player2.deck.cards.shift(3)
@spoils_of_war.flatten! #needed to flatten array, was getting a array inside of array
elsif type == :mutually_assured_destruction
@player1.deck.cards.shift(3) # shift(n) → new_array Removes and returns leading elements. in this case we need to remove the first 3
@player2.deck.cards.shift(3)
end
end

def award_spoils(winner)
@spoils_of_war.shuffle! #wont pass with this although I think its #needed to prevent predictability to the game by shuffling the cards before awwarding them to the winner
winner.deck.cards.concat(@spoils_of_war)# concat: Adds to array all elements from each Array
@spoils_of_war.clear
end
end
8 changes: 5 additions & 3 deletions spec/card_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require 'rspec'
require './lib/card'

require 'pry'
# was missing (require 'pry';binding.pry)
RSpec.describe Card do
it "exists" do
card = Card.new(:diamond, 'Queen', 12)

expect(card).to be_an_instance_of(Card)
end

it "has readable attributes" do
card = Card.new(:diamond, 'Queen', 12)

expect(card.suit).to eq(:diamond)
expect(card.value).to eq('Queen')
expect(card.rank).to eq(12)

end
end
78 changes: 78 additions & 0 deletions spec/deck_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'rspec'
require './lib/card'
require './lib/deck'
require 'pry'

RSpec.describe Deck do
#describe "#initialize" do
it "exists" do
card1 = Card.new(:diamond, 'Queen', 12)# setup creates an instance of the Card
card2 = Card.new(:spade, '3', 3)#setup creates an instance of the Card
card3 = Card.new(:heart, 'Ace', 14)#setup creates an instance of the Card
cards = [card1, card2, card3] #setup creates an instance of the array

deck = Deck.new(cards) # execution this creates a new instance of the Deck class, initializing it with the array of cards cards.

expect(deck.cards).to eq([card1, card2, card3]) #assertion: checking that the decks cards match the expected cards
end

it "can find rank at a index" do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
cards = [card1, card2, card3]

deck = Deck.new(cards)

expect(deck.rank_of_card_at(0)).to eq(12)#assertion: checking if card at index 0 has a rank of 12
expect(deck.rank_of_card_at(2)).to eq(14)#assertion: checking if card at index 2 has a rank of 14
end

it "high ranking cards" do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
cards = [card1, card2, card3]

deck = Deck.new(cards)

expect(deck.high_ranking_cards).to eq([card1, card3])# assertion: checking if deck.high_ranking_cards returns only the high-ranking cards [card1, card3].
end

it "percentage high ranking" do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
cards = [card1, card2, card3]
expected_percentage = (2.to_f / 3.to_f * 100).round(2) #calculating the expected percentage and rounding it to two decimals places. (to_f: changes it to a float)
deck = Deck.new(cards)
expect(deck.percent_high_ranking).to eq(expected_percentage)#assertion checking that deck.percent_high_ranking matches the expected percentage
end

it "removes card" do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
cards = [card1, card2, card3]

deck = Deck.new(cards)
expect(deck.remove_card).to eq(card1)#execute and asserts that the card removed is card1
expect(deck.cards).to eq([card2, card3])#asserts that the cards left are card2 and card3
end

it "adds card" do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
cards = [card1, card2, card3]
card4 = Card.new(:club, '5', 5)#the card that is added

deck = Deck.new(cards)

deck.add_card(card4)#execution adds card4 to the deck
expect(deck.cards).to eq([card1, card2, card3, card4])#assertion checks to see if the card was added

end

#end
end
47 changes: 47 additions & 0 deletions spec/game_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require './lib/card'
require './lib/deck'
require './lib/player'
require './lib/turn'
require './lib/game'

RSpec.describe Game do
describe'#initialize' do
it 'is instance of game' do
suits = [:heart, :diamond, :spade, :club]

all_cards = []

suits.each do |suit|
all_cards << Card.new(:suit, "2", 2)
all_cards << Card.new(:suit, "3", 3)
all_cards << Card.new(:suit, "4", 4)
all_cards << Card.new(:suit, "5", 5)
all_cards << Card.new(:suit, "6", 6)
all_cards << Card.new(:suit, "7", 7)
all_cards << Card.new(:suit, "8", 8)
all_cards << Card.new(:suit, "9", 9)
all_cards << Card.new(:suit, "10", 10)
all_cards << Card.new(:suit, "Jack", 11)
all_cards << Card.new(:suit, "Queen", 12)
all_cards << Card.new(:suit, "King", 13)
all_cards << Card.new(:suit, "Ace", 14)
end

all_cards.shuffle!

deck1 = Deck.new(all_cards[0...26])
deck2 = Deck.new(all_cards[26..51])



player1 = Player.new("Megan", deck1)
player2 = Player.new("Aurora", deck2)

game = Game.new(player1, player2)

expect(game).to be_instance_of(Game)
expect(game.player1).to eq(player1)
expect(game.player2).to eq(player2)
end
end
end
35 changes: 35 additions & 0 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require './lib/card'
require './lib/deck'
require './lib/player'
require 'rspec'
require 'pry'
# player is initialized witha deck, and can determine if they have lost a game.
RSpec.describe Player do
it "exists" do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
deck = Deck.new([card1, card2, card3])

player = Player.new('Clarisa', deck)#Execution creates player OBJECT with name and deck(reminder that deck was already created)


expect(player.name).to eq('Clarisa')#assertion checks to see if the players name is equal to 'clarisa'
expect(player.deck).to eq(deck)#assertion checks to see if the players deck is equal to the deck that was given to the player upon creation
end

it "can determine if they have lost" do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
deck = Deck.new([card1, card2, card3])

player = Player.new('Clarisa', deck)
player.deck.remove_card#execution removes card from the deck
player.deck.remove_card
player.deck.remove_card
expect(player.deck.cards).to eq([])#assertion checks to see if all the cards in the deck have been removed
expect(player.has_lost?).to eq(true)# assertion checks to see if the player has lost
end
#binding.pry
end
Loading