mastermind

This is a discussion on mastermind within the C Programming forums, part of the General Programming Boards category; HALLO i need to write a " mastermind game " and i have some problems with the functions ...hope your ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    mastermind

    HALLO
    i need to write a " mastermind game " and i have some problems with the functions ...hope your can help me..
    Code:
    #define DIGITS 4
    #define LINES_IN_PAGE 20
    #define NUMBERS_IN_ROW 9
    #define ROWS 10
    #define COLS 1000
    #define STR_CHECK "CHECK"
    #define STR_ALL "ALL"
    #define STR_QUIT "QUIT"
    
    #include <stdio.h> //for printf, scanf
    #include <stdlib.h> //for system
    #include <ctype.h> //for isupper
    
    /***************************************************************/
    /********** Functions/Functions-Declarations section *********/
    /***************************************************************/
    int MakeValidsMatrix(int Matrix[][COLS]); //make valid nums matrix
    void DisplayAllValids(int Matrix[][COLS]); //show the nums
    int check(int NumToCheck); // check if the nums are ok
    int ColWithMostValids(int Matrix[][COLS]); //    return the j with most valids nums       
    int TotalInCol(int Matrix[][COLS],int col); // return = of all the j
    void GetRequest(char strInp[]); //takes char with the user requeset
    int CheckOne(); //  take num and say if its ok
    int Same(char str1[],char str2[]); // check 2 chars 
    
    
    /***************************************************************/
    /* ,התכנית מייצרת את כל המספרים הרלוונטיים למשחק בול פגיעה */
    /* .כלומר כל המספרים בני 4 ספרות ללא ספרות חוזרות */
    /* .השיטה: מעבר על כל המספרים הקטנים מ-10000 ובדיקת חוקיותם */
    /* .התוצאה מוכנסת למטריצת תוצאות - 1 עבור מספר חוקי ו0- אחרת */
    /***************************************************************/
    main()
    /***************************************************************/
    { 
    char strRequest[256];
    int MatrixOfValids[ROWS][COLS]={0},most;
    do
    {
    //קליטת בקשת המשתמש
    GetRequest(strRequest);
    //ביצוע הבקשה
    if (Same(strRequest,STR_CHECK))
    {
    //============================
    //בדיקת מספר אחד
    //============================
    if (CheckOne()) 
    printf("\n\t<<<<<<<< VALID >>>>>>>>\n");
    else
    printf("\n\t<<<<<<<< NOT VALID >>>>>>>>\n");
    }
    else
    {
    if(Same(strRequest,STR_ALL))
    {
    //============================
    //טבלה שלמה
    //============================
    // יצירת מטריצת חוקיים והצגת כמות החוקיים 
    printf("\nTotal Valid Numbers:\t%d\n",MakeValidsMatrix(MatrixOfValids));;
    // הצגת המספרים
    DisplayAllValids(MatrixOfValids);
    // הצגת מספר העמודה שמכילה הכי הרבה מספרים תקניים,וסכום התקניים בעמודה זו
    printf("\nColumn %d has most valids (%d)\n\n",
    most=ColWithMostValids(MatrixOfValids),TotalInCol(MatrixOfValids,most));
    }
    }
    }while(!Same(strRequest,STR_QUIT));
    }
    
    void GetRequest(char strInp[]) //קולטת מחרוזת עם בקשת המשתמש
    {
    char strRequest[256]={0};
    
    printf ("please enter:\n CHECK\n ALL\n QUIT\n");
    fflush(stdin);
    gets(strRequest);
    
    }
    
    
    int Same(char str1[],char str2[]) // בודקת שוויון מחרוזות 
    {
    char strRequest[256]={0};
    int same=0 ;
    int i;
    strcmp (strRequest,STR_CHECK);
    
    //if (strcmp==0) 
    // same=1 ;
    // return same; 
    
    }
    
    int CheckOne() // קולטת מספר ומודיעה אם חוקי
    { 
    char arr[4];
    int z;
    int flag=0;
    
    printf (" please enter 4 digits num:\n ");
    gets (arr); 
    
    for (z=1;z<arr;z++)
    {
    if (arr[0] == arr[z])
    flag=1 ;
    }
    for (z=2;z<arr;z++)
    {
    if (arr[1] == arr[z])
    flag=1 ;
    }
    for (z=3;z<arr;z++)
    {
    if (arr[2] == arr[z])
    flag=1 ;
    }
    return flag;
    }
    int MakeValidsMatrix(int Matrix[][COLS]) //מייצרת מטריצת מספרים תקינים
    {
    int i,j,y;
    char dits[4];
    int flag2=0 ;
    int MatrixOfValids[ROWS][COLS]={0};
    int counter=0;
    
    for (i=0;i<ROWS;i++)
    {
    for (j=0;j<COLS;j++)
    
    { 
    dits[4]=i*1000+j ;
    
    
    for (y=1;y<4;y++)
    {
    if (dits[0] == dits[y])
    flag2=1 ;
    }
    for (y=2;y<dits;y++)
    {
    if (dits[1] == dits[y])
    flag2=1 ;
    }
    for (y=3;y<dits;y++)
    {
    if (dits[2] == dits[y])
    flag2=1 ;
    }
    if (flag2==1)
    MatrixOfValids[ROWS][COLS]=0;
    else 
    MatrixOfValids[ROWS][COLS]=1;
    counter++;
    
    }
    }
    return counter ;
    } 
    
    void DisplayAllValids(int Matrix[][COLS]) //מציגה את המספרים
    { 
    int c,d;
    
    for (c=0;c<ROWS;c++)
    {
    for (d=0;d<COLS;d++)
    
    { printf ("MatrixOfValids[c][d]");
    }
    printf("\n");
    }
    }
    
    int ColWithMostValids(int Matrix[][COLS]) //מחזירה מספר עמודה עם הכי הרבה מספרים תקניים 
    {
    int a,b,i;
    int counter2=0 ;
    int mem=0;
    int MatrixOfValids[ROWS][COLS];
    
    for (b=0,counter2=0;b<COLS;b++)
    { 
    for (a=0;a<ROWS;a++)
    {
    if (MatrixOfValids[a][b]==1)
    counter2++;
    }
    
    if (mem<counter2)
    mem=b ;
    
    }
    return mem;
    }
    
    int TotalInCol(int Matrix[][COLS],int col)// מחזירה סכום עמודה מבוקשת
    {
    int a,b,i;
    int counter2=0 ;
    int mem=0;
    int MatrixOfValids[ROWS][COLS];
    
    for (b=0,counter2=0;b<COLS;b++)
    { 
    for (a=0;a<ROWS;a++)
    {
    if (MatrixOfValids[a][b]==1)
    counter2++;
    }
    
    if (mem<counter2)
    mem=counter2 ;
    
    }
    return mem;
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    i have some problems with the functions ...hope your can help me..
    Sure...but you actually forgot to ask something... state your problem with what you expect and what you get (or not)...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    Also, learning how to indent code (or at least post indented code) would be a big plus.
    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.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    HALLO AGAIN
    sorry about the the problems...its my first time i am in here for help.

    do its like this:

    if the user type :

    check- the user is need to press anumber of 4 digits..
    there will preform if the number is legel..

    legel number is :
    4 dig number...with no same num retreat;

    legel numbers: 1234 2703 0485
    ilegel numbers: 123 12345 1232




    all- (0 or 1) true or false parmaters will write into the matrix
    all the legel nums should be shown
    the number of the column with most legel numbers should be shown


    quit- should quit



    the function - MakeValidsMatrix gets matrix (with 0 inside) of ROWS(10)
    (1000)COLS and puts 1 were there is fitting a legel number ..

    example:

    the number 1234 is legel so..Matrix[1][234] gets 1.

    the number 0595 is ilegel so..Matrix[0][595] stay with 0.


    i get amission to write only the 8 fucnctions....so the problem is over there

    my first problem is that after the menu screen
    if i write no matter what... it ask me to enter 4 numbers..


    thanx for all the helpers and for your posts....i appraiser you...
    sorry for the deficient spelling....

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you plan to get the typed string out of the GetRequest function?

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    by the function:

    [code]
    int Same(char str1[],char str2[])
    {
    char strRequest[256]={0};
    int same=0 ;
    int i;
    strcmp (strRequest,STR_CHECK);



    i campare tham... and if its same its need to do the option


    just to tell you one more time...i get the code ready with the prototypes..i just need to
    do/write the functions.....i cannot change the code...(void main)...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's ... interesting. That's the function Same, which is completely and totally irrelevant to the question I asked. I asked whether you were planning to write the function GetRequest in such a way that it works (i.e., that you would be able to see the typed-in string).

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    i am sorry..but my english is not that good...so there will be some missunderstanding..
    by this function:
    Code:
    void GetRequest(char strInp[])      //קולטת מחרוזת עם בקשת המשתמש
    {
     char strRequest[256]={0};
    
    printf ("please enter:\n  CHECK\n  ALL\n  QUIT\n");
    fflush(stdin);
    gets(strRequest);
    
    }
    whats wrong about that?....the user type string that get into stRequest...

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Code:
    gets(strRequest);
    Elysia will be here in a second.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    fflush(stdin);
    And so will I :-)

    See the FAQ. fflush(stdin) is wrong, wrong, wrong!

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And not only all of that, but the strRequest array here is in no way, shape, or form the same strRequest array that is in main. You read your string into a local array that quite simply disappears at the very next line -- meaning all that work for nothing.

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    i really dont know how to do it...

    printf ("please enter:\n CHECK\n ALL\n QUIT\n");

    scanf("%s",strInp);

    ...
    can you show me how the function need to be...?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mastermind program problem with iterator
    By Truckomobil in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2009, 01:16 PM
  2. Can anyone teach me how to write mastermind in GUI??
    By prefectticky in forum C++ Programming
    Replies: 18
    Last Post: 04-17-2009, 08:57 AM
  3. Algorithmic Mastermind Program
    By pr0n in forum C Programming
    Replies: 1
    Last Post: 05-12-2008, 11:02 PM
  4. Replies: 37
    Last Post: 12-13-2007, 02:40 PM
  5. coding mastermind in C, only got 2 probs..
    By lepricaun in forum C Programming
    Replies: 17
    Last Post: 07-24-2004, 09:15 AM

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