Solved

Highscore system.

Posted on 2002-04-18
2
193 Views
Last Modified: 2013-11-20
Hi,

I have created a game but am having trouble solving a scoring problem.

Here's my code:

     if(!GameOver){bQuit=TRUE;}
     else{bQuit=FALSE;}

     CRegistry myreg(HKEY_LOCAL_MACHINE);

     CString MyKey = TEXT("Software\\TwosUp");

     CString m_Hightemp1,m_Hightemp2,m_Hightemp3,m_Hightemp4,m_Hightemp5;

     myreg.LoadKey(MyKey, TEXT("Highscore1"), m_Hightemp1);
     myreg.LoadKey(MyKey, TEXT("Highscore2"), m_Hightemp2);
     myreg.LoadKey(MyKey, TEXT("Highscore3"), m_Hightemp3);
     myreg.LoadKey(MyKey, TEXT("Highscore4"), m_Hightemp4);
     myreg.LoadKey(MyKey, TEXT("Highscore5"), m_Hightemp5);

     int m_Highscore1 = atoi(m_Hightemp1);
     int m_Highscore2 = atoi(m_Hightemp2);
     int m_Highscore3 = atoi(m_Hightemp3);
     int m_Highscore4 = atoi(m_Hightemp4);
     int m_Highscore5 = atoi(m_Hightemp5);

     int Stop = 0;
     int WhichCase=0;

     if(m_Highscore5<m_iScore && m_Highscore4>=m_iScore)
     {
          m_Highscore5 = m_iScore;WhichCase=5;Stop = 1;
     }
     if(Stop==0)
     if(m_Highscore4<m_iScore && m_Highscore5>=m_iScore)
     {
          m_Highscore4 = m_iScore;WhichCase=4;Stop = 1;
     }
     if(Stop==0)
     if(m_Highscore3<m_iScore && m_Highscore4>=m_iScore)
     {
          m_Highscore3 = m_iScore;WhichCase=3;Stop = 1;
     }
     if(Stop==0)
     if(m_Highscore2<m_iScore && m_Highscore3>=m_iScore)
     {
          m_Highscore2 = m_iScore;WhichCase=2;Stop = 1;
     }
     if(Stop==0)
     if(m_Highscore1<m_iScore)
     {
          m_Highscore1 = m_iScore;WhichCase=1;Stop = 1;
     }


     CString TempString = "";

     switch(WhichCase){

     case 1:     TempString.Format("%d",m_iScore);
               myreg.SaveKey(MyKey, TEXT("Highscore1"), TempString);break;

     case 2: TempString.Format("%d",m_iScore);
               myreg.SaveKey(MyKey, TEXT("Highscore2"), TempString);break;

     case 3:     TempString.Format("%d",m_iScore);
               myreg.SaveKey(MyKey, TEXT("Highscore3"), TempString);break;

     case 4: TempString.Format("%d",m_iScore);
               myreg.SaveKey(MyKey, TEXT("Highscore4"), TempString);break;

     case 5: TempString.Format("%d",m_iScore);
               myreg.SaveKey(MyKey, TEXT("Highscore5"), TempString);break;
     }

     OnOK();


It's the conditional stuff which I can't get right, can anyone help me on this?  All I want to do is if current score (m_iScore) is higher than highscore1 set high score1 or if current score is higher than highscore2 but lower than highscore1 set high score2, etc.

There are five slot's for these scores, any help would be appriciated.
0
Comment
Question by:zippyuk
2 Comments
 
LVL 3

Expert Comment

by:Crius
ID: 6950819
The way I read your code, you are only replacing 1 score. Is this really what you want? I suggest the following conditional statement:

if(m_iScore > m_Highscore1)
{
   //New score is highest, reset all scores below it.
   m_HighScore5 = m_HighScore4;
   m_HighScore4 = m_HighScore3;
   m_HighScore3 = m_HighScore2;
   m_HighScore2 = m_HighScore1;
   m_HighScore1 = m_iScore;
}
else if(m_iScore > m_Highscore2)
{
   m_HighScore5 = m_HighScore4;
   m_HighScore4 = m_HighScore3;
   m_HighScore3 = m_HighScore2;
   m_HighScore2 = m_iScore;
}
else if(m_iScore > m_Highscore3)
{
   m_HighScore5 = m_HighScore4;
   m_HighScore4 = m_HighScore3;
   m_HighScore3 = m_iScore;
}
else if(m_iScore > m_Highscore4)
{
   m_HighScore5 = m_HighScore4;
   m_HighScore4 = m_iScore;
}
else if(m_iScore > m_Highscore5)
{
   m_HighScore5 = m_iScore;
}

Then write in all the keys...


Just one other note: You can rewrite your code to be:
    if(m_Highscore5<m_iScore && m_Highscore4>=m_iScore)
    {
         m_Highscore5 = m_iScore;WhichCase=5;
    }
    else
    if(m_Highscore4<m_iScore && m_Highscore3>=m_iScore)
    {
         m_Highscore4 = m_iScore;WhichCase=4;
    }
    else
    if(m_Highscore3<m_iScore && m_Highscore2>=m_iScore)
    {
         m_Highscore3 = m_iScore;WhichCase=3;
    }
    else
    if(m_Highscore2<m_iScore && m_Highscore1>=m_iScore)
    {
         m_Highscore2 = m_iScore;WhichCase=2;
    }
    else
    if(m_Highscore1<m_iScore)
    {
         m_Highscore1 = m_iScore;WhichCase=1;
    }

This way you get rid of the stop variable too. Also note, I corrected some code above...

    if(m_Highscore4<m_iScore && m_Highscore3>=m_iScore)
    if(m_Highscore3<m_iScore && m_Highscore2>=m_iScore)
    if(m_Highscore2<m_iScore && m_Highscore1>=m_iScore)
0
 
LVL 16

Accepted Solution

by:
AlexNek earned 31 total points
ID: 6952014
2 zippyuk
Why you don't want to use sorted arrays? (With 6 items)
Here is C-way

int score[6];

//load score[0]...[4]
score[5] = m_iScore;
qsort( (void *)&score[0], (size_t)6, sizeof( int ), CompareInt );
//store score[0]...[4]


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to know only "File created" with EventLog 3 32
fizzArray  challenge 1 58
unix example issues 18 73
how to split multiple lines delimiter : 8 78
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short film showing how OnPage and Connectwise integration works.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now