Skip to content

Commit

Permalink
Format all source code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Oct 30, 2015
1 parent b3b1223 commit 0f4e586
Show file tree
Hide file tree
Showing 300 changed files with 3,728 additions and 3,774 deletions.
8 changes: 4 additions & 4 deletions docs/char.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ struct Char
# and thus its byte representation.
#
# ```
# 'a'.ord #=> 97
# '\0'.ord #=> 0
# '\u007f'.ord #=> 127
# '☃'.ord #=> 9731
# 'a'.ord # => 97
# '\0'.ord # => 0
# '\u007f'.ord # => 127
# '☃'.ord # => 9731
# ```
def ord : Int32
1
Expand Down
12 changes: 6 additions & 6 deletions docs/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ module Macros
# define_method "bar", 2
# define_method baz, 3
#
# puts foo #=> prints 1
# puts bar #=> prints 2
# puts baz #=> prints 3
# puts foo # => prints 1
# puts bar # => prints 2
# puts baz # => prints 3
# ```
def id : MacroId
end
Expand All @@ -116,7 +116,7 @@ module Macros
# {{ "foo".class_name }}
# end
#
# puts test #=> prints StringLiteral
# puts test # => prints StringLiteral
# ```
def class_name : StringLiteral
end
Expand Down Expand Up @@ -146,8 +146,8 @@ module Macros
# {% end %}
# end
#
# test 1 #=> prints "Got a number literal"
# test "hi" #=> prints "Didn't get a number literal"
# test 1 # => prints "Got a number literal"
# test "hi" # => prints "Didn't get a number literal"
# ```
def is_a?(name) : BoolLiteral
end
Expand Down
78 changes: 39 additions & 39 deletions samples/2048.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ module Screen
TILES = {
0 => {:white, :black},
2 => {:black, :white},
4 => {:blue, :white},
4 => {:blue, :white},
8 => {:black, :yellow},
16 => {:white, :red},
32 => {:black, :red},
64 => {:white, :magenta},
128 => {:red, :yellow},
256 => {:magenta, :yellow},
128 => {:red, :yellow},
256 => {:magenta, :yellow},
512 => {:white, :yellow},
1024 => {:white, :yellow},
2048 => {:white, :yellow},
4096 => {:white, :black},
8192 => {:white, :black},
16384 => {:white, :black},
32768 => {:white, :black},
65536 => {:white, :black}
65536 => {:white, :black},
}

def self.colorize_for(tile)
Expand Down Expand Up @@ -69,8 +69,8 @@ module Screen
end

class Drawer
INNER_CELL_WIDTH = 16
INNER_CELL_HEIGHT = 6
INNER_CELL_WIDTH = 16
INNER_CELL_HEIGHT = 6

def initialize
@n = 0
Expand All @@ -79,11 +79,11 @@ class Drawer
@content_line = false
end

def set_current_row row
def set_current_row(row)
@current_row = row
end

def draw grid
def draw(grid)
@grid = grid
@n = @grid.size

Expand All @@ -94,21 +94,21 @@ class Drawer
def box
top_border

(@n-1).times do |row|
(@n - 1).times do |row|
tile row
mid_border
end

tile @n-1
tile @n - 1

bottom_border
end

def tile row
def tile(row)
set_current_row @grid[row]

INNER_CELL_HEIGHT.times do |i|
if i == (@n / 2)+1
if i == (@n / 2) + 1
content_line
else
space_line
Expand Down Expand Up @@ -140,21 +140,21 @@ class Drawer
line "", "", "", ""
end

def line left, fill, inner, right
def line(left, fill, inner, right)
print left

(@n-1).times do |cell|
(@n - 1).times do |cell|
cell_line fill, cell

print inner
end

cell_line fill, @n-1
cell_line fill, @n - 1

puts right
end

def cell_line fill, cell
def cell_line(fill, cell)
content = @current_row.at(cell) { "empty" }
tile_value = (content == "empty" ? 0 : (content.to_i? || 0)).to_i
content = "" if !@content_line || content == "empty"
Expand Down Expand Up @@ -225,7 +225,7 @@ class Game

empty_cells = @grid.map(&.count &.nil?).sum

fill_cell = empty_cells > 1 ? rand(empty_cells-1)+1 : 1
fill_cell = empty_cells > 1 ? rand(empty_cells - 1) + 1 : 1

empty_cell_count = 0

Expand All @@ -240,14 +240,14 @@ class Game
end

def each_cell_with_index
0.upto(@grid.size-1) do |row|
0.upto(@grid.size-1) do |col|
0.upto(@grid.size - 1) do |row|
0.upto(@grid.size - 1) do |col|
yield @grid[row][col], row, col
end
end
end

def execute_action action
def execute_action(action)
if [:up, :down, :left, :right].includes? action
if can_move_in? action
shift_grid action
Expand All @@ -264,38 +264,38 @@ class Game
end
end

def shift_grid direction
def shift_grid(direction)
drow, dcol = offsets_for direction
shift_tiles_to_empty_cells direction, drow, dcol
merge_tiles direction, drow, dcol
shift_tiles_to_empty_cells direction, drow, dcol
end

