Skip to content

Instantly share code, notes, and snippets.

@kexline4710
Created December 15, 2013 21:30
Show Gist options
  • Select an option

  • Save kexline4710/7978433 to your computer and use it in GitHub Desktop.

Select an option

Save kexline4710/7978433 to your computer and use it in GitHub Desktop.
class Die
attr_accessor :selected
def initialize
@selected = false
end
end
require 'rspec'
describe Die do
let(:die) {Die.new}
describe "#new" do
it "creates a Die object" do
expect(die).to be_an_instance_of(Die)
end
it "sets selected to false" do
expect(die.selected).to eq(false)
end
end
end
require_relative 'die'
require 'debugger'
class Player
attr_reader :total_score, :dice_set
def initialize
@total_score = 0
@dice_set = []
end
def roll_dice
@dice_set = [Die.new] * 6
end
def make_selection(*indices)
# debugger
indices.each do |selection|
index = selection - 1
die = @dice_set[index]
die.selected = true
end
@dice_set.each {|die| print "#{die.selected} "}
end
end
require 'rspec'
require_relative 'player'
describe Player do
let(:player) { Player.new }
describe "#new" do
it "creates Player object" do
expect(player).to be_an_instance_of(Player)
end
it "sets player's total score" do
expect(player.total_score).to eq(0)
end
end
describe "#roll_dice" do
it "generates array of 6 die" do
expect(player.roll_dice.length).to eq(6)
expect(player.roll_dice.first).to be_an_instance_of(Die)
end
it "sets dice to dice_set" do
player.roll_dice
expect(player.dice_set.length).to eq(6)
end
end
describe "#make_selection" do
it "changes die values to selected with 1 value" do
player.roll_dice
player.make_selection(1)
expect(player.dice_set[0].selected).to eq(true)
end
it "changes die values to select with more than 1 value" do
player.roll_dice
player.make_selection(1,3)
expect(player.dice_set[2].selected).to eq(true)
end
it "does not change die that are not selected" do
player.roll_dice
player.make_selection(1)
expect(player.dice_set[5].selected).to eq(false)
end
end
end
@kexline4710
Copy link
Author

Figured it out (with some help)! In the roll_dice method, I was creating an array of 6 die that were the SAME object.

now it is
6.times {@dice_set << Die.new}
@dice_set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment