Skip to content

Instantly share code, notes, and snippets.

View mjansen401's full-sized avatar

Mike Jansen mjansen401

View GitHub Profile
@mjansen401
mjansen401 / about_scoring_project.rb
Created February 23, 2011 16:17
Ruby Koan for how to score the dice game "Greed"
require File.expand_path(File.dirname(__FILE__) + '/edgecase')
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
@mjansen401
mjansen401 / greed.rb
Created March 2, 2011 02:41
Greed koan refactored
class Greed
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
def run
game_setup
run_the_game
restart while play_again?
rescue Exception
exit
end
@mjansen401
mjansen401 / gist:894373
Created March 30, 2011 13:23
TTT game_setup
def game_setup
@player_one = setup_player(PLAYER_ONE,DEFAULT_PLAYER_ONE_MARK)
@player_two = setup_player(PLAYER_TWO,DEFAULT_PLAYER_TWO_MARK)
@board = Board.new(@player_one, @player_two)
end
@mjansen401
mjansen401 / gist:901565
Created April 4, 2011 12:41
AI factory
class AIFactory
def create_ai(ai_type)
if ai_type == :unbeatable
ai = Minimax.new
elsif ai_type == :easy
ai = Random.new
end
ai
end
end
@mjansen401
mjansen401 / gist:901578
Created April 4, 2011 12:50
AI Abstract Factory
class UnbeatableFactory
def self.create
Minimax.new
end
end
class EasyFactory
def self.create
Random.new
end
@mjansen401
mjansen401 / gist:945540
Created April 28, 2011 00:20
Google Chart Bar
<% chart = Gchart.bar(
# see http://code.google.com/apis/chart/docs/gallery/bar_charts.html for Google Charts API bar chart info
#
:grouped => 'true',
:bar_colors => "D6A061,331F08",
:bar_width_and_spacing => 'a,5,30', # bar width, spacing width, group spacing width
:size => '700x400',
:title => "Expected vs Actual Velocity",
:bg => 'FFFFFF00', # transparent
:legend => ['Expected', 'Actual'],
@mjansen401
mjansen401 / gist:963646
Created May 9, 2011 23:37
_square.html.erb_spec.rb
require 'spec_helper'
describe "games/_square.html.erb" do
before(:each) do
@ttt = mock(TicTacToe)
end
it "displays a filled square" do
@ttt.stub!(:square_empty?).and_return false
@ttt.stub!(:get_square_value).and_return "X"
@mjansen401
mjansen401 / gist:963645
Created May 9, 2011 23:36
_square.html.erb
<td class="square">
<% if @ttt.square_empty?(location.to_i) && @ttt.in_progress? %>
<%= submit_tag(location, :class => "square", :name => "location") %>
<% elsif !@ttt.square_empty?(location.to_i) %>
<%= @ttt.get_square_value(location.to_i) %>
<% elsif !(@ttt.in_progress?) %>
<% end%>
</td>
@mjansen401
mjansen401 / gist:963639
Created May 9, 2011 23:32
_board.html.erb
<table class="board">
<tr>
<%= render :partial => 'games/square', :locals => {:location => '0'}%>
<%= render :partial => 'games/square', :locals => {:location => '1'}%>
<%= render :partial => 'games/square', :locals => {:location => '2'}%>
</tr>
<tr>
<%= render :partial => 'games/square', :locals => {:location => '3'}%>
<%= render :partial => 'games/square', :locals => {:location => '4'}%>
<%= render :partial => 'games/square', :locals => {:location => '5'}%>