Thread: Value order

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    8

    Question Value order

    Hi, I'm writing the program that will be a simulation of Camel Up board game. I found an isue with the part of a game in which the camels can stack on each other. So when there's a camel on field with numer 3 and other camel go on field number 3 it stack on the previous camel. So when the first camel moves to next field, camel that is on stack moves with it. And there is my question. Is there any posibility if I could check order of stepping on the field by camels?

    tl;dr
    if there are x,y and x becomes x=2, then y=2 can I check which one was first to by equal to 2?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    And what do you want us to do about it?
    Without your code, there isn't anything useful we can do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    8
    This is the code, so it is just a simulation of race without camels stacking.




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include <string.h>
    
    
    //zmienne globalne
    int bluefield=0,greenfield=0,orangefield=0,yellowfield=0,whitefield=0;
    
    
    
    
    
    
    
    
    
    
    //kolory kosci 0-niebieski, 1-zielony, 2-pomaranczowy, 3- zolty, 4- bialy
    
    
    int main ()
    {
    
    
    
    
    while(bluefield<17&&greenfield<17&&orangefield<17&&yellowfield<17&&whitefield<17)
     {    //losowanie i rzut koscia
        int colour=rand()%5;
        int die=rand()%3;
            if (colour==0)//jesli niebieski
            { if (die==0){bluefield++; printf ("Wypadła kość niebieska, 1 oczko\n");}
            else if(die==1){bluefield+=2;printf ("Wypadła kość niebieska, 2 oczka\n");}
            else if(die==2){bluefield+=3;printf ("Wypadła kość niebieska, 3 oczka\n");}}
             else if(colour==1)//jesli zielony
              { if(die==0){greenfield++;printf ("Wypadła kość zielona, 1 oczko\n");}
              else if(die==1){greenfield+=2;printf("Wypadła kość zielona, 2 oczka\n");}
              else if(die==2){greenfield+=3;printf("Wypadła kość zielona, 3 oczka\n");}}
                else if(colour==2)//jesli pomaranczowy
                { if (die==0){orangefield++;printf("Wypadła kość pomaranczowa, 1 oczko\n");}
                else if(die==1){orangefield+=2;printf("Wypadła kość pomaranczowa, 2 oczka\n");}
                else if(die==2){orangefield+=3;printf("Wypadła kość pomaranczowa, 3 oczka\n");}}
                  else if(colour==3)//jesli zolty
                  {if (die==0){yellowfield++;printf("Wypadła kość zolta, 1 oczko\n");}
                  else if (die==1){yellowfield+=2;printf("Wypadła kość zolta, 2 oczka\n");}
                  else if (die==2){yellowfield+=3;printf("Wypadła kość zolta, 3 oczka\n");}}
                    else if(colour==4)//jesli bialy
                    {if(die==0){whitefield++;printf("Wypadła kość biala, 1 oczko\n");}
                    else if (die==1){whitefield+=2;printf("Wypadła kość biala, 2 oczka\n");}
                    else if (die==2){whitefield+=3;printf("Wypadła kość biala, 2 oczka\n");}};
    
    
    }
    printf ("blue %i green %i orange %i yellow %i white %i\n",bluefield,greenfield,orangefield,yellowfield,whitefield);
    
    
    
    
    
    
    
    
    
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    If you were using a decent indent style you should be able to see that you have quite bit of code duplication that can be easily removed. I suggest you start by simplifying your logic, you really don't need all of those if/else if() statements a simple calculation or two can get rid of it all.

  5. #5
    Registered User
    Join Date
    Jan 2018
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    If you were using a decent indent style you should be able to see that you have quite bit of code duplication that can be easily removed. I suggest you start by simplifying your logic, you really don't need all of those if/else if() statements a simple calculation or two can get rid of it all.
    A little hint?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    A little hint?
    Okay here you go, your code formatted with a more sensible indent style.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //zmienne globalne
    int bluefield = 0, greenfield = 0, orangefield = 0, yellowfield = 0, whitefield = 0;
    
    
    int main()
    {
        while(bluefield < 17 && greenfield < 17 && orangefield < 17 && yellowfield < 17 && whitefield < 17)
        {  
             //losowanie i rzut koscia
            int colour = rand() % 5;
            int die = rand() % 3;
            if(colour == 0)  //jesli niebieski
            {
                if(die == 0) {
                    bluefield++;
                    printf("Wypadła kość niebieska, 1 oczko\n");
                }
                else if(die == 1) {
                    bluefield += 2;
                    printf("Wypadła kość niebieska, 2 oczka\n");
                }
                else if(die == 2) {
                    bluefield += 3;
                    printf("Wypadła kość niebieska, 3 oczka\n");
                }
            }
            else if(colour == 1)  //jesli zielony
            { 
                if(die == 0) {
                    greenfield++;
                    printf("Wypadła kość zielona, 1 oczko\n");
                }
                else if(die == 1) {
                    greenfield += 2;
                    printf("Wypadła kość zielona, 2 oczka\n");
                }
                else if(die == 2) {
                    greenfield += 3;
                    printf("Wypadła kość zielona, 3 oczka\n");
                }
            }
            else if(colour == 2)  //jesli pomaranczowy
            {   
                if(die == 0) {
                    orangefield++;
                    printf("Wypadła kość pomaranczowa, 1 oczko\n");
                }
                else if(die == 1) {
                    orangefield += 2;
                    printf("Wypadła kość pomaranczowa, 2 oczka\n");
                }
                else if(die == 2) {
                    orangefield += 3;
                    printf("Wypadła kość pomaranczowa, 3 oczka\n");
                }
            }
            else if(colour == 3)  //jesli zolty
            {   
                if(die == 0) {
                    yellowfield++;
                    printf("Wypadła kość zolta, 1 oczko\n");
                }
                else if(die == 1) {
                    yellowfield += 2;
                    printf("Wypadła kość zolta, 2 oczka\n");
                }
                else if(die == 2) {
                    yellowfield += 3;
                    printf("Wypadła kość zolta, 3 oczka\n");
                }
            }
            else if(colour == 4)  //jesli bialy
            {   
                if(die == 0) {
                    whitefield++;
                    printf("Wypadła kość biala, 1 oczko\n");
                }
                else if(die == 1) {
                    whitefield += 2;
                    printf("Wypadła kość biala, 2 oczka\n");
                }
                else if(die == 2) {
                    whitefield += 3;
                    printf("Wypadła kość biala, 2 oczka\n");
                }
            }
        }
        printf("blue %i green %i orange %i yellow %i white %i\n", bluefield, greenfield, orangefield, yellowfield, whitefield);
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2018
    Posts
    8
    Quote Originally Posted by jimblumberg View Post
    Okay here you go, your code formatted with a more sensible indent style.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //zmienne globalne
    int bluefield = 0, greenfield = 0, orangefield = 0, yellowfield = 0, whitefield = 0;
    
    
    int main()
    {
        while(bluefield < 17 && greenfield < 17 && orangefield < 17 && yellowfield < 17 && whitefield < 17)
        {  
             //losowanie i rzut koscia
            int colour = rand() % 5;
            int die = rand() % 3;
            if(colour == 0)  //jesli niebieski
            {
                if(die == 0) {
                    bluefield++;
                    printf("Wypadła kość niebieska, 1 oczko\n");
                }
                else if(die == 1) {
                    bluefield += 2;
                    printf("Wypadła kość niebieska, 2 oczka\n");
                }
                else if(die == 2) {
                    bluefield += 3;
                    printf("Wypadła kość niebieska, 3 oczka\n");
                }
            }
            else if(colour == 1)  //jesli zielony
            { 
                if(die == 0) {
                    greenfield++;
                    printf("Wypadła kość zielona, 1 oczko\n");
                }
                else if(die == 1) {
                    greenfield += 2;
                    printf("Wypadła kość zielona, 2 oczka\n");
                }
                else if(die == 2) {
                    greenfield += 3;
                    printf("Wypadła kość zielona, 3 oczka\n");
                }
            }
            else if(colour == 2)  //jesli pomaranczowy
            {   
                if(die == 0) {
                    orangefield++;
                    printf("Wypadła kość pomaranczowa, 1 oczko\n");
                }
                else if(die == 1) {
                    orangefield += 2;
                    printf("Wypadła kość pomaranczowa, 2 oczka\n");
                }
                else if(die == 2) {
                    orangefield += 3;
                    printf("Wypadła kość pomaranczowa, 3 oczka\n");
                }
            }
            else if(colour == 3)  //jesli zolty
            {   
                if(die == 0) {
                    yellowfield++;
                    printf("Wypadła kość zolta, 1 oczko\n");
                }
                else if(die == 1) {
                    yellowfield += 2;
                    printf("Wypadła kość zolta, 2 oczka\n");
                }
                else if(die == 2) {
                    yellowfield += 3;
                    printf("Wypadła kość zolta, 3 oczka\n");
                }
            }
            else if(colour == 4)  //jesli bialy
            {   
                if(die == 0) {
                    whitefield++;
                    printf("Wypadła kość biala, 1 oczko\n");
                }
                else if(die == 1) {
                    whitefield += 2;
                    printf("Wypadła kość biala, 2 oczka\n");
                }
                else if(die == 2) {
                    whitefield += 3;
                    printf("Wypadła kość biala, 2 oczka\n");
                }
            }
        }
        printf("blue %i green %i orange %i yellow %i white %i\n", bluefield, greenfield, orangefield, yellowfield, whitefield);
    
        return 0;
    }

    Oh you meant to only change style of the code. Yeah that's better, but I still don't know how to solve my problem with camels stacking

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    Oh you meant to only change style of the code.
    No I meant for you to start by using a reasonable indent style, that way you may be able to see what I meant by all of the code duplication that should be removed. Then once you remove all the code duplication perhaps you might be able to see how to solve your "stacking" issue for yourself.

  9. #9
    Registered User
    Join Date
    Nov 2015
    Posts
    72
    I think all those if statements in post #6 could altogether be replaced with the following lines of code:

    Code:
        const *char[] szykKolorowy = { "niebieska", "zielona", "pomaranczowa",
                                        "zolta", "biala"};
        int *fields[] = { &bluefield, &greenfield, &orangefield,
                            &yellowfield, &whitefield };
        *fields[colour] += die + 1;
        printf("Wypadła kość %s, 1 oczk%c\n", szykKolorowy[colour], (die==0)?'o':'a');
    I tried to express 'color array' in Polish but probably failed miserably.
    Last edited by bot; 01-06-2018 at 10:22 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    Perhaps if you started with
    int field[5];

    You could start to use loops rather than copy/paste.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2015
    Posts
    72
    Yes, I too would rather work with vectors and arrays directly myself. If someone somehow however would want to maintain a clear association between each camel color and field, my suggestion above could be one way of doing it.

    I think the first thing to do is to understand more deeply what exactly it is you are trying to program. This is more related to the concepts of the game mechanics rather than the c code itself. Once you are in the clear about what to do, then the programming itself won't be that difficult.

    I researched this "Camel Up" board game and it turns out to have a certain level of complexity that is higher than what one might expect at first. It is based upon the basic board game where a dice is rolled and each player is moved between what we could call "slots". Moreover, players can be stacked on top of each other on the slots which will have consequences on the next round of movements. Also, slots can be modified between rounds which will alter the movement in ensuing rounds. On top of all that, you have a betting system which I don't fully understand.

    I watched a few YouTube clips and have discovered that the following rules apply:

    So there are up to 8 camels depending on how many players are participating.
    There are 16 different slots at which each of these camels can reside.
    At each round, each camel can move 1-3 steps forward, i.e. the dice are 3 sided. Each round of movement is a permutation of dice rolls and is called a "leg". E.g. {red white blue}, {white blue red}, {blue red white}, ... At the very first leg, the camels are placed on slot 1-3 on the board. So they are not really starting off at the first slot.
    As the dice are rolled and the camels are placed, if several camels end up on the same location/slot, they are stacked on top of each other. The one on the top, i.e. the one that got the die rolled last is assumed to be first.
    On the next round, if a camel of a stack gets its die cast, the camels on top of it will move along and stacks of camels will be stacked on top of other stacks of camels. The camels underneath will not move along. They will stay on the slot.


    Between each dice roll, each player can either make bets on which camel will win, or lose, or place a desert/oasis tile into one of the slots on the board. I'm not sure how it is determined which user has this tile in possession, only those who have it can place it. I don't know how the bets are placed, perhaps each player is bound to each camel so the owner of the camel that just moved get to make bets and place the tile. I don't know how many tiles are allowed on the game and how these tiles are distributed between players. I don't know whether bets can be reversed and how those bets are compounded at the end of the race.


    The tile as far as I understand can only be placed on unpopulated slots and on slots that come after the slot of the last camel. If a camel lands on a tiled desert slot, it will have to move backwards one slot and ends up at the bottom of the stack on that slot. If a camel lands on a tiled oasis slot, it will move forward one additional slot and end up on the top of that stack. The desert/oasis tile, is a two-sided tile and the player gets to decide which side of that tile to play .
    It is far from complete as I have not yet understood all of the rules completely.
    Last edited by bot; 01-06-2018 at 11:35 AM.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    Then once you remove all the code duplication
    For example look at this snippet:

    Code:
            if(colour == 0)  //jesli niebieski
            {
                if(die == 0) {
                    bluefield++;
                    printf("Wypadła kość niebieska, 1 oczko\n");
                }
                else if(die == 1) {
                    bluefield += 2;
                    printf("Wypadła kość niebieska, 2 oczka\n");
                }
                else if(die == 2) {
                    bluefield += 3;
                    printf("Wypadła kość niebieska, 3 oczka\n");
                }
            }
    Do you see that the only difference in the print out are those three constants. Do you see that those three constants are based on the "die" value? So to remove that duplication all you have to do is print the value of the die + 1.

    So now you end up with something like:
    Code:
            if(colour == 0)  //jesli niebieski
            {
                printf("Wypadła kość niebieska, %d oczko\n", die + 1);
    
                if(die == 0) {
                    bluefield++;
    
                }
                else if(die == 1) {
                    bluefield += 2;
                }
                else if(die == 2) {
                    bluefield += 3;
                }
            }
    Now look at those if() statements, do you notice that bluefield is incremented by the same factor (die + 1)? So when you simplify you end up with something like:

    Code:
            if(colour == 0)  //jesli niebieski
            {
                printf("Wypadła kość niebieska, %d oczko\n", die + 1);
                bluefield += (die + 1);
            }
    Now if you do this for each of the colour if() statements you should study what each of those if() statements contain. Perhaps you should see some more duplication that can be removed by using a couple of arrays and altering the print statement.

  13. #13
    Registered User
    Join Date
    Nov 2015
    Posts
    72
    This stack could be dynamically allocated (with malloc()) or be fixed which is viable as you know that there will be no more than 8 players. For a fixed stack, you could let the slots be an 8 levels deep array of stacks, i.e. a 16x8 array of integers. To keep track of the population in each stack, you could ether have an additional array of slots telling how many camels are residing in each stack or let it loop until you hit 0 or maximum size of each stack as you iterate through any given stack.


    With that sorted out you can write a routine that sorts the movements out and updates the stacks accordingly.


    At the end of the race, you could just let a routine iterate through the array of stacks backwards and push all players into an array showing which one is first.

  14. #14
    Registered User
    Join Date
    Jan 2018
    Posts
    8
    Thanks a lot, now I see that I can replace that "if"s. I will read more about arrays and will try to figure it out, but thank you for the patience. I'm new to programming, so I don't know what I can do in the code and what I cannot. I want to do the version of the game with 5 players, I do have this board game and it's easier when you see camels moving and you can figure out what has to to happen next. I need some time to read carefully your messages. So when I will know something new and have more questions, I'll be back here in few hours or tomorrow.

  15. #15
    Registered User
    Join Date
    Jan 2018
    Posts
    8
    Quote Originally Posted by bot View Post
    Yes, I too would rather work with vectors and arrays directly myself. If someone somehow however would want to maintain a clear association between each camel color and field, my suggestion above could be one way of doing it.

    I think the first thing to do is to understand more deeply what exactly it is you are trying to program. This is more related to the concepts of the game mechanics rather than the c code itself. Once you are in the clear about what to do, then the programming itself won't be that difficult.

    I researched this "Camel Up" board game and it turns out to have a certain level of complexity that is higher than what one might expect at first. It is based upon the basic board game where a dice is rolled and each player is moved between what we could call "slots". Moreover, players can be stacked on top of each other on the slots which will have consequences on the next round of movements. Also, slots can be modified between rounds which will alter the movement in ensuing rounds. On top of all that, you have a betting system which I don't fully understand.

    I watched a few YouTube clips and have discovered that the following rules apply:



    It is far from complete as I have not yet understood all of the rules completely.
    I skip betting part for now, just want to code the camels movement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. Replies: 4
    Last Post: 03-06-2008, 03:38 PM
  3. Tab order again
    By knutso in forum Windows Programming
    Replies: 9
    Last Post: 07-29-2003, 05:39 PM
  4. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM

Tags for this Thread