Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiQmar committed May 21, 2016
0 parents commit fb90b39
Show file tree
Hide file tree
Showing 68 changed files with 1,089 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

# Ignore all logfiles and tempfiles.
/log/*
!/log/.keep
/tmp
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source 'https://rubygems.org'


gem 'rails', '4.2.3'
gem 'mysql2'
gem 'haml'
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
end

124 changes: 124 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.2.3)
actionpack (= 4.2.3)
actionview (= 4.2.3)
activejob (= 4.2.3)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.3)
actionview (= 4.2.3)
activesupport (= 4.2.3)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.3)
activesupport (= 4.2.3)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (4.2.3)
activesupport (= 4.2.3)
globalid (>= 0.3.0)
activemodel (4.2.3)
activesupport (= 4.2.3)
builder (~> 3.1)
activerecord (4.2.3)
activemodel (= 4.2.3)
activesupport (= 4.2.3)
arel (~> 6.0)
activesupport (4.2.3)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (8.2.5)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
erubis (2.7.0)
globalid (0.3.6)
activesupport (>= 4.1.0)
haml (4.0.7)
tilt
i18n (0.7.0)
json (1.8.3)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
mime-types (3.0)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0221)
mini_portile2 (2.0.0)
minitest (5.8.4)
mysql2 (0.3.18)
nokogiri (1.6.7.2)
mini_portile2 (~> 2.0.0.rc2)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.2.3)
actionmailer (= 4.2.3)
actionpack (= 4.2.3)
actionview (= 4.2.3)
activejob (= 4.2.3)
activemodel (= 4.2.3)
activerecord (= 4.2.3)
activesupport (= 4.2.3)
bundler (>= 1.3.0, < 2.0)
railties (= 4.2.3)
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.7)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (4.2.3)
actionpack (= 4.2.3)
activesupport (= 4.2.3)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.1.2)
spring (1.7.1)
sprockets (3.6.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.0.4)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.2)
tzinfo (1.2.2)
thread_safe (~> 0.1)
web-console (2.3.0)
activemodel (>= 4.0)
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)

PLATFORMS
ruby

DEPENDENCIES
byebug
haml
mysql2
rails (= 4.2.3)
spring
web-console (~> 2.0)

BUNDLED WITH
1.11.2
28 changes: 28 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
== README

This README would normally document whatever steps are necessary to get the
application up and running.

Things you may want to cover:

* Ruby version

* System dependencies

* Configuration

* Database creation

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...


Please feel free to use a different markup language if you do not plan to run
<tt>rake doc:app</tt>.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks
Empty file added app/assets/images/.keep
Empty file.
16 changes: 16 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/game.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
15 changes: 15 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require_tree .
*= require_self
*/
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
Empty file added app/controllers/concerns/.keep
Empty file.
23 changes: 23 additions & 0 deletions app/controllers/game_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class GameController < ApplicationController

def index
end

def start
@game = Game.new
@game.deal_initial_cards
render 'game.html.haml'
end

def hit
@game = Game.new(params[:game_id])
@game.deal_card(Game::USER[:player])
render 'game.html.haml'
end

def stand
@game = Game.new(params[:game_id])
@game.stand
render 'game.html.haml'
end
end
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ApplicationHelper
end
2 changes: 2 additions & 0 deletions app/helpers/game_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module GameHelper
end
Empty file added app/mailers/.keep
Empty file.
Empty file added app/models/.keep
Empty file.
19 changes: 19 additions & 0 deletions app/models/card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Card < ActiveRecord::Base

def self.get_score(user, game_id)
cards = Card.where(game_id: game_id, user: user)
cards.collect(&:value)
end

def get_value
case value
when 'A'
11
when 'K', 'Q', 'J'
10
else
value.to_i
end
end

end
Empty file added app/models/concerns/.keep
Empty file.
72 changes: 72 additions & 0 deletions app/models/game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class Game

attr_reader :id, :player_cards, :dealer_cards, :player_score, :dealer_score, :winner

USER = {:player => 1, :dealer => 2}


def initialize(id=nil)
if id.nil?
@decks = initialize_decks(6)
@id = (Card.last and Card.last.game_id + 1) || 0
@player_cards = []
@dealer_cards = []
else
@id = id
cards = Card.where(game_id: id)
@player_cards = cards.select{|card| card.user == USER[:player]}
@dealer_cards = cards.select{|card| card.user == USER[:dealer]}
@decks = initialize_decks(6, (@player_cards+@dealer_cards).map{|a| a.value})
update_scores
end
end

def deal_initial_cards
2.times { @player_cards << Card.create(user: USER[:player], value: pop_card, game_id: self.id ) }
@dealer_cards << Card.create(user: USER[:dealer], value: pop_card, game_id: self.id )
update_scores
who_won?
end

def deal_card(user)
card = Card.create(user: user, value: pop_card, game_id: self.id)
if user == USER[:player]
@player_cards << card
else
@dealer_cards << card
end
update_scores
who_won?
end

def stand
return if winner.present?
while (@dealer_score < 17) do
deal_card(USER[:dealer])
end
@winner = (@player_score > @dealer_score) ? 'Player' : 'Dealer' if winner.blank?
end

private

def update_scores
@player_score = @player_cards.collect {|card| card.get_value }.sum
@dealer_score = @dealer_cards.collect {|card| card.get_value }.sum
end

def who_won?
if @player_score == 21 || @dealer_score > 21
@winner = 'Player'
elsif @player_score > 21 || @dealer_score == 21
@winner = 'Dealer'
end
end

def pop_card
@decks.shuffle.delete_at(0)
end

def initialize_decks(num_of_decks, existing_cards=[])
['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'K', 'Q', 'J'] * num_of_decks - existing_cards
end
end
Loading

0 comments on commit fb90b39

Please sign in to comment.