Skip to content

Instantly share code, notes, and snippets.

@hjroh0315
Last active August 9, 2023 00:12
Show Gist options
  • Save hjroh0315/c254b0c5f844e51a79085e753443b469 to your computer and use it in GitHub Desktop.
Save hjroh0315/c254b0c5f844e51a79085e753443b469 to your computer and use it in GitHub Desktop.
baseline code for stupid 2048 ai
class Array
def rot
self.transpose.map(&:reverse)
end
end
def left a
ar=a.dup
tmp=(1..4).map{[]}
merged=(1..4).map{[]}
(0..3).each do|i|
(0..3).each do|j|
tmp[i]<<ar[i][j]
merged[i]<<0
if tmp[i].size>1&&tmp[i][-1]==tmp[i][-2]&&merged[i][-2]==0
ad=tmp[i][-2]*2
tmp[i].pop;tmp[i].pop;
merged[i].pop
merged[i].pop
tmp[i]<<ad;merged[i]<<1
end
end
end
(0..3).each{|i|tmp[i][4]=0;tmp[i].pop;tmp[i].map!{|i|i||0}}
tmp
end
def down a
left(a.rot).rot.rot.rot
end
def right a
left(a.rot.rot).rot.rot
end
def up a
left(a.rot.rot.rot).rot
end
def perf c,a
if c=='u'
up a
elsif c=='d'
down a
elsif c=='l'
left a
else
right a
end
end
def full c
if c=='u'
"UP"
elsif c=='d'
"DOWN"
elsif c=='l'
"LEFT"
else
"RIGHT"
end
end
arr=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
$>.sync=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment