Thread: Value order

  1. #16
    Registered User
    Join Date
    Nov 2015
    Posts
    72
    I wouldn't call myself an experienced programmer but I would suggest to break down the complex task into a series of simpler problems. For example you could implement the 16 slots or whatever the number of slots you want to use and let them be a one dimensional array of stacks. Each member of the stack can be a number between 0 and 5 where 0 means that the cell of the stack is unoccupied and 1-5 represents each of the camels. So a stack could be represented by a 1x5 element array. Or you could use a linked list.

    Then you could implement a function that moves a camel in one stack to another. A function that makes sure that camels above it moves along while the ones underneath stay on the old one.

    You could have a vector of the players that keeps track of in which slot each player is located. That way you won't have to search through the 16x5 array for the camel location on each turn.

    You could write a function that pushes a camel on top of the stack, that could be especially useful on the first round. You could also write a function that pushes the camel from the bottom which can be useful if you want to implement a desert tile later.

    While implementing this you could make sure that variables that keeps track of the stack sizes and the players locations are properly updated.

    Then you could write a routine that determines which dice to throw, throws it and then moves the camels using the prior routines that you mentioned. Make sure that e.g. if a camel is on slot 15, the slot prior to the last slot and a 2 or a 3 is retrieved from the dice, then it only moves one step ahead to the final slot. You should group the dice rolls in groups or "legs" as described above and make sure that what is randomized is a permutation so that each camel gets its dice thrown before moving on to the next leg of the race. There may be routines for randperm in cmath.h libs, otherwise you can find a an algorithm to implement in c with google (for example Fisher–Yates shuffle) or improvise one yourself.

    Then you can write a routine that checks whether the race has ended, which I assume happens as soon as at least one of the camels hits the final slot.

    When the race has ended you could write a vector containing say 5 integers. So the first element of the vector is the first player, i.e. the player that won, the second is the player who came to the second place etc. So you can write a routine that starts from the top of the stack of the last slot and iterates backwards through the stacks until it finds the player that ended up as last. As it iterates it pushes them onto this vector of winners from 0 to end.


    So now you have what you need to fill the main routine with tasks that could involve; allocate memory, initialize first move, while(race not ended) do (roll dice, move camels, check for end of race, ...), calculate final results, deallocate memory, exit program...

    This is just a design suggestion, there are many other ways of doing it of course.
    Last edited by bot; 01-06-2018 at 05:01 PM.

  2. #17
    Registered User
    Join Date
    Jan 2018
    Posts
    8
    I'll do my best to write a code from your advice. I'll let you know when I'll do some progress Thanks

  3. #18
    Registered User
    Join Date
    Jan 2018
    Posts
    8
    So that's what I have by now. Next thing to do is which place every camel has after finishing the game and then I can focus on desert tile.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int bluefield=0, greenfield=0, orangefield=0, yellowfield=0, whitefield=0;
    
    
    void stacki(int *fields[], int indeks[], int colour, int die)
    {int i=0;
        for (i=0;i<5;i++)
            { if (i!=colour && indeks[colour]==i)
                {
                    *fields[i]+=die+1;
                    stacki(fields, indeks, i, die);
                    return;
                }
            }
    }
    
    
    
    
    
    
    int main ()
    {
        srand(time(NULL));
    
    
        int indeks[]={-1 , -1, -1, -1, -1};//tak jak w kolorach
    
    
    while(bluefield < 17 && greenfield < 17 && orangefield < 17 && yellowfield < 17 && whitefield <17)
        {
    
    
    
    
            int colour = rand()%5, die = rand()%3;
    
    
           /* printf("Jaki kolor kosci wypadl\n");
            scanf ("%d",&colour);
            printf("Ile oczek wypadlo\n");
            scanf ("%d", &die);*/
    
    
        const char *tablicaKolor[] = {"niebieska", "zielona", "pomaranczowa", "zolta", "biala"};
    
    
    
    
        int *fields[] = {&bluefield, &greenfield, &orangefield, &yellowfield, &whitefield};
    
    
            *fields[colour] += die +1;
        int i=0;
            for (i=0;i<5;i++)
            {
                if (i != colour && indeks[i] == colour) indeks[i]=-1;
            }
    
    
    
    
           i=0;
            for (i=0;i<5;i++)
                {
                    if (i!=colour && *fields[i]==*fields[colour]&& indeks[i]==-1) indeks[i]=colour;
                }
    
    
    
    
            stacki(fields, indeks, colour, die);
    
    
        printf ("Wypadla kość %s, %d oczk%c\n",tablicaKolor[colour], die+1, (die==0)? 'o' : 'a');
        printf ("blue %d green %d orange %d yellow %d white %d  indeks blue %d indeks green %d indeks orange %d indeks yellow %d indeks white %d\n\n",bluefield, greenfield, orangefield, yellowfield, whitefield, indeks[0], indeks[1], indeks[2], indeks[3], indeks[4]);
        }
    
    
    printf("blue %i green %i orange %i yellow %i white %i\n", bluefield, greenfield, orangefield, yellowfield, whitefield);
    
    
    
    
    
    
        return 0;
    }

  4. #19
    Registered User
    Join Date
    Nov 2015
    Posts
    72
    The first thing you should do is to have a consistent style of indentation. You can read more about indentation in programming here:

    Indenting C Programs
    The Only Correct Indent Style | Terminally Incoherent

    That will dramatically improve the readability of your code. I prefer the K&R style of indentation but to each his own.

    You also don't have to go over board with new lines. I usually add a few extra lines so that the main() function is distinguishable from other functions if the file contains many functions. Other people add a big block of comments right before the main() to make it easy to find. Integrated Development Environments such as VisualStudio has functionality built in that makes it easy to find main().

    I just added "*fields[] = ( &bluefield, &greenfield, &orangefield, &yellowfield, &whitefield );" so that you could copy and paste my code directly into yours. It will probably be more convenient to do as Salem says in post 10 and use a vector of the fields without the independent ...field variables. It's up to you anyway.

    Why use two for-loops? I don't understand what you intend with them. What is the purpose of "indeks[]"?

    I don't understand the function stacki() and what you intend to do with it.

    There are many opinions on how much comments you should have in your code. Say that you write this program and end up with say 500 lines of code. Let's say in 5 years from now you have to go back to your code for any given reason and apply necessary modification. At that point you will be punished for your lacking efforts of making it understandable.

    If you're not into commenting the code. The least you can do is to let variables and functions have names that make it easy to understand their purpose and what they are doing. You could also have each procedure in a subfunction. So instead of a long list of code in the main() function, you could have a subfunction for each step with a proper name so it is easy to understand what is happening in that step.

    Recommended naming conventions for variables and functions is "Camel case" and "Hungarian notation". So you can google these up to get a description.

    The functions can also be put after the main() function. You only need to declare the name of the function before main(), e.g. "void stacki(int *fields[], int indeks[], int colour, int die);", then the full function can be put after main(). You can do whichever. I have written programs that both have main() first and programs that have main() last and I have seen code from experienced programmers that have both ways.
    Last edited by bot; 01-09-2018 at 05:16 PM.

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