redefinition of input

This is a discussion on redefinition of input within the C Programming forums, part of the General Programming Boards category; I am trying to make a poker scoring code and am struggling a bit with the 'picture cards'. The user ...

  1. #1
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67

    redefinition of input

    I am trying to make a poker scoring code and am struggling a bit with the 'picture cards'. The user inputs the hand
    for example 3c 7d 9h js ad
    I have worked out nearly all of the source code, except straights that include jack queen king ace.
    Is there anyway that when a user types in J the code will recognize it as 11, then queen as 12, etc etc. So from there I could still use the same code I have already made. Thank you for your help.
    Here is the part of my code that works out the straight.
    Code:
    #include<stdio.h>
    int main(void)
    {   int c;
        int n;
        int count1[127] = {0};
        int count2[127] = {0};
        int count3 = {0};
        int i;
        int a;
        int temp;
        int j;
        int difference; 
        char array[5];
        char array1[5];
        n = 5;
         
         
        for (c = 0; c < n; c++) 
            scanf("%c%c ", &array1[c], &array[c]);
            getchar();
        for (i=0;i<n; i++)
        {
        for (i=0; i<4; i++)
       {
          for (j=i+1; j<5; j++)
          {
             if(array1[i] > array1[j]) {
                temp = array1[i];
                array1[i] = array1[j];
                array1[j] = temp;
             }
          }   
       }
        
       for(i=0;i<n-1;i++) 
        {
        difference=(array1[i+1]-array1[i]);
           {if ( difference == 1)   
            count3++;
           }  
        
       }
        if ( count3 == 4)
           printf("Straight ");

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Code:
             if(array1[i] > array1[j]) {
                temp = array1[i];
                array1[i] = array1[j];
                array1[j] = temp;
             }
    This only swaps the rank, not the suit.
    So swapping 3c with 7d results in 7c and 3d

    Use a struct to keep related data together.
    Code:
    struct card {
        char rank;
        char suit;
    };
    Then you can swap hand[i] and hand[j] (where hand is an array of card), using the method you have at the moment.

    As for turning J into 11 etc, consider a separate function which reads a letter and returns a number.
    Code:
    if ( ch == 'J' ) rank = 11;
    // and so on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I understand what you have mentioned above and am working on correcting that. However I am a bit confused what you have wrote inside the if statment, ( ch == 'j' ). What is ch?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    ch is the generic name (like foo) for the char you just read with scanf() or getchar()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I am so very close to finishing my programming but I still get the source to certain input from the user as different numbers. so when a user types in J the code will recognize it as 11, then queen as 12, etc etc. Any help will be greatly appreciated

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,051
    Save yourself the headaches and make some conversion functions to repesent cards as integers. Example:

    Code:
    int ch2rank(char chrank) {
        switch(toupper(chrank)) {
        case 'A': return 1;
        case '1': return 1;
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
        case '0': return 10;
        case 'J': return 11;
        case 'Q': return 12;
        case 'K': return 13;
        }
        return 0;
    }
    
    int ch2suit(char chsuit) {
        switch(toupper(chsuit)) {
        case 'S': return 1;
        case 'H': return 2;
        case 'D': return 3;
        case 'C': return 4;
        }
        return 0;
    }
    How would you use the functions? Convert some characters into the appropriate values:

    Code:
    printf("Enter a card in the format RS (R: rank, S: suit).\n");
    char chrank, chsuit;
    scanf(" %c %c", &chrank, &chsuit);
    int rank = ch2rank(chrank);
    int suit = ch2suit(chsuit);
    printf("%c%c: --> (%d, %d)\n", chrank, chsuit, rank, suit);
    Simply repeat this process for all cards entered. For example, given the input 3c 7d 9h js ad, the output is:

    3c: --> (3, 4)
    7d: --> (7, 3)
    9h: --> (9, 2)
    js: --> (11, 1)
    ad: --> (1, 3)
    Last edited by c99tutorial; 01-23-2013 at 01:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. redefinition
    By goran00 in forum C Programming
    Replies: 6
    Last Post: 03-21-2008, 12:05 PM
  2. Redefinition of variables.
    By therealwill in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2006, 10:30 PM
  3. template redefinition
    By Mortissus in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2005, 09:50 AM
  4. WHAT redefinition?
    By CodeMonkey in forum C++ Programming
    Replies: 11
    Last Post: 02-09-2003, 05:35 PM
  5. redefinition of an int
    By The Gweech in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2002, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21