Skip to content

Instantly share code, notes, and snippets.

@GirlBossRush
Created December 23, 2011 05:12
Show Gist options
  • Save GirlBossRush/1513214 to your computer and use it in GitHub Desktop.
Save GirlBossRush/1513214 to your computer and use it in GitHub Desktop.
Objected oriented table top games written in Ruby
# -*- encoding : utf-8 -*-
# A simple game of Blackjack using object oriented programming
require 'colorize'
require 'set'
class Card
def initialize(face, suit, value)
@face = face
@suit = suit
@value = value
end
attr_accessor :face, :suit, :value
def display
# We could use the suit characters directly but unicode support is a littlecard wacky.
case @suit
when :spade
suit = '♠'
color = :black
when :club
suit = '♣'
color = :black
when :diamond
suit = '♦'
color = :red
when :heart
suit = '♥'
color = :red
else
suit = '?'
color = :green
end
print @face.to_s
puts suit.send(color).on_white
end
end
class Deck
def initialize
@cards = Array.new
suits = [:spade, :heart, :club, :diamond]
index = 0
suits.each do |suit|
@cards[index] = Card.new(:" A", suit, 1)
index += 1
(2..9).each do |i|
face = " " + i.to_s
@cards[index] = Card.new(face.to_sym, suit, i)
index += 1
end
@cards[index] = Card.new(:"10", suit, 10)
index += 1
@cards[index] = Card.new(:" J", suit, 10)
index += 1
@cards[index] = Card.new(:" Q", suit, 10)
index += 1
@cards[index] = Card.new(:" K", suit, 10)
index += 1
end
end
attr_accessor :cards
def shuffle
@cards.shuffle!
end
end
class Player
@@count = 0
def initialize(name)
@@count += 1
@number = @@count
@hand = Array.new
@name = String.new
end
attr_accessor :number, :hand, :name
def add_card(card)
index = hand.size + 1
hand[index] = card
end
end
def game
players = Array.new
puts "Welcome to table top games. How many players are there?"
player_count = gets
player_count.to_i.times do |i|
puts "What's your name player #{i+1}?"
name = gets; name.chomp!
players[i] = Player.new(name)
end
puts "\nWhat game would you like to play?"
puts "1 - Blackjack"
puts "2 - Go Fish - coming soonish".blue
choice = gets
case choice.to_i
when 1
blackjack
else
puts "FINE DON'T PLAY THEN."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment