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; Originally Posted by ervinako Help. Try that. Code: #include <stdio.h> int main () { char ans; do{ int rows[] = ...

  1. #61
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ervinako View Post
    Help.

    Try that.



    Code:
    #include <stdio.h>
    
    
    int main ()
    {
    
        char ans;
    
        do{
    
            int rows[] = {0, 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[1]+rows[2]+rows[3];
    
            while(sum>0)
            {
    
               for(i = 1; i <=3; i++)
               {
                  if(rows[i])
                  {
                     printf("\nRow %d:", i);
                     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);
    
            while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen]== 0)
            {
                printf("Error! Please enter a row with sticks: ");
                scanf("%d", &rowChosen);
            }
    
            printf("Please Enter Number of sticks to remove: ");
            scanf("%d", &sticksChosen);
    
            while(sticksChosen > rows[rowChosen] || sticksChosen < 1)
            { 
                printf("Error! Please Enter Number of sticks to remove: ");
                scanf("%d", &sticksChosen); 
            }    
            rows[rowChosen] -= sticksChosen;
            sum -= sticksChosen;
    
            if(sum==0)
                {
                    printf("\n\nPlayer %d You lose!\n\n", player);
                }
            }
    
            printf("Do you want to try again? (Y/N)");
            scanf(" %c", &ans);
    
        }while (ans == 'Y' || ans == 'y');
    
     return 0;
    
    }
    Note that I moved all the rows up by one, so rows[0] is empty and not part of the game, anymore.

  2. #62
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    After you check to see if the row was a valid input, check to see if the row is not equal to zero with an 'if' statement in exactly the same way -> Yes it is that easy
    Fact - Beethoven wrote his first symphony in C

  3. #63
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    @Adak, Sir the the one who will picks the last stick will be the looser not the one who gets last.

    I got the problem:

    Player 2 turn:

    Row1: I
    Row2:
    Row3:

    Enter row: 1
    Enter sticks to remove: 1

    Player 2 is the looser.

    My code actually says: Player 1 is the looser. how come? T.T the player 2 must be the looser cause he/she picks the last stick.



    Code:
      if(sum == 0)
                {
                    printf("\n\nPlayer %d You lose!\n\n", player);
                }

  4. #64
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Click_here View Post
    After you check to see if the row was a valid input, check to see if the row is not equal to zero with an 'if' statement in exactly the same way -> Yes it is that easy
    What are you trying to say?..

    Code:
                for(i = 0; i < 3; i++)
                    {
                        if(rows[i] ==0) 
                            {
                                printf("\nRow %d:", i+1);
                                for(j = 0; j < rows[i] ; j++)
                            {
                                printf("\t|");
                            }
    
                                printf("\n");
                            }
                    }
    is this what you try to say? or what?..

  5. #65
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Adak's solution to the problem does what I meant, but one step further. Instead of two 'if' statements, Adak has combined them into one and held the code's execution there until the correct input is received.

    Code:
    while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen]== 0)
    
    Fact - Beethoven wrote his first symphony in C

  6. #66
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    On second thought, it might be better to split the 2 apart - Check that the rowChosen is one, two, or three before
    Code:
    rows[rowChosen-1] == 0
    If you try to read out of bounds for the array, you will crash your program.
    Fact - Beethoven wrote his first symphony in C

  7. #67
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    @Adak, oh the program works sorry for not reading it.. now i know.
    But i wanted that the row1: (empty) and it not part of the game anymore,
    do you have an idea?..

  8. #68
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Quote Originally Posted by ervinako
    Can you please tell were to put the this
    I already have

    Quote Originally Posted by click_here
    After you check to see if the row was a valid input, check to see if the row is not equal to zero with an 'if' statement in exactly the same way
    Fact - Beethoven wrote his first symphony in C

  9. #69
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    It deleted the row i wanted that the row is still there but its not part of the game anymore..
    anyone?

    Code:
    #include <stdio.h>
    
    
    int main ()
    {
    
        char ans;
    
        do{
    
            int rows[] = {0, 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[1]+rows[2]+rows[3];
    
            while(sum>0)
            {
    
               for(i = 1; i <=3; i++)
               {
                  if(rows[i])
                  {
                     printf("\nRow %d:", i);
                     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);
    
            while(rowChosen > 3 || rowChosen < 1 || rows[rowChosen]== 0)
            {
                printf("Error! Please enter a row with sticks: ");
                scanf("%d", &rowChosen);
            }
    
                printf("Please Enter Number of sticks to remove: ");
                scanf("%d", &sticksChosen);
    
            while(sticksChosen > rows[rowChosen] || sticksChosen < 1)
            {
                printf("Error! Please Enter Number of sticks to remove again: ");
                scanf("%d", &sticksChosen);
            }
    
            rows[rowChosen] -= sticksChosen;
            sum -= sticksChosen;
    
            if(sum==0)
                {
                    printf("\n\nPlayer %d You lose!\n\n", player);
                }
            }
    
            printf("Do you want to try again? (Y/N)");
            scanf(" %c", &ans);
    
        }while (ans == 'Y' || ans == 'y');
    
     return 0;
    
    }

  10. #70
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    One problem also is, when it declared the looser the looser will be the one who picks the
    last stick not the one who is not picking the stick anymore..

  11. #71
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Code:
    rows[rowChosen-1]== 0
    Fact - Beethoven wrote his first symphony in C

  12. #72
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Click_here View Post
    Code:
    rows[rowChosen-1]== 0
    Code:
    rows[rowChosen-1] == 0;
    This doesnt delete any of sticks in a row.

  13. #73
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    The input to the array of 3 elements can only be
    row[0]
    row[1]
    row[2]

    Your input from your user is 1, 2, 3
    Fact - Beethoven wrote his first symphony in C

  14. #74
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    Hmmm... I see you've added a redundant element to your array -> Don't worry about it
    Fact - Beethoven wrote his first symphony in C

  15. #75
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,384
    It deleted the row http://im.cprogramming.com/images/smilies/frown.png i wanted that the row is still there but its not part of the game anymore..
    anyone? http://im.cprogramming.com/images/smilies/frown.png
    If you look at the if statement just before you print it out, you will see
    Code:
    if (rows[i])
    Fact - Beethoven wrote his first symphony in C

Page 5 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