Gambling Game Code Implementation in Matlab and C - Simulation

No comments

Gambling Game Code Implementation in Matlab and C - Simulation


Problem:

Write the gambling game code in Matlab.

Logic / Algorithm for Gambling game code:

Initialize head = 0, tail = 0, difference = 0
while (1)
toss the coin and make value from 0 to 10 using reminder
  if (toss > 4)
     tail = tail + 1
  else
     head = head + 1
  take difference = head - tail
  if (difference == 3)
    result = 8 - upto_time
    if (result < 0)
       lose
    else
       won
  else

Gambling game Code in Matlab

clc;
n = input('How many time you want to play the game : ');

difference = 0;
tail = 0;
head = 0;

fprintf('\n---------------------------------------------------------------\n');
fprintf('Game No.\tSl No.\tRandom Number\tHeads\tTails\tDifference');
fprintf('\n---------------------------------------------------------------\n');

for i = 1:n
    j = 1;
    
    while 1
        toss = int8((10-0)*rand(1) + 0); %Generate a random number between 0 to 10%
        
        if (toss > 4)
            tail = tail + 1;
        else
            head = head + 1;
        end
        
        difference = abs((head - tail));
        fprintf('\n\t%d\t\t%d\t\t\t%d\t\t\t%d\t\t%d\t\t%d\t\t%d', i, j, toss, head, tail, difference);
        
        if(difference == 3) %The rules of the Gambling - If difference = 3 then Count the lose or won coins
            result = 8 - j;
            if(result < 0)
                fprintf('\n\t\tYou lose %d coins\n', abs(result));
            else
                fprintf('\n\t\tYou won %d coins\n', abs(result));
            end
            break;
        end
        j = j + 1;
    end % End while
end  % End For



Output Gambling game Code in Matlab

Gambling Game Code Implementation in Matlab and C - Simulation




Gambling game Code in C Language

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int tail,head,i,n,j,difference=0,result,toss;
    printf("How many time you want to play ? ");
    scanf("%d",&n);
    printf("\n");

    tail = 0;
    head = 0;

    printf("Serial \t Head: \t Tail: \t Difference: \t\n");

    for(i=1; i<=n; i++)
    {
        j = 1;


        while(1)
        {
            // taking random number
            toss = rand() % 10;
            if(toss > 4)
            {
                // if(5,6,7,8,9) then tail will increase
                tail = tail + 1;
            }
            else
            {
                // if(,1,2,3,4) then head will increase
                head = head + 1;
            }
            // difference between head and tail
            difference = abs((head - tail));
            printf("%d\t%d\t%d\t%d\n",j,head,tail,difference);
            if(difference == 3)
            {
                // counting result suppose the difference match after 10 step then difference = 8-10=2
                result = 8 - j;
                if(result < 0)
                {
                    // if answer negative then you are in lose this is the amount of losed coin
                    printf("You lose %d Coin\n",abs(result));
                }
                else
                {
                    // if answer positive then you are in benefit this is the amount of benefited/win coin
                    printf("You win %d Coin\n",abs(result));
                }
                break;
            }
            // increament the count like serial number
            j++;
        }
    }
    return 0;
}




Gambling game Code in C Language Output

Gambling Game Code Implementation in Matlab and C - Simulation


Tags:

Gambling Game Code Implementation in Matlab and C - Simulation, Gambling Matlab code, Gambling game C code, gambling game code implementation, gambling game

No comments :

Post a Comment