Skip to content

Instantly share code, notes, and snippets.

@colinrubbert
Created September 14, 2015 15:15
Show Gist options
  • Save colinrubbert/14cff5a16f95109cc8aa to your computer and use it in GitHub Desktop.
Save colinrubbert/14cff5a16f95109cc8aa to your computer and use it in GitHub Desktop.
Code Examples
#### Code Examples From Group Capstone Chess Application
## This determines whether or not a King's move is valid
def valid_move?(x_destination, y_destination)
x_distance = (x_coordinates - x_destination).abs <=1
y_distance = (y_coordinates - y_destination).abs <=1
x_distance && y_distance
end
## This method determines whether the game is in check or not.
def in_check?(current_color)
check = []
king = pieces.find_by(type: 'King', color: current_color)
opponent_pieces = pieces.where.not(color: current_color)
opponent_pieces.each do |opponent_piece|
if opponent_piece.type != "King" && opponent_piece.status == 'active'
if opponent_piece.valid_move?(king.x_coordinates, king.y_coordinates)
check << opponent_piece
end
# A new valid_move method for king (king_valid_move_for_in_check?) is used in this iteration
# in order to prevent executing can_castle?, which would lead
# to executing obstructed?, which would throw a runtime error
# when checking the path between two opposite kings
elsif opponent_piece.type == "King"
if opponent_piece.king_valid_move_for_in_check?(king.x_coordinates, king.y_coordinates)
check << opponent_piece
end
end # End opponent_piece valid_move! check
end # End opponent_pieces block for determining if game is in_check
# If check variable has more than 0 items the game is in check otherwise is not in check
if check.count > 0
true
else
false
end
end # End in_check? method
## This section updates the Chess board with the players move,
## there is a stipulation that states that if the player moves
## themselves into check that it will revert the move and make it invalid
def update
@piece = Piece.find(params[:id])
@game = @piece.game
@color = @piece.color
x_coordinates = params[:x_coordinates].to_i
y_coordinates = params[:y_coordinates].to_i
Piece.transaction do
@piece.perform_move!(x_coordinates, y_coordinates)
if @game.in_check?(@color)
raise ActiveRecord::Rollback
end
end
respond_to do |format|
format.html do
redirect_to game_path(@game) # redirect to game show page
end
format.json do
json_result = { valid: @piece.valid, captured: @piece.captured, castle: @piece.castle, status: @piece.status, moved_into_check: @piece.moved_into_check, not_color: @piece.not_color }
render json: json_result
end
end
end
#### Algorithm based code examples
## Luhn's Alogrithm; determines whether or not a credit card number is a valid credit card number
## Luhn's Alogrithm is used in the financial and payments industry to fight credit card fraud
require 'minitest/autorun'
module Luhn
def self.is_valid?(number)
digits = []
number = number.to_s.split("").each {|n| digits << n.to_i }
sum = 0
digits.reverse!
digits.each_with_index do |i, index|
ip = index + 1
if (ip % 2 == 0)
i = i * 2
if i >= 10
i = i - 9
end
else
i
end
sum = sum + i
puts "#{ip} : #{i}"
end
if sum % 10 == 0
true
else
false
end
end
end
class TestLuhn < MiniTest::Unit::TestCase
def test_luhn_valid
assert Luhn.is_valid?(4194560385008504)
end
def test_luhn_invalid
assert ! Luhn.is_valid?(4194560385008505)
end
end
## Fibonacci Sequence and determining what is more efficient iterative or recursive
def recursive_fibonacci(n)
if n < 2
return n
else
return (recursive_fibonacci(n-1) + recursive_fibonacci(n-2))
end
end
def iterative_fibonacci(n)
x = 0
y = 1
if n == 0
return n
else
(1..n).each do
z = x
x = y
y = z + y
end
return x
end
end
# Verify that the recursive_fibonacci & iterative_fibonacci are returning the same number
puts recursive_fibonacci(35)
puts iterative_fibonacci(35)
# Benchmark both of the fibonacci sequences
require 'benchmark'
num = 35
Benchmark.bm do |x|
x.report("recursive_fibonacci") { recursive_fibonacci(num) }
x.report("iterative_fibonacci") { iterative_fibonacci(num) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment