Like Tree1Likes
  • 1 Post By ledow

struggling with my dice function to switch between players

This is a discussion on struggling with my dice function to switch between players within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am writing a snakes and ladder program and I'm almost finished, but I am struggling with the ...

  1. #1
    HJH
    HJH is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    3

    struggling with my dice function to switch between players

    Hello everyone,
    I am writing a snakes and ladder program and I'm almost finished, but I am struggling with the dice to work in the way I want it to work.
    I want the dice to work like this :
    Before each player throw the dice they must start at 0.
    Each player must throw a 6 on the dice to move on the board.
    If a player threw a 6 on the dice, that player can throw again.

    But I ended up with two seperate dice, one for each player (game is only for two players).And when I run the program, both players don't start at 0. And when I throw the dice, both players move at the same time but with different values.
    If one of my players threw a 6, they just keep on throwing until someone wins the game. I tried to use a switch and if statements but I couldn't get it right.
    And so I did this :


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<windows.h>
    #define SpaceBar 32// 32 is an ASCII value for a spacebar
     
       
    
    //Prototypes
    void TrowDice();
     
    int dice, null, score=0;
    int X, Y;
    char Select;
    int score,above, dice2, score2, above2;
    int main(void)// main function
    {
    do
    {
        system("cls");// this is only to clear the screen and print the new values
        ThrowDice();// the throw dice function
        Select = getch();//select spacebar input if pressed, to roll the dice once
        if(score==100)
        {
            printf("congratulaions Player 1 won the game");
            return 0;
        }
        if(score2==100)
        {
            printf("congratulations Player 2 won the game");
            return 0;
        }
    }
    while(Select == SpaceBar);
    }
    void ThrowDice()//dice function
    {
        printf("the dice rolled = %d", dice);
        printf("\nPlayer1 current position = %d\n",score);
        srand(time(null));//to make the dice random
        dice =(rand() % 6) + 1;//dice
        score=score+dice;//total score of dice throws
    
       if(score >100)//player must land on 100 exactly.
        {
            above = (score-100);
            score = (100 - above);
        }
        switch(score)//this all my snakes and ladders for player 1
            {
                //snakes
            case 28 :score=8;break;
            case 57 :score=37;break;
            case 81 :score=61;break;
            case 91 :score=50;break;
            case 97 :score=84;break;
                //ladders
            case 16 :score=25;break;
            case 32 :score=52;break;
            case 39 :score=79;break;
            case 75 :score=86;break;
            }//end switch
    
        //This is my Player 2 section starting from here on.
        printf("the dice rolled = %d", dice2);
        printf("\nPlayer2 current position = %d\n",score2);
        srand(time(null));// to make dice random
        dice2 =(rand() % 6) + 1;// diece
        score2=score2+dice2;// total dice throws for player 2
       if(score2 >100)// must land on 100 exactly
        {
            above2 = (score2-100);
            score2 = (100 - above2);
        }
        switch(score2)// all the snakes and ladders for player 2
            {
                //snakes
            case 28 :score2=8;break;
            case 57 :score2=37;break;
            case 81 :score2=61;break;
            case 91 :score2=50;break;
            case 97 :score2=84;break;
                //ladders
            case 16 :score2=25;break;
            case 32 :score2=52;break;
            case 39 :score2=79;break;
            case 75 :score2=86;break;
            }
    }
    Can someone please tell me what I'm doing wrong or give me hints on what to do.
    Thank you.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Why does ThrowDice roll both players dice? That's what's confusing you and making a mess of the code. Why not have it called twice (once for each player) and have some notion of which players "turn" it is? Then ThrowDice can look at whose turn it is, change the appropriate score (two scores, sounds like a job for an array!), and carry on. Then you can change the turn, run it again, and the SAME logic will apply to both players without any problems.

    Where do you initialise score2? Why do you declare score twice? Why isn't your compiler moaning (presumably you've ignored all the warnings that it can be made to generate)? Players won't start at zero if you don't initialise the score properly (and here, score really means "what square am I on", so it's a bit mis-named).

    At what point do you check to see if someone is "on" the board or not (a piece of information that could be invaluable)? And where's your check that if they roll a six they "come onto" the board?

    Fix those, and then maybe you'll find out why someone rolling a six messes things up.

    P.S. DON'T use srand more than once, don't use it all the time, and don't use it in loops. You're "randomising" dice far too much and doing it every loop (which actually makes it "less random"). Put it at the start of your program and never call it again after that.

    P.P.S. That switch is horrible, but I assume it just takes the place of a snake/ladder by moving you to certain points if you land on, say, square 28 (where it moves you to square 8, etc.). It works, but it's horrible.

    P.P.P.S. Where do you get "null" from, as opposed to "NULL"? Are you sure you've "include"'d all the correct headers so things work? Where do you prototype your function before you use it so you don't get warnings (hint: You misspelled something)?
    Last edited by ledow; 03-25-2013 at 08:51 AM.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    HJH
    HJH is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Thank you for pointing out my mistakes.
    I've tried something a little bit better, but now having problems with the final score of the players. The program shows me the number that the dice rolled, for each player, and when someone throw a 6, they can throw again.
    But the adition of the dice values and the scores doesn't make any sense at all. If the dice rolled for example, a 4 for player 1, it adds a 3 score(the position) value for player 1.
    I can't see what the problem is.
    Is it possible that I used to many "select=getch()" lines in my code tha'ts messing around?
    If I don't put the "select=getch()" lines in the switch in each case, the scores(or in this case the positions) of each player does not match the result of the dice.

    this is what I did this time:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define SpaceBar 32
    
    int player1Score=0, player2Score=0;
    int Dice;
    int switchPlayer;
    char Select;
    char call;
    void throwdice();
    int main(void)
    {
        call = getch();
        srand(time(NULL));
        throwdice();
        switchPlayer = 1;
        do
        {
            system("cls");
            throwdice();
            printf("player %d threw a :  %d\n",switchPlayer, Dice);
            printf("\nPlayer 1 is on position : %d\n",player1Score );
            printf("Player 2 is on position : %d\n", player2Score );
            Select = getch();
            switch (switchPlayer)
            {
                case 1 :
                {
                    throwdice();
                    player1Score = player1Score + Dice;
                    if (Dice <6)
                    {
                        switchPlayer = 2;
                        break;
                    }//end if
                    if (Dice == 6)
                    {
                        printf("\nplayer2 rolled a 6, throw again\n");
                        Select=getch();
                        switchPlayer = 1;
                        throwdice();
                        player1Score = player1Score + Dice;
                    }//end if
                }//end case 1
    
                //start case 2
                case 2:
                {
                    throwdice();
                    player2Score = player2Score + Dice;
                    if (Dice <6)
                    {
                        switchPlayer = 1;
                        break;
                    }//end if
                    if (Dice == 6)
                    {
                        printf("\nplayer1 rolled a 6, throw again\n");
                        Select=getch();
                        switchPlayer = 2;
                        throwdice();
                        player2Score = player2Score + Dice;
                    }//end if
                }//end case 2
            }//end switch
    
            if (player1Score > 100)
            {
                player1Score = player1Score-100;
                player1Score = 100-player1Score;
            }//end if
    
            if (player2Score > 100)
            {
                player2Score = player2Score-100;
                player2Score = 100-player2Score;
            }//end if
    
            if (player1Score==100)
            {
                system("cls");
                printf("player 1 have won");
                break;
            }//end if
    
            if (player2Score== 100)
            {
                system("cls");
                printf("player 2 have won");
                break;
            }//end if
    
        }//end do loop
        while(Select == SpaceBar);
    }//end main
    
    void throwdice()
    {
        int Result;
        Result = (rand()%6)+1;
        Dice = Result;
    }

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by HJH View Post
    But the adition of the dice values and the scores doesn't make any sense at all. If the dice rolled for example, a 4 for player 1, it adds a 3 score(the position) value for player 1.
    I can't see what the problem is.
    Look at this part:

    Code:
        do
        {
            system("cls");
            throwdice();
            printf("player %d threw a :  %d\n",switchPlayer, Dice);
            printf("\nPlayer 1 is on position : %d\n",player1Score );
            printf("Player 2 is on position : %d\n", player2Score );
            Select = getch();
            switch (switchPlayer)
            {
                case 1 :
                {
                    throwdice();
                    player1Score = player1Score + Dice;
    ...
                case 2:
                {
                    throwdice();
                    player2Score = player2Score + Dice;
    You throw the dice and print it's value. But then you throw it again and add the value of the second throw to the score, thus you don't really know what is added to the score.

    Frankly speaking, your code is a big mess.

    Bye, Andreas

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Also, throwdice changes a global variable to provide its result. There's a reason that functions can "return" values.

    I agree that the code is scrappy, but you're trying, and working on it, so keep going. We're all learning.
    Salem likes this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  6. #6
    HJH
    HJH is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Thank you everybody for helping me.
    My program is working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming Dice Display Function?
    By Brian Westerhof in forum C Programming
    Replies: 8
    Last Post: 02-21-2012, 07:11 AM
  2. Dice game: How to handle 2 players and separate totals?
    By crazychile in forum C Programming
    Replies: 7
    Last Post: 10-20-2008, 01:01 AM
  3. Struggling w/ string_replacement function
    By cwafavre in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 08:08 AM
  4. Dice Function..
    By findme in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2005, 10:56 PM
  5. Random function's application:Dice Roller
    By angeljicu in forum C Programming
    Replies: 0
    Last Post: 11-02-2002, 05:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21