This is my simple application to showcase how an Array works.
It compares the cards (as strings) dealt to two players, like a slowmo gamesmaster demo.
Moving stuff into methods worked easily, but class has more features and a simple inheritance is disallowed. So what would be needed here instead? A getter, a property, an include? Please demonstrate by replacing the first line with a proper Crystal class initialization. The rest of the application is complete and should return different results and scores with each run on the CLI terminal.
class Card < String
def card_value
case self
when "ace"
14
when "king"
13
when "queen"
12
else
super.to_i
end
end
end
def card_factory
card_deck = [] of Card
4.times do card_deck << "ace" end
4.times do card_deck << "king" end
4.times do card_deck << "queen" end
4.times do card_deck << "6" end
return card_deck
end
# card_deck = card_factory.shuffle
# player01 = card_deck.pop
# player02 = card_deck.pop
# puts "We have a winner!" unless player01 == player02
# sleep 2
# puts "#{player01} vs #{player02}"
# puts "It was a tie." unless player01 != player02
def deal_all_cards(argument)
shuffled_deck = argument.shuffle
hand1 = [] of Card
hand2 = [] of Card
until shuffled_deck.size < 2
hand1 << shuffled_deck.pop
hand2 << shuffled_deck.pop
end
return hand1, hand2
end
player1, player2 = deal_all_cards card_factory
score_player1 = score_player2 = tie_count = 0
player1.each_with_index do |card, index|
if card.card_value > player2[index].card_value
score_player1 += 1
elsif card.card_value < player2[index].card_value
score_player2 += 1
else
tie_count += 1
end
break if score_player1 == 3
break if score_player2 == 3
# break if score_player1 == 3 | score_player2 == 3 #abuse of | bugs crystal, keep breaks singular.
end
puts "Player 1 has #{score_player1} points." if score_player1 != 1
puts "Player 1 has #{score_player1} point." if score_player1 == 1
puts "Player 2 has #{score_player2} points." unless score_player2 == 1
puts "Player 2 has #{score_player2} point." unless score_player2 != 1
puts "#{tie_count} rounds ended in a tie"
# puts
# puts "player one's cards: #{player1}"
# puts " vs"
# puts "player two's cards: #{player2}"