User File #12274234568774031

Upload All User Files

#12274234568774031 - Q-Bert Bot (fixed messy code)

Q-Bert_Bot_alt.lua
Game: Q*bert ( NES, see all files )
905 downloads
Uploaded 1/26/2014 6:07 PM by TASeditor (see all 188)
This still works the same as the previous one, but I made the code look prettier and shortened it.
The bot is still not in its final form.

NeededColor = 148;
BlocksColor = {};
LivesMem = Lives;
Pos = {X = 0, Y = 0};
i = 0;
i_3 = 0;
n = 0;
FastestAttempt = 3000;
Attempts = 1;

while (true) do

BlocksColor[0 .. 0] = memory.read_u8(0x15);
BlocksColor[0 .. 1] = memory.read_u8(0x34);
BlocksColor[1 .. 0] = memory.read_u8(0x36);
BlocksColor[0 .. 2] = memory.read_u8(0x53);
BlocksColor[1 .. 1] = memory.read_u8(0x55);
BlocksColor[2 .. 0] = memory.read_u8(0x57);
BlocksColor[0 .. 3] = memory.read_u8(0x62);
BlocksColor[1 .. 2] = memory.read_u8(0x64);
BlocksColor[2 .. 1] = memory.read_u8(0x66);
BlocksColor[3 .. 0] = memory.read_u8(0x68);
BlocksColor[0 .. 4] = memory.read_u8(0x01);
BlocksColor[1 .. 3] = memory.read_u8(0x03);
BlocksColor[2 .. 2] = memory.read_u8(0x05);
BlocksColor[3 .. 1] = memory.read_u8(0x07);
BlocksColor[4 .. 0] = memory.read_u8(0x09);
BlocksColor[0 .. 5] = memory.read_u8(0x20);
BlocksColor[1 .. 4] = memory.read_u8(0x22);
BlocksColor[2 .. 3] = memory.read_u8(0x24);
BlocksColor[3 .. 2] = memory.read_u8(0x26);
BlocksColor[4 .. 1] = memory.read_u8(0x28);
BlocksColor[5 .. 0] = memory.read_u8(0x2A);

Lives = memory.read_u8(0x08);

XPos = memory.read_u8(0x2B);
YPos = memory.read_u8(0x43);

Frame = emu.framecount();

-- Displays the color of each block
gui.text(77+62,25+56,""..BlocksColor[0 .. 0]);
gui.text(65+50,53+56,""..BlocksColor[0 .. 1]);
gui.text(93+78,53+56,""..BlocksColor[1 .. 0]);   
gui.text(53+38,81+58,""..BlocksColor[0 .. 2]);      
gui.text(77+62,81+58,""..BlocksColor[1 .. 1]);           
gui.text(105+90,81+58,""..BlocksColor[2 .. 0]);     
gui.text(41+26,109+60,""..BlocksColor[0 .. 3]);       
gui.text(65+50,109+60,""..BlocksColor[1 .. 2]);      
gui.text(93+78,109+60,""..BlocksColor[2 .. 1]);    
gui.text(117+102,109+60,""..BlocksColor[3 .. 0]);       
gui.text(29+14,137+62,""..BlocksColor[0 .. 4]);        
gui.text(65+26,137+62,""..BlocksColor[1 .. 3]);                 
gui.text(77+62,137+62,""..BlocksColor[2 .. 2]);        
gui.text(105+90,137+62,""..BlocksColor[3 .. 1]);       
gui.text(129+114,137+62,""..BlocksColor[4 .. 0]);        
gui.text(16+4,165+63,""..BlocksColor[0 .. 5]);          
gui.text(41+26,165+63,""..BlocksColor[1 .. 4]);         
gui.text(65+50,165+63,""..BlocksColor[2 .. 3]);       
gui.text(93+78,165+63,""..BlocksColor[3 .. 2]);     
gui.text(117+102,165+63,""..BlocksColor[4 .. 1]);          
gui.text(141+128,165+63,""..BlocksColor[5 .. 0]);    

-- Defines a positon for each block, 
-- The game is rotated 45� counterclockwise

if YPos == 25
then if XPos == 77
	 then X = 0; Y = 0;
	 end
elseif YPos == 53
	then if XPos == 65
		 then X = 0; Y = 1;
		 elseif XPos == 93
			then X = 1; Y = 0;
			end
	elseif YPos == 81
		then if XPos == 53
			 then X = 0; Y = 2;
			 elseif XPos == 77
				 then X = 1; Y = 1;
				 elseif XPos == 105
					 then X = 2; Y = 0;
					 end
		elseif YPos == 109
			then if XPos == 41
				 then X = 0; Y = 3;
				 elseif XPos == 65
					 then X = 1; Y = 2;
					 elseif XPos == 93
						 then X = 2; Y = 1;
						 elseif XPos == 117
							 then X = 3; Y = 0;
							 end
			elseif YPos == 137
				then if XPos == 29
					 then X = 0; Y = 4;
					 elseif XPos == 53
						 then X = 1; Y = 3;
						 elseif XPos == 77
							 then X = 2; Y = 2;
							 elseif XPos == 105
								 then X = 3; Y = 1;
								 elseif XPos == 129
									 then X = 4; Y = 0;
									 end
				elseif YPos == 165
					then if XPos == 16
						 then X = 0; Y = 5;
						 elseif XPos == 41
							 then X = 1; Y = 4;
							 elseif XPos == 65
								 then X = 2; Y = 3;
								 elseif XPos == 93
									 then X = 3; Y = 2;
									 elseif XPos == 117
										 then X = 4; Y = 1;
										 elseif XPos == 141
											 then X = 5; Y = 0;
											 end
