Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

Studies

פתרון

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define WIN_SCORE 100
#define ROLL_ACTION 0
#define BANK_ACTION 1
#define DIE_SIDES 6

#define PENALTY_DICE 1
#define MULT_DICE 6
#define ROLLED_SUPER_PENALTY -1
#define ROLLED_PENALTY -2
#define ROLLED_MULT -3

#define COMPUTER_ROLLS 3

bool rolled_super_penalty(int dice1, int dice2) {
    return dice1 == PENALTY_DICE && dice2 == PENALTY_DICE;
}

bool rolled_penalty(int dice1, int dice2) {
    return (dice1 == PENALTY_DICE && dice2 != PENALTY_DICE) ||
           (dice1 != PENALTY_DICE && dice2 == PENALTY_DICE);
}

bool rolled_mult(int dice1, int dice2) {
    return dice1 == MULT_DICE || dice2 == MULT_DICE;
}

int get_dice(void) { return (rand() % DIE_SIDES) + 1; }

int get_roll_result(void) {
    int dice1 = get_dice();
    int dice2 = get_dice();
    printf("Dices show (%d, %d)\n", dice1, dice2);

    if (rolled_super_penalty(dice1, dice2)) {
        return ROLLED_SUPER_PENALTY;
    }
    if (rolled_penalty(dice1, dice2)) {
        return ROLLED_PENALTY;
    }
    if (rolled_mult(dice1, dice2)) {
        return ROLLED_MULT;
    }
    return dice1 + dice2;
}

int get_player_action(void) {
    int selection;
    do {
        printf("Press %d to roll, %d to bank\n", ROLL_ACTION, BANK_ACTION);
        scanf("%d", &selection);
    } while (selection != ROLL_ACTION || selection != BANK_ACTION);
    return selection;
}

int take_player_turn(int player_score) {
    printf("Player turn!\n");
    int banked_score = 0;
    while (1) {

        switch (get_player_action()) {
        case ROLL_ACTION:
            int roll = get_roll_result();
            switch (roll) {
            case ROLLED_SUPER_PENALTY:
                printf("Rolled super penalty!\n");
                return 0;

            case ROLLED_PENALTY:
                printf("Rolled penalty!\n");
                return player_score;

            case ROLLED_MULT:
                printf("Rolled multiplier!\n");
                return player_score * 2;

            default:
                banked_score += roll;
                printf("Player banked %d points, total bank: %d\n",
                    roll,
                    banked_score);
                continue;
            }

        case BANK_ACTION:
            return player_score + banked_score;

        default:
            printf("Unreachable");
            break;
        }
    }
}

int take_computer_turn(int computer_score) {
    printf("Computer turn!\n");
    int banked_score = 0;

    for (int i = 0; i < COMPUTER_ROLLS; i++) {
        int roll = get_roll_result();
        switch (roll) {
        case ROLLED_SUPER_PENALTY:
            printf("Rolled super penalty!\n");
            return 0;

        case ROLLED_PENALTY:
            printf("Rolled penalty!\n");
            return computer_score;

        case ROLLED_MULT:
            printf("Rolled multiplier!\n");
            return computer_score * 2;

        default:
            banked_score += roll;
            printf("Computer banked %d points, total bank: %d\n",
                roll,
                banked_score);
        }
    }
    printf("Computer banks %d points\n", banked_score);
    return computer_score + banked_score;
}

void print_scores(int player_score, int computer_score) {
    printf("Player score: %d\n", player_score);
    printf("Computer score: %d\n", computer_score);
}

int main(void) {
    srand(time(NULL));
    int player_score = 0, computer_score = 0;

    while (1) {
        print_scores(player_score, computer_score);
        player_score = take_player_turn(player_score);
        printf("Finishing player turn! Player score is %d\n", player_score);

        if (player_score > WIN_SCORE) {
            printf("Player won!\n");
            break;
        }

        computer_score = take_computer_turn(computer_score);
        printf("Finishing computer turn! Computer score is %d\n",
            computer_score);
        if (computer_score > WIN_SCORE) {
            printf("Computer won!\n");
            break;
        }
    }
}