Thread: Simple yatzee game

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    18

    Simple yatzee game

    I made a function generating 5 random numbers into an array. After that I made an multi-dimensional array and stored dices in six diffrent char. Now im trying to use a switch case to decide which 5 dices to print out depending on which 5 numbers was generated into the first array.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void kastar (int kast2[5]);
    
    void generate_random_numbers(int kast2[]);
    
    int main()
    {
    
    int kast2[5];
    
    
    
    
    srand((unsigned) time( NULL ) );
    
    generate_random_numbers(kast2);
    
    
    
    
    kastar(kast2);
    
    
    
        return 0;
    }
    
    void generate_random_numbers(int kast2[])
    {
        int k;
        for(k=0;k<5;k++)
    
    kast2[k] = rand() % 6 + 1;
    
    
    }
    
    
    
    void kastar (int kast2[5])
    {
        int k, rad,kolumn;
    
        char one [3][6]={{' ',' ',' '},{' ','*',' '},{' ',' ',' '}};
        char two [3][6]={{' ',' ','*'},{' ',' ',' '},{'*',' ',' '}};
        char three[3][6]={{'*',' ',' '},{' ','*',' '},{' ',' ','*'}};
        char four[3][6]={{'*',' ','*'},{' ',' ',' '},{'*',' ','*'}};
        char five [3][6]={{'*',' ','*'},{' ','*',' '},{'*',' ','*'}};
        char six [3][6]={{'*',' ','*'},{'*',' ','*'},{'*',' ','*'}};
    for(rad=0;rad<3;rad++)
        {
    for(kolumn=0;kolumn<6;kolumn++)
    switch(kast2[k])
    {
    case 1: printf("%c", one   [rad][kolumn]); break;
    
    case 2: printf("%c", two   [rad][kolumn]); break;
    
    case 3: printf("%c", three [rad][kolumn]); break;
    
    case 4: printf("%c", four  [rad][kolumn]); break;
    
    case 5: printf("%c", five  [rad][kolumn]); break;
    
    case 6: printf("%c", six   [rad][kolumn]); break;
    
    }
    
        }
    
    }
    My program just crash whenever I start it.

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    228
    This is what I get when trying to compile your code with the warnings turned on (-Wall):

    Code:
    test.c: In function ‘kastar’:
    test.c:41:16: warning: ‘k’ may be used uninitialized in this function [-Wmaybe-uninitialized]
        switch(kast2[k]) {
                    ^
    I bet that's what crashes your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write a Yatzee Program
    By newbi in forum C Programming
    Replies: 5
    Last Post: 10-17-2012, 11:03 PM
  2. Help with C++ on a simple game
    By Luminous Lizard in forum Game Programming
    Replies: 24
    Last Post: 05-25-2011, 07:18 AM
  3. A Simple Game
    By lukec2 in forum C++ Programming
    Replies: 9
    Last Post: 03-11-2011, 11:34 PM
  4. help with a simple game..
    By ninne in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2006, 05:48 AM
  5. A simple game using RNG
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 11:05 AM