end;

i = math.random(0,3);
i_3 = math.random(0,2);

Pos = {X, Y};

-- Decides to which block to jump


-- Movement for the block at the top

Top=function()

	if i <= 1
	then if BlocksColor[0 .. 1] ~= NeededColor
		then joypad.set({Down=true},1)
		elseif BlocksColor[1 .. 0] ~= NeededColor 
			then joypad.set({Right=true},1)
			else joypad.set({Down=true},1)
		end 		  
	elseif i >= 2 
		then if BlocksColor[1 .. 0] ~= NeededColor
			then joypad.set({Right=true},1)
			elseif BlocksColor[0 .. 1] ~= NeededColor 
				then joypad.set({Down=true},1)
				else joypad.set({Right=true},1)
			end
	end	 

end;


-- Movement for the bottom row, except the both at the corners

Bottom=function()

	if i <= 1
	then if BlocksColor[X-1 .. Y] ~= NeededColor
		then joypad.set({Left=true},1)
		elseif BlocksColor[X .. Y-1] ~= NeededColor 
			then joypad.set({Up=true},1)
			else joypad.set({Left=true},1)
		end 		  
	elseif i >= 2 
		then if BlocksColor[X .. Y-1] ~= NeededColor
			then joypad.set({Up=true},1)
			elseif BlocksColor[X-1 .. Y] ~= NeededColor 
				then joypad.set({Left=true},1)
				else joypad.set({Up=true},1)
			end
	end
	 
end;


-- Movement for the right row, except the corner

Right=function()

	if i_3 == 0
	then if BlocksColor[X-1 .. 0] ~= NeededColor
		then joypad.set({Left=true},1)
		elseif BlocksColor[X .. Y+1] ~= NeededColor
			then joypad.set({Down=true},1)
			elseif BlocksColor[X+1 .. 0] ~= NeededColor
				then joypad.set({Right=true},1)
				else joypad.set({Left=true},1)
		end
	elseif i_3 == 1  
		then if BlocksColor[X .. Y+1] ~= NeededColor
			then joypad.set({Down=true},1)
			elseif BlocksColor[X-1 .. 0] ~= NeededColor
				then joypad.set({Left=true},1)
				elseif BlocksColor[X+1 .. 0] ~= NeededColor
					then joypad.set({Right=true},1)
					else joypad.set({Down=true},1)
			end
		elseif i_3 == 2
			then if BlocksColor[X+1 .. 0] ~= NeededColor
				then joypad.set({Right=true},1)
				elseif BlocksColor[X-1 .. 0] ~= NeededColor
					then joypad.set({Left=true},1)
					elseif BlocksColor[X .. Y+1] ~= NeededColor
						then joypad.set({Down=true},1)
						else joypad.set({Right=true},1)
				end
	end	 

end;


-- Movement for the left row, except the corner

Left=function()

	if i_3 == 0
	then if BlocksColor[0 .. Y-1] ~= NeededColor
		then joypad.set({Up=true},1)
		elseif BlocksColor[0 .. Y+1] ~= NeededColor
			then joypad.set({Down=true},1)
			elseif BlocksColor[X+1 .. Y] ~= NeededColor
				then joypad.set({Right=true},1)
				else joypad.set({Up=true},1)
		end
	elseif i_3 == 1  
		then if BlocksColor[0 .. Y+1] ~= NeededColor
			then joypad.set({Down=true},1)
			elseif BlocksColor[0 .. Y-1] ~= NeededColor
				then joypad.set({Up=true},1)
				elseif BlocksColor[X+1 .. Y] ~= NeededColor
					then joypad.set({Right=true},1)
					else joypad.set({Down=true},1)
			end
		elseif i_3 == 2
			then if BlocksColor[X+1 .. Y] ~= NeededColor
				then joypad.set({Right=true},1)
				elseif BlocksColor[0 .. Y+1] ~= NeededColor
					then joypad.set({Down=true},1)
					elseif BlocksColor[0 .. Y-1] ~= NeededColor
						then joypad.set({Up=true},1)
						else joypad.set({Right=true},1)
				end
	end

end;


-- Movement for the blocks in the middle, those with four adjacent blocks

