Exemple #1
0
ErrorCode RandFoodLocation(Matrix *matrix) {
	Point p;
	do {
		/*Rebecca's change*/
		get_random_bytes(&p.x, sizeof(int));
		p.x = p.x % N;
		get_random_bytes(&p.y, sizeof(int));
		p.y = p.y % N;
	} while (!(IsAvailable(matrix, p) || IsMatrixFull(matrix))); //fixed by Piazza

	if (IsMatrixFull(matrix))
		return ERR_BOARD_FULL;
	(*matrix)[p.y][p.x] = FOOD;
	return ERR_OK;
}
Exemple #2
0
bool Update(Matrix *matrix, Player player, privateGameData* PGD, char move)
{
	ErrorCode e;
	Point p = GetInputLoc(matrix, player, move);

	if (!CheckTarget(matrix, player, p))
	{
		PGD->myGame->Winner =-player;
		return FALSE;
	}
	e = CheckFoodAndMove(matrix, player, p);
	if (e == ERR_BOARD_FULL)
	{
        PGD->myGame->Winner = TIE;
		return FALSE;
	}
	if (e == ERR_SNAKE_IS_TOO_HUNGRY)
	{
		PGD->myGame->Winner =-player;
		return FALSE;
	}
	// only option is that e == ERR_OK
	if (IsMatrixFull(matrix))
	{
		PGD->myGame->Winner = TIE;
		return FALSE;
	}

	return TRUE;
}
Exemple #3
0
ErrorCode RandFoodLocation(Matrix *matrix)
{
	Point p;
	int i=0;
	do
	{
		get_random_bytes(&p.x, sizeof(int));
		p.x %= N;
		get_random_bytes(&p.y, sizeof(int));
		p.y %= N;
		++i;
	} while (!(IsAvailable(matrix, p) || IsMatrixFull(matrix)));

	if (IsMatrixFull(matrix))
		return ERR_BOARD_FULL;

	(*matrix)[p.y][p.x] = FOOD;
	return ERR_OK;
}
Exemple #4
0
bool Update(Matrix *matrix, PlayerS* player, char move) {
	//the matrix we got is the board of the current game
	ErrorCode e;
	Point p = GetInputLoc(matrix, player->color, atoi(move));
	Game* currentGame = player->myGame;

	//TODO: check why doesn't recognize PlayerS fields
	if (!CheckTarget(matrix, player->color, p)) {
		// if the move is illegal, out of bounds, or trying to eat a snake
		down(&(currentGame->isFinishedLock));
		currentGame->isFinished = TRUE;
		up(&(currentGame->isFinishedLock));
		//set winner
		down(&(currentGame->winnerLock));
		if (player->color == WHITE) {
			currentGame->winner = BLACK_IS_WINNER;
		} else {
			currentGame->winner = WHITE_IS_WINNER;
		}
		up(&(currentGame->winnerLock));
		return FALSE;
	}
	e = CheckFoodAndMove(matrix, player->color, p);
	//Rebeca's change
	if (e == ERR_SNAKE_IS_TOO_HUNGRY) {
		down(&(currentGame->isFinishedLock));
		currentGame->isFinished = TRUE;
		up(&(currentGame->isFinishedLock));
		down(&(currentGame->winnerLock));
		if (player->color == WHITE) {
			currentGame->winner = BLACK_IS_WINNER;
		} else {
			currentGame->winner = WHITE_IS_WINNER;
		}
		up(&(currentGame->winnerLock));
		return FALSE;
	}
	//probabely checks if board is full after the snake has moved
	if (e == ERR_BOARD_FULL || IsMatrixFull(matrix)) {
		down(&(currentGame->isFinishedLock));
		currentGame->isFinished = TRUE;
		up(&(currentGame->isFinishedLock));
		down(&(currentGame->winnerLock));
		currentGame->winner = A_TIE;
		up(&(currentGame->winnerLock));
		return FALSE;
	}
	return TRUE;
}