Thread: nim game

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    44

    nim game

    Code:
    #include<iostream>
    #include<ctime>
    
    using namespace std;
    int pile = 20;
    int humanMarbles;
    void gameStart();
    bool evaluate(int x);
    
    int main()
    {
        gameStart();
        cout << "Pile is set to " << pile << endl;
        cout << "Your turn";
        cin >> humanMarbles;
        if (evaluate(humanMarbles) == true);
        {
            pile = pile - humanMarbles;
            cout << "Pile is set to " << pile;
        }
      
        //system("pause");
        return 0;
    }
    
    void gameStart()
    {
         //system("cls");
         cout << "This is a Nim Game implementation \n" 
                 "--------------------------------- \n"
                 "you play against the computer taking turns removing marbles from a marble pile \n"
                 "you may take 1 up to pile/2. The one who takes the last marble loses \n";
         cout << endl << endl;        
         //srand(static_cast<unsigned int>(time(0)));
         //pile = rand() % 100 + 1;
         return;                 
    }
    
    bool evaluate(int x)
    {
         bool check;
         if (x <= pile/2 && x>=1)
         {
            check = true;
         }
         else
         {
             cout << "Cannot take marbles";
             check = false;
         }    
         
         return check;
    }
    I am trying to implement a nim game where the computer and a player take turns in removing marbles from a pile, the last one to take a marble loses. I came across this problem. I am using an evaluate fuction to determine whether the user inputs a number inside the desired interval(1,pile/2). I does not work correctly though. The program will subtract the marble inputed by the user whether it is inside the interval or not.

    Also the lines highlighted as comment cause a compiler error in a platform different than dev c++. Implicit declaration of fiction...What is that about?!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The program will subtract the marble inputed by the user whether it is inside the interval or not.
    You have a small typo. Look again at the if statement. Consider turning up the warning level on your compiler and it might wanr you about this problem.

    >> Also the lines highlighted as comment cause a compiler error in a platform different than dev c++.
    You need to #include <cstdlib> for srand and rand.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Care to elaborate on the if statement problem?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    $&#37;^@#$%& I cant believe I typed a semicolon after an if statement !!!! It was the only place I did not look for a mistake.

  5. #5
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Not really NIM this way, more of a pick up sticks. Nim has multiple piles that you can take as much from as you want and requires a working knowledge of binary to crack. Gonna write a nim trainer one day.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 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. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  4. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM