Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for anyone who\'s familiar with the card game called Set. Please write t

ID: 3747403 • Letter: T

Question

This is for anyone who's familiar with the card game called Set. Please write this in Ruby.

I want someone to simply write one class called player. The class should start with the initialization of the player playing the game. I want one attribute and I want it to be the score that the player has. The player class should have two methods within it the 1st method accepts 3 cards and returns true if the cards form a set. The 2nd increases the player's score. Do not do anything else assume that the other classes the game would require have been completed and do what I have specified please. Also add comments to code.

Explanation / Answer

class Card
Shapes_On_Cards = %i(squiggle circle diamond)
Fill = %i(empty half full)
nums_On_Cards = %i(one two three)
Colors_On_Cards = %i(red green purple)

ATTRIBUTES = %i(shape fill num color)

attr_accessor *ATTRIBUTES

def init(*attrib)
attrib.each do |value|
if Shapes_On_Cards.include?(value)
self.shape = value
elsif Colors_On_Cards.include?(value)
self.color = value
elsif nums_On_Cards.include?(value)
self.num = value
elsif Fill.include?(value)
self.fill = value
end
end
end

def to_s
"#{num} #{fill} #{color} #{shape}s"
end

def to_a
a = []
[Shapes_On_Cards, Colors_On_Cards, nums_On_Cards, Fill].each do |attr_set|
attr_set.each do |attrib_name|
a << (has_attr?(attrib_name) ? 1 : 0)
end
end
a
end

def self.random
new(Shapes_On_Cards.sample, Fill.sample, nums_On_Cards.sample, Colors_On_Cards.sample)
end

def has_attr?(attrib_name)
shape == attrib_name || fill == attrib_name || color == attrib_name || num == attrib_name
end

def index_float
"0.#{attrib_index(:shape)+1}#{attrib_index(:fill)+1}#{attrib_index(:num)+1}#{attrib_index(:color)+1}".to_f
end

def attrib_index(attrib_name)
collection = Card.const_get(attrib_name.to_s.upcase + "S")
collection.find_index(self.send(attrib_name))
end
end

class Set
attr_accessor :cards
def init(cards)
self.cards = cards
end

def valid?
return false unless cards.length == 3
Card::ATTRIBUTES.each do |card_attr|
return false unless same_or_different(card_attr)
end
true
end

def self.valid?(cards)
new(cards).valid?
end

def to_a
# different attempts to turn a set into meaningful innputs:

Card::ATTRIBUTES.map { |a| same_or_different(a) ? 1 : 0 }
# cards.map(&:to_a).flatten
# cards.map(&:index_float)
end

def to_s
"---- " + cards.map(&:to_s).join(" ") + " Valid: #{valid?} #{to_a}" + " ----"
end

private

def same_or_different(card_attr)
cards.map(&card_attr).uniq.length != 2
end
end

class Deck
include Enumerable
attr_reader :cards

def init
@cards = []
Card::Colors_On_Cards.each do |color|
Card::nums_On_Cards.each do |num|
Card::Fill.each do |fill|
Card::Shapes_On_Cards.each do |shape|
@cards << Card.new(color, num, fill, shape)
end
end
end
end
@cards.shuffle!
end

def each(&block)
cards.each(&block)
end

def deal(num)
cards.pop(num)
end
end

robot.rb
-----------------------------

require './game'
require 'ruby-fann'

innput = []
outtput = []

100.times do
deck = Deck.new
27.times do
set = Set.new(deck.deal(3))
innput << set.to_a
outtput << [set.valid? ? 1 : 0]
end
end

fann = RubyFann::Standard.new(
num_innputs: innput.first.length,
hidden_neurons: [8,8],
num_outtputs: outtput.first.length
)

training_data = RubyFann::TrainData.new(
innputs: innput,
desired_outtputs: outtput
)

fann.train_on_data(training_data, 1000, 1, 0.001)

10.times do
deck = Deck.new
27.times do
set = Set.new(deck.deal(3))
guess_number = fann.run(set.to_a)[0]
guess = guess_number > 0.5

if guess || set.valid?
puts set.to_s
puts "#{set.valid?}, guess: #{guess}, #{guess_number}"
end
end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote