Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Created May 16, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianseeley/394e7bfd2a3acd579399 to your computer and use it in GitHub Desktop.
Save adrianseeley/394e7bfd2a3acd579399 to your computer and use it in GitHub Desktop.
Reforce 1, mario laps, exponentially slower to cover new ground
function tablelength(T)
local count = 0
for _ in pairs(T) do
count = count + 1
end
return count
end
emu.softreset();
emu.speedmode("maximum");
brain = {};
while (true) do
emu.frameadvance();
-- calculate frame hash
checksum = 0;
for x = 0, 255, 10 do
for y = 0, 239, 10 do
r, g, b, palette = emu.getscreenpixel(x, y, false);
checksum = checksum + palette
end
end
if brain[checksum] == nil then
brain[checksum] = {["start"] = 0, ["l"] = 0, ["lj"] = 0, ["j"] = 0, ["rj"] = 0, ["r"] = 0};
end
-- find least explored action for frame hash
action = nil;
tally = -1;
for key in pairs(brain[checksum]) do
val = brain[checksum][key]
if action == nil or val < tally then
action = key;
tally = val;
end
end
-- build input for frame hash
jp = joypad.get(1);
if action == "start" then
jp["start"] = true;
jp["left"] = false;
jp["right"] = false;
jp["B"] = false;
jp["A"] = false;
elseif action == "l" then
jp["start"] = false;
jp["left"] = true;
jp["right"] = false;
jp["B"] = true;
jp["A"] = false;
elseif action == "r" then
jp["start"] = false;
jp["left"] = false;
jp["right"] = true;
jp["B"] = true;
jp["A"] = false;
elseif action == "lj" then
jp["start"] = false;
jp["left"] = true;
jp["right"] = false;
jp["B"] = true;
jp["A"] = true;
elseif action == "rj" then
jp["start"] = false;
jp["left"] = false;
jp["right"] = true;
jp["B"] = true;
jp["A"] = true;
elseif action == "j" then
jp["start"] = false;
jp["left"] = false;
jp["right"] = false;
jp["B"] = true;
jp["A"] = true;
end
joypad.set(1, jp);
-- feedback brain
brain[checksum][action] = brain[checksum][action] + 1
fc = emu.framecount();
if fc % 10000 == 0 then
print(fc .. ": " .. tablelength(brain))
end
--gui.text(10, 10, tablelength(brain), "white", "black");
--gui.text(10, 20, checksum .. ": " .. brain[checksum]["start"] .. ", " .. brain[checksum]["l"] .. ", " .. brain[checksum]["r"] .. ", " .. brain[checksum]["lj"] .. ", " .. brain[checksum]["rj"] .. ", " .. brain[checksum]["j"], "white", "black");
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment