Results 1 to 8 of 8

Thread: mastermind problem

  1. Post mastermind problem

    hi guys i am working on the mastermind code and the tutor team tells me there is a problem that will not cause the game to crash but will stop the game from being complete the problem is found in the pegs.cpp constructor they say it's something to do with the for and if statement here is my code in the Pegs.cpp file

    #include "Pegs.hpp"
    #include <time.h>

    Pegs::Pegs(unsigned int pCount, unsigned int cCount)
    {
    init(pCount,cCount);
    }
    Pegs::Pegs(unsigned int pCount,unsigned int cCount,std::string guess)
    {
    init(pCount,cCount);
    for (unsigned int x = 0; x < pegCount;x++)
    {
    if (guess[x]>48 && guess[x] <48+(colourCount-1))
    {
    ourPegs [x] = guess[x]- 48;
    }
    }
    }
    void Pegs::init(unsigned int pCount,unsigned int cCount)
    {
    pegCount = pCount;
    colourCount = cCount;
    for(int x =0; x <pegCount; x++)
    {
    ourPegs.push_back(0);
    }
    }

    void Pegs::Random()

    {
    srand((unsigned int)time(NULL));
    for(unsigned int pCount =0; pCount<pegCount;pCount++)
    {
    unsigned int ourPeg = rand() % colourCount;
    ourPegs[pCount] = ourPeg;
    }
    }


    unsigned int Pegs::GetPeg(unsigned int pegNumber)
    {
    return ourPegs[pegNumber];
    }

    bool Pegs::Match(Pegs * matchTo, std::string &result)
    {
    unsigned int correctGuesses = 0;
    for (unsigned int peg =0; peg < pegCount; peg++)
    {
    if (matchTo->GetPeg(peg)== ourPegs[peg])
    {
    correctGuesses++;
    result+="B";
    }
    }
    if (correctGuesses ==pegCount)
    {
    return true;
    }
    return false;
    }

    any help will be grateful
    The pain of being alone...is not an easy one to bear..why is it...that I can understand your pain?
    But...I already have many people who are important to me now...and I can't let you hurt any of them...even if I have to kill you!

  2. #2

    Default

    Having a quick look. You are setting guess to be a string. But then checking if it is an int.

    You are checking guess[x] > 48.

    My C++ is very rusty. Just started on the course again due to ill health. But wouldnt that being compairing a string to an int. So it will never satisfy?

  3. Default

    i'm not sure i have just looked over the video and took a screenshot of the guess loop i am trying to work out

    the code is

    #include "Pegs.hpp"
    #include <time.h>

    Pegs::Pegs(unsigned int pCount, unsigned int cCount)
    {
    init(pCount, cCount);
    }
    Pegs::Pegs(unsigned int pCount, unsigned int cCount, std::String guess)
    {
    init(pcount,cCount);
    for (unsigned int x=0; x < pegCount; x++)
    {
    if (guess[X]>48 && guess[X]<48+(colourCount-1)
    {
    ourPegs [x] = guess [x] -48;
    }
    }
    }
    The pain of being alone...is not an easy one to bear..why is it...that I can understand your pain?
    But...I already have many people who are important to me now...and I can't let you hurt any of them...even if I have to kill you!

  4. #4

    Default

    What would a normal "ColourCount" Value be?

  5. Default

    The value of the colour count starts at 4 which increased by 1 ever level until level 10, then the value stays at 9 colours
    The pain of being alone...is not an easy one to bear..why is it...that I can understand your pain?
    But...I already have many people who are important to me now...and I can't let you hurt any of them...even if I have to kill you!

  6. #6

    Default

    And what would a typical guess[x] value be?

    Asking because if
    Code:
    Guess[x]
    is something like 60~ and then colourCount is no more than 9. Then you are trying to get if (guess[X]>48 && guess[X]<48+(colourCount-1)

    Which would be. 60 > 48. YES. 60<48+9-1.NO so then
    Code:
    ourPegs [x] = guess [x] -48;
    This would never be achived. So you cannot the minumum for 48+(colourCount-1) would be 48+9-1 which would be 56. So it would have to be between 48 and 56. But if Guess[x] is not between then them what it will never run ourPegs [x] = guess [x] -48;.

    That is the only thing I can see which is wrong with the code. But that might be your desired effect
    Last edited by SamboH; 06-29-2015 at 04:49 PM.

  7. Default

    im not to sure the full existent of the [x] statement all i know is u followed the LIT and it was in that
    The pain of being alone...is not an easy one to bear..why is it...that I can understand your pain?
    But...I already have many people who are important to me now...and I can't let you hurt any of them...even if I have to kill you!

  8. #8

    Default

    [x] would be the value from the for loop.

    So you are doing
    Code:
    for (unsigned int x=0; x < pegCount; x++)
    So that is saying that x is going to start at 0. So when you call
    Code:
     guess[x]
    That is pulling the array index value from guess. So it getting the value of the array guess at position 0. So
    Code:
    guess[0]
    then it will increment through x until it is the same as "pegCount". So if the value of
    Code:
    guess[x]
    is not between 48 and 56 then the rest of the code will never run inside that loop.

    Might be worth asking the tutors about this one. As I have confused myself while writting this. Again my C++ is a bit rusty as have just started the course again.

Similar Threads

  1. New problem
    By misswho42 in forum Newbies Area
    Replies: 2
    Last Post: 07-19-2013, 04:24 PM
  2. 3ds max problem
    By Crazee in forum T2G Artist and Animator, Technical Discussions
    Replies: 10
    Last Post: 12-12-2012, 01:54 PM
  3. i have a problem
    By HollowEspada1 in forum T2G QA Tester Discussions
    Replies: 0
    Last Post: 12-07-2012, 01:36 AM
  4. max problem
    By phykal in forum T2G Artist and Animator, Technical Discussions
    Replies: 4
    Last Post: 03-18-2012, 01:38 PM
  5. 64 Bit problem!
    By DanH in forum T2G Designer, General Discussions
    Replies: 6
    Last Post: 03-02-2011, 10:02 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
  •