]> git.saurik.com Git - wxWidgets.git/blobdiff - demos/forty/game.cpp
1.
[wxWidgets.git] / demos / forty / game.cpp
index 4ac59941ebc892ec93e13a78115d8bcf80029d9d..9eed283867f3924fe0e2cfa28a430cb5419c8a2c 100644 (file)
@@ -72,6 +72,30 @@ Game::Game(int wins, int games, int score) :
 }
 
 
+void Game::Layout()
+{
+    int i;
+
+    m_pack->SetPos(2, 2 + 4 * (CardHeight + 2));
+
+    m_discard->SetPos(2, 2 + 5 * (CardHeight + 2));
+
+    for (i = 0; i < 8; i++)
+    {
+                m_foundations[i]->SetPos(2 + (i / 4) * (CardWidth + 2),
+                                         2 + (i % 4) * (CardHeight + 2));
+    }
+
+    for (i = 0; i < 10; i++)
+    {
+        m_bases[i]->SetPos(8 + (i + 2) * (CardWidth + 2), 2);
+    }
+    delete m_bmap;
+    delete m_bmapCard;
+    m_bmap = 0;
+    m_bmapCard = 0;
+}
+
 // Make sure we delete all objects created by the game object
 Game::~Game()
 {
@@ -139,7 +163,7 @@ void Game::DoMove(wxDC& dc, Pile* src, Pile* dest)
     {
                if (src == dest)
                {
-                       wxMessageBox("Game::DoMove() src == dest", "Debug message",
+                       wxMessageBox(_T("Game::DoMove() src == dest"), _T("Debug message"),
                                   wxOK | wxICON_EXCLAMATION);
                }
                m_moves[m_moveIndex].src = src;
@@ -151,16 +175,46 @@ void Game::DoMove(wxDC& dc, Pile* src, Pile* dest)
     }
     else
     {
-               wxMessageBox("Game::DoMove() Undo buffer full", "Debug message",
+               wxMessageBox(_T("Game::DoMove() Undo buffer full"), _T("Debug message"),
                           wxOK | wxICON_EXCLAMATION);
     }
 
-    if (!m_inPlay)
+       if (!m_inPlay)
        {
                m_inPlay = TRUE;
                m_numGames++;
        }
-    DisplayScore(dc);
+       DisplayScore(dc);
+
+       if (HaveYouWon())
+       {
+               wxWindow *frame = wxTheApp->GetTopWindow();
+               wxWindow *canvas = (wxWindow *) NULL;
+
+               if (frame)
+               {
+                       wxNode *node = (wxNode *)frame->GetChildren().GetFirst();
+                       if (node) canvas = (wxWindow*)node->GetData();
+               }
+
+               // This game is over
+               m_inPlay = FALSE;
+
+               // Redraw the score box to update games won
+               DisplayScore(dc);
+
+               if (wxMessageBox(_T("Do you wish to play again?"),
+                       _T("Well Done, You have won!"), wxYES_NO | wxICON_QUESTION) == wxYES)
+               {
+                       Deal();
+                       canvas->Refresh();
+               }
+               else
+               {
+                       // user cancelled the dialog - exit the app
+                       ((wxFrame*)canvas->GetParent())->Close(TRUE);
+               }
+       }
 }
 
 
@@ -187,25 +241,25 @@ void Game::DisplayScore(wxDC& dc)
        int w, h;
        {
                long width, height;
-               dc.GetTextExtent("Average score:m_x", &width, &height);
+               dc.GetTextExtent(_T("Average score:m_x"), &width, &height);
                w = width;
                h = height;
        }
        dc.DrawRectangle(x + w, y, 20, 4 * h);
 
-    char str[80];
-    sprintf(str, "%d", m_currentScore);
-    dc.DrawText("Score:", x, y);
+    wxString str;
+    str.Printf(_T("%d"), m_currentScore);
+    dc.DrawText(_T("Score:"), x, y);
     dc.DrawText(str, x + w, y);
     y += h;
 
-    sprintf(str, "%d", m_numGames);
-    dc.DrawText("Games played:", x, y);
+    str.Printf(_T("%d"), m_numGames);
+    dc.DrawText(_T("Games played:"), x, y);
     dc.DrawText(str, x + w, y);
     y += h;
 
-    sprintf(str, "%d", m_numWins);
-    dc.DrawText("Games won:", x, y);
+    str.Printf(_T("%d"), m_numWins);
+    dc.DrawText(_T("Games won:"), x, y);
     dc.DrawText(str, x + w, y);
     y += h;
 
@@ -214,8 +268,8 @@ void Game::DisplayScore(wxDC& dc)
        {
                average = (2 * (m_currentScore + m_totalScore) + m_numGames ) / (2 * m_numGames);
        }
-    sprintf(str, "%d", average);
-    dc.DrawText("Average score:", x, y);
+    str.Printf(_T("%d"), average);
+    dc.DrawText(_T("Average score:"), x, y);
     dc.DrawText(str, x + w, y);
 }
 
@@ -289,6 +343,7 @@ void Game::Redraw(wxDC& dc)
                // Initialise the card bitmap to the background colour
                wxMemoryDC memoryDC;
                memoryDC.SelectObject(*m_bmapCard);
+        memoryDC.SetPen( *wxTRANSPARENT_PEN );
                memoryDC.SetBrush(FortyApp::BackgroundBrush());
                memoryDC.DrawRectangle(0, 0, CardWidth, CardHeight);
                memoryDC.SelectObject(*m_bmap);
@@ -409,8 +464,8 @@ void Game::LButtonDblClk(wxDC& dc, int x, int y)
                        {
                                for(i = 0; i < 4; i++)
                                {
-                                       Card* m_topCard;
-                                       if ((m_topCard = m_foundations[i]->GetTopCard()))
+                                       Card* m_topCard = m_foundations[i]->GetTopCard();
+                                       if ( m_topCard )
                     {
                                                if (m_topCard->GetSuit() == card->GetSuit() &&
                             m_foundations[i + 4] != pile &&
@@ -713,14 +768,14 @@ void Pack::Shuffle()
     }
     for (i = 0; i <= m_topCard; i++)
     {
-       int pos = rand() % (m_topCard + 1);
-       while (temp[pos])
-       {
-           pos--;
-           if (pos < 0) pos = m_topCard;
-       }
-       m_cards[i]->TurnCard(facedown);
-       temp[pos] = m_cards[i];
+           int pos = rand() % (m_topCard + 1);
+           while (temp[pos])
+           {
+               pos--;
+               if (pos < 0) pos = m_topCard;
+           }
+           m_cards[i]->TurnCard(facedown);
+           temp[pos] = m_cards[i];
         m_cards[i] = 0;
     }
 
@@ -729,12 +784,12 @@ void Pack::Shuffle()
         // unoccupied position after the random position.
     for (i = 0; i <= m_topCard; i++)
     {
-       int pos = rand() % (m_topCard + 1);
-       while (m_cards[pos])
-       {
-           pos++;
+           int pos = rand() % (m_topCard + 1);
+           while (m_cards[pos])
+           {
+               pos++;
             if (pos > m_topCard) pos = 0;
-       }
+           }
         m_cards[pos] = temp[i];
     }
 }
@@ -743,9 +798,10 @@ void Pack::Redraw(wxDC& dc)
 {
     Pile::Redraw(dc);
 
-    char str[10];
-    sprintf(str, "%d  ", m_topCard + 1);
+    wxString str;
+    str.Printf(_T("%d  "), m_topCard + 1);
 
+    dc.SetBackgroundMode( wxSOLID );
        dc.SetTextBackground(FortyApp::BackgroundColour());
        dc.SetTextForeground(FortyApp::TextColour());
     dc.DrawText(str, m_x + CardWidth + 5, m_y + CardHeight / 2);
@@ -760,7 +816,7 @@ void Pack::AddCard(Card* card)
     }
     else
     {
-               wxMessageBox("Pack::AddCard() Undo error", "Forty Thieves: Warning",
+               wxMessageBox(_T("Pack::AddCard() Undo error"), _T("Forty Thieves: Warning"),
                   wxOK | wxICON_EXCLAMATION);
     }
     card->TurnCard(facedown);
@@ -771,7 +827,7 @@ Pack::~Pack()
 {
     for (m_topCard = 0; m_topCard < NumCards; m_topCard++)
     {
-       delete m_cards[m_topCard];
+           delete m_cards[m_topCard];
     }
 };
 
@@ -791,9 +847,9 @@ bool Base::AcceptCard(Card* card)
 
     if (m_topCard >= 0)
     {
-       if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
-           m_cards[m_topCard]->GetPipValue() - 1 == card->GetPipValue())
-       {
+           if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
+               m_cards[m_topCard]->GetPipValue() - 1 == card->GetPipValue())
+           {
             retval = TRUE;
         }
     }
@@ -825,15 +881,15 @@ bool Foundation::AcceptCard(Card* card)
 
     if (m_topCard >= 0)
     {
-       if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
-           m_cards[m_topCard]->GetPipValue() + 1 == card->GetPipValue())
-       {
+           if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
+               m_cards[m_topCard]->GetPipValue() + 1 == card->GetPipValue())
+           {
             retval = TRUE;
         }
     }
     else if (card->GetPipValue() == 1)
     {
-       // It's an ace and the pile is empty - ACCEPT
+           // It's an ace and the pile is empty - ACCEPT
         retval = TRUE;
     }
     return retval;
@@ -857,22 +913,22 @@ void Discard::Redraw(wxDC& dc)
 {
     if (m_topCard >= 0)
     {
-       if (m_dx == 0 && m_dy == 0)
-       {
+           if (m_dx == 0 && m_dy == 0)
+           {
             m_cards[m_topCard]->Draw(dc, m_x, m_y);
-       }
-       else
-       {
-           int x = m_x;
-           int y = m_y;
-           for (int i = 0; i <= m_topCard; i++)
+           }
+           else
            {
-               m_cards[i]->Draw(dc, x, y);
-               x += m_dx;
-               y += m_dy;
-               if (i == 31)
-               {
-                   x = m_x;
+               int x = m_x;
+               int y = m_y;
+               for (int i = 0; i <= m_topCard; i++)
+               {
+                       m_cards[i]->Draw(dc, x, y);
+                       x += m_dx;
+                       y += m_dy;
+                       if (i == 31)
+                       {
+                           x = m_x;
                     y = m_y + CardHeight / 3;
                 }
             }
@@ -889,18 +945,18 @@ void Discard::GetTopCardPos(int& x, int& y)
 {
     if (m_topCard < 0)
     {
-       x = m_x;
-       y = m_y;
+           x = m_x;
+           y = m_y;
     }
     else if (m_topCard > 31)
     {
-       x = m_x + m_dx * (m_topCard - 32);
-       y = m_y + CardHeight / 3;
+           x = m_x + m_dx * (m_topCard - 32);
+           y = m_y + CardHeight / 3;
     }
     else
     {
-       x = m_x + m_dx * m_topCard;
-       y = m_y;
+           x = m_x + m_dx * m_topCard;
+           y = m_y;
     }
 }