Game of Nim (PROJECT HELP!)

This is a discussion on Game of Nim (PROJECT HELP!) within the C Programming forums, part of the General Programming Boards category; @Adak, Dude it almost done but when i compile it it has error. :/ Code: #include <stdio.h> int main () ...

  1. #31
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    @Adak, Dude it almost done but when i compile it it has error. :/

    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6}; //Printing Game board
        int i, j; // Variable in Printing Gameboard
        int player =1; //The players
        int rowChosen, sticksChosen; //Scan purpose
        int sum;
    
    
    
        printf("\n\n\tWELCOME TO GAME OF NIM\t\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("\t|");
                }
    
              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;
    
            }
    
    
        return 0;
    
    }

  2. #32
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    @Adak, and dude when i add the conditions it will doesn't get the sticks anymore. Why..

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let's try some bug spray!

  4. #34
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    Let's try some bug spray!
    WHAT YOU MEAN?..

    Everything is alright but when i add the conditions it never get the sticks in the game board..

  5. #35
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > switch(rowChosen)
    > case 1:
    I think you missed the ENTIRE point of using arrays.
    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.

  6. #36
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Salem View Post
    > switch(rowChosen)
    > case 1:
    I think you missed the ENTIRE point of using arrays.

    in what line?.. T.T

  7. #37
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Got it i remove the break; so that the program will remove the sticks but now when i remove 3 in row 1 it just remove 2 only instead of 3..

    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6}; //Printing Game board
        int i, j; // Variable in Printing Gameboard
        int player =1; //The players
        int rowChosen, sticksChosen; //Scan purpose
        int sum;
    
    
    
        printf("\n\n\tWELCOME TO GAME OF NIM\t\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("\t|");
                }
    
              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);
                        }
                }
    
    
    
                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);
                        }
                }
    
    
    
                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);
                        }
                }
    
    
                rows[rowChosen -1] -= sticksChosen;
                sum -= sticksChosen;
    
            }
    
        }
    
        return 0;
    
    }

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6}; //Printing Game board
        int i, j; // Variable in Printing Gameboard
        int player =1; //The players
        int rowChosen, sticksChosen; //Scan purpose
        int sum;
    
    
    
        printf("\n\n\tWELCOME TO GAME OF NIM\t\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("You lose!\n");
    
    
        }
        return 0;
    
    }
    See if that fixed the bug.

  9. #39
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    Code:
    #include <stdio.h>
    
    int main ()
    {
        int rows[] = { 2, 4, 6}; //Printing Game board
        int i, j; // Variable in Printing Gameboard
        int player =1; //The players
        int rowChosen, sticksChosen; //Scan purpose
        int sum;
    
    
    
        printf("\n\n\tWELCOME TO GAME OF NIM\t\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("You lose!\n");
    
    
        }
        return 0;
    
    }
    See if that fixed the bug.
    This code works but it only prints

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

    so i change the

    Code:
    int rows[] = { 2, 4, 6}; to { 3, 4, 6}
    Then it Fixed the problem.. Well dude Thank you very much let me try things for errors.

  10. #40
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When the data is 2, 4, 6, it prints up, 2, 4, and 6 sticks, just like it should.

    It was printing the wrong number of sticks, before. If you want 3, 5 and 7, you need to have those numbers in the array data.

  11. #41
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    When the data is 2, 4, 6, it prints up, 2, 4, and 6 sticks, just like it should.

    It was printing the wrong number of sticks, before. If you want 3, 5 and 7, you need to have those numbers in the array data.
    Amm dude. the one who will get the last stick will be losser how to code that for example the player one got the last stick. how to code that it will say player 1 is the loseer?

    Code:
    printf("%d is the Losser", player)
    or what?..

  12. #42
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe spell it a bit better:

    Code:
    if(sum==0)   //this is already in the code, at the bottom of the while loop
       printf("Player %d, you lose!\n",player);   //so add this, instead of what is there now

  13. #43
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    Maybe spell it a bit better:

    Code:
    if(sum==0)   //this is already in the code, at the bottom of the while loop
       printf("Player %d, you lose!\n",player);   //so add this, instead of what is there now
    Got it, and dude last problem when the slot is empty that slot will be counted..

    for example

    Row1:
    Row2: I I I I I I

    when i put row 1 then get 3
    it continued... how to say error the row is empty..

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's because the logic in the switch statement has nothing to do with the current number of sticks in the row, see?

    It's a fixed number, and the sticks in the row, are constantly changing. The fixed numbers should be replaced with rows[i], and it should be done in a simple for loop. If sticksChosen > rows[rowChosen], then it's a user error.

  15. #45
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    That's because the logic in the switch statement has nothing to do with the current number of sticks in the row, see?

    It's a fixed number, and the sticks in the row, are constantly changing. The fixed numbers should be replaced with rows[i], and it should be done in a simple for loop. If sticksChosen > rows[rowChosen], then it's a user error.
    So it cant be solved?.. or what?..

Page 3 of 6 FirstFirst 123456 LastLast
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-22-2004, 11:28 PM
  5. new game project need help
    By L_I_Programmer in forum Game Programming
    Replies: 1
    Last Post: 03-15-2003, 08:41 PM

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