Esempio n. 1
0
int AI::betterMemory()
{
    int position = 0;
    int nextPos = 0;
    int lastPos = 0;
    bool ok = false;
    Direction lastDir = AI::UP;
    bool keepPosAndDir = false;

    do
    {
        // Check if an active ship is available
        if(shipActive())
        {
            if(!keepPosAndDir)
            {
                // Get information about the last turn
                lastPos = _alreadyTried.last();
                lastDir = lastDirection();
                keepPosAndDir = false;
            }

            // Get next position
            if(lastHit())
            {
                nextPos = positionInDirection(lastDir, lastPos);
            }
            else
            {
                if(_currentHitsOnShip >= 2)
                {
                    if(!keepPosAndDir)
                        lastDir = oppositeDirection(lastDir);
                    nextPos = positionInDirection(lastDir, _shipFirstHit);
                }
                else
                {
                    if(!keepPosAndDir)
                        lastDir = nextDirection(lastDir);
                    nextPos = positionInDirection(lastDir, _shipFirstHit);
                }
            }

            // If the position is not valid, try an other one
            if(nextPos < 0)
            {
                if(_currentHitsOnShip >= 2)
                    lastDir = oppositeDirection(lastDir);
                else
                    lastDir = nextDirection(lastDir);

                lastPos = _shipFirstHit;
                keepPosAndDir = true;
                continue;
            }

            // If position was already tried, try an other one
            if(_alreadyTried.contains(nextPos))
            {
                lastDir = nextDirection(lastDir);
                lastPos = _shipFirstHit;
                keepPosAndDir = true;
                continue;
            }
            else
            {
                ok = true;
            }

            // Set the next position
            position = nextPos;
        }
        else
        {
            // If no active ship is available, pick random position
            position = simpleMemory();
            _shipSunken = false;
            ok = true;
        }

    }while(!ok);

    _alreadyTried.append(position);
    setLastDirection(lastDir);

    return position;
}
Esempio n. 2
0
int main(void)
{
	unsigned char temp_sqr,temp_dir,temp_diff,temp;
	unsigned int i;
	unsigned char channel=150;
	hardwareInit();
	//FOLD_ARMS;
	/*__delay_ms(50);
	do{
		if(key_UP)
		{
			channel+=10;
			LED_ON;
			__delay_ms(20);
			LED_OFF;
		}
		if(key_DN)
		{
			channel-=10;
			LED_ON;
			__delay_ms(20);
			LED_OFF;
		}
		__delay_ms(300);
		readButtons();
	}	while(!key_GO);
	LED_ON;
	__delay_ms(200);
	LED_OFF;
	CC2500_init();
	CC2500_write_register(CHANNR,channel);
	CC2500_idle_mode();
	CC2500_receive_mode();
	do{
		if(key_UP)
		{
			speed++;
			if(speed>4)
				speed = 4;
			LED_ON;
			__delay_ms(20);
			LED_OFF;
		}
		if(key_DN)
		{
			speed--;
			if(speed<0)
				speed = 0;
			LED_ON;
			__delay_ms(20);
			LED_OFF;
		}
		__delay_ms(300);
		readButtons();
	}while(!key_GO);*/
	LED_ON;
	__delay_ms(150);
	LED_OFF;
	__delay_ms(150);
	LED_ON;
	__delay_ms(150);
	LED_OFF;
	
	#ifdef MACHINE_A
		statusInitA();
	#endif
	#ifdef MACHINE_B
		statusInitB();
	#endif
	assamble_packet();
	CC2500_idle_mode();
	CC2500_clear_rx_fifo();
	CC2500_clear_tx_fifo();
	CC2500_receive_mode();
	__delay_ms(100);
	i = CC2500_read_status_register( RXBYTES);
	i&=0xFF;
	sprintf(outBuf,"\r\n%u\r\n",i);
	putsUART(outBuf);
	if(i)
	{
		CC2500_idle_mode();
		CC2500_clear_rx_fifo();
		CC2500_clear_tx_fifo();
		CC2500_receive_mode();
		while(GDO0==0);
		while(GDO0==1);
		SYSTIM_TMR = 9581;
		__delay_ms(3);
		commCount = 0;
		startSystemTimer();
		noCommCount=0;
		commEnabled = TRUE;
		if(waitDataUpdate())
			my.obzEntrCnt = fellow.obzEntrCnt;
		/*LED_ON;
		__delay_ms(2000);*/
		
	}
	else
	{
		startSystemTimer();
		noCommCount=0;
		commEnabled = TRUE;
	}
	#ifdef MACHINE_A
	while(!key_GO){
		if(noCommCount>100)
		{
			commEnabled = FALSE;
			waitToTick();
			CC2500_init();
			commEnabled = TRUE;
			__delay_ms(40);
			//noCommCount = 0;
		}
	}
	#endif

	if(displayEnabled)
	{
		switch(resetSource())
		{
			case POWER_ON_RESET:
				sprintf(outBuf,"\r\nBattry Voltage : %u",batteryVoltage);
				putsUART(outBuf);
				break;
			
			case EXTERNAL_RESET:
				sprintf(outBuf,"\r\nUser Reset...");
				putsUART(outBuf);
				break;
			
			default:
				sprintf(outBuf,"\r\nUnknown Reset");
				putsUART(outBuf);
				break;
		}
	}
	/*while(TRUE)
	{
		printSensors();
		__delay_ms(500);
	}*/
	
	gridInit();
	#ifdef MACHINE_B
	while(TRUE)
	{
		if(noCommCount>100)
		{
			commEnabled = FALSE;
			waitToTick();
			CC2500_init();
			commEnabled = TRUE;
			__delay_ms(40);
			//noCommCount = 0;
		}
		if(dataUpdated)
		{
			printStatus(&fellow);
			/*rx_buff_temp[0] = rx_buff0;
			rx_buff_temp[1] = rx_buff1;
			rx_buff_temp[2] = rx_buff2;
			rx_buff_temp[3] = rx_buff3;
			rx_buff_temp[4] = rx_buff4;
			rx_buff_temp[5] = rx_buff5;
			sprintf(outBuf,"\r\n\r\n%u,%u,%u,%u,%u,%u",
												rx_buff_temp[0],
												rx_buff_temp[1],
												rx_buff_temp[2],
												rx_buff_temp[3],
												rx_buff_temp[4],
												rx_buff_temp[5]);
			putsUART(outBuf);*/
			dataUpdated = FALSE;
		}		
	}
	//while(!key_GO);
	while(fellow.heading==10&&!key_GO);
	//while(fellow.heading==10);
	#endif
	
	while(TRUE)
	{
		if(noCommCount>100)
		{
			commEnabled = FALSE;
			waitToTick();
			CC2500_init();
			commEnabled = TRUE;
			__delay_ms(40);
			//noCommCount = 0;
		}
		switch(mcState)
		{
			case MC_START:
			{
				cubeSensorsOn();
				waitToTick();
				waitToTick();
				cubeSensorsOff();
				temp_sqr = nextSquare(my.location,my.heading&0x07);
				grid[temp_sqr] |= VISITED;
				if(frontCube)
				{
					myCube[++cubeInd] = temp_sqr;
					mcState=MC_DELIVER;
				}
				else
					mcState=MC_SCAN;
				break;
			}
			case MC_SCAN:
			{
				temp_sqr = scanMap[my.smInd];
				if(my.location==temp_sqr)
				{
					my.smInd++;
					break;
				}
				my.goalSquare = temp_sqr;
				mcNextState = MC_SCAN;
				mcState = MC_GET_M_THR;
				/*temp_dir = nextDirection(my.location,temp_sqr);
				temp_diff = temp_dir - (my.heading&0x07);
					
				if(prState==PR_STR_ACC||prState==PR_STR_DEC)
				{
					if(temp_diff)
						break;
					waitDataUpdate();
					if(commonSquare(temp_sqr))
						break;
					my.heading &= 0x07;
					finalPos += COUNTS_PER_CELL*256;
					if(prState==PR_STR_DEC)
						prState = PR_STR_ACC;
					while(relativeDistance>distance[ORTHO_SENS_DIST]*2);
				}
				else if(prState==PR_FINISHED)
				{
					if(temp_diff)
					{
						commandList[cmlInd++] = getTurnCmd(temp_diff);
						my.heading = temp_dir | NODIR;
					}
					waitDataUpdate();
					if(commonSquare(temp_sqr))
					{
						commandList[cmlInd++] = CMD_STOP;
						getNextMove();
						break;
					}
					my.heading &= 0x07;
					commandList[cmlInd++] = CMD_STRAIGHT | CMD_FORWARD;
					commandList[cmlInd++] = CMD_STOP;
					getNextMove();
				}
				else
					break;
				while(prState!=PR_STR_ACC);
				while(relativeDistance<distance[ORTHO_SENS_DIST]*2);
				my.location = nextSquare(my.location,my.heading);
				grid[my.location] |= ONROUTE;
				my.heading |= NODIR;
				if(scanNeighbours())
				{
					mcState = MC_DELIVER;
				}*/
				my.smInd++;
				if(my.smInd>=17)
				{
					my.smInd = 0;
					#ifdef MACHINE_A
					my.goalSquare = 20;
					#endif
					#ifdef MACHINE_B
					my.goalSquare = 60;
					#endif
					mcNextState = MC_SCAN;
					mcState = MC_GET_M_THR;
				}
			break;
			}
			case MC_GET_M_THR:
			{
				waitDataUpdate();
				updateGrid();
				floodGrid(my.goalSquare);
				/*if(map[my.location] == 0)
				{
					mcState = mcNextState;
					break;
				}*/
				temp_sqr = nextNeighbour(my.location);
				temp_dir = nextDirection(my.location,temp_sqr);
				temp_diff = temp_dir - (my.heading&0x07);
				waitDataUpdate();
				if(commonSquare(temp_sqr))
				{
					break;
				}
				if((!inOBZ(my.location)) && inOBZ(temp_sqr))
				{
					if(!my.obzClear || !my.delivering)
						break;

				}
				if(inOBZ(my.location) && (!inOBZ(temp_sqr)))
				{
					my.obzEntrCnt+=2;
				}
				if(isDipoSquare(temp_sqr)&& my.delivering)
				{
					mcState = MC_DELIVER_FINISH;
					break;
				}
				if(prState==PR_STR_ACC||prState==PR_STR_DEC)
				{
					if(temp_diff)
						break;
					my.heading &= 0x07;
					finalPos += COUNTS_PER_CELL*256;
					if(prState==PR_STR_DEC)
						prState = PR_STR_ACC;
					while(relativeDistance>distance[ORTHO_SENS_DIST]*2);
				}
				else if(prState==PR_FINISHED)
				{
					if(temp_diff)
					{
						commandList[cmlInd++] = getTurnCmd(temp_diff);
						commandList[cmlInd++] = CMD_STOP;
						getNextMove();
						my.heading = temp_dir|NODIR;
						break;
					}
					else
					{
						if(!(grid[temp_sqr]&VISITED));
						{
							cubeSensorsOn();
							waitToTick();
							waitToTick();
							cubeSensorsOff();
							if(!commonSquare(temp_sqr))
							{
								grid[temp_sqr] |= VISITED;
								if(frontCube)
								{
									updateCube(temp_sqr);
									break;
								}
							}
						}
						my.heading &= 0x07;
						commandList[cmlInd++] = CMD_STRAIGHT | CMD_FORWARD;
						commandList[cmlInd++] = CMD_STOP;
						getNextMove();
					}
				}
				else
					break;
				while(prState!=PR_STR_ACC);
				while(relativeDistance<distance[ORTHO_SENS_DIST]*2);
				my.location = nextSquare(my.location,my.heading);
				grid[my.location] |= ONROUTE;
				my.heading |= NODIR;
				if(scanNeighbours()&&(my.delivering==FALSE))
				{
					mcState = MC_DELIVER;
					break;
				}
				if(map[my.location] == 0)
					mcState = mcNextState;
				break;
			}
			case MC_DELIVER:
			{
				temp_sqr = unDipoCube();
				if(temp_sqr)
				{
					waitDataUpdate();
					if(inOBZ(temp_sqr)&&!my.obzClear)
						break;
					temp_dir = nextDirection(my.location,temp_sqr);
					if(temp_dir==NODIR)
					{
						temp_sqr = nearestNeighbourOfCube();
						if(temp_sqr)
						{
							my.goalSquare = temp_sqr;
							mcNextState = MC_DELIVER;
							mcState = MC_GET_M_THR;
						}
						else
						{
							do{
								my.smInd++;
							}while(grid[scanMap[my.smInd]]&ONROUTE);
							my.goalSquare = scanMap[my.smInd];
							mcNextState = MC_SCAN;
							mcState = MC_GET_M_THR;
						}
						break;
					}
					else
					{
						grabCube(temp_dir);
						if(my.delivering)
						{
							temp = temp_sqr;
							my.goalSquare = my.location;
							temp_sqr = getDipoSquare();
							if(temp_sqr)
							{
								for(i=0; i<4; i++)
								{
									if((myCube[i]<81)&&myCube[i]==temp)
										temp = i;
								}
								
								myCube[/*cubeIndForMcDeliver*/temp] = temp_sqr;
								my.goalSquare = temp_sqr;
								mcNextState = MC_DELIVER;
								mcState = MC_GET_M_THR;
								break;
							}
						}
					}
					break;
				}
				do{
					my.smInd++;
				}while(grid[scanMap[my.smInd]]&ONROUTE);
				my.goalSquare = scanMap[my.smInd];
				mcNextState = MC_SCAN;
				mcState = MC_GET_M_THR;
				break;
			}
			case MC_DELIVER_FINISH:
			{
				deliverCube();
				if(unDipoCube())
				{
					mcState = MC_DELIVER;
					break;
				}
				do{
					my.smInd++;
				}while(grid[scanMap[my.smInd]]&ONROUTE);
				my.goalSquare = scanMap[my.smInd];
				mcNextState = MC_SCAN;
				mcState = MC_GET_M_THR;
			}
		}
	}	
	while(TRUE);
	return 0;
}