Thread: Game of Nim (PROJECT HELP!)

  1. #46
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    This is my final code.. Just one problem when the blank row.. T.T How to fix it..
    when the row is empty it should not be counted anymore T.T help help any solutions..

    Code:
    #include <stdio.h>
    
    int main ()
    {
    
    
            char ans;
    
        do{
            int rows[] = { 3, 5, 7};
            int i, j;
            int player =1;
            int rowChosen, sticksChosen;
            int sum;
    
    
    
            printf("\n\n\t\t\WELCOME TO GAME OF NIM\t\t\t\t\n\n");
            printf("\t\tBy:Ervin And Orven\t\t\t\t\n\n\n\n");
    
            sum=rows[0]+rows[1]+rows[2];
    
            while(sum>0)
            {
    
            for(i = 0; i < 3; i++)
                {
                    printf("\nRow %d:", i+1);
    
                    for(j = 0; j < rows[i] ; j++)
                        {
                            printf("\t|");
                        }
    
                    printf("\n");
                }
    
              printf("\n");
    
                    printf("\nPlayer %d's turn! \n\n",player);
                    player++;
                    if(player == 3)
                        {
                            player = player - 2;
                        }
    
                            printf("Please Enter a Row: ");
                            scanf("%d", &rowChosen);
    
            if(rowChosen > 3 || rowChosen < 1)
            {
                printf("Error! Enter number from 1-3!\n");
    
                while (rowChosen > 3|| rowChosen < 1)
                {
                    printf("Please Enter a Row Again:  ");
                    scanf("%d", &rowChosen);
                }
            }
    
                            printf("Please Enter Number of sticks to remove: ");
                            scanf("%d", &sticksChosen);
    
    
            switch(rowChosen)
            {
                case 1:
                {
                    if(sticksChosen > 3 ||sticksChosen < 1)
                        printf("Error! Enter number from 1-3!\n");
    
    
                    while(sticksChosen > 3||sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
    
                case 2:
                {
                    if(sticksChosen > 5|| sticksChosen < 1)
                    {
                      printf("Error! Enter number from 1-5!\n");
                    }
    
    
    
                    while(sticksChosen > 5 || sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
    
                case 3:
                {
                    if(sticksChosen > 7 || sticksChosen < 1)
                        printf("Error! Enter number from 1-7!\n");
    
    
                    while(sticksChosen > 7 || sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
            }
    
            rows[rowChosen -1] -= sticksChosen;
            sum -= sticksChosen;
    
            if(sum==0)
               printf("\n\nPlayer %d You lose!\n\n", player);
            }
    
            printf("Do you wanna try again? (Y/N)");
            scanf("%s", &ans);
    
        }while (ans == 'Y' || ans == 'y');
    
     return 0;
     
    }

  2. #47
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe a test before printing the row number?

    Code:
           if(rows[i])     //new
              printf("\nRow %d:", i+1);

  3. #48
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    Maybe a test before printing the row number?

    Code:
           if(rows[i])     //new
              printf("\nRow %d:", i+1);
    Where can i put it??

  4. #49
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The if(rows[i]) should be on line 27

  5. #50
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    The if(rows[i]) should be on line 27
    It automatically shut the program..

    the header will print but the game will stop..

  6. #51
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ervinako View Post
    It automatically shut the program..

    the header will print but the game will stop..
    Post your new code, line 20 through line 40. I need to see what you're doing.

  7. #52
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    Post your new code, line 20 through line 40. I need to see what you're doing.
    Code:
          while(sum>0)
            {
    
                if(rows[i])
                    {
                        printf("\nRow %d:", i+1);
                    }
    
    
                for(i = 0; i < 3; i++)
                    {
                        printf("\nRow %d:", i+1);
    
                        for(j = 0; j < rows[i] ; j++)
                            {
                                printf("\t|");
                            }

  8. #53
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    From post #46, above:

    Code:
            for(i = 0; i < 3; i++)
              {
                  if(rows[i]) {
                       printf("\nRow %d:", i+1);
                       for(j = 0; j < rows[i] ; j++)
                       {
                            printf("\t|");
                       }
     
                       printf("\n");
                   } //end of new if statement
                }

  9. #54
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    From post #46, above:

    Code:
            for(i = 0; i < 3; i++)
              {
                  if(rows[i]) {
                       printf("\nRow %d:", i+1);
                       for(j = 0; j < rows[i] ; j++)
                       {
                            printf("\t|");
                       }
     
                       printf("\n");
                   } //end of new if statement
                }
    i will delete the code? from #26 to #37 and put it in #46?..

  10. #55
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by ervinako View Post
    i will delete the code? from #26 to #37 and put it in #46?..
    Code:
    #include <stdio.h>
    
    int main ()
    {
    
            char ans;
    
        do{
    
            int rows[] = { 3, 5, 7};
            int i, j;
            int player =1;
            int rowChosen, sticksChosen;
            int sum;
    
            printf("\n\n");
            printf("\t\t******************************\t");
            printf("\n\t\t****WELCOME TO GAME OF NIM****\t\t\n");
            printf("\t\tErvin Ardiente & Orven Dumasig\t\n");
            printf("\t\t******************************\t\n\n");
    
            sum=rows[0]+rows[1]+rows[2];
    
            while(sum>0)
            {
    
                if(rows[i])
                    {
                        printf("\nRow %d:", i+1);
                    }
    
                    printf("\nPlayer %d's turn! \n\n",player);
                    player++;
                    if(player == 3)
                        {
                            player = player - 2;
                        }
    
                for(i = 0; i < 3; i++)
                    {
                        if(rows[i])
                            {
                                printf("\nRow %d:", i+1);
                                for(j = 0; j < rows[i] ; j++)
                            {
                                printf("\t|");
                            }
                                printf("\n");
                            }
                    }
                            printf("Please Enter a Row: ");
                            scanf("%d", &rowChosen);
    
            if(rowChosen > 3 || rowChosen < 1)
            {
                printf("Error! Enter number from 1-3!\n");
    
                while (rowChosen > 3|| rowChosen < 1)
                    {
                        printf("Please Enter a Row Again:  ");
                        scanf("%d", &rowChosen);
                    }
            }
    
    
                printf("Please Enter Number of sticks to remove: ");
                scanf("%d", &sticksChosen);
    
    
            switch(rowChosen)
            {
                case 1:
                {
                    if(sticksChosen > 3 ||sticksChosen < 1)
                        printf("Error! Enter number from 1-3!\n");
    
    
                    while(sticksChosen > 3||sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
    
                case 2:
                {
                    if(sticksChosen > 5|| sticksChosen < 1)
                    {
                      printf("Error! Enter number from 1-5!\n");
                    }
    
    
    
                    while(sticksChosen > 5 || sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
    
                case 3:
                {
                    if(sticksChosen > 7 || sticksChosen < 1)
                        printf("Error! Enter number from 1-7!\n");
    
    
                    while(sticksChosen > 7 || sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
            }
    
            rows[rowChosen -1] -= sticksChosen;
            sum -= sticksChosen;
    
            if(sum==0)
                {
                    printf("\n\nPlayer %d You lose!\n\n", player);
                }
            }
    
            printf("Do you wanna try again? (Y/N)");
            scanf("%s", &ans);
    
        }while (ans == 'Y' || ans == 'y');
    
     return 0;
    
    }
    is this what you mean or what?..

    it crashes the program..

  11. #56
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ervinako View Post
    i will delete the code? from #26 to #37 and put it in #46?..
    NO!

    You will delete the lines 4 - 8 from your post reply #52. (Not line 52, POST reply #52. In your code.

    Then you will add the lines I marked in red above, just as they are in my post, right into your outer for loop, just like I have it.

    Not above the first for loop, but BETWEEN the two for loops.

  12. #57
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Is this what you mean? ) Sorry for not getting..
    It will delete the row1? but if i wanted not to delete row 1? just let it stay there but
    if the person chooses the row one it will promt error! the row have no more sticks anymore.

    like this

    Row1:
    Row2: I I I I I
    Row3: I I I I I I I

    Enter row: 1
    Opps Row 1 have no more sticks anymore, please try again.

    Code:
    #include <stdio.h>
    
    int main ()
    {
    
            char ans;
    
        do{
    
            int rows[] = { 3, 5, 7};
            int i, j;
            int player =1;
            int rowChosen, sticksChosen;
            int sum;
    
            printf("\n\n");
            printf("\t\t******************************\t");
            printf("\n\t\t****WELCOME TO GAME OF NIM****\t\t\n");
            printf("\t\tErvin Ardiente & Orven Dumasig\t\n");
            printf("\t\t******************************\t\n\n");
    
            sum=rows[0]+rows[1]+rows[2];
    
            while(sum>0)
            {
    
                for(i = 0; i < 3; i++)
                    {
                        if(rows[i])
                            {
                                printf("\nRow %d:", i+1);
                                for(j = 0; j < rows[i] ; j++)
                            {
                                printf("\t|");
                            }
    
                                printf("\n");
                            }
                    }
    
    
              printf("\n");
    
                    printf("\nPlayer %d's turn! \n\n",player);
                    player++;
                    if(player == 3)
                        {
                            player = player - 2;
                        }
    
                            printf("Please Enter a Row: ");
                            scanf("%d", &rowChosen);
    
            if(rowChosen > 3 || rowChosen < 1)
            {
                printf("Error! Enter number from 1-3!\n");
    
                while (rowChosen > 3|| rowChosen < 1)
                    {
                        printf("Please Enter a Row Again:  ");
                        scanf("%d", &rowChosen);
                    }
            }
    
                printf("Please Enter Number of sticks to remove: ");
                scanf("%d", &sticksChosen);
    
    
            switch(rowChosen)
            {
                case 1:
                {
                    if(sticksChosen > 3 ||sticksChosen < 1)
                        printf("Error! Enter number from 1-3!\n");
    
    
                    while(sticksChosen > 3||sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
    
                case 2:
                {
                    if(sticksChosen > 5|| sticksChosen < 1)
                    {
                      printf("Error! Enter number from 1-5!\n");
                    }
    
    
    
                    while(sticksChosen > 5 || sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
    
                case 3:
                {
                    if(sticksChosen > 7 || sticksChosen < 1)
                        printf("Error! Enter number from 1-7!\n");
    
    
                    while(sticksChosen > 7 || sticksChosen < 1)
                    {
                        printf("Please Enter Number of sticks to remove again:");
                        scanf("%d", &sticksChosen);
                    }
                }
    
    
                break;
    
            }
    
            rows[rowChosen -1] -= sticksChosen;
            sum -= sticksChosen;
    
            if(sum==0)
                {
                    printf("\n\nPlayer %d You lose!\n\n", player);
                }
            }
    
            printf("Do you wanna try again? (Y/N)");
            scanf("%s", &ans);
    
        }while (ans == 'Y' || ans == 'y');
    
     return 0;
    
    }

  13. #58
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Solution...

    Code:
    #include <stdio.h>
    
    #define ARRAY_SIZE 3
    
    
    int MyArray[ARRAY_SIZE] = { 2, 4, 6};
    int print()
    {
        int result=0;
        int i,j;
        for(i=0;i<ARRAY_SIZE;i++) {
            printf("Row %d:", i+1);
            for(j=0;j<MyArray[i];j++) {
                printf(" |");
            }
            printf("\n"); fflush(stdout);
        }
        return 0;
    }
    int sum()
    {
        int result=0;
        int i;
        for(i=0;i<ARRAY_SIZE;i++) {
            printf("MyArray[ %d ] = %d\n", i, MyArray[i]);
            result += MyArray[i];
        }
        return result;
    }
    
    
    main()
    {
        int row, sticks;
        int player=2;
        while(sum() != 0) {
            player = (player == 1) ? 2: 1;
            print();
    
    
            printf("\nPlayer %d plays\n", player);
    
    
            printf("row:"); fflush(stdout);
            scanf("%d", &row);
            printf("sticks:"); fflush(stdout);
            scanf("%d", &sticks);
    
    
            if( (row <= 0) || (row > ARRAY_SIZE) ) {
                printf("ERROR: row %d is invalid\n", row);
                player = (player == 1) ? 2: 1;
                continue;
            }
    
    
            row -= 1;
    
    
            if ( (sticks==0) || (MyArray[row] < sticks) ) {
                printf("ERROR: sticks %d is invalid\n", sticks);
                player = (player == 1) ? 2: 1;
                continue;
            }
    
    
            MyArray[row] -= sticks;
        }
        printf("Player %d Lost\n", player); 
    
    
    }

  14. #59
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by garbag View Post
    Code:
    #include <stdio.h>
    
    #define ARRAY_SIZE 3
    
    
    int MyArray[ARRAY_SIZE] = { 2, 4, 6};
    int print()
    {
        int result=0;
        int i,j;
        for(i=0;i<ARRAY_SIZE;i++) {
            printf("Row %d:", i+1);
            for(j=0;j<MyArray[i];j++) {
                printf(" |");
            }
            printf("\n"); fflush(stdout);
        }
        return 0;
    }
    int sum()
    {
        int result=0;
        int i;
        for(i=0;i<ARRAY_SIZE;i++) {
            printf("MyArray[ %d ] = %d\n", i, MyArray[i]);
            result += MyArray[i];
        }
        return result;
    }
    
    
    main()
    {
        int row, sticks;
        int player=2;
        while(sum() != 0) {
            player = (player == 1) ? 2: 1;
            print();
    
    
            printf("\nPlayer %d plays\n", player);
    
    
            printf("row:"); fflush(stdout);
            scanf("%d", &row);
            printf("sticks:"); fflush(stdout);
            scanf("%d", &sticks);
    
    
            if( (row <= 0) || (row > ARRAY_SIZE) ) {
                printf("ERROR: row %d is invalid\n", row);
                player = (player == 1) ? 2: 1;
                continue;
            }
    
    
            row -= 1;
    
    
            if ( (sticks==0) || (MyArray[row] < sticks) ) {
                printf("ERROR: sticks %d is invalid\n", sticks);
                player = (player == 1) ? 2: 1;
                continue;
            }
    
    
            MyArray[row] -= sticks;
        }
        printf("Player %d Lost\n", player); 
    
    
    }
    This code is actually the same as mine and it make me so confuse.

    what I'm trying to say is: For example row1: has no sticks anymore.
    if the user will choose the row 1 it will print error cause theirs no more sticks in it.

    like this:

    Row1: I I I
    Row2:
    Row3: I I I I I I I

    Enter a row: 2
    Opps!, that row has no sticsk anymore please try again:
    Enter a row: 3
    How many sticks to remove:

    Thats what i'm trying to code..

  15. #60
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ and SDL amateur looking for a game project.
    By traitor_651 in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-15-2010, 11:16 AM
  2. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 01:12 AM
  3. Little game project
    By Gen. in forum Projects and Job Recruitment
    Replies: 7
    Last Post: 08-19-2006, 10:29 PM
  4. 2D Game Project with DirectX and C++
    By VMJC in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 12-23-2004, 12:28 AM
  5. new game project need help
    By L_I_Programmer in forum Game Programming
    Replies: 1
    Last Post: 03-15-2003, 09:41 PM