ทำอย่างไรให้ฟังก์ชันนี้ฉลาดขึ้นกว่าที่เป็นอยู่ในปัจจุบัน
int SnakeDecision::snakeMove(SnakeResource *resource){
int i;
int mapx[4]={0,0,-1,1};
int mapy[4]={-1,1,0,0};
word localData = resource->tempData; // ค่าในตัวแปรนี้สามารถแบ่งไปใช้ และนำไปเก็บไว้เพื่อใช้ตาต่อไปได้ครับ
// ตัวอย่างการใช้งานตัวแปร localData
int aa,bb;
aa = localData &0xff;
bb = localData >> 8;
aa++;
bb+=3;
localData = (aa&0xff)|((bb&0xff)<<8);
// จบตัวอย่างการใช้งานตัวแปร localData
int direction;
//ถ้ามีไอเท็มใกล้ตัว กินซะ
for(i=0;i<4;i++)
{
if(resource->getLocalMap(mapx[i],mapy[i])==I_SHORT) break; //หดหางตัวเอง
if(resource->getLocalMap(mapx[i],mapy[i])==I_SHORT_O) break; //หดหางคนอื่น
if(resource->getLocalMap(mapx[i],mapy[i])==I_DELAY) break; //ทำให้ตัวเองช้า
if(resource->getLocalMap(mapx[i],mapy[i])==I_DELAY_O) break; //ทำให้ตัวเองเร็ว
}
// เลือกที่จะกินไอเท็ม หากพบไอเท็ม
direction = i;
if(i==4){ // ในกรณีที่ไม่พบไอเท็ม ตัดสินใจเดินต่อตามปกติ
srand((int)time(NULL));
direction=(rand()+(int)time(NULL))%4;
if(resource->getLocalMap(0,-1)==resource->getOwnerColorCode()) direction = DOWN;
if(resource->getLocalMap(0,1) ==resource->getOwnerColorCode()) direction = UP;
if(resource->getLocalMap(-1,0)==resource->getOwnerColorCode()) direction = RIGHT;
if(resource->getLocalMap(1,0) ==resource->getOwnerColorCode()) direction = LEFT;
if((rand()+(int)time(NULL))%2){
if((direction==DOWN) && (resource->getLocalMap(0,1) !=BACKGROUND)) direction=LEFT;
if((direction==LEFT) && (resource->getLocalMap(-1,0)!=BACKGROUND)) direction=UP;
if((direction==UP) && (resource->getLocalMap(0,-1)!=BACKGROUND)) direction=RIGHT;
if((direction==RIGHT)&& (resource->getLocalMap(1,0) !=BACKGROUND)) direction=DOWN;
if((direction==DOWN) && (resource->getLocalMap(0,1) !=BACKGROUND)) direction=LEFT;
if((direction==LEFT) && (resource->getLocalMap(-1,0)!=BACKGROUND)) direction=UP;
if((direction==UP) && (resource->getLocalMap(0,-1)!=BACKGROUND)) direction=RIGHT;
}else{
if((direction==DOWN) && (resource->getLocalMap(0,1) !=BACKGROUND)) direction=RIGHT;
if((direction==RIGHT)&& (resource->getLocalMap(1,0) !=BACKGROUND)) direction=UP;
if((direction==UP) && (resource->getLocalMap(0,-1)!=BACKGROUND)) direction=LEFT;
if((direction==LEFT) && (resource->getLocalMap(-1,0)!=BACKGROUND)) direction=DOWN;
if((direction==DOWN) && (resource->getLocalMap(0,1) !=BACKGROUND)) direction=RIGHT;
if((direction==RIGHT)&& (resource->getLocalMap(1,0) !=BACKGROUND)) direction=UP;
if((direction==UP) && (resource->getLocalMap(0,-1)!=BACKGROUND)) direction=LEFT;
}
}
return (direction<<16)|localData;
}