Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Second try at 2048.rb

View 2048.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#!/usr/bin/env ruby
require 'pp'
require 'stringio'
 
class GameOver < StandardError; end
 
class Board
attr_accessor :board, :size, :score
 
MOVES = {
'w' => 3,
's' => 1,
'a' => 0,
'd' => 2
}
SEEDS = [2,4]
 
 
 
def initialize(size=4)
@board = Array.new(size) { Array.new(size,0) }
@size = size
@score = 0
 
spawn_new
spawn_new
end
 
def rotate!(n=1)
@board = (0..@size-1).map {|i| @board.map{|row| row[i] }.reverse }
end
 
# Slide the puzzle lefts
# To slide other directions,
def slide
moves = 0
 
# Collapse zeroes
(0...@size).each do|row|
(0...@size-1).each do|col|
while @board[row][col] == 0 && @board[row][col+1..@size-1].max > 0
@board[row][col...@size-1] = @board[row][col+1..@size-1] + [0]
moves += 1
end
end
end
 
# Collapse like cells
(0...@size).each do|row|
(0...@size-1).each do|col|
if @board[row][col] != 0 && @board[row][col] == @board[row][col+1]
@board[row][col..@size-1] = @board[row][col+1..@size-1] + [0]
@board[row][col] *= 2
@score += @board[row][col]
moves += 1
end
end
end
 
return moves
end
 
def game_over?
if @board.flatten.include?(0)
return false
end
 
game_over = true
4.times do
@board.each do|row|
row.each_cons(2) do|nums|
game_over = false if nums.first == nums.last
end
end
 
rotate!
end
 
return game_over
end
 
 
def move(char)
m = MOVES[char]
return 0 if m.nil?
 
m.times { rotate! }
moves = slide
(4-m).times { rotate! }
 
spawn_new if moves != 0
return moves
end
 
 
# Spawning
def empty_cells
@board.map.with_index do|row, rowidx|
row.map.with_index do|num, colidx|
[rowidx, colidx, num]
end
end.flatten(1).find_all{|cell| cell[2] == 0}
end
 
def spawn_new
empty = empty_cells
 
cell = empty.sample(1).flatten
@board[cell[0]][cell[1]] = SEEDS.sample(1).first
end
 
 
# Display
def format_border(cell_size)
"+" + (['-' * cell_size] * @board.size).join('+') + "+"
end
 
def format_row(cell_size, row)
row_strings = @board[row].map do|cell|
if cell == 0
" " * cell_size
else
padding_left = (cell_size - cell.to_s.length) / 2
padding_right = cell_size - padding_left - cell.to_s.length
 
(" " * padding_left) + cell.to_s + (" " * padding_right)
end
end
 
"|" + row_strings.join('|') + "|"
end
 
def to_s
cell_size = @board.flatten.max.to_s.length + 2
str = StringIO.new
str.puts "Score: #{@score}"
 
str.puts format_border(cell_size)
 
(0...@size).each do|row|
str.puts format_row(cell_size, row)
str.puts format_border(cell_size)
end
 
str.string
end
 
end
 
begin
b = Board.new
 
#b.board = [
# [1,2,3,4],
# [5,6,7,8],
# [9,10,11,12],
# [13,14,15,0]
#]
 
loop do
puts b
raise GameOver if b.game_over?
 
print "? "
b.move gets.chomp
end
rescue GameOver
puts "Game over man, game over!"
puts "Your score was: #{b.score}"
puts
puts
retry
end

How can I run this in Interactive Ruby?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.