joe97

Alright here's a rough code of how i started. some where some mistake has happened but not sure where and i wasnt sure how could i declare the functions im asked to use (readCC, isValid and runLuhn)

#include <stdio.h>

int isvalid(long num);
int sumofdoubleevenplace(long num);
int getdigit(int num);
int sumofoddplace(long num);
int prefixmatched(long num,int d);
int getsize(long d);
int getprefix(long num,int k);

main(){
  long cardnum=0;
  printf("Enter credit card number ");
  scanf("%ld",&cardnum);
  if(isvalid(cardnum)==1)
    printf("Valid card number\n");
else
    printf("Invalid card number\n ");
return 0;
}

int isvalid(long num){
if(((sumofoddplace(num)+sumofdoubleevenplace(num))%10==0)
   && (getsize(num)<=16 && getsize(num)>=13)
   && (prefixmatched(num,4)==1 || prefixmatched(num,5)==1 ||
       prefixmatched(num,6)==1 || prefixmatched(num,37==1)))
    return 1;
else
    return 0;
}

int sumofdoubleevenplace(long num){
int numdigits=getsize(num)-1;
int sum=0,i;
num/=10;
for(i=0;i<numdigits;i+=2){
    sum+=getdigit((int)(2*(num % 10)));
    num/=100;
}
return sum;
}

joe97

guys i have this assignment about luhns algorithm and i cant seem to know where to start (i am very weak in functions and arrays) , if anyone can help me out with the start of the i would be very thankful then later on i will be disscussing my doubts. thank you in advance.

Credit cards use a system of blocked numbers similar to the ISBN. The first digit of a
credit card number is the Major Industry Identifier (MII) and identifies which group
issued the card. The next block of numbers is the issue identifier. Including the MII, the
Issue Identifier is 6 digits long. The account number begins with the 7th digit and ends
with the next-to-last digit. The final digit is the check digit.
The process used to calculate the check digit is the Luhn algorithm1
(mod 10
algorithm), named after IBM scientist Hans Peter Luhn. The algorithm is in the public
domain and is used to validate Canadian social insurance numbers.
It is designed to protect against accidental errors, such as single transpositions of
adjacent digits although it won’t detect transposition of two-digit sequences. This
algorithm works as follows:
• Double all even-positioned digits when counting from right to left.
• Determine the sum of the undoubled digits from the products (Step 1) and each of
the unaffected (odd-positioned) digits in the original number.
• Verify the account number by determining if the sum from step 2 is a multiple of 10.
For example consider ...

joe97

ohh alright, ill try modifying my code and see how it runs. but thanks a lot David, ill surely refer to this website whenever i need any help.

joe97

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

int takeInInt( const char* msg );
int takeInChr( const char* msg );
int more();

void showDice( int randNum );
void playGame();

int main()
{
    do
    {
        playGame();
    }
    while( more() );

    return 0;
}

