Пример #1
0
bool AI::runHuman()
{
  cout<<"Human Turn: "<<turnNum()<<endl;
  if(turnNum() == 1)
  {
    for(unsigned int w=0;w<weapons.size();w++)
    {
      cout<<"WeaponID: "<<w<<endl;
      displayWeapon(weapons[w]);          
    }
    vector<location> nearby = validMoves(0,0);
    while(nearby.size()<5)
    {
      //should keep trying to find 6 valid spots at random on the map
      nearby = validMoves(rand()%(getMapSize()*2)-getMapSize(),rand()%(getMapSize()*2)-getMapSize());
    }
    for(int i=0;i<getHumansReady();i++)
    {
      Human::spawn(weapons[i], nearby[i].x, nearby[i].y);
    }
  }
  if(turnNum() == 3)
  {
    for(unsigned int i=0;i<humans.size();i++)    
    {
      cout<<"Human["<<humans[i].id()<<"] is carrying weapon "<<humans[i].weaponID()<<endl;
      displayWeapon(weapons[humans[i].weaponID()]);
    }
  }
  
  for(unsigned int i=0;i<humans.size();i++)
  {
    for(unsigned int m=0;m<humans[i].moves();m++)
    {
      vector<location> nearby = validMoves(humans[i].x(),humans[i].y());
      if(nearby.size()>0)
      {
        int pick = rand()%nearby.size();
        humans[i].move(nearby[pick].x,nearby[pick].y);
      }
      else
      {
        cout<<"Human["<<humans[i].id()<<"] is completely surrounded"<<endl;
      }
    }
  }
  cout<<"End Turn"<<endl;
  return true;
}
Пример #2
0
vector<Move> Board::validMoves() const
{
	vector<Move> result;
	for(BoardPoint p: playerPieces()) {
		for(Move m: validMoves(p)) {
			result.push_back(m);
		}
	}
	return result;
}
Пример #3
0
vector<Move> Board::sortedMoves() const
{
	MoveHeuristic heuristic(*this);
	
	vector<Move> moves = validMoves();
	vector<pair<float, Move>> sorted;
	sorted.reserve(moves.size());
	for(Move move: moves) {
		// Estimate move strength
		float strength = heuristic.evaluate(move);
		
		sorted.push_back(make_pair(strength, move));
	}
	sort(sorted.begin(), sorted.end());
	reverse(sorted.begin(), sorted.end());
	
	moves.clear();
	for(auto p: sorted) {
		moves.push_back(p.second);
	}
	return moves;
}
Пример #4
0
int findBestMove(Communicator comm, char origB[BS], char color, int depth) {

	int wallClockTime;
	int processorTime;
	int boardCount;

	int possibleMoves[BS];
	int scores[BS];
	int totalWorkRequests = 0;
	int i;
	int resultScores[BS];
	int resultCount[BS];
	char logBuffer[200];
    struct timeval startTime, endTime;

	gettimeofday(&startTime, NULL);

	
	WorkRequest* wReq;// = new WorkRequest();
	
	validMoves(origB, color, possibleMoves, scores);

	// seed the system - push all current valid moves onto the queue one at a time
	cout << "Ready to process possible moves.\n";
	wReq = new WorkRequest();
	processorTime = 0;
	boardCount = 0;
	wallClockTime = 0;
	for (int whichPossibleMove=0; possibleMoves[whichPossibleMove]; whichPossibleMove++) {
		strncpy(wReq->b,origB,BS);
		wReq->color = color;
		wReq->depth = depth;
		wReq->history[1] = 0;

		resultScores[possibleMoves[whichPossibleMove]] = 0;
		resultCount[possibleMoves[whichPossibleMove]] = 0;
		
		wReq->history[0] = possibleMoves[whichPossibleMove];

		processRequest(comm, wReq, totalWorkRequests, resultScores, resultCount);
		cout << comm.rank << "\tDone processing " << possibleMoves[whichPossibleMove] << endl;
	}
	delete wReq;

	gettimeofday(&endTime, NULL);
	
	int elapsed = elapsedTime(&endTime, &startTime);
	logIt(elapsed);
	
	cout << "*********************************************" << endl;
	cout << "***    all done processing possible moves  **" << endl;
	cout << "*********************************************" << endl;
	cout << "requests sent: " << totalWorkRequests << endl;


	double bestValue = -9.0e100;
	int bestPosition = 0;
	for (i=0; possibleMoves[i]; i++) {
		int move = possibleMoves[i];
		double finalScore = (double)resultScores[move] / (double)resultCount[move];
		cout << "Position " << move << " final score: " << finalScore << " (" << resultScores[move] << "/" << resultCount[move] << ")" << endl;
		if (bestValue < finalScore) {
			bestValue = finalScore;
			bestPosition = move;
		}
	}
  	cout << "best position appears to be " << bestPosition << endl;

	return bestPosition;
}
Пример #5
0
int main(int argc, char** argv) {
	char b[BS];
	int r, c;


	Communicator comm(argc, argv);
	int validHMoves[BS];
	int validCMoves[BS];
	int scoreHMoves[BS];
	validCMoves[0] = 0;
	char buf[100];
	ofstream outputFile;
	if (comm.rank == 0) {
		
		sprintf(buf,"\n\nP = %d", comm.nprocs);
		logIt(buf);
		int hMove = 0;
		int depth = atoi(argv[1]);
		
		srand((int) time(NULL));
		setupBoard(b);
		do {
			validMoves(b,H,validHMoves,scoreHMoves);
			displayBoard(b);
			if (validHMoves[0]) {
				hMove = moveHuman(b,validHMoves);	// to get a manually entered move
				if (hMove == 0) {
					break;
				}
				cout << "After your move the board is:\n";
				displayBoard(b,C);
			} else {
				cout << "You Don't have any valid moves.\n";
			}
			cout << "Computer is plotting your demise.\n";

			int best = findBestMove(comm, b, C, depth);

			if (best != 0) {
				squareScore(b,best,C,true);
				aiToBs(best, r, c);
				cout << "Computer moves to [" << r << "," << c << "]\n";
			} else {
				cout << "Computer does not have a valid move\n";
			}
		} while ((validCMoves[0] || validHMoves[0]) && (hMove != 0));

		WorkRequest* wReq = new WorkRequest();
		wReq->b[0] = 'T';
		for(int i = 1; i < comm.nprocs; i++)
			comm.send(i, (char *) wReq, 1, TAG_DO_THIS_WORK);
		cout << "\n\nGame over\n\n";
		
	} else { // not comm.rank 0
		worker(comm);
	}
	
	comm.finalize();
	
	return (EXIT_SUCCESS);
}
Пример #6
0
bool Board::isValidMove(Move move) const
{
	vector<Move> moves = validMoves();
	return find(moves.begin(), moves.end(), move) != moves.end();
}