Develop - CMD Line Archive
The following .rb files are the core of command line algorithm.
./classes/card.rb
class Card # there are 81 unique cards in a Set deck. we give each card an ternary ID number. 0-80 # color: first digit. 0: green, 1: purple, 2: red # shape: second digit. # # def initialize(id) id_base3 = ("000" + id.to_s(3))[-4,4] @id = id @id_base3 = id_base3 @color = CardColor.new(id_base3[3].to_i) @shape = CardShape.new(id_base3[2].to_i) @shading = CardShading.new(id_base3[1].to_i) @number = CardNumber.new(id_base3[0].to_i) end def color @color end def shape @shape end def shading @shading end def number @number end def id @id end def id_base3 @id_base3 end def to_s "#{@number.to_s} #{@shading.to_s} #{@color.to_s} #{@shape.to_s}#{@number.to_i > 1 ? 's' : nil}" end end class CardColor def initialize(n) @color = n end def to_s case @color when 0 "Green" when 1 "Purple" when 2 "Red" else "Invalid Color" end end end class CardShape def initialize(n) @shape = n end def to_s case @shape when 0 "Diamond" when 1 "Squiggle" when 2 "Oval" else "Invalid Shape" end end end class CardShading def initialize(n) @shading = n end def to_s case @shading when 0 "Open" when 1 "Striped" when 2 "Solid" else "Invalid Shading" end end end class CardNumber def initialize(n) @number = n end def to_s (@number + 1).to_s end def to_i @number + 1 end end
./classes/deck.rb
require_relative './card' class Deck def initialize @deck = Array.new(81) {|i| i} end def draw drawn_card_index = rand(@deck.size) drawn_card = Card.new(@deck[drawn_card_index]) @deck.delete_at(drawn_card_index) drawn_card end def size @deck.size end end
./classes/methods.rb
require_relative '../classes/card' def is_set(card1, card2, card3) id_sum = (card1.id_base3.to_i + card2.id_base3.to_i + card3.id_base3.to_i).to_s; digits = id_sum.count("0") + id_sum.count("3") + id_sum.count("6") digits == 4 end def count_sets(table_array) num = 0 for i in 0...(table_array.size - 2) for j in (i + 1)...(table_array.size - 1) for k in (j + 1)...table_array.size if is_set(table_array[i], table_array[j], table_array[k]) num += 1 end end end end num end def put_sets(table_array) num = 0 for i in 0...(table_array.size - 2) for j in (i + 1)...(table_array.size - 1) for k in (j + 1)...table_array.size if is_set(table_array[i], table_array[j], table_array[k]) num += 1 puts "\nSet #{num}:\n #{table_array[i].to_s}\n #{table_array[j].to_s}\n #{table_array[k].to_s}" end end end end end
set.rb
require_relative './classes/deck' require_relative './classes/methods' puts "Here are the 12 cards on table. Take a look if you can find a set!" #initialize deck and table game_deck = Deck.new table = Array.new found_set = 0 #draw 12 cards to the table index = 0 while index < 12 game_card = game_deck.draw puts "[#{index.to_s}] #{game_card.to_s}" table.push(game_card) index += 1 end #keep adding 3 cards if there is no set num_sets = count_sets(table) while num_sets == 0 index = 0 while index < 3 game_card = game_deck.draw puts "[#{index.to_s}] #{game_card.to_s}" table.push(game_card) end num_sets = count_sets(table) end #game ends when there is no card left in deck and no set on table while game_deck.size > 0 || num_sets > 0 puts put_sets(table) #get let user pick 3 cards to form a set puts "Enter your first card index starting from 0:" choice1 = gets.chomp puts "Enter your second card index starting from 0:" choice2 = gets.chomp puts "Enter your third card index starting from 0:" choice3 = gets.chomp #check if set check = is_set(table[choice1.to_i],table[choice2.to_i],table[choice3.to_i]) puts check #if its a set, remove 3 cards and new from deck if check == true found_set += 1 puts "That is a set! You have found #{found_set.to_s} set." #remove the 3 cards that player found table.delete_at(choice1.to_i) table.delete_at(choice2.to_i-1) table.delete_at(choice3.to_i-2) if game_deck.size != 0 num_sets = count_sets(table) #add 3 new cards from the deck while table.size < 12 || num_sets == 0 game_card = game_deck.draw table.push(game_card) game_card = game_deck.draw table.push(game_card) game_card = game_deck.draw table.push(game_card) end puts "Here is the new table:" index = 0 while index < table.size puts "[#{index.to_s}] #{table[index].to_s}" index += 1 end end #if its not a set, then ask for a set again else puts "That is not a set, try again" end num_sets = count_sets(table) end