function setboard()
--row 1
mainmemory.writebyte(0x1F35,1)
mainmemory.writebyte(0x1F36,0)
mainmemory.writebyte(0x1F37,2)
mainmemory.writebyte(0x1F38,3)
mainmemory.writebyte(0x1F39,2)
--row 2
mainmemory.writebyte(0x1F43,1)
mainmemory.writebyte(0x1F44,2)
mainmemory.writebyte(0x1F45,3)
mainmemory.writebyte(0x1F46,3)
mainmemory.writebyte(0x1F47,3)
--row 3
mainmemory.writebyte(0x1F51,0)
mainmemory.writebyte(0x1F52,1)
mainmemory.writebyte(0x1F53,3)
mainmemory.writebyte(0x1F54,3)
mainmemory.writebyte(0x1F55,1)
--row 4
mainmemory.writebyte(0x1F5F,1)
mainmemory.writebyte(0x1F60,1)
mainmemory.writebyte(0x1F61,0)
mainmemory.writebyte(0x1F62,2)
mainmemory.writebyte(0x1F63,2)
--row 5
mainmemory.writebyte(0x1F6D,1)
mainmemory.writebyte(0x1F6E,1)
mainmemory.writebyte(0x1F6F,2)
mainmemory.writebyte(0x1F70,3)
mainmemory.writebyte(0x1F71,3)
--row 6
mainmemory.writebyte(0x1F7B,2)
mainmemory.writebyte(0x1F7C,0)
mainmemory.writebyte(0x1F7D,1)
mainmemory.writebyte(0x1F7E,2)
mainmemory.writebyte(0x1F7F,2)
--row 7
mainmemory.writebyte(0x1F89,3)
mainmemory.writebyte(0x1F8A,2)
mainmemory.writebyte(0x1F8B,2)
mainmemory.writebyte(0x1F8C,2)
mainmemory.writebyte(0x1F8D,2)
--row 8
mainmemory.writebyte(0x1F97,3)
mainmemory.writebyte(0x1F98,1)
mainmemory.writebyte(0x1F99,2)
mainmemory.writebyte(0x1F9A,2)
mainmemory.writebyte(0x1F9B,1)
--row 9
mainmemory.writebyte(0x1FA5,3)
mainmemory.writebyte(0x1FA6,3)
mainmemory.writebyte(0x1FA7,1)
mainmemory.writebyte(0x1FA8,0)
mainmemory.writebyte(0x1FA9,1)
--row 10
mainmemory.writebyte(0x1FB3,1)
mainmemory.writebyte(0x1FB4,2)
mainmemory.writebyte(0x1FB5,2)
mainmemory.writebyte(0x1FB6,1)
mainmemory.writebyte(0x1FB7,1)
end
function setborder()
--row 1
for i = 0x1F3A, 0x1F42 do
mainmemory.writebyte(i,4)
end
--row 2
for i = 0x1F48, 0x1F50 do
mainmemory.writebyte(i,4)
end
--row 3
for i = 0x1F56, 0x1F5E do
mainmemory.writebyte(i,4)
end
--row 4
for i = 0x1F64, 0x1F6C do
mainmemory.writebyte(i,4)
end
--row 5
for i = 0x1F72, 0x1F7A do
mainmemory.writebyte(i,4)
end
--row 6
for i = 0x1F80, 0x1F88 do
mainmemory.writebyte(i,4)
end
--row 7
for i = 0x1F8E, 0x1F96 do
mainmemory.writebyte(i,4)
end
--row 8
for i = 0x1F9C, 0x1FA4 do
mainmemory.writebyte(i,4)
end
--row 9
for i = 0x1FAA, 0x1FB2 do
mainmemory.writebyte(i,4)
end
--row 10 + rest
for i = 0x1FB8, 0x1FF8 do
mainmemory.writebyte(i,4)
end
end
function nullfraadv()
joypad.set({})
emu.frameadvance()
end
local pixelAddresses = {
0x1F35, 0x1F36, 0x1F37, 0x1F38, 0x1F39,
0x1F43, 0x1F44, 0x1F45, 0x1F46, 0x1F47,
0x1F51, 0x1F52, 0x1F53, 0x1F54, 0x1F55,
0x1F5F, 0x1F60, 0x1F61, 0x1F62, 0x1F63,
0x1F6D, 0x1F6E, 0x1F6F, 0x1F70, 0x1F71,
0x1F7B, 0x1F7C, 0x1F7D, 0x1F7E, 0x1F7F,
0x1F89, 0x1F8A, 0x1F8B, 0x1F8C, 0x1F8D,
0x1F97, 0x1F98, 0x1F99, 0x1F9A, 0x1F9B,
0x1FA5, 0x1FA6, 0x1FA7, 0x1FA8, 0x1FA9,
0x1FB3, 0x1FB4, 0x1FB5, 0x1FB6, 0x1FB7
}
local startaddress = 0x1F35
local endaddress = 0x1FF8
local total = 0
while mainmemory.readbyte(0x1992) == 0 do
setboard()
setborder()
-- selecting a random colour on go 1
local random_count = math.random(1,2)
if random_count == 1 then
joypad.set({A=true})
emu.frameadvance()
nullfraadv()
else
for i = 1, 2 do
joypad.set({Down=true})
emu.frameadvance()
joypad.set({A=true})
emu.frameadvance()
nullfraadv()
end
end
while mainmemory.readbyte(0x1FFD) > 16 do
savestate.saveslot(2)
local random_count = math.random(1, 5) --for slecting a random colour
local prev_color = mainmemory.readbyte(0x1F35)
local prev_coloramt = 0
-- generate a random colour
for i = 1, random_count do
joypad.set({Up=true})
emu.frameadvance()
joypad.set({})
emu.frameadvance()
end
for i = 1, 5 do
emu.frameadvance()
end
-- get amount of pixels for colour that is going to be played
local new_color = mainmemory.readbyte(0x1F2C)
local new_colorprevamt = 0 -- amount of pixels of colour before being played
for i = startaddress, endaddress do
if mainmemory.readbyte(i) == mainmemory.readbyte(0x1F2C) then
new_colorprevamt = new_colorprevamt + 1
end
end
-- set colour
if mainmemory.readbyte(0x1F2C) ~= 4 then
joypad.set({A=true})
emu.frameadvance()
end
-- skip potential lag frames
for i = 1, 10 do
joypad.set({})
emu.frameadvance()
end
savestate.saveslot(3)
joypad.set({Up=true})
emu.frameadvance()
joypad.set({A=true})
emu.frameadvance()
joypad.set({})
for i = 1, 5 do
emu.frameadvance()
end
local new_coloramt = 0
-- get amount of pixels of selected colour
for i = startaddress, endaddress do
if mainmemory.readbyte(i) == new_color then
new_coloramt = new_coloramt + 1
end
end
print(new_coloramt,new_colorprevamt)
if new_coloramt < new_colorprevamt then
savestate.loadslot(3)
elseif new_coloramt == new_colorprevamt then
savestate.loadslot(2)
print("Retrying")
end
local allValuesEqual = true -- Assume all values are equal initially
for i = 1, 49 do
local address1 = pixelAddresses[i]
local address2 = pixelAddresses[i + 1]
local value1 = mainmemory.readbyte(address1)
local value2 = mainmemory.readbyte(address2)
if value1 ~= value2 then
allValuesEqual = false
break -- Exit the loop early if any pair of consecutive values are not equal
end
end
if allValuesEqual then
while mainmemory.readbyte(0x1F2C) ~= 4 do
joypad.set({Down=true})
emu.frameadvance()
nullfraadv()
end
joypad.set({A=true})
emu.frameadvance()
end
end
savestate.loadslot(1)
end
print("Solution found")
emu.pause()