Thread: Assignment help

  1. #16
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    if(player == 1)
    player = 2;
    else player = 1;

    that piece of code should be at the bottom of your while loop. Like I said earlier, you have redundant code in your while loop. Take a look at it again, you basically copy and pasted the same code in your while loop, which defeats the purpose of having a loop in the first place. Ex) lines 138 to 183

    P.S. Do you know anything about pointers? Right now you are having trouble with the rows being displayed correctly, that is because your variables are local to there respective functions. Meaning that row=row-rocknum gets reset back to the original row number after the function ends.
    Last edited by camel-man; 11-11-2012 at 12:50 AM.

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Ah thanks so much! That solves problem 2! That one was frustrating me the most.

    I wrote most of the program on linux and it never had the first problem (repeating the draw function twice before inputs were required), but when I started some more work on it on my pc it started doing that! And problem three I cant for the life of me understand why. Should the variables row_1, row_2 and row_3 in draw_nim retain their previous values calculated in play_nim? I can't figure out why it resets to the originally defined board!

    (by the way a HUGE thanks to you for taking the time to help me; all the TA's are gone on the weekend and I live too far from my school to easily visit on weekends for help anyways! Im glad there are people willing to help)

  3. #18
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    For problem number 1) remove line 79 and 182(the other drawnim in the while loop), it draws the board, but then you call your playnum function which in turn calls drawnim. That should solve the double drawing of your board.

  4. #19
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    I need to show the board before the players start making changes though, that's why that line is there. And I'm fairly certain it is outside of main loop, yet the double drawing occurs during the game loop too.

    Edit: Yup, every time player 2 goes there is the double!

    Edit2: I still removed it though and it was fixed! I just realized what you meant by it being in the play_nim anyways, thanks
    Last edited by ADH94; 11-11-2012 at 01:03 AM.

  5. #20
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Post your revised code

  6. #21
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Revised code:

    Code:
    #include <stdio.h>
    
    void draw_nim(int row_1, int row_2, int row_3) //function draws the gameboard
    {
        //Each of the following take the inputted row value and proceeds to output the number of O's that is inputted as well
        //for Row 1
        
        printf("Row 1: ");
        for(int i = 0; i < row_1; i = i + 1)
        {
    	printf("O");
        }
        printf("\n");
    
    
        //Row 2
        
        printf("Row 2: ");
        for(int i = 0; i < row_2; i = i + 1)
        {
    	printf("O");
        }
        printf("\n");
        
        //Row 3
        
        printf("Row 3: ");
        for(int i = 0; i < row_3; i = i + 1)
        {
    	printf("O");
        }
        printf("\n");
    }
    
    
    int play_nim(int rownum, int rocknum, int row_1, int row_2, int row_3) //The play function. This takes the inputted changes to the board and actually impliments them
    {
        if(row_1 > 0 && row_2 > 0 && row_3 > 0) //checks to make sure all the rows are are greater than zero (still have rocks present)
        {
            if(rownum == 1) //for row 1 it removes the rocks the player inputs
            {
    	    row_1 = row_1 - rocknum;
            }
    
    
            if(rownum == 2) //for row 2 it removes the rocks the player inputs
            {
    	    row_2 = row_2 - rocknum;
            }
        
            if(rownum == 3) //for row 3 it removes the rocks the player inputs
            {
    	    row_3 = row_3 - rocknum;
            }
        
            draw_nim(row_1, row_2, row_3);
        }
        else return 0; //if there are no rocks left on the board the function returns zero
    }
    
    
    int main(void)
    {
        int row_1, row_2, row_3; //The number of rocks in each row
        int rownum, rocknum; //The inputted row number and number of rocks to be removed from said row
        int player; //The variable stating which player is currently playing
        
        //Game Setup
        
        printf("Enter the number of rocks in each row: "); //Player imputs how many rocks rows 1, 2 and 3 each have
        scanf("%d %d %d", &row_1, &row_2, &row_3);
        
        while(row_1 <= 0 || row_2 <= 0 || row_3 <= 0) //checks to make sure that only a positive value is imputted for each row
        {
    	printf("You must enter in values greater than zero. Try again:");
    	scanf("%d %d %d", &row_1, &row_2, &row_3); //lets the player input new values if the first are invalid
        }
        
        //Main Game
        
        player = 1; //makes player 1 is active
        
        while(play_nim(rownum, rocknum, row_1, row_2, row_3) > 0) //will loop until the "play_nim" function returns a zero
        {
    	printf("Player %d - Enter the row and number of rocks to remove: ", player); //Player one picks the row and number of rocks to remove
    	scanf("%d %d", &rownum, &rocknum);
    	
    	//Defensive Programming and subsequent reentry of variables in case of invalid inputs
    	
    	while(rownum > 3 && rownum <= 0) //Makes sure only rows 1-3 are imputted
    	{
    	    printf("Invalid move. Try again: ");
    	    scanf("%d %d", &rownum, &rocknum);
    	}
    	
    		if(rownum == 1)
    	{
    	    while(rocknum > row_1 || row_1 == 0) //ensures that player 1 cannot take more rocks than row 1 contains
    	    {
    		printf("You can't take more rocks than there is left in the row! Try again: ");
    		scanf("%d %d", &rownum, &rocknum);
    	    }
    	}
    	
    	if(rownum == 2) //ensures that player 1 cannot take more rocks than in row 2
    	{
    	    while(rocknum > row_2 || row_2 == 0)
    	    {
    		printf("You can't take more rocks than there is left in the row! Try again: ");
    		scanf("%d %d", &rownum, &rocknum);
    	    }
    	}
    	
    	if(rownum == 3)
    	{
    	    while(rocknum > row_3 || row_3 == 0) //player 1 cannot take more rocks than in row 2
    	    {
    		printf("You can't take more rocks than there is left in the row! Try again: ");
    		scanf("%d %d", &rownum, &rocknum);
    	    }
    	}
    	
    	while(rocknum <= 0) //player 1 cannot take more rocks than in row 3
    	{
    	    printf("You cannot take zero or negative rocks! Try again: ");
    	    scanf("%d %d", &rownum, &rocknum);
    	}
    	
    	play_nim(rownum, rocknum, row_1, row_2, row_3); //takes the inputted values and sticks them in the play function
    	
    	if(player == 1)
            player = 2;
        else player = 1;
    	
    	printf("Player %d - Enter the row and number of rocks to remove: ", player);
    	scanf("%d %d", &rownum, &rocknum);
    	
    	//Defensive Programming (all the lines of code are the exact same as the programming for player 1)
    	
    	while(rownum > 3 && rownum <= 0)
    	{
    	    printf("You may only pick rows 1 to 3. Try again: ");
    	    scanf("%d %d", &rownum, &rocknum);
    	}
    	
    	if(rownum == 1)
    	{
    	    while(rocknum > row_1)
    	    {
    		printf("You can't take more rocks than there is left in the row! Try again: ");
    		scanf("%d %d", &rownum, &rocknum);
    	    }
    	}
    	
    	if(rownum == 2)
    	{
    	    while(rocknum > row_2)
    	    {
    		printf("You can't take more rocks than there is left in the row! Try again: ");
    		scanf("%d %d", &rownum, &rocknum);
    	    }
    	}
    	
    	if(rownum == 3)
    	{
    	    while(rocknum > row_3)
    	    {
    		printf("You can't take more rocks than there is left in the row! Try again: ");
    		scanf("%d %d", &rownum, &rocknum);
    	    }
    	}
    	
    	while(rocknum <= 0)
    	{
    	    printf("You cannot take zero or negative rocks! Try again: ");
    	    scanf("%d %d", &rownum, &rocknum);
    	}
    	
    	play_nim(rownum, rocknum, row_1, row_2, row_3);
    	
    		if(player == 2)
            player = 1;
        else player = 1;
        }
        
        printf("Player %d wins!", player); //when the rows all read zero and the play function returns a zero, the game ends. This declares the winner
        
        return 0; //the program then proceeds to die.
    }

  7. #22
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Lines 128 to 180 delete completely, it is redundant and unnecessary, It will also rid you the problem of double drawing the board.

  8. #23
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Ok thanks, that completely got rid of the doubles! Now to make the drawing pick up from where the last player changed it without resetting!

  9. #24
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You will have to read up on pointers a little bit. Right now you are having trouble with the rows being displayed correctly, that is because your variables are local to there respective functions. Meaning that row=row-rocknum gets reset back to the original row number after the function ends.

  10. #25
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Alright, will do. I was under the impression though that since draw_nim gets its variables from play_nim which gets ITS variables from main, that if I changed the variable's value in main it would ripple back up. I still have much to learn haha. Thanks for everything so far now, im going to get some sleep and then read up on pointers and try to finish this guy.

    Again, thanks. You've helped ease a very frustrated mind!

  11. #26
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You are welcome. Yes read up on pointers, you will soon realize that the "ripple effect" that you described earlier will quickly fade after you notice how local variables only "work"(meaning you cannot reflect the change back to main unless you return the variable one by one) within the respective functions.

  12. #27
    Registered User
    Join Date
    Nov 2012
    Posts
    29
    Ok so i've tried reading about pointers and I'm having difficulty understanding the concept of it. Is there a way I could use the row_# variables I initially declare and in the play function I convert them into new variables?

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nope! <smile>

    We've called a council of the World Order of Pointers United, and we have decided to force pointers, by injecting them directly into your neurons!

    You want pointers, and they are simple for the most part.

    You are taking a trip, but need someone to care for your dog, at home. So you hire a housekeeper to do that. You have the hk's phone number - that is your pointer address.

    You are in another function, and want to change something in a variable - in how your dog gets fed - so you use the pointer, to call the keeper to make that change possible.

    Code:
    #include <stdio.h>
    
    void changeNumber(int *);
    
    int main(void) {
       int number = 10;
    
       printf("Number: %d\n\n",number);
       /* call changeNumber with the address of "number" */
       changeNumber(&number); 
    
       printf("Number: %d\n",number);
       return 0;
    }
    void changeNumber(int *myNumbersPointer) {
    
    /*
       I'm not changing the address of myNumbersPointer. I'm using the * to refer back to the original number variable, in main(), to change it's value 
    */
       (*myNumbersPointer)++;
    
         /*note that pointers have odd precedence, so you have to use odd ()'s around things like this */
       
    
    }
    If you were to change the address of the pointer, then it's like you changed the phone number to the housekeeper, and you wouldn't be able to call the hk, any more, to change anything.

    The thing that makes pointer difficult, is, we can't see the addresses they hold or know what those addresses mean, easily. There's no word associated with their addresses, and there is no graphical or auditory clues, either.

    It's just an address - like your phone has just a telephone number. Lose it, or dial it wrong, and you're suddenly LOST.

    The precedence pointers have is odd, which adds to the problem of working with them. Try removing the ( ) in the above code, and see what happens!!

    <Yikes!>
    Last edited by Adak; 11-11-2012 at 07:14 PM.

  14. #29
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    @Adak- I know you are really smart and experienced, but when you first started programming did pointers "BrainF**K" you at all? I know that pointers tend to have that affect on many people(including myself), when using them for the first time.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by camel-man View Post
    @Adak- I know you are really smart and experienced, but when you first started programming did pointers "BrainF**K" you at all? I know that pointers tend to have that affect on many people(including myself), when using them for the first time.
    Oh yeah! And not so smart!

    I learned BASIC when it was VERY basic (no functions (we used gosubs and goto's, no loops like we have now, only a line editor, etc.), When I ran into pointers in C, I went hyper on them, over-thinking what they do, and how they work. They're really simple data types. After I read a little hardware book on computers, it started seeping into my brain, why they're so dang useful.

    In general, I don't use pointers a lot. When they're used a lot, they really make code hard to understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with my assignment
    By atlas07 in forum C++ Programming
    Replies: 4
    Last Post: 04-25-2010, 12:10 PM
  2. S2 assignment
    By pelisa in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2010, 11:48 AM
  3. Need help with assignment
    By C_Davis in forum C Programming
    Replies: 4
    Last Post: 03-11-2010, 12:49 AM
  4. Need help with Assignment 2
    By krazyxazn in forum C Programming
    Replies: 1
    Last Post: 02-09-2010, 10:36 PM
  5. Replies: 3
    Last Post: 04-26-2009, 08:54 AM