Last active
August 29, 2015 14:07
-
-
Save andresperezl/8397ef4ba475cf6528f5 to your computer and use it in GitHub Desktop.
2048 CLI Ruby Version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
module Game2048 | |
class Matrix | |
attr_reader :squares | |
def initialize | |
@prng = Random.new | |
@did_move = false | |
@score = 0 | |
@moves = 0 | |
@squares = [] | |
(1..4).each { @squares << [0,0,0,0] } | |
gen_value | |
gen_value | |
end | |
def move_combine(array) | |
a = Array.new(array) | |
moved, a = move_zero(a) | |
@did_move ||= moved | |
(0..2).each do |i| | |
next if a[i]==0 | |
if (a[i] == a[i+1]) | |
@did_move = true | |
a[i] <<= 1 | |
@score += a[i] | |
a[i+1] = 0 | |
a = move_zero(a)[1] | |
end | |
end | |
a | |
end | |
def move_zero(array) | |
temp = Array.new(array) | |
array.reject!{|v| v == 0 } | |
moved = (array != []) | |
array << 0 while(array.size < 4) | |
moved &&= (temp != array) | |
[moved, array] | |
end | |
def gen_value | |
i = @prng.rand(4) | |
j = @prng.rand(4) | |
while( @squares[i][j]!=0 ) | |
i = @prng.rand(4) | |
j = @prng.rand(4) | |
end | |
@squares[i][j] = (@prng.rand(8)==7 ? 4 : 2) | |
end | |
def move(dir) | |
case dir | |
when "u" | |
@squares = @squares.transpose.map do |col| | |
move_combine(col) | |
end.transpose | |
when "d" | |
@squares = @squares.transpose.map do |col| | |
move_combine(col.reverse).reverse | |
end.transpose | |
when "l" | |
@squares = @squares.map do |row| | |
move_combine(row) | |
end | |
when "r" | |
@squares = @squares.map do |row| | |
move_combine(row.reverse).reverse | |
end | |
end | |
if @did_move | |
gen_value | |
@moves += 1 | |
@did_move = false | |
end | |
end | |
def print_matrix | |
@squares.each do |row| | |
row.each do |v| | |
print "#{c(v)}\t" | |
end | |
print "\n" | |
end | |
puts "Score: #{@score}\t|\tMoves: #{@moves}" | |
end | |
def c(v) | |
t = v.to_s | |
if $HAVE_COLORS | |
t.on_black | |
case v | |
when 0 | |
t = "#".reset | |
when 2 | |
t = t.bold | |
when 4 | |
t = t.cyan | |
when 8 | |
t = t.cyan.bold | |
when 16 | |
t = t.magenta | |
when 32 | |
t = t.magenta.bold | |
when 64 | |
t = t.blue | |
when 128 | |
t = t.blue.bold | |
when 256 | |
t = t.yellow | |
when 512 | |
t = t.bold | |
when 1024 | |
t = t.green | |
when 2048..(2**17) | |
t = t.red.bold | |
end | |
end | |
t | |
end | |
end | |
class Game | |
def initialize | |
@matrix = Matrix.new | |
end | |
def start | |
input = "" | |
while (input != "e") | |
system "clear" ; system "cls" | |
@matrix.print_matrix | |
print "Move <up|down|left|right|exit>: " | |
input = gets.chomp.downcase[0] | |
if(input != "exit") | |
@matrix.move(input) | |
end | |
end | |
end | |
end | |
end | |
begin | |
require 'term/ansicolor' | |
$HAVE_COLORS = true | |
class String | |
include Term::ANSIColor | |
end | |
rescue LoadError | |
$HAVE_COLORS = false | |
end | |
Game2048::Game.new.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will require the library
Term::ANSIColor
if you want color in your game: