如何在C中的Dice Roll模拟器中存储/重新掷骰子

时间:2016-09-10 19:34:24

标签: c dice

我在尝试搜索以前的问题和答案时四处搜索,但我似乎无法找到我要找的东西。至少不是普通的C,所以我走了。

作为大学任务的一部分,我正在用C编写Roll Dice模拟器。我已经完成了初始任务,可以选择最多三个玩家/用户,并且我已经可以掷骰子三每个玩家/用户的时间。

最有可能的是它可以更容易(使用更少的代码),但到目前为止它正在工作。该任务的下一部分是可以保存骰子,只重新注册其中一些。我正在考虑制作整数/变量,如p1_keep1(播放器1骰子1),p1_keep2(播放器1骰子2),等等,但我真的陷入如何在代码中实现它,以便节省一些骰子然后重新开始。

我考虑过在RollingDiceP1,RollingDiceP2和RollingDiceP3下进行第二次循环(请参阅下面的代码进行参考),其中掷骰子,但我不确定如何实现它,而不会让它变得复杂。我尝试在do-while循环中使用while循环,但它变得如此复杂,有十个嵌套的'if else',所以我删除了它,确信必须有更好的方法,尽管我没有设法找到/发现它爱好。

为了找到解决方案,我能做些什么来使我的初始代码“更容易”,换句话说,我可以用更少的代码获得相同的结果吗? 如果我能以某种方式压缩代码,也许更容易发现/看到如何保存一些骰子的解决方案,同时重新滚动其余的。

正如我上面所说,我在do-while循环中尝试了一个while循环,但它很快变得如此庞大和复杂,以至于无法标记,我相信有更好的方法,尽管我尚未设法发现它们。任何提示/帮助或推进正确的方向将不胜感激。正如你可能在下面的代码中看到的那样,这是我在c的第三周,作为兼职学生,我还没能用c工作那么多。我确信必须存在某种循环,但不确定是否是最合适的一段时间。

这是我目前遇到的挑战: 挑战:未来实施你的骰子程序,以便用户可以保存一些骰子,然后只滚动他们没有保存的骰子数量。就像你通常在Yahtzee做的那样。

我现在的代码看起来像这样,并且非常感谢帮助/提示或向正确的方向推进:

/*
Author: Thorbjørn Elvestad
Student ID: XXXXX
E-mail: XXXXXXXXXXXXX@XXXXX.XXX

This is a Roll the Dice (for Yahtzee) simulation, where the computer rolls the dice,
and present the result to the users. Only the result is presented. The
user does not physically see the dice rolled. There can be a set number
of players, and each players got three rolls each before the next player
role.

This is Activity 1 of the Week 3 weekly activity.
*/

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

/* Setting all functions to be used in the program */
void players (void);
void RollingDiceP1 (void);
void RollingDiceP2 (void);
void RollingDiceP3 (void);

/* Loading all global variables to be available for all functions in the program */

    /* Setting variable for amount of players */
    int numberPlayers = 0;
    /* Setting variables for player 1-3 dices */
    int p1_dice1 = 0, p1_dice2 = 0, p1_dice3 = 0, p1_dice4 = 0, p1_dice5 = 0;
    int p2_dice1 = 0, p2_dice2 = 0, p2_dice3 = 0, p2_dice4 = 0, p2_dice5 = 0;
    int p3_dice1 = 0, p3_dice2 = 0, p3_dice3 = 0, p3_dice4 = 0, p3_dice5 = 0;

    /* Seting variable for allowed rounds of rolling the dice */
    int rolldice_counter = 1;

    char press_enter;



/*Initiating the main function */
int main(void)
{
    /* Loading the Random Number generator */
    srand(time(NULL));

    printf("Howdy! \n");
    printf("\n Welcome to the Roll a Dice Yahtzee Simulator, \n");

    /* Loading functions for choosing amount of players */
    players();
    printf("Let us roll some dice! \n");
    RollingDiceP1();                                                                                           

    /* Checking if there are more players waiting for their turn */
    while (1)
    {
        if (numberPlayers == 3)
        {
            printf("\n \n \n \n Player two, stay tuned... \n \n");
            RollingDiceP2();
            printf("\n \n \n \n Player three, stay tuned... \n \n");
            RollingDiceP3();
            break;
        }


    else if (numberPlayers == 2){
            printf("\n \n \n \n Player 2, stay tuned... \n");
            RollingDiceP2 ();}
    else
        printf("No more players waiting for rolling their dice, thanks for playing! \n");
        break;
    }
    printf("No more players waiting for rolling their dices, thanks for playing! \n");

return 0;
}


/* Lets choose how many players to use the Yahtzee Roll Dice Simulator */
void players()
{
    printf("Please type how many players (1-3):");

    while (1)
    {
     scanf("%d", &numberPlayers);
     if (numberPlayers == 1)
        {
            printf("You have selected %d players! ", numberPlayers);
            break;
        }
    else if (numberPlayers == 2)
        {
            printf("You have selected %d players! ", numberPlayers);
            break;
        }
    else if (numberPlayers == 3)
        {
            printf("You have selected %d players! ", numberPlayers);
            break;
        }
    else printf("Invalid number of players, please choose 1-3 players:");

    }
}

/* Rolling dices for player 1 */
void RollingDiceP1()
{
    do
        {
        /* Rolling the dices, and fetching random numbers between 1-6 for Player 1 dices */
        printf("\n Hold tight player 1, I am rolling your dices: \n");
        p1_dice1 = rand() % 6 + 1;
        p1_dice2 = rand() % 6 + 1;
        p1_dice3 = rand() % 6 + 1;
        p1_dice4 = rand() % 6 + 1;
        p1_dice5 = rand() % 6 + 1;

        /* Showing the result for player 1 */
        printf("Your dices show:\n Dice 1: %d \n Dice 2: %d \n Dice 3: %d \n Dice 4: %d \n Dice 5: %d \n \n", p1_dice1, p1_dice2, p1_dice3, p1_dice4, p1_dice5);
        printf("This is your roll number %d of 3 rolls, press any key and enter to continue \n", rolldice_counter);
        scanf("%s", &press_enter);
        } while (++rolldice_counter <= 3); /* Ending the do-while loop when rolldice_counter reach 0 */
        rolldice_counter = 1;
}

/* Rolling dices for player 2 */
void RollingDiceP2()
{
    do
        {
        /* Rolling the dices, and fetching random numbers between 1-6 for Player 2 dices */
        printf("\n Hold tight player 2, I am rolling your dices: \n");
        p2_dice1 = rand() % 6 + 1;
        p2_dice2 = rand() % 6 + 1;
        p2_dice3 = rand() % 6 + 1;
        p2_dice4 = rand() % 6 + 1;
        p2_dice5 = rand() % 6 + 1;

        /* Showing the result for player 2 */
        printf("Your dices show:\n Dice 1: %d \n Dice 2: %d \n Dice 3: %d \n Dice 4: %d \n Dice 5: %d \n \n", p2_dice1, p2_dice2, p2_dice3, p2_dice4, p2_dice5);
        printf("This is your roll number %d of 3 rolls, press any key and enter to continue \n", rolldice_counter);
        scanf("%s", &press_enter);
        } while (++rolldice_counter <= 3); /* Ending the do-while loop when rolldice_counter reach 0 */
        rolldice_counter = 1;
}

/* Rolling dices for player 3 */
void RollingDiceP3()
{
    do
        {
        /* Rolling the dices, and fetching random numbers between 1-6 for Player 3 dices */
        printf("\n Hold tight player 3, I am rolling your dices: \n");
        p3_dice1 = rand() % 6 + 1;
        p3_dice2 = rand() % 6 + 1;
        p3_dice3 = rand() % 6 + 1;
        p3_dice4 = rand() % 6 + 1;
        p3_dice5 = rand() % 6 + 1;

        /* Showing the result for player 3 */
        printf("Your dices show:\n Dice 1: %d \n Dice 2: %d \n Dice 3: %d \n Dice 4: %d \n Dice 5: %d \n \n", p3_dice1, p3_dice2, p3_dice3, p3_dice4, p3_dice5);
        printf("This is your roll number %d of 3 rolls, press any key and enter to continue \n", rolldice_counter);
        scanf("%s", &press_enter);
        } while (++rolldice_counter <= 3); /* Ending the do-while loop when rolldice_counter reach 0 */
        rolldice_counter = 1;
}

0 个答案:

没有答案