Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Gin rummy score keeper CLI
class Game
attr_accessor :players, :round, :counter
def initialize
@players = Player.all
@round = 1
@counter = 0
end
def next_round
@round += 1
if @counter == @players.length-1
@counter = 0
else
@counter += 1
end
end
def show_scores
sorted_players = @players.sort {|a, b| b.score <=> a.score}
puts ""
puts "-"*26
puts "Round #{@round} Scores"
puts "-"*26
puts "Name Score"
sorted_players.each do |player|
space = " "*(12-player.name.length-player.score.to_s.length)
puts "#{player.name}#{space}#{player.score}"
end
puts "-"*26
puts "\n\n"
end
def won?
@players.find {|player| player.score >= 500} ? true : false
end
def winner
scores = []
@players.each {|player| scores << player.score}
@players[scores.find_index(scores.max)]
end
end
class Player
attr_accessor :name, :score
@@all = []
def initialize(name)
@name = name
@score = 0
@@all << self
end
def self.all
@@all
end
def add_score(score)
@score += score.to_i
end
end
class GameController
attr_accessor :game
def self.new_game
puts "-"*26
puts "Gin Rummy Score Calculator"
puts "-"*26
puts "How many players will there be?"
player_num = gets.chomp.to_i
counter = 1
player_num.times do
puts "Enter name for Player #{counter}. #{'Player 1 deals first' if counter == 1}"
Player.new(gets.chomp)
counter += 1
end
@game = Game.new
until @game.won? do
self.play_round
end
puts "*~"*16 + "*"
puts "Congratulations #{@game.winner.name}, you won!"
puts "*~"*16 + "*"
end
def self.play_round
puts ""
puts "Round #{@game.round}"
puts "-"*26
puts "It's #{@game.players[@game.counter].name}'s turn to deal."
puts "\n"
@game.players.each do |player|
puts "Enter the Round #{@game.round} score for #{player.name}"
score = gets.chomp
puts "Look good? Y/n"
until gets.chomp.downcase == "y"
puts "Enter the Round #{@game.round} score for #{player.name}"
score = gets.chomp
puts "Look good? Y/n"
end
player.add_score(score)
end
@game.show_scores
@game.next_round
end
end
class GinRummy
def self.call
GameController.new_game
end
end
GinRummy.call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment