static void game_start() {
	srand(time(NULL));
	
	game_wins = 0;
	if (game_is_first && persist_exists(0)) {
		game_wins = persist_read_int(0);
	}
	game_is_first = false;
	round_start();
	
	game_timer = app_timer_register(WAIT_RESTING, game_update, NULL);
	layer_mark_dirty(game_layer);
}
static void game_update(void *data) {
	switch(game_state) {
		case STATE_RESTING:
			game_state = STATE_COUNTDOWN_1;
			game_timer = app_timer_register(WAIT_COUNTDOWN / 2, game_update, NULL);
		 layer_mark_dirty(game_layer);
		break;
		case STATE_COUNTDOWN_1:
			if (game_hand_open) {
				game_state = STATE_COUNTDOWN_2;
				game_hand_open = false;
			} else { game_hand_open = true; }
			game_timer = app_timer_register(WAIT_COUNTDOWN / 2, game_update, NULL);
		 layer_mark_dirty(game_layer);
		break;
		case STATE_COUNTDOWN_2:
			if (game_hand_open) {
				game_state = STATE_COUNTDOWN_3;
				game_hand_open = false;
			} else { game_hand_open = true; }
			game_timer = app_timer_register(WAIT_COUNTDOWN / 2, game_update, NULL);
		 layer_mark_dirty(game_layer);
		break;
		case STATE_COUNTDOWN_3:
			if (game_hand_open) {
				game_state = STATE_COUNTDOWN_WAIT;
				game_hand_open = false;
				game_timer = app_timer_register(WAIT_COUNTDOWN + (rand() % game_suspense), game_update, NULL);
				layer_mark_dirty(game_layer);
			} else {
				game_hand_open = true; 
				game_timer = app_timer_register(WAIT_COUNTDOWN / 2, game_update, NULL);
		 	layer_mark_dirty(game_layer);
			}
		break;
		case STATE_COUNTDOWN_WAIT:
			game_state = STATE_COUNTDOWN_GO;
		 layer_mark_dirty(game_layer);
			game_timer = app_timer_register(game_easiness, game_update, NULL);
			game_waiting_for_reaction = true;
			game_player_reacted = false;
		break;
		case STATE_COUNTDOWN_GO:
			layer_mark_dirty(game_layer);
			game_waiting_for_reaction = false;
			vibes_short_pulse();
			if (game_player_reacted) {
				game_state = STATE_SHOT;
				game_timer = app_timer_register(WAIT_RESULT, game_update, NULL);
				layer_mark_dirty(game_layer);
			} else {
				game_state = STATE_HIT;
				game_timer = app_timer_register(WAIT_RESULT, game_update, NULL);
				layer_mark_dirty(game_layer);
			}
		break;
		
		case STATE_SHOT:
			game_state = STATE_WIN;
			game_wins++;
			game_timer = app_timer_register(WAIT_RESTING, game_update, NULL);
			layer_mark_dirty(game_layer);
		break;
		
		case STATE_HIT:
			game_state = STATE_LOSE;
			game_wins = 0;
			game_timer = app_timer_register(WAIT_RESTING, game_update, NULL);
			layer_mark_dirty(game_layer);
		break;
		
		case STATE_WIN:
		case STATE_LOSE:
		 if (game_wins < GAME_MAX_WINS) {
				round_start();
				game_timer = app_timer_register(WAIT_COUNTDOWN, game_update, NULL);
				layer_mark_dirty(game_layer);
			} else {
				game_state = STATE_SPLASH;
				persist_write_int(0, 0);
			}
		break;
	}
}
Beispiel #3
0
int main(int argc, char **argv)
{
	int i, cc;
	char msg[MSG_BFR_SZ];
	char tag[MSG_BFR_SZ];

	struct player_data *p = NULL;

	--argc; ++argv;
	setbuf(stdout, NULL);
	setbuf(stdin , NULL);

	if (!client_setup(&argc, &argv))
		return EXIT_FAILURE;

	recv(msg); sscanf(msg, "%*s %d", &SELF.id);
	sprintf(msg, "NAME %s", BOT_NAME); send(msg);

	while ((cc = recv(msg)))
	{
		sscanf(msg, "%s", tag);
		
		if (!strcmp(tag, "READY")) break;
		else if (!strcmp(tag, "PLAYERS"))
		{
			sscanf(msg, "%*s %u", &numplayers);
			for (i = 0, p = players; i < numplayers; ++i, ++p)
			{
				recv(msg); sscanf(msg, "%u %u", &p->id, &p->pool);
				p->wager = p->card = p->active = 0;
			}
		}
		else if (!strcmp(tag, "CARDS"))
			sscanf(msg, "%*s %u %u", &XRANGE, &XDUP);
		else if (!strcmp(tag, "ANTE"));
			sscanf(msg, "%*s %*d %u", &ROUNDS_TO_DBL);
	}

	if (!p) EXPECTED(tag, "PLAYERS");
	copyself(); game_setup(players, numplayers);

	while ((cc = recv(msg)))
	{
		sscanf(msg, "%s", tag);
		
		if (!strcmp(tag, "ENDGAME")) break;
		else if (!strcmp(tag, "ROUND"))
		{
			unsigned int rnum, pstart, rante;
			sscanf(msg, "%*s %u %u %u", &rnum, &pstart, &rante);
			copyself(); round_start(rnum, pstart, rante);

			while ((cc = recv(msg)))
			{
				sscanf(msg, "%s", tag);
				if (!strcmp(tag, "TURN"))
				{
					for (i = 0, p = players; i < numplayers; ++i, ++p)
					{
						recv(msg);
						sscanf(msg, "%u %u %u %u %u", &p->id, &p->card, &p->pool, 
							&p->wager, &p->active);
					}

					copyself(); recv(tag);
					if (strcmp(tag, "GO")) EXPECTED(tag, "GO");
					int k = player_turn(players, numplayers);

					// perform the chosen action
					switch (k)
					{
						case CALL: sprintf(msg, "CALL"); break;
						case FOLD: sprintf(msg, "FOLD"); break;
						default:   sprintf(msg, "WAGER %d", k); break;
					}
					send(msg);
				}
				else if (!strcmp(tag, "ENDROUND"))
				{
					int winnings; sscanf(msg, "%*s %u", &winnings);
					for (i = 0, p = players; i < numplayers; ++i, ++p)
					{
						recv(msg); p->wager = p->active = 0;
						sscanf(msg, "%u %u %u", &p->id, &p->card, &p->pool);
					}
					copyself(); round_end(players, numplayers, winnings);
					break;
				}
				// got an unexpected message...
				else EXPECTED(tag, "TURN/ENDROUND");
			}
		}
		// got an unexpected message...
		else EXPECTED(tag, "ROUND/ENDGAME");
	}

	game_end();
	return EXIT_SUCCESS;
}