NIM Game

needs your input.

Page 1 of 1

2 Replies - 584 Views - Last Post: 05 March 2010 - 08:59 AM Rate Topic: -----

#1 RGLAsnakeMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 05-March 10

NIM Game

Posted 05 March 2010 - 01:09 AM

// The main NIM Program 
#include<iostream>

using namespace std;

const char HUMAN_PLAYER = true;
const char COMPUTER_PLAYER = false;
int main() 
{ 
      int pileSize; 
      int count = 2; 
        int compTurn; 
        char start; 
        bool theWinner; 
        bool currentPlayer; 
         
        cout << "Welcome to the game of Single Pile NIM.\nThere are 22 sticks in the pile at the start of the game and each\nplayer must take between 1 and 4 sticks at each turn. The player\nwho takes the last stick wins." << endl << endl; 
 
              while(count >= 0) 
              { 
                      cout << "Whose go? [H]uman or [C]omputer?" <<endl; 
                      cin >> start; 
                      switch(start) 
                      { 
                      case 'c': 
                      case 'C': cout << "Computer start." << endl; 
                            count = -1; 
                            compTurn = true; 
                      break; 
                 
                      case 'h': 
                      case 'H': cout << "Human start." <<endl; 
                           count = -1; 
                      true; 
                          compTurn = true; 
                      break; 
                      
                      default: 
                      cout << count << " try(s) remaining." <<endl; 
                    } 
                if(count == 0) 
                        cout << "No go." << endl << endl; 
        --count; 
            } 
        //return(compTurn); 
  
  // Now that we know who plays first, we can start 
  // the game by initializing the heap of sticks 
  int nimSticks = 22;  
 
  // CORE GAME LOOP: as long as there are sticks left, 
  // we'll need to get moves from the opponents 
      while  (int nimSticks = false) 
        cout<<  "The game is over." <<endl<<endl; 
        return 0; 
         
         
    // Show the status of the game 
      int stickStatus = pileSize; 
      cout << "There are/is " << stickStatus << " stick(s) left." << endl << endl; 
   
 
    // Let's get a move from the current player 
    if (HUMAN_PLAYER) 
    { 
    // getHumanPlayerMove() - Ask the player for a number 
    // of sticks to take out of the pile 
      int pickstick; 
        int no = 3; 
      int nimSticks = pileSize; 
  
     
        while(no != 0) 
        { 
                cout << "Take how many sticks?: "; 
                cin >> pickstick; 
                cout << endl; 
                if(1 <= pickstick && pickstick <= 4) 
                { 
                        nimSticks -= pickstick; 
                        cout<< "Number of sticks left: " << nimSticks << endl << endl; 
                        /*if(winner() == 1) 
                        { 
                                cout << "The hu-man wins!" <<endl; 
                                exit(0); 
                        }*/ 
                        no = 0; 
                } 
                else 
                { 
                        no--; 
                        cout << "Error. " << no << " trys left. " <<endl; 
                }} 
        } 
 
    else if (COMPUTER_PLAYER) 
     
      int nimSticks = pileSize; 
        int pick = nimSticks - nimSticks % (1 + 4); 
        int just = nimSticks - pick; 
        int currentPlayerMove; 
         
        if (just == 0) 
        { 
                just = 1 + rand() % (4 - 1 + 1); 
                pick -= just;  
      } 
   
 
    // Now that we know how many sticks the current 
    // player wants to take, we remove them from the 
    // pile 
    nimSticks -= currentPlayerMove;  
 
   
 
  if (HUMAN_PLAYER == theWinner) 
  { 
    cout << "You win. Congratulations!" << endl; 
  } 
  else 
  { 
    cout << "I win! Better luck next time..." << endl; 
  } 
  
  return 0; 
}  

This post has been edited by RGLAsnakeMan: 05 March 2010 - 08:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: NIM Game

#2 RGLAsnakeMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 05-March 10

Re: NIM Game

Posted 05 March 2010 - 08:09 AM

*bump* :rolleyes2:
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: NIM Game

Posted 05 March 2010 - 08:59 AM

You're going to have difficulties getting help here for a couple of reasons:

1. You just threw some code up there with no explanation or questions.
2. The code looks like a bunch of thrown-together statements with little thought behind it; like a bunch of pseudo-code presented for us to mold into a working program for you.

Let's start with the first couple of lines:
const char HUMAN_PLAYER = true;
const char COMPUTER_PLAYER = false;

Is the data type represented by true and false of const char type (a single constant character)? Should either of these variables you're declaring BE either constant OR single characters?

Sit down with a piece of paper and write down what should be happening in this program from beginning to end. THEN start coding it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1