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

Player class #392

Open
wants to merge 17 commits into
base: main
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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
## War or Peace
### Mentors
This project is given with intent to work through with mentors at least twice through its duration. The following are interactions I had with both Heidi and Jonathan:

This is the starter repo for the BE Mod1 **War or Peace** project.
#### Heidi 11/6
- Heidi and I worked on this Monday night. She helped me with the test spec as I had a difficult time in the Deck class knowing what to instantiate. It's the Deck class but I had to instantiate card objects since they are the makeup of the deck. That was confusing and took some time to work through.

- We started working on the rank_of_card_at method. I couldn't figure out how to start this and with some loose guiding we got there. I'm still struggling to create a method without help starting it. I don't know why this concept is so difficult for me to get on my own.

- I find myself not thinking very clearly when I work through things with a mentor after classes. My brain and logic muscles are tired! We are working on scheduling some things earlier in the mornings and this week we will plan on meeting again on Friday when we both have time off and flexibility to meet in the middle of the day.
-

### Project reflections
What worked well for me in this project?
- Wow. I'm actually understanding things now. I was able to utilize pry more comprehensively which allowed me to play around a lot with code to get it in order. Understanding TDD was very helpful as well.


What didn't work so well - ie. what changes will I implement for my next project?
-


What piece of code am I the most proud of?
- I'm writing this in the second day after release and I'm already pumped about developing my own methods and REALLY understanding the entirety of the code, not just kind of understanding what others helped me with.
9 changes: 9 additions & 0 deletions lib/card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Card
attr_reader :suit, :value, :rank

def initialize(suit, value, rank)
@suit = suit
@value = value
@rank = rank
end
end
34 changes: 34 additions & 0 deletions lib/deck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Deck
attr_reader :cards

def initialize(cards)
@cards = cards
end

def rank_of_card_at(index)
cards[index].rank
end

def high_ranking_cards
high_rank_cards = []
cards.find_all do |card|
if card.rank >= 11
high_rank_cards << card
end
end
high_rank_cards
end

def percent_high_ranking
((self.high_ranking_cards.count.to_f / cards.count.to_f).round(4)) * 100
end

def remove_card
cards.delete_at(0)
cards
end

def add_card(card)
cards.unshift(card)
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_reader :name,
:deck

def initialize(name, deck)
@name = name
@deck = deck
end

def has_lost?
deck == []
end
end

Empty file added lib/turn.rb
Empty file.
17 changes: 11 additions & 6 deletions spec/card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

RSpec.describe Card do
it "exists" do
card = Card.new(:diamond, 'Queen', 12)
card1 = Card.new(:diamond, 'Queen', 12)

expect(card).to be_an_instance_of(Card)
expect(card1).to be_an_instance_of(Card)
end

it "has readable attributes" do
card = Card.new(:diamond, 'Queen', 12)
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:heart, 'Jack', 11)

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

expect(card1.suit).to eq(:diamond)
expect(card1.value).to eq('Queen')
expect(card1.rank).to eq(12)
expect(card2.suit).to eq(:heart)
expect(card2.value).to eq('Jack')
expect(card2.rank).to eq(11)
end
end
69 changes: 69 additions & 0 deletions spec/deck_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'rspec'
require './lib/card'
require './lib/deck'

RSpec.describe Deck do
describe 'initialize' do
it 'deck is an instance of a deck' 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).to be_a(Deck)
expect(card1).to be_a(Card)
expect(deck.cards).to eq(cards)
end
end

describe 'rank of card' do
it '#rank_of_card_at' 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)
expect(deck.rank_of_card_at(1)).to eq(3)
end

it 'determines 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)

deck.high_ranking_cards
expect(deck.high_ranking_cards).to eq([card1, card3])
end

it 'determines percentage of 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)

deck.percent_high_ranking

expect(deck.percent_high_ranking).to eq(66.67)
end
end

describe 'can add or remove cards from deck' do
it 'can #remove_card from the top and #add_card to bottom of the deck' do
card1 = Card.new(:diamond, 'Queen', 12)
card2 = Card.new(:spade, '3', 3)
card3 = Card.new(:heart, 'Ace', 14)
card4 = Card.new(:club, '5', 5)
cards = [card1, card2, card3]
deck = Deck.new(cards)

expect(deck.remove_card).to eq([card2, card3])
expect(deck.add_card(card4)).to eq(cards)
end
end
end
31 changes: 31 additions & 0 deletions spec/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require './lib/card'
require './lib/deck'
require './lib/player'

RSpec.describe Player do
describe 'initializes' do
it 'exists, has name and deck' 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)

expect(player).to be_an_instance_of(Player)
expect(player.name).to eq("Clarisa")
expect(player.deck).to eq(deck)
end
end

describe 'can tell if player has lost' do
it '#has_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)

expect(player.has_lost?).to eq(false)
end
end
end
15 changes: 15 additions & 0 deletions spec/turn_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

require './lib/card'
require './lib/deck'
require './lib/player'
require './lib/turn'

RSpec.describe Turn do
describe 'initialize' do
it 'exists' do
turn = Turn.new(player1, player2)

expect(turn).to be a Turn
end
end
end