Example #1
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;
}
Example #2
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;
}