Console based Dice Game Code C

A Console based turn based dice rolling game. The player with highest number on dice wins. If both have same dice then the game draws.

CODE:

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

#define TOTAL_PLAYERS 2

int rollDices(int sides);
int tossCoin();
void exitMessage();
void invalidOp();
void playerDiceOptions(int tc, int sides);

struct player
{
    char name[100];
    bool played;
    int dice;
} p[TOTAL_PLAYERS];


int main()
{
    int sides = 6;
    char tcAgree = '\0';


    for(int i=0; i<TOTAL_PLAYERS; i++)
    {
        printf("Enter Player %d name:\n", i+1);
        scanf("%s", &p[i].name);
    }


    int tc = tossCoin();
    printf("Toss COIN: (Y/N)?\n");
    getchar();
    tcAgree = getchar();


    printf("\n");
    if(tcAgree - 'y'==0|| tcAgree - 'Y'==0)
    {
        printf("Player %d \"%s\" will play first\n", tc+1, p[tc].name);
        p[tc].played = true;
    }
    else if(tcAgree - 'n'==0 || tcAgree - 'N'==0)
    {
        exitMessage();
        return 0;
    }
    else
    {
        invalidOp();
        return 0;
    }

    playerDiceOptions(tc, sides);


    //invert the player
    if(tc==0)
        tc=1;
    else
        tc=0;

    printf("\n");
    for(int i=0; i<TOTAL_PLAYERS; i++)
    {
        if(!p[i].played)
        {
            printf("Player %d \"%s\" will play\n", i+1, p[i].name);
            playerDiceOptions(tc, sides);
        }
    }

    printf("\n");
    Sleep(2000);

    int k=0;
    if(p[k].dice>p[k+1].dice)
        printf("Player %d \"%s\" wins\n", k+1, p[k].name);
    else if(p[k].dice<p[k+1].dice)
        printf("Player %d \"%s\" wins\n", (k+1)+1, p[k+1].name);
    else
        printf("the game is a draw\n");

    return 0;
}

int rollDices(int sides)
{
    int dice1 = rand() % sides + 1;
    return dice1;
}

int tossCoin()
{
    srand(time(NULL));
    return rand() % 2;
}

void exitMessage()
{
    printf("Exiting.....");
    Sleep(2000);
}

void invalidOp()
{
    printf("Invalid Operation\n");
}

void playerDiceOptions(int tc, int sides)
{
    char rollAgree = '\0';

    printf("Roll dice: (Y/N)\n");
    getchar();
    rollAgree = getchar();
    if(rollAgree - 'y'==0|| rollAgree - 'Y'==0)
    {
        p[tc].dice = rollDices(sides);
        printf("Player %d \"%s\" got on dice: %d\n", tc+1, p[tc].name, p[tc].dice);
    }
    else if(rollAgree - 'n'==0 || rollAgree - 'N'==0)
    {
        printf("I get it you don't want to play\n");
        exitMessage();
        return 0;
    }
    else
    {
        printf("Invalid operation\n");
        return 0;
    }
}
Advertisements

Factorization Code C

Sample Input:

48
18

Sample Output:

1 * 48
2 * 24
3 * 16
4 * 12
6 * 8
1 * 18
2 * 9
3 * 6

CODE:

#include<stdio.h>
#include<math.h>

int main(){
   int n=0;

   printf("Enter number to find factorization:\n");
   scanf("%d", &n);

   printf("Factorization:\n");
   for(int i=1; i<=sqrt(n); i++){
      if(n%i==0)
          printf("%d * %d\n", i, n/i);
   }
   return 0;
}

Prime Number Check Primality Test Code C

Sample Input:

8009
800
-99
1
0

Sample Output:

Prime
Not Prime
Not Prime
Not Prime
Not Prime

CODE:

#include<stdio.h>
#include<math.h>

int main()
{
    int i,j,n;
    i=j=n=0;

//
    printf("Enter a number to check if prime:\n");
    scanf("%d", &n);

//
    if(n<=1)
    {
        printf("Not prime\n");
        return 0;
    }

    for(i=2; i<=sqrt(n); i++)
    {
        if(n%i==0)
        {
            printf("Not prime\n");
            return 0;
        }
    }

    printf("prime\n");

    return 0;
}

Console based Static Clock with Structure C

Create a 24 hour clock with struct that takes hour, minutes, seconds as input. First check if inputs are valid. The output is 1 second plus the input. Input format is (H M S) hour, minute then second.

Input:

2 3 9
4 2 59
8 59 59
23 59 59

Output:

2 : 3 : 10
4 : 3 : 0
9 : 0 : 0
0 : 0 : 0

CODE:

#include<stdio.h>

struct time{
 short _h, _m, _s;
} t;

int main(){
   while(1){
       printf("Enter Hour, Minute, Second:\n");
       scanf("%d %d %d", &t._h, &t._m, &t._s);
       if(t._h<0 || t._h>23 || t._m<0 || t._m>60 || t._s<0 || t._s>60)
           printf("\nWrong input try again!\n\n");
       else break;
       printf("\n");

       if(++t._s == 60)
           t._s = 0;
       if(++t._m == 60)
           t._m = 0;
       if(++t._h == 24)
           t._h = 0;
    }
    printf("%d : %d : %d\n", t._h, t._m, t._s);
    return 0;
}

Hexadecimal to Decimal Conversion Code C

Sample Input:

F7B

Sample Output:

3963

CODE:

#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
 int sum,x;
 sum=x=0;
 char bin[100]={'\0'};

 gets(bin);
 int len = strlen(bin);
 x = len-1;

 for(int k=0; k<len; k++){
   if(bin[k]-'0'>=0 && bin[k]-'0'<=9){
     sum += (bin[k]-'0') * pow(16, x);
     --x;
   }else if(bin[k]>='A' && bin[k]<='F'){
     sum += (bin[k]-55) * pow(16, x);
     --x;
   }else{
     printf("Invalid input");
     return 0;
   }
 }

 printf("%d", sum);
 return 0;
}

Octal to Decimal Conversion Code C

Sample Input:

147

Sample Output:

103

CODE:

#include<stdio.h>
#include<string.h>;
#include<math.h>;
#include<stdbool.h>;

int main(){
   int sum,x;
   sum=x=0;
   char bin[100]={'\0'};
   bool valid = true;

   gets(bin);
   int len = strlen(bin);
   x = len-1;

   for(int k=0; k<len; k++){
       if(bin[k]-'0'>=0 && bin[k]-'0'<=7){
          sum += (bin[k]-'0') * pow(8, x);
          --x;
       }else
          valid = false;
   }

   if(valid)
       printf("%d", sum);

   return 0;
}


	

Binary to Decimal Converter Code C

Sample Input:

11111

Sample Output:

31

CODE:

#include<stdio.h>
#include<string.h>
#include<math.h>

int main(){
   int sum,x;
   sum = x = 0;
   char bin[100]={'\0'};

   printf("Enter binary digit:\n");
   gets(bin);
   int len = strlen(bin);

   for(int k=len-1; k>=0; k--){
           if(bin[k]-'0'==1 || bin[k]-'0'==0){
           sum += (bin[k]-'0') * pow(2, x);
           ++x;
       }else{
           printf("\nError\n");
           return 0;
       }
   }

   printf("Decimal: %d", sum);
   return 0;
}