Пример #1
0
void sinap(void)
{
	U score_limit = 1000U, accG, accL, dice_count, r_dice_count;
	UC dice[6];
	int c;
	Occurrences o;
	Player a = {0, "A", 0}, b = {1, "B", 0},
	       *p = random_number(0,1) == 0 ? &a : &b;

	while (a.score < score_limit && b.score < score_limit) {
		printf("\nScore: '%s'=%u '%s'=%u. Press enter to continue..",
				a.name, a.score, b.name, b.score);
		(void) fflush(stdout);
		while (getchar() != '\n');
		/* switch player */
		p = p->id == a.id ? &b : &a;
		accG = accL = 0;
		dice_count = r_dice_count = 6U;
		while (1) {
			printf("Player '%s' rolls: ", p->name);
			roll_dice(dice, dice_count);
			print_dice(dice, dice_count);
			o = calculate_occurrences(dice, dice_count);
			accL = calc_score(o);
			r_dice_count = remaining_dice(o, dice_count);
			printf(" ## s=%u ", accL);
			if (accL > 0) {
				printf("S=%u ", accL + accG);
				printf("t=%u ", p->score);
				printf("T=%u ", accL + accG + p->score);
				printf("r=%u", r_dice_count);
			}
			putchar('\n');
			if (accL == 0) {
				break;
			} else {
				dice_count = r_dice_count;
				accG += accL;
				if (accG + p->score > score_limit) {
					p->score += accG;
					break;
				}
			}
			if (!(p->score == 0 && accG < 35)) {
				printf("Continue? [Y/n]: ");
				(void) fflush(stdout);
				c = getchar();
				if (c != '\n')
					while (getchar() != '\n');
				if (c == 'n') {
					p->score += accG;
					break;
				}
			}
		}
	}

	printf("Player '%s' wins with score %u.\n", p->name, p->score);
}
Пример #2
0
int main(int argc, char* argv[])
{
	struct dice a[5];
	struct board b = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
	int out = 0;
	
	while(1)
	{
		for (int i=0; i<5; i++)
		{
			a[i].is_hold = 0;
		}
		
		int i = 0;
		dice_random(a);
		while(1)
		{
			system("clear");
			printf("\n\n++++++++++++Yahtzee!++++++++++++\n\n\n");
			print_dice(a);
			printf("Time of rolls: %d\n", i+1);
			printf("\nControl Keys\n[r: roll dice] [t: stop roll]\n[q, w, e, a, s: hold or unhold each dice]\n\n");
			print_board(0, a, &b);
			if (i == 2)
			{
				break;
			}
			int input = getch();
			if (input == 116)
			{
				i = 0;
				break;
			}
			else if (input == 114)
			{
				dice_random(a);
			}
			else if (input == 113)
			{
				change_hold(0, a);
				continue;
			}
			else if (input == 119)
			{
				change_hold(1, a);
				continue;
			}
			else if (input == 101)
			{
				change_hold(2, a);
				continue;
			}
			else if (input == 97)
			{
				change_hold(3, a);
				continue;
			}
			else if (input == 115)
			{
				change_hold(4, a);
				continue;
			}
			else
			{
				continue;
			}
			i++;
		}
		
		while(1)
		{
			system("clear");
			printf("\n\n++++++++++++Yahtzee!++++++++++++\n\n\n");
			print_dice(a);
			printf("Finished rolling!\n\n");
			printf("Please assign your point to one category!\n");
			printf(COLOR_RED"Red number"COLOR_RESET);
			printf(" indicates that category is already assigned!\n");
			printf(COLOR_GREEN"Green number"COLOR_RESET);
			printf(" indicates the possible value of that category!\n\n");
			printf("Control Keys\n[x: exit] [1-6: assign point]\n\n");
		
			print_board(1, a, &b);
		
			int input = getch();
			if (input == 120)
			{
				out = 1;
				break;
			}
			else if (input == 49)
			{
				if (b.ones != -1)
				{
					continue;
				}
				b.ones = upper(1, a);
				check_upper(&b);
				break;
			}
			else if (input == 50)
			{
				if (b.twos != -1)
				{
					continue;
				}
				b.twos = upper(2, a);
				check_upper(&b);
				break;
			}
			else if (input == 51)
			{
				if (b.threes != -1)
				{
					continue;
				}
				b.threes = upper(3, a);
				check_upper(&b);
				break;
			}
			else if (input == 52)
			{
				if (b.fours != -1)
				{
					continue;
				}
				b.fours = upper(4, a);
				check_upper(&b);
				break;
			}
			else if (input == 53)
			{
				if (b.fives != -1)
				{
					continue;
				}
				b.fives = upper(5, a);
				check_upper(&b);
				break;
			}
			else if (input == 54)
			{
				if (b.sixes != -1)
				{
					continue;
				}
				b.sixes = upper(6, a);
				check_upper(&b);
				break;
			}
			else
			{
				continue;
			}
		}
		
		if (out)
		{
			printf("\nBye!\n");
			break;
		}
		if (b.sum != -1)
		{
			system("clear");
			printf("\n\n++++++++++++Yahtzee!++++++++++++\n\n\n");
			printf("Final Score\n\n");
			print_board(0, a, &b);
			printf("\nBye!\n");
			break;
		}
	}
	
	return 0;
}