def shift_tiles_to_empty_cells direction, drow, dcol
def shift_tiles_to_empty_cells(direction, drow, dcol)
modified = true
while modified
modified = false
movable_tiles(direction, drow, dcol) do |tile, row, col|
unless @grid[row+drow][col+dcol]
@grid[row+drow][col+dcol] = tile
unless @grid[row + drow][col + dcol]
@grid[row + drow][col + dcol] = tile
@grid[row][col] = nil
modified = true
end
end
end
end

def merge_tiles direction, drow, dcol
def merge_tiles(direction, drow, dcol)
movable_tiles(direction, drow, dcol) do |tile, row, col|
if @grid[row+drow][col+dcol] == tile
if @grid[row + drow][col + dcol] == tile
@grid[row][col] = nil
@grid[row+drow][col+dcol] = tile*2
@grid[row + drow][col + dcol] = tile*2
end
end
end

def movable_tiles direction, drow, dcol
max = @grid.size-1
def movable_tiles(direction, drow, dcol)
max = @grid.size - 1
from_row, to_row, from_column, to_column =
case direction
when :up, :left
Expand All @@ -315,18 +315,18 @@ class Game
end
end

def can_move_in? direction
def can_move_in?(direction)
drow, dcol = offsets_for direction

movable_tiles(direction, drow, dcol) do |tile, row, col|
target_tile = @grid[row+drow][col+dcol]
target_tile = @grid[row + drow][col + dcol]
return true if !target_tile || target_tile == tile
end

false
end

def offsets_for direction
def offsets_for(direction)
drow = dcol = 0

case direction
Expand All @@ -345,16 +345,16 @@ class Game
{drow, dcol}
end

def to_border? direction, row, col, drow, dcol
def to_border?(direction, row, col, drow, dcol)
case direction
when :up
row+drow < 0
row + drow < 0
when :down
row+drow >= @grid.size
row + drow >= @grid.size
when :left
col+dcol < 0
col + dcol < 0
when :right
col+dcol >= @grid.size
col + dcol >= @grid.size
else
false
end
Expand All @@ -370,10 +370,10 @@ class Game

def can_move?
can_move_in?(:up) || can_move_in?(:down) ||
can_move_in?(:left) || can_move_in?(:right)
can_move_in?(:left) || can_move_in?(:right)
end

def end_game msg
def end_game(msg)
puts msg
exit
end
Expand Down
1 change: 1 addition & 0 deletions samples/binary-trees.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Node
def initialize(@a, @b, @c)
end

property :a
property :b
property :c
Expand Down
23 changes: 11 additions & 12 deletions samples/brainfuck.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class Program
pc = 0
while pc < @chars.size
case @chars[pc]
when '>'; tape.advance
when '<'; tape.devance
when '+'; tape.inc
when '-'; tape.dec
when '.'; print tape.get.chr
when '['; pc = @bracket_map[pc] if tape.get == 0
when ']'; pc = @bracket_map[pc] if tape.get != 0
when '>'; tape.advance
when '<'; tape.devance
when '+'; tape.inc
when '-'; tape.dec
when '.'; print tape.get.chr
when '['; pc = @bracket_map[pc] if tape.get == 0
when ']'; pc = @bracket_map[pc] if tape.get != 0
end
pc += 1
end
Expand Down Expand Up @@ -75,15 +75,14 @@ class Program
end

text = if ARGV.size > 0
File.read(ARGV[0])
else
" Benchmark brainf*ck program
File.read(ARGV[0])
else
" Benchmark brainf*ck program
>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
++++++++[>++++++++++[>++++++++++[>++++++++++[>+
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.
"
end
end

Program.parse(text).run

30 changes: 15 additions & 15 deletions samples/conway.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module IO
end

struct ConwayMap
WIDTH = 40
WIDTH = 40
HEIGHT = 30

include Math
Expand Down Expand Up @@ -49,8 +49,8 @@ struct ConwayMap
WIDTH.times do |j|
nlive = 0

max(i-1, 0).upto(min(i+1, HEIGHT-1)) do |i2|
max(j-1, 0).upto(min(j+1, WIDTH-1)) do |j2|
max(i - 1, 0).upto(min(i + 1, HEIGHT - 1)) do |i2|
max(j - 1, 0).upto(min(j + 1, WIDTH - 1)) do |j2|
nlive += 1 if old_map[i2][j2] && (i2 != i || j2 != j)
end
end
Expand All @@ -76,19 +76,19 @@ struct ConwayMap
end
end

PAUSE_MILLIS = 20
PAUSE_MILLIS = 20
DEFAULT_COUNT = 300
INITIAL_MAP = [
" 1 ",
" 1 1 ",
" 11 11 11",
" 1 1 11 11",
"11 1 1 11 ",
"11 1 1 11 1 1 ",
" 1 1 1 ",
" 1 1 ",
" 11 ",
]
INITIAL_MAP = [
" 1 ",
" 1 1 ",
" 11 11 11",
" 1 1 11 11",
"11 1 1 11 ",
"11 1 1 11 1 1 ",
" 1 1 1 ",
" 1 1 ",
" 11 ",
]

map = ConwayMap.new INITIAL_MAP

Expand Down
Loading

0 comments on commit 0f4e586

Please sign in to comment.