Created
December 15, 2013 21:30
-
-
Save kexline4710/7978433 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Die | |
attr_accessor :selected | |
def initialize | |
@selected = false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
Goal: to create a Farkle game in pure ruby using Rspec and TDD. Eventually a user will be able to select one or more dice in a set of 6.
Problem: My #make_selection method in the Player class is not working the way I intend it to. If the method is given one or more numbers to represent the user's selection, I want to be able to change the "selected" attribute of the Die objects in the @dice_set array to TRUE.
After lots of debugging I determined that line 22 in the player.rb file is assigning ALL of the Die objects in the dice_set selected attribute to TRUE, rather than the die at a particular index. You can see this from the output from line 24.
I'm trying to understand why this is changing the attributes of ALL the objects in the array, and whether or not there is a better option to achieve this goal.