void playGame()
{
    int gameTotal = 0, playerOne = 0, playerTwo = 0;
    char choice = 'p'; /* start with player one */

    srand( time(0) );

    printf( "Welcome to the  Game of Pig\n");

    gameTotal = takeInInt( "What is the game total? ");

    while( playerOne < gameTotal && playerTwo < gameTotal )
    {

        char roll;
        if( choice == 'p' )
        {
            printf( "It's player one's turn\n");
            do
            {
                roll = takeInChr( "Do you want to roll or hold (r/h) ? " );
            }
            while( roll != 'r' && roll != 'h' );

            if( roll == 'r' )
            {
                int value = rand() % 6 +1;
                showDice( value );
                playerOne += value;
                if( value == 1 )
                {

                    choice = 'c';
                    printf( "It's player two's turn\n" );

                }

        } 
        else
        {
            printf( "Alright, hold it is \n" );
            choice = 'c';
        }
    }
    else
    {           
            int value = rand() % 6 +1;
            takeInChr( "Do you want to roll or hold (r/h) ?" );
            playerTwo += value;
            showDice( value );
            if( value == 1 )
            {
                choice = 'p';
            }
        }

        printf( "Player one has %d and player two has %d\n", playerOne, playerTwo );
    }

     if( playerOne < playerTwo )          

    {
            if( playerTwo >= gameTotal )

                printf( "player one wins! %d\n", playerOne ); ...

joe97

thank you so much for the help! the game runs smoothly.

joe97

im not sure this is whats asked. the rule is if a player rolls a value between 2 and 6 t is added to their score, and they can roll again. if they roll 1, they lose their turn and add nothing to their score. At any point after rolling a 2-6, the player may choose to hold, which ends their turn, and adds their turn total to their score. the winner is the first player to get a score greater than or equal to the total.

my when my code runs, this all is implemented except for the part where if any of the players roll a 1 (not 2, 4 or 6) they lose their turn and nothing is added to their score.

what could be the code to for that action to happen?

joe97

forget what i mentioned above.. i made some changes and the program ran somewhat fine. but i missed the part where if a player rolls a 1, the score should not be added to his total so it'll be the second players to roll. i couldnt seem to get that command to work so far; any idea?

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

void showDice( int randomNumber );
void playGame();

int takeInChr( const char* msg );
int more(); /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */

int takeInInt( const char* msg );
int main()
{
    do
    {
        playGame();
    }
    while( more() );
    return 0;
}
void playGame()
{
    int gameTotal = 0, playerOne = 0, playerTwo = 0;
    char choice = 'p'; /* start with player one */
    srand( time(0) ); /* seed rand... */
    printf( "Welcome to the GAME of PIG\n" );
    gameTotal = takeInInt( "Enter the game total (for example 40): " );
    while( playerOne < gameTotal && playerTwo < gameTotal )
    {
        char roll;
        if( choice == 'p')
        {
            printf( "It's player one's turn\n" );
            do
            {
                roll = takeInChr( "Your choice roll or hold (r/h) ? " );
            }
            while( roll != 'r' && roll != 'h' );
            if( roll == 'r' )
            {
                int value = rand() % 6 + 1;
                playerOne += value;
                showDice( value );
            }
            else
            {
                printf( "Ok ... hold it is\n" );
                choice = 'c'; /* playerTwo */
            }
        }
        else
        {
            int value = rand() % ...

joe97

thanks a lot David, but it gives me a warning:

too many arguments for format [-Wformat-extra-args]

printf( "player wins! %d\n", player,  gameTotal );

too many arguments for format [-Wformat-extra-args]

printf( "computer wins! %d\n", computer,  gameTotal );

In function 'showDice':
warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]

printf( "|    0    |\n");

in function 'showDice' :
undefined reference to 'printf'

any idea how i can tackle these problems because it still cant seem to compile?

joe97

here's what ive done so far, any assistance would be appreciated. my code cant seem to complie for some reason i cant figure out whats wrong. any what is the next step i should do ater printing the dice?

P.S (i am very bad at coding and i know my sheet is filled with mistakes so sorry if its gonna give you a hard time)

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

int gamePlay()

int main(void)
{
    gamePlay();

    return 0;
}

{
    int gameTotal = 0;
    char PlayerSelection;

    printf("Welcome to the game of pig\n");
    printf("What is the game total?");
    scanf("%d", &gameTotal);

    printf("It's player one's turn\n");
    printf("Would you like to (r)oll or (h)old? ")
    scanf("&c", &playerSelection);
    printf("c\n", playerSelection);

    }
    if(playerSelection = 'r');
    {
    void showDice(int randomNumber);
    int showDice(randomNumber);

    }
    switch(randomNumber)    
{   
    if(randomNumber == 1);
    {
        printf("+--------+\n")
        printf("|        |\n")
        printf("|    0   |\n")
        printf("|        |\n")
        printf("+--------|\n")
    }   

    if(randomNumber == 2);
    {
        printf("+--------+\n")
        printf("| 0      |\n")
        printf("|        |\n")
        printf("|    0   |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 3);
    {
        printf("+--------+\n")
        printf("| 0      |\n")
        printf("|    0   |\n")
        printf("|      0 |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 4);
    {
        printf("+--------+\n")
        printf("|  0   0 |\n")
        printf("|        |\n")
        printf("|  0   0 |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 5);
    {
        printf("+--------+\n")
        printf("| 0    0 |\n")
        printf("|    0   |\n")
        printf("| 0    0 |\n")
        printf("+--------|\n")
    }

    if(randomNumber == 6);
    {
        printf("+--------+\n")
        printf("| 0  0  0|\n")
        printf("|        |\n")
        printf("| 0  0  0|\n")
        printf("+--------|\n")
    }
     return 0;
     }

joe97

Pig is a simple dice game first described by John Scarne in 19451. It is a form of
jeopardy dice game, where a players decisions are between protecting current gains,
or risking current gains to make greater gains.
In Pig, each player takes a turn rolling a 6-sided dice, playing to reach a certain total
(typically 100). If a player rolls a value between 2 and 6 it is added to their score, and
they can roll again. If they roll a 1 (a “pig”), they lose their turn and add nothing to
their score. At any point after rolling a 2-6, the player may choose to to hold, which
ends their turn, and adds their turn total to their score. The winner is the first player to
get a score greater than or equal to the total.
When looking at the simple rules of the game, one can surmise that the game involves
some form of repetition to deal with the different turns of the players, and decision
statements to deal with the choice of roll or hold. The game also involves a random
number generator to simulate the dice. The game has two players, so an algorithm
may perform like this (starting with one player):

  1. Ask the player whether they wish to roll or hold?
    1.1. If roll is chosen, use the random number generator to generate a number
    between 1 and 6.
    1.1.1. If the number “rolled” is 1, go to step 2.
    1.1.2. If the ...