Middle=function()

	if i == 0
	then if BlocksColor[X-1 .. Y] ~= NeededColor
		then joypad.set({Left=true},1)
		elseif BlocksColor[X .. Y-1] ~= NeededColor
			then joypad.set({Up=true},1)
			elseif BlocksColor[X .. Y+1] ~= NeededColor
				then joypad.set({Down=true},1)
				elseif BlocksColor[X+1 .. Y] ~= NeededColor
					then joypad.set({Right=true},1)
					else joypad.set({Left=true},1)
		end
	elseif i == 1  
		then if BlocksColor[X .. Y-1] ~= NeededColor
			then joypad.set({Up=true},1)
			elseif BlocksColor[X-1 .. Y] ~= NeededColor
				then joypad.set({Left=true},1)
				elseif BlocksColor[X .. Y+1] ~= NeededColor
					then joypad.set({Down=true},1)
					elseif BlocksColor[X+1 .. Y] ~= NeededColor
						then joypad.set({Right=true},1)
						else joypad.set({Up=true},1)
			end
		elseif i == 2
			then if BlocksColor[X .. Y+1] ~= NeededColor
				then joypad.set({Down=true},1)
				elseif BlocksColor[X-1 .. Y] ~= NeededColor
					then joypad.set({Left=true},1)
					elseif BlocksColor[X .. Y-1] ~= NeededColor
						then joypad.set({Up=true},1)
						elseif BlocksColor[X+1 .. Y] ~= NeededColor
							then joypad.set({Right=true},1)
							else joypad.set({Down=true},1)
				end
			elseif i == 3
			then if BlocksColor[X+1 .. Y] ~= NeededColor
				then joypad.set({Right=true},1)
				elseif BlocksColor[X-1 .. Y] ~= NeededColor
					then joypad.set({Left=true},1)
					elseif BlocksColor[X .. Y-1] ~= NeededColor
						then joypad.set({Up=true},1)
						elseif BlocksColor[X .. Y+1] ~= NeededColor
							then joypad.set({Down=true},1)
							else joypad.set({Right=true},1)
				end
	end

end;


-- Movement for the corners

Corner=function()

	if Pos[1] == 5
	then joypad.set({Left=true},1)
	end;
	
	if Pos[2] == 5
	then joypad.set({Up=true},1)
	end;

end;


if Pos[1] == 0 and Pos[2] == 0
then Top();
end;

if Pos[1] + Pos[2] == 5 and Pos[1] > 0 and Pos[2] > 0
then Bottom();
end;

if Pos[1] > 0 and Pos[1] < 5 and Pos[2] == 0
then Right();
end;

if Pos[1] == 0 and Pos[2] > 0 and Pos[2] < 5
then Left();
end;

if Pos[1] > 0 and Pos[2] > 0 and Pos[1] + Pos[2] < 5
then Middle();
end;

if Pos[1] == 5 and Pos[2] == 0 or Pos[1] == 0 and Pos[2] == 5
then Corner();
end;


-- Checks if all blocks have the needed color

if n == 0
then if BlocksColor[0 .. 0] == NeededColor and BlocksColor[0 .. 1] == NeededColor and BlocksColor[1 .. 0] == NeededColor and BlocksColor[0 .. 2] == NeededColor and BlocksColor[1 .. 1] == NeededColor and BlocksColor[2 .. 0] == NeededColor and BlocksColor[0 .. 3] == NeededColor and BlocksColor[1 .. 2] == NeededColor and BlocksColor[2 .. 1] == NeededColor and BlocksColor[3 .. 0] == NeededColor and BlocksColor[0 .. 4] == NeededColor and BlocksColor[1 .. 3] == NeededColor and BlocksColor[2 .. 2] == NeededColor and BlocksColor[3 .. 1] == NeededColor and BlocksColor[4 .. 0] == NeededColor and BlocksColor[0 .. 5] == NeededColor and BlocksColor[1 .. 4] == NeededColor and BlocksColor[2 .. 3] == NeededColor and BlocksColor[3 .. 2] == NeededColor and BlocksColor[4 .. 1] == NeededColor and BlocksColor[5 .. 0] == NeededColor 																																																					
then FastestAttempt = emu.framecount();
     savestate.saveslot(9);
	 savestate.saveslot(7);
	 n = 1;
     end																		
else n = 0;
end;


-- Another try if the attempt is slower than the previous attempt

if FastestAttempt < Frame
then savestate.loadslot(0);
	 Attempts = Attempts + 1;
end;


-- Another try if the player loses a life

if Lives < LivesMem
then savestate.loadslot(0);
	 Attempts = Attempts + 1;
end;

LivesMem = Lives;


-- Displays some information 

gui.text(160,10,"Position: X = "..X..", Y = "..Y);
gui.text(160,20,"Lives: "..Lives);
gui.text(160,30,"Bot RNG1: "..i.." Bot RNG2: "..i_3);
gui.text(160,40,"Fastest Attempt: "..FastestAttempt);
gui.text(160,50,"Attempts: "..Attempts);
gui.text(160,60,"Needed Color: "..NeededColor);

emu.frameadvance();

end;