Beispiel #1
0
void checkWin(GameState* gs) {
	int win = getWinner(gs);
	
	if (win) {
		printf("Game over! %d wins!\n", win);
		printGameState(gs);
		exit(0);
	}

	if (isDraw(gs)) {
		printf("Game over! Draw!\n");
		printGameState(gs);
		exit(0);
	}
	     
}
Beispiel #2
0
int main(int argc, char** argv) {
	startNewGame();
	while (1) {
		int move;
		printf("Move? ");
		scanf("%d", &move);
	       
		playerMove(move);
 
		printGameState(globalState);
 
		checkWin(globalState);
 
		computerMove(LOOK_AHEAD);
 
		printGameState(globalState);
 
		checkWin(globalState);
	}
 
	freeGameState(globalState);
 
	return 0;
}
Beispiel #3
0
void handleMsg(char *buf){
	int oldMyTurn = game.isMyTurn;

	struct gameData currGame;
	assert(9 <= parseGameData(buf, &currGame));
	if (currGame.msg != 2){//it's a message!
		char txt[MSGTXT_SIZE];
		strncpy(txt, currGame.msgTxt, strlen(currGame.msgTxt));
		txt[strlen(currGame.msgTxt)] = '\0';
		printf("Client %d: %s\n", currGame.msg + 1, txt);
		return;
	}
	//it's a turn!
	assert(11 <= parseGameData(buf, &game));
	//updateStaticParams(); TODO
	myTurn = game.isMyTurn;

	if (oldMyTurn == 1 && myTurn == 0){//turn is changed. I must have sent a move!
		if (game.valid == 1){
			printf(LEGAL_MOVE);
		}
		else{
			printf(ILLEGAL_MOVE);
		}
	}
	if (oldMyTurn == 0 && myTurn == 1){
		if (game.LastTurnHeap == -1){
			printf("Client %d made an illegal move", opponentId());
		}
		else {
			printf("Client %d takes %d cubes from Heap %c", opponentId(), game.LastTurnRemoves, (char)(game.LastTurnHeap + (int)'A'));
		}
	}
	if (myTurn != 1){
		printGameState(game);
	}
	if (myTurn == 1 && game.win == -1){
		printf(YOUR_TURN);
	}
}
Beispiel #4
0
int main(int argc, char const *argv[]){
	char port[20];
	char address[50];
	int i, j;

	if (argc<1 || argc>3){
		printf(ILLEGAL_ARGS);
		exit(1);
	}
	if (argc == 1 || argc == 2){
		strcpy(port, DEFAULT_PORT);
		if (argc == 1) strcpy(address, DEFAULT_HOST);
	}
	if (argc == 2 || argc == 3){
		strcpy(address, argv[1]);
		if (argc == 3){
			strcpy(port, argv[2]);
		}
	}
	int sock = socket(AF_INET, SOCK_STREAM, 0); // Get socket
	if (sock == -1){
		printf(SOCKET_ERROR, strerror(errno));
		return errno;
	}
	// Connect to server
	sock = connectToServer(sock, address, port);
	// Get initial data
	char readBuf[BUF_SIZE];
	int bufSize = BUF_SIZE;
	receive_all(sock, readBuf, &bufSize, 1);
	// Get the initial data from the server
	if (game.valid == 0){
		printf(CONNECTION_REJECTION);
		return 0;
	}

	playerId = game.myPlayerId;
	myTurn = game.isMyTurn;
	printf("You are client %d\n", playerId + 1);
	if (playerId == 0){
		printf("Waiting to client 2 to connect.\n");
		receive_all(sock, readBuf, &bufSize, 1); //wait until second player connects
	}

	printGameState(game);
	int addReadyForSend = 0;
	fd_set fdSetRead, fdSetWrite;
	struct clientMsg cm;

	if (myTurn == 1){
		printf(YOUR_TURN);
	}
	while (game.win == -1){
		int maxClientFd = sock;

		FD_ZERO(&fdSetRead);
		FD_ZERO(&fdSetWrite);
		FD_SET(STDIN, &fdSetRead);
		FD_SET(sock, &fdSetRead);

		if (addReadyForSend == 1){
			FD_SET(sock, &fdSetWrite);
		}

		int fdReady = select(maxClientFd + 1, &fdSetRead, &fdSetWrite, NULL, NULL);
		if (fdReady == 0){ //chicken check.
			continue;
		}

		if (FD_ISSET(sock, &fdSetWrite) && addReadyForSend == 1){//packets are ready for send
			int buf = BUF_SIZE;
			char cmBuffer[BUF_SIZE];
			i = 0;
			while (i<cmQueueLength){ //send as fifo
				createClientMsgBuff(cmQueue[i], cmBuffer);
				if (send_all(sock, cmBuffer, &buf) == -1){
					break;
				}
				i++;
			}
			j = -1;
			while (i<cmQueueLength){ //reorganize cmQueue
				j++;
				cmQueue[j] = cmQueue[i];
				i++;
			}
			cmQueueLength = j + 1;
			if (cmQueueLength == 0){ addReadyForSend = 0; };
		}
		if (FD_ISSET(STDIN, &fdSetRead)){// there is input from cmd
			int rSize = BUF_SIZE;
			fgets(readBuf, rSize, stdin);
			cm = getMoveFromInput(sock, readBuf);
			if (cm.msg == 1){ //it's a message! send it right away!
				cmQueue[cmQueueLength] = cm;
				cmQueueLength++;
				addReadyForSend = 1;
			}
			else{//it's a turn.
				if (myTurn != 1){// not my turn!
					printf(MOVE_REJECTED);
				}
				else{
					cmQueue[cmQueueLength] = cm;
					cmQueueLength++;
					addReadyForSend = 1;
				}
			}
		}
		if (FD_ISSET(sock, &fdSetRead)){
			char rBuf[BUF_SIZE];
			int rSize = BUF_SIZE;
			receive_all(sock, rBuf, &rSize, 0);
		}
	}
	printWinner(game);
	return 0;
}
Beispiel #5
0
void updateGameState(struct game_t* game,
                     struct input_history_t* inputHistory,
                     struct section_table_t* sectionTable,
                     struct game_history_t* gameHistory,
                     struct tap_state* dataPtr)
{
    assert(game != NULL);

