Nim Project. It doesn't display the matches but it does display correct number.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Nim Project. It doesn't display the matches but it does display correct number.

  1. #1
    Join Date
    Mar 2013
    Posts
    4

    Nim Project. It doesn't display the matches but it does display correct number.

    Hello everyone,

    I am having one issue with my project. We are making a game of Nim code.I'm 99% done with it, i worked hard on it and i feel like i did a good job, however my project is not displaying the matches i want. For example, it display's the inital number of them 23. But once the first player subtracts a number, it doesn't display matches for player 2, just the number of matches remaining. Then as i keep running the program the same thing happens. This is what it's supposed to
    output:

    "Input/Output sample
    WELCOME TO NIM
    ------- -- ---
    Enter the starting player's name (no spaces)-->John
    Enter the second player's name (no spaces)-->Mary
    There are 23 matches.
    ooooooooooooooooooooooo
    |||||||||||||||||||||||
    Player John please enter the number of matches to remove-->2
    There are 21 matches.
    ooooooooooooooooooooo
    |||||||||||||||||||||
    Player Mary please enter the number of matches to remove-->3
    There are 18 matches.
    oooooooooooooooooo
    ||||||||||||||||||
    Player John please enter the number of matches to remove-->1
    "

    This is entire code.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	// Holds variables.
    
    	string player1;
    	string player2;
    	int topMatch = 23;
    	int bottomMatch = 23;
    	const int total = 23;
    	int n, mtotal;
    	bool winner = false;
    
    	mtotal = total;
    
    	// Display's Welcome Message to user.
    
    	cout<<"WELCOME TO NIM"<<endl;
    	cout<<"------- -- ---"<<endl;
    
    	// Skips a line.
    
    	cout<<endl;
    
    	// Asks player 1 to enter their name.
    
    	cout<<"Enter the starting player's name (no spaces) -->";
    	cin>>player1;
    
    	// Skips a line.
    
    	cout<<endl;
    
    	// Asks player 2 to enter their name.
    
    	cout<<"Enter the second player's name (no spaces) -->";
    	cin>>player2;
    
    	// Skips a line.
    
    	cout<<endl;
    	
    
    
    // Defines the number of matches.
    
    topMatch=23, bottomMatch=23;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    // Beginning of the code.
    
    while (!winner)
    
    { 
    	if(mtotal >= 0)
    
    // Display's message with the total number of matches.
    
     cout<<"There are "<<mtotal<< " matches." <<endl;
    
    
    // Display's matches.
    
    while (topMatch > 0)
    {
    	cout<<"o";
    	topMatch--;
    }
    
    cout<<endl;
    
    while (bottomMatch > 0)
    {
    	cout<<"|";
    	bottomMatch--;
    }
    
    cout<<endl<<endl;
    
    // Ask's player 1 to enter the number of matches he wants to remove.
     cout<<"Player "<<player1<<" please enter the number of matches to remove-->";
    
     cin>> n;
    
     cout<<endl;
     
    
     // Removes only one match if the player enter a number lower than one and higher than 3.
    
     if ( n<1 || n>3)
    
     {
    	 n=1;
     }
    
     
    
    // Removes 1 to 3 matches depending on the number entered.
    
     if ( n >=1 && n <=3)
    
     {
     mtotal -= n;
    
     }
    
     // Equation that removes "matches".
    
     topMatch=topMatch-n;
    
     bottomMatch= bottomMatch -n;
    
     // Removes the number of matches the user inputs.
     while (topMatch > 0)
    {
    	cout<<"o";
    	topMatch--;
    }
    
    cout<<endl;
    
    while (bottomMatch > 0)
    {
    	cout<<"|";
    
    	bottomMatch--;
    }
     // Display's Message if player is the winner.
    
     if (mtotal==0 || mtotal<=0) 
    
     {winner = true;
    
    cout<<"Game over. Player "<<player2<<" is the winner!"<<endl;
     cin.get ( ); cin.get ( );
     }
     
    
    
    
    
    
    
    
    
    
    
    
    // Beginning of player 2 coding.
    
     if(mtotal >= 0)
    
    // Display's the number of matches remaining after player 1 turn.
    
    cout<<"There are "<<mtotal<<" matches"<<endl<<endl;
    
      // Equation that removes "matches".
    
     topMatch=topMatch-n;
    
     bottomMatch= bottomMatch -n;
    
    
    
     // Removes the number of matches the user inputs.
     while (topMatch > 0)
    {
    	cout<<"o";
    	topMatch--;
    }
    
    cout<<endl;
    
    while (bottomMatch > 0)
    {
    	cout<<"|";
    
    	bottomMatch--;
    }
    
    // Asks player 2 to remove matches.
    
    cout<<"Player "<<player2<<" please enter the number of matches to remove-->";
    
    cin>>n;
    
    cout<<endl;
    
     // Removes only one match if the player enter a number lower than one and higher than 3.
    
     if ( 1>n || n>3)
    
     {
    	 n=1;
     }
    
     // Equation that removes "matches".
    
     topMatch=topMatch-n;
    
     bottomMatch= bottomMatch-n;
    
     // Removes the number of matches the user inputs.
     while (topMatch > 0)
    {
    	cout<<"o";
    	topMatch--;
    }
    
    cout<<endl;
    
    while (bottomMatch > 0)
    {
    	cout<<"|";
    
    	bottomMatch--;
    }
    // Removes number of matches entered as long as it's a number between 1 and 3.
    
    if ( n >=1 && n <=3)
    
    
    	// Display's the number of matches removed from player 2.
    
     {
    
    mtotal -= n;}
    
    
    
    
    
    // Display's congratulation message if player 2 has won.
    
    if (mtotal==0 || mtotal<=0) 
    
     {winner = true;
    
    cout<<"Game over. Player "<<player1<<" is the winner"<<endl;
    cin.get ( ); cin.get ( );
    }
    
    }
     cin.get ( ); cin.get ( );
    
    
     
    }



    I will attach my code as well.ProjectNim.cpp

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Nim Project. It doesn't display the matches but it does display correct number.

    Quote Originally Posted by hadesfalcon View Post
    Hello everyone,

    I am having one issue with my project. We are making a game of Nim code.I'm 99% done with it, i worked hard on it and i feel like i did a good job, however my project is not displaying the matches i want. For example, it display's the inital number of them 23. But once the first player subtracts a number, it doesn't display matches for player 2, just the number of matches remaining. Then as i keep running the program the same thing happens.
    And the big question is this:

    What debugging have you done to fix the problem? Have you run the code with the debugger to identify where the program goes wrong?

    Read the FAQ, especially post #2.

    http://forums.codeguru.com/showthrea...ork-assignment

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 9th, 2013 at 09:46 PM.

  3. #3
    Join Date
    Mar 2013
    Posts
    4

    Re: Nim Project. It doesn't display the matches but it does display correct number.

    I have done everything i know how to do, if you looked at my program everything seems fine and i tried debugging it with step over but nothing seems to work for me. I wrote that program with no help, i'm just asking for very little help. I'm sure everyone needs help sometimes, but if ya'll can't or do not want to help that's fine. Thank you

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,610

    Re: Nim Project. It doesn't display the matches but it does display correct number.

    Quote Originally Posted by hadesfalcon View Post
    i tried debugging it with step over but nothing seems to work for me.
    Deline "[hl]nothing seems to work[/hl"
    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Nim Project. It doesn't display the matches but it does display correct number.

    Code:
    while (topMatch > 0) {
         cout << "o";
         topMatch--;
    }
    After this code has executed, topMatch will be 0.

    Code:
    topMatch = topMatch - n;
    topMatch will now be -n (ie negative!). The same applies to bottomMatch.

    If, as Paul suggested, you had debugged properly your program using the debugger then you should have found this! It is not a difficult problem to find.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Nim Project. It doesn't display the matches but it does display correct number.

    Quote Originally Posted by hadesfalcon View Post
    I have done everything i know how to do, if you looked at my program everything seems fine and i tried debugging it with step over but nothing seems to work for me.
    If you really debugged your program, then the problem would have been found very easily as 2kaud pointed out.

    If you wrote the program yourself, then you must be able to debug the code you wrote. That is a requirement for all student programmers, and that is to debug your own code. Otherwise, it would be considered cheating if you as a student, gets a professional or expert programmer to take your program, and then just wait a couple of hours for their answers as to what's wrong.

    You had a plan in mind -- you put the plan on paper -- you transferred that plan on paper into a C++ program. Now when you run the program, somewhere the program deviates from all of this planning. So you take the debugger, step through the program, inspect variables, and see where the program deviates from the plan. Then when you discover where the deviation occurs, you make the necessary fixes and adjustments to the code. Sometimes those fixes are easy, other times they require a rethink of the initial design. But in either of those scenarios, the programmer must be able to identify by themselves what went wrong.

    Given all of this, there is no way to claim that you can't debug or not know where your program fails. It is virtually impossible to not be able to use the debugger to fix and/or identify the problem yourself if as you claim, you wrote the program yourself.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 10th, 2013 at 07:56 AM.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    Re: Nim Project. It doesn't display the matches but it does display correct number.

    Given all of this, there is no way to claim that you can't debug or not know where your program fails. It is virtually impossible to not be able to use the debugger to fix and/or identify the problem yourself if as you claim, you wrote the program yourself.
    I would go further and say that given the source - even if it's not code you have written yourself - then you should be able to compile and debug it. Indeed, I would make debugging a non-working program written by someone else a specific assignment in programming courses.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Nim Project. It doesn't display the matches but it does display correct number.

    Quote Originally Posted by 2kaud View Post
    I would go further and say that given the source - even if it's not code you have written yourself - then you should be able to compile and debug it. Indeed, I would make debugging a non-working program written by someone else a specific assignment in programming courses.
    That would be a good idea.

    I guess too many new programmers think that the requirement is just to produce compilable source code, run the program, and if it doesn't run correctly, have an expert fix the problem or have the expert tell them what's wrong and what to fix. If I found out that a student went to an expert or professional programmer to fix their code, that would be major points deducted, if not an outright failure.

    The only exception would be if the student is misusing some aspect of C++, but only an experienced C++ programmer would have figured this out. Even in that respect, the student should (no, must) have pinpointed the spot in their own code that is failing and inform us of where the failure occurred. Instead, we get posts of entire programs with the poster sitting back and waiting for an answer by one of us (with many times the title of the discussion thread having the word "URGENT!!!!" in it somewhere).

    I know it may sound harsh, but at some point it has to be stated that debugging is part of learning how to write a program. It isn't just about writing source code and running a program to see if it's OK.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 10th, 2013 at 08:55 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center