コード例 #1
0
ファイル: main.c プロジェクト: tonglil/bang
int main() {
    // INIT COMS
    cd = (Comm_data*) malloc(sizeof(Comm_data));
    uart = init_clear_uart(cd);

    alt_timestamp_start();
    srand(alt_timestamp());
    CardCtrl *cardCtrl = (CardCtrl*)malloc(sizeof(CardCtrl));
    PlayerCtrl* playerCtrl = (PlayerCtrl*)malloc(sizeof(PlayerCtrl));
    initCards(cardCtrl);

    printf("Waiting for players\n");
    while(connected_count < 3) {
    	receivedFromAndroid();
    }
    printf("Got all players\n");

    initPlayers(playerCtrl, connected_count);

    int y;
    for (y = 0; y < connected_count; y++) {
    	if (pid_connected[y] == 1) {
    		tell_user_pid_role(y, playerCtrl->players[y]);
    		Message message = receivedFromAndroid();
			if (message.type == ACKNOWLEDGE);
    	}
    }

    int i, j;
    for (i = 0; i < connected_count; i++) {
    	drawCardsForId(i, cardCtrl, 2, playerCtrl);
    }

    field = malloc(sizeof(Field));
    alt_up_char_buffer_dev *charBuffer = initCharBuffer();
    alt_up_pixel_buffer_dma_dev *pixelBuffer = initPixelBuffer();
    initField(field, playerCtrl, cardCtrl, charBuffer);

	*leds = *switches;
	alt_timestamp_start();
	srand(alt_timestamp());

    Message message;

	tell_user_all_opponent_range_role(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn));
	message = receivedFromAndroid();
	if (message.type == ACKNOWLEDGE);
	tell_user_all_opponent_blue_lives(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn));
	message = receivedFromAndroid();
	if (message.type == ACKNOWLEDGE);
    tell_user_their_turn(0);
    while (1){
        int listening = 1;
        while (listening) {
            Message message = receivedFromAndroid();
            switch (message.type) {
            case DRAW_CARDS:
                drawCardsForId(message.fromId, cardCtrl, message.count, playerCtrl);
                tell_user_ok(message.fromId);
                break;
            case UPDATE_HAND:
                updateHandForId(playerCtrl, message.fromId, message.count, message.cards);
                break;
            case UPDATE_BLUE:
                updateBlueCardsForId(playerCtrl, message.fromId, message.count, message.cards);
                tell_user_ok(message.fromId);
                break;
            case UPDATE_LIVES:
                updateLivesForId(playerCtrl, message.fromId, message.count);
                break;
            case GATLING:
                startGatling(playerCtrl, message.fromId);
                tell_user_ok(message.fromId);
                break;
            case ALIENS:
                startAliens(playerCtrl, message.fromId);
        		tell_user_ok(message.fromId);
                break;
            case BEER:
            {
            	//TODO
                //updateLivesForId(playerCtrl, message.fromId, message.count);
            	Message message = receivedFromAndroid();
            	if (message.type == UPDATE_LIVES)
            		updateLivesForId(playerCtrl, message.fromId, message.count);
            	updateHand(message.fromId, playerCtrl);
                break;
            }
            case GENERAL_STORE:
                startStore(playerCtrl, message.fromId, cardCtrl);
                break;
            case SALOON:
                startSaloon(playerCtrl, message.fromId);
                tell_user_ok(message.fromId);
                break;
            case ZAP: {
            	startZap(playerCtrl, message.toId, message.fromId, message.self);
            	if (message.self == 0) {
            		tell_user_ok(message.fromId);
            	}
				break;
            }
            case PANIC:
                startPanic(playerCtrl, message.toId, message.fromId, message.self);
            	if (message.self == 0) {
            		tell_user_ok(message.fromId);
            	}
                break;
            case CAT_BALOU:
                startCatBalou(playerCtrl, message.toId, message.fromId, message.self);
            	if (message.self == 0) {
            		tell_user_ok(message.fromId);
            	}
                break;
            case DUEL:
                startDuel(playerCtrl, message.toId, message.fromId, message.self);
            	if (message.self == 0) {
            		tell_user_ok(message.fromId);
            	}
                break;
            case JAIL:
            	//TODO
                tell_user_jail(message.toId, message.cards);
                break;
            case END_TURN:
                listening = 0;
                break;
            default:
                break;
            }
        	alt_up_char_buffer_clear(charBuffer);
        	runField(field, isGameEnd, winningPlayer);
        }
        endTurn(playerCtrl);
    	tell_user_all_opponent_range_role(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn));
		message = receivedFromAndroid();
		if (message.type == ACKNOWLEDGE);
    	tell_user_all_opponent_blue_lives(playerCtrl->turn, getPlayersInfoForId(playerCtrl, playerCtrl->turn));
		message = receivedFromAndroid();
		if (message.type == ACKNOWLEDGE);
		int y;
		for (y = 0; y < MAX_CARDS; y++ ) {
			if (playerCtrl->players[playerCtrl->turn].blueCards[y] >= 72 && playerCtrl->players[playerCtrl->turn].blueCards[y] <= 74) {
				drawCardsForId(playerCtrl->turn, cardCtrl, 1, playerCtrl);
				break;
			}
		}
    	tell_user_their_turn(playerCtrl->turn);
        alt_up_char_buffer_clear(charBuffer);
        runField(field, isGameEnd, winningPlayer);
    }
    return 0;
}
コード例 #2
0
ファイル: tanks.c プロジェクト: kevhui/EECE381-Tanks
int main(void) {
	while (1) {
		state = 0;
		int setTime = 15;
		numPlayers = 2;
		initScreen();
		clearScreen();
		initCharBuffer();
		clean_up();
		initKeyboard();
		initState0();
		initAI();


		//Bypass the menu system for testing
		if (IORD(keys,0) == 8) {
			initPlayer(pOne, MARIO, "pOne", 50, 100, HUMAN);
			initPlayer(pTwo, LUIGI, "pTwo", 50, 100, COMPUTER);
			state = 2;
		} else {
			while (state == 0) {
				decode_scancode(ps2, &decode_mode, buf, &ascii);
				state_0(decode_mode, buf[0]);
			};
			initState1(pOne);
			if(aOn)file_handle = initAudio(fname);
			if(aOn)alt_irq_register(AUDIO_0_IRQ, &ab, (alt_isr_func) write_fifo);
			if(aOn)		alt_up_audio_enable_write_interrupt(ab->audio);
			while (state == 1) {
				decode_scancode(ps2, &decode_mode, buf, &ascii);
				state_1(decode_mode, buf[0], ascii);
				if(aOn)loop_audio(file_handle, fname, ab);
			};
		}

		//clean_up();
		clearCharBuffer();
		clearScreen();

		//enable keyboard IRQ
		void* keyboard_control_register_ptr = (void*) (KEYBOARD_BASE + 4);
		alt_irq_register(KEYBOARD_IRQ, keyboard_control_register_ptr,
				keyboard_ISR);
		alt_up_ps2_enable_read_interrupt(ps2);

		//Draw field and UI to both buffers
		initField();

		updateField();
		drawName(p[pOne].name, p[pTwo].name, p[pThree].name, p[pFour].name);
		drawGas(p[pOne].gas);
		drawHealth(p[pOne].hp, p[pTwo].hp, p[pThree].hp, p[pFour].hp);
		drawBullet(p[pOne].bulletType);
		//drawWindIndicator(1);
		updateScreen();

		updateField();
		drawName(p[pOne].name, p[pTwo].name, p[pThree].name, p[pFour].name);
		drawGas(p[pOne].gas);
		drawHealth(p[pOne].hp, p[pTwo].hp, p[pThree].hp, p[pFour].hp);
		drawBullet(p[pOne].bulletType);
		//drawWindIndicator(1);

		float time;
		alt_timestamp_start();


		int start_timer_flag = 1;
		//printf("NUM PLAYERA %i\n", numPlayers);
		int i;
		while (state == 2) {
			int fallFlag = 1;

			//Checks to see if any players are falling
			while (fallFlag == 1) {
				fallFlag = 0;
				for (i = 0; i < numPlayers; i++) {
					if (p[i].alive) {
						if (p[i].y + TANK_HEIGHT >= SCREEN_HEIGHT-1) {
							p[i].hp = 0;
							p[i].alive = DEAD;
						}
						checkPlayerFalling(i);
						if (p[i].isFalling) {
							undrawPlayer(i);
							updatePlayer(i);
							fallFlag = 1;
						}
					}
				}
				if (fallFlag == 1) {
					updateScreen();
				}
			}

			if(start_timer_flag){
				start_time = (float) alt_timestamp() / (float) alt_timestamp_freq();
				start_timer_flag = 0;
			}
			time = (float) alt_timestamp() / (float) alt_timestamp_freq()-start_time;
			if (time >= setTime) {
				setPlayerTurn();
			}
			if (p[turn].type == HUMAN) {
				runGame();

			} else {
				p[turn].deg = 0;
				aiMain(turn);
				setPlayerTurn();
			}
			printTimer(setTime - time);
			int deadCount = 0;
			for (i = 0; i < numPlayers; i++) {
				if (p[i].alive == DEAD)
					deadCount++;
			}
			if (deadCount == numPlayers - 1) {
				usleep(500000);
				state = 3;
			}
		}

		alt_up_ps2_disable_read_interrupt(ps2);
		if(aOn)alt_up_audio_disable_write_interrupt(ab->audio);

		GameOverScreen();
	}
}