    game->prevState = game->curState;

    // Copy the data that we set in the MAME process.
    game->curState = *dataPtr;

    if (isInPlayingState(game->curState.state) && game->curState.level < game->prevState.level)
    {
        printGameState(game);
    }

    if (sectionTable)
    {
        if (isInPlayingState(game->curState.state) && game->curState.level - game->prevState.level > 0)
        {
            // Push a data point based on the newly acquired game state.
            updateSectionTable(sectionTable, game);
        }
    }

    // Before the game has begun, save the game mode since tgm2p removes mode
    // modifiers when the game ends.
    if (!isInPlayingState(game->prevState.state))
    {
        game->originalGameMode = game->curState.gameMode;

        // If the game just started push an input history element so we can view
        // inputs for the initial piece.
        if (isInPlayingState(game->curState.state))
        {
            if (inputHistory)
                pushInputHistoryElement(inputHistory, game->curState.level);
        }
    }

    // Piece is locked in
    if (isInPlayingState(game->curState.state) &&
        game->prevState.state == TAP_ACTIVE &&
        game->curState.state != TAP_ACTIVE)
    {
        if (gameHistory)
            utringbuffer_push_back(game->blockHistory, &game->prevState);

        if (inputHistory)
            pushInputHistoryElement(inputHistory, game->curState.level);
    }

    // Check if a game has completely ended
    if (isInPlayingState(game->prevState.state) && !isInPlayingState(game->curState.state))
    {
        // Update gold STs now that the game is over. There is technically a
        // "Credit Roll" game mode but it doesn't seem to interfere with normal
        // pb updates.
        struct pb_table_t* pb = getPBTable(&sectionTable->pbHash, game->originalGameMode);
        updateGoldSTRecords(pb, sectionTable);

        pushStateToGameHistory(gameHistory, game->blockHistory, game->prevState, game->originalGameMode);

        resetGame(game);

        if (inputHistory)
            resetInputHistory(inputHistory);
        if (sectionTable)
            resetSectionTable(sectionTable);
    }
}
Beispiel #6
0
int main(int argc, char const *argv[])
{ 
	/*printf("Client Started\n");*/
	char port[20];
	char address[50];

	assert(argc >= 1 && argc <= 3);

	if (argc < 2)
	{
		strcpy(address,"localhost");
	}else{
		strcpy(address,argv[1]);
	}

	if (argc < 3)
	{
		strcpy(port,"6325");
	}
	else
	{
		strcpy(port,argv[2]);
	}

	/*printf("trying to get socket\n");*/
	// Get socket
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == -1)
	{
		printf( "Error opening the socket: %s\n", strerror( errno ) );
   	   	return errno;
	}
	/*printf("Succesfully got a socket number: %d\n", sock);*/

	// Connect to server
	sock = connectToServer(sock, address, port);
	
	char buf[msgSize];
	struct move m;
	// Get initial data
	struct gameData game = receiveDataFromServer(sock);
	if (game.isMisere == 1)
	{
		printf("This is a Misere game\n");
	}else{
		printf("This is a Regular game\n");
	}

	printGameState(game);
	while(game.win == -1){
		m = getMoveFromInput(sock);
		sprintf(buf, "%d$%d", m.heap,m.amount);
		if(sendAll(sock, buf, &msgSize) == -1){
			close(sock);
			exit(0);
		}
		game = receiveDataFromServer(sock);

		// Check if move was valid
		printValid(game);
		// keep on playing
		printGameState(game);
	}

	printWinner(game);

	return 0;
}