3 Replies - 2750 Views - Last Post: 03 June 2012 - 07:44 PM Rate Topic: -----

#1 Sp1995   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-May 12

Number guessing game: Computer player needs to revise it's guesses

Posted 03 June 2012 - 05:03 PM

Hi. I've to create a number guessing game in which there is an instance of human vs human, computer vs computer, and human vs computer. Now I've got the base core of the program working, using inheritance I've got the program to work for all three instances using a play function. Now the problem I'm having with is the fact that the computer player does not make an educated guess whereas obviously a human can if we are told if our last number was to high or to high.

What I basically need help with is implementing a system where the computer player is told that it's last guess was to low or to high and revising it's next guess using that information. So far I do not have any of that working, and only the base of the project.

Here is my code/files:

main
#include "Player.h"
#include "HumanPlayer.h"
#include "ComputerPlayer.h"



using namespace Saagar;
int main()
{
   Player player1, player2, play;
   HumanPlayer human1, human2;
   ComputerPlayer computer1, computer2;
   char option, ans;


do
{
cout << "Welcome to the Guessing Game: by Saagar Patel " << endl << endl;
cout << "Would you like to play against a human, against a computer,"
" or watch two computers battle it out?" << endl;
cout << "Choose '1' for human vs human, '2' for human vs computer, or '3'"
" for computer vs computer" << endl;

cin >> option;

if (option == '1')
{
   cout << "Human Vs. Human: "<< endl << endl;
   play.play(human1, human2);
}
else if (option == '2')
{
   cout << "Human Vs. Computer" << endl << endl;
   play.play(human1, computer1);
}
else if (option == '3')
{
  cout << "Computer Vs. Computer: " << endl << endl;
  play.play(computer1, computer2);
}

cout << "\nWould you like to play again? (Y/N): ";
cin >> ans;
cout << "\n";
}while(ans == 'Y' || ans == 'y');


system("PAUSE");



}



player.h
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <ctime>



using namespace std;

namespace Saagar
{


    class Player
    {
      public:
      Player();
      Player(Player&);
      ~Player();

      virtual int getGuess() const;

    void play(Player &player1, Player &player2);
    virtual bool checkForWin(int guess, int answer);

    };




}
#endif // PLAYER_H_INCLUDED



PlayerImplementation.cpp
#include "Player.h"

namespace Saagar
{

    Player::Player()
    {
         srand(time(0));

    }

    Player::Player(Player&)
    {

    }
    Player::~Player()
    {

    }

    int Player:: getGuess() const
    {
        return 0;
    }

    bool Player::checkForWin(int guess, int answer)
    {

        if(answer==guess)
        {
            cout << "You guessed the right number! You win!" << endl;
            return true;
        }
        else if (answer < guess)
        cout << "Your guess is too high." << endl << endl;
        else
        cout << "Your guess is too low." << endl << endl;
        return false;
    }
    void Player::play(Player &player1, Player &player2)
    {

        int answer = 0, guess = 0;
        answer = rand()%100;
        bool win = false;

        while (!win)
        {
            cout << "Player 1's turn to guess." << endl;
            guess = player1.getGuess();
            win = checkForWin(guess, answer);
            if (win) return;

            cout << "Player 2's turn to guess." << endl;
            guess = player2.getGuess();
            win = checkForWin(guess, answer);
        }
    }



}



ComputerPlayer.h
#ifndef COMPUTERPLAYER_H_INCLUDED
#define COMPUTERPLAYER_H_INCLUDED
#include "Player.h"


namespace Saagar
{

    class ComputerPlayer : public Player
     {

         public:
         ComputerPlayer();
         ComputerPlayer(ComputerPlayer&);
         ~ComputerPlayer();

        int getGuess() const;
        bool checkForWin(int guess, int answer);




     };
}


#endif // COMPUTERPLAYER_H_INCLUDED


ComputerPlayerImplementation.cpp
#include "ComputerPlayer.h"


namespace Saagar
{

    ComputerPlayer::ComputerPlayer()
    {

    }

    ComputerPlayer::ComputerPlayer(ComputerPlayer&)
    {

    }
    ComputerPlayer::~ComputerPlayer()
    {

    }

    bool ComputerPlayer::checkForWin(int answer, int guess)
    {

    }

    int ComputerPlayer::getGuess()const
    {
        int guess;

cout << "The computer will now randomly generate a value: ";

    guess = rand()%100;
    cout << guess << endl;



        return guess;

    }



}


HumanPlayer.h
#ifndef HUMANPLAYER_H_INCLUDED
#define HUMANPLAYER_H_INCLUDED
#include "Player.h"

namespace Saagar
{
    class HumanPlayer : public Player
    {
        public:
        HumanPlayer();
        HumanPlayer(HumanPlayer&);
        ~HumanPlayer();

         int getGuess() const;



    };
}



#endif // HUMANPLAYER_H_INCLUDED



HumanPlayerImplementation.cpp
#include "HumanPlayer.h"

namespace Saagar
{

    HumanPlayer::HumanPlayer()
    {

    }
    HumanPlayer::HumanPlayer(HumanPlayer&)
    {

    }
    HumanPlayer::~HumanPlayer()
    {

    }

int HumanPlayer::getGuess()const
{
    int guess;

   do
   {


    cout << "What is your guess? (Between 0-100): ";
    cin >> guess;
   }while(guess < 0 || guess > 100);


   return guess;

}




}


Thank you in advance.

If the question was still not understood, let me clear it up a bit.

I need a way so that the computer player can revise it's next guess based on information sent to it, for example if it's last guess was too high, it'll make sure it's next guess will not be higher then the last number.

Thank you once again, I appreciate any help I can get and will continue to work on this in the meantime. If I figure it out, I'll update here.

Is This A Good Question/Topic? 0
  • +

Replies To: Number guessing game: Computer player needs to revise it's guesses

#2 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Number guessing game: Computer player needs to revise it's guesses

Posted 03 June 2012 - 06:51 PM

To guess a value in a range:

Let us say range is 20 to 30.
The difference is 30 - 20 = 10.
So use rand to give value 0 - 10 (inclusive) or 1 - 9 (exclusive).
Add to lower limit to give guess.
Was This Post Helpful? 1
  • +
  • -

#3 Sp1995   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-May 12

Re: Number guessing game: Computer player needs to revise it's guesses

Posted 03 June 2012 - 07:05 PM

Hmm.. I've figured that part out, but still cannot figure out how to implement that in my actual code. Thank you nevertheless. I'm still fairly new to c++ and programming as a whole, therefore the syntax part of it is confusing for me.
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Number guessing game: Computer player needs to revise it's guesses

Posted 03 June 2012 - 07:44 PM

You could write a small program to check rand out.

rand
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1