From 0eb592d7feea6c1c77f7b361823e5c66fb96079e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 12 Aug 2008 13:03:36 +0000 Subject: [PATCH] * Fixed off by one error in the number of clicks to finish the game (it can hardly be odd). * Fixed spelling error Genarate -> Generate. * Minor cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26941 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/pairs/PairsView.cpp | 45 +++++++++++++++++----------------- src/apps/pairs/PairsView.h | 11 +++++---- src/apps/pairs/PairsWindow.cpp | 43 ++++++++++++++++---------------- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/src/apps/pairs/PairsView.cpp b/src/apps/pairs/PairsView.cpp index eda8ddcc21..e612f2cae7 100644 --- a/src/apps/pairs/PairsView.cpp +++ b/src/apps/pairs/PairsView.cpp @@ -2,6 +2,7 @@ * Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved. * Distributed under the terms of the MIT License. */ + #include "PairsView.h" #include @@ -38,8 +39,8 @@ PairsView::PairsView(BRect frame, const char* name, uint32 resizingMode) } -void -PairsView::CreateGameBoard() +void +PairsView::CreateGameBoard() { // Show hidden buttons for (int32 i = 0; i < CountChildren(); i++) { @@ -47,7 +48,7 @@ PairsView::CreateGameBoard() if (child->IsHidden()) child->Show(); } - _GenarateCardPos(); + _GenerateCardPos(); } @@ -55,7 +56,7 @@ PairsView::~PairsView() { for (int i = 0; i < 8; i++) delete fCard[i]; - + for (int i = 0; i < 16; i++) delete fDeckCard[i]; } @@ -133,8 +134,8 @@ PairsView::_ReadRandomIcons() } } - // pick eight random bitmaps from the ones we got in the list - srand((unsigned)time(0)); + // pick eight random bitmaps from the ones we got in the list + srand((unsigned)time(0)); for (int i = 0; i < 8; i++) { int32 index = rand() % bitmaps.CountItems(); @@ -156,40 +157,40 @@ PairsView::_ReadRandomIcons() void PairsView::_SetPairsBoard() -{ +{ for (int i = 0; i < 16; i++) { fButtonMessage = new BMessage(kMsgCardButton); fButtonMessage->AddInt32("ButtonNum", i); - + int x = i % 4 * (kBitmapSize + 10) + 10; int y = i / 4 * (kBitmapSize + 10) + 10; - + fDeckCard[i] = new TopButton(x, y, fButtonMessage); AddChild(fDeckCard[i]); - } + } } void -PairsView::_GenarateCardPos() +PairsView::_GenerateCardPos() { _ReadRandomIcons(); - srand((unsigned)time(0)); - - int positions[16]; + srand((unsigned)time(0)); + + int positions[16]; for (int i = 0; i < 16; i++) positions[i] = i; - + for (int i = 16; i >= 1; i--) { int index = rand() % i; fRandPos[16-i] = positions[index]; - + for (int j = index; j < i - 1; j++) positions[j] = positions[j + 1]; } - + for (int i = 0; i < 16; i++) { fPosX[i] = (fRandPos[i]) % 4 * (kBitmapSize + 10) + 10; fPosY[i] = (fRandPos[i]) / 4 * (kBitmapSize + 10) + 10; @@ -201,17 +202,15 @@ void PairsView::Draw(BRect updateRect) { SetDrawingMode(B_OP_ALPHA); - + // draw rand pair 1 & 2 for (int i = 0; i < 16; i++) - DrawBitmap(fCard[i % 8], BPoint(fPosX[i], fPosY[i])); + DrawBitmap(fCard[i % 8], BPoint(fPosX[i], fPosY[i])); } int PairsView::GetIconFromPos(int pos) -{ - return fRandPos[pos]; +{ + return fRandPos[pos]; } - - diff --git a/src/apps/pairs/PairsView.h b/src/apps/pairs/PairsView.h index d6603ecb80..e5f4137b90 100644 --- a/src/apps/pairs/PairsView.h +++ b/src/apps/pairs/PairsView.h @@ -2,31 +2,32 @@ * Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved. * Distributed under the terms of the MIT License. */ - #ifndef PAIRS_VIEW_H #define PAIRS_VIEW_H #include + + class TopButton; class PairsView : public BView { public: PairsView(BRect frame, const char* name, uint32 resizingMode); - + virtual ~PairsView(); virtual void AttachedToWindow(); virtual void Draw(BRect updateRect); virtual void CreateGameBoard(); - + TopButton* fDeckCard[16]; int GetIconFromPos(int pos); - + private: void _SetPairsBoard(); void _ReadRandomIcons(); - void _GenarateCardPos(); + void _GenerateCardPos(); BMessage* fButtonMessage; BBitmap* fCard[8]; diff --git a/src/apps/pairs/PairsWindow.cpp b/src/apps/pairs/PairsWindow.cpp index a67a7e05ff..89cf596ba8 100644 --- a/src/apps/pairs/PairsWindow.cpp +++ b/src/apps/pairs/PairsWindow.cpp @@ -3,10 +3,12 @@ * Distributed under the terms of the MIT License. */ +#include "PairsWindow.h" + #include #include -#include +#include #include #include #include @@ -14,7 +16,6 @@ #include "Pairs.h" #include "PairsGlobal.h" -#include "PairsWindow.h" #include "PairsView.h" #include "PairsTopButton.h" @@ -50,24 +51,24 @@ void PairsWindow::MessageReceived(BMessage* message) { switch (message->what) { - case kMsgCardButton: + case kMsgCardButton: if (fIsPairsActive) { - fButtonClicks = fButtonClicks + 1; + fButtonClicks++; int32 num; if (message->FindInt32("ButtonNum", &num) < B_OK) break; - //look what Icon is behind a button + // look what Icon is behind a button for (int h = 0; h < 16; h++) { if (fPairsView->GetIconFromPos(h) == num) { fPairCard = (h % 8); fButton = fPairsView->GetIconFromPos(h); break; - } + } } - //gameplay + // gameplay fPairsView->fDeckCard[fButton]->Hide(); if (fIsFirstClick) { @@ -84,9 +85,9 @@ PairsWindow::MessageReceived(BMessage* message) } fIsFirstClick = !fIsFirstClick; - } + } break; - + case kMsgPairComparing: delete fPairComparing; fPairComparing = NULL; @@ -100,20 +101,20 @@ PairsWindow::MessageReceived(BMessage* message) fPairsView->fDeckCard[fButtonTmp]->Show(); } - //game end and results + // game end and results if (fFinishPairs == 8) { BString strAbout; - strAbout + strAbout << "Pairs\n" << "\twritten by Ralf Schülke\n" << "\tCopyright 2008, Haiku Inc.\n" << "\n" - << "You completed the game in " << fButtonClicks + 1 + << "You completed the game in " << fButtonClicks << " clicks.\n"; - - BAlert* alert = new BAlert("about", strAbout.String(), "New game", - "Quit game"); - + + BAlert* alert = new BAlert("about", strAbout.String(), + "New game", "Quit game"); + BTextView* view = alert->TextView(); BFont font; @@ -124,23 +125,21 @@ PairsWindow::MessageReceived(BMessage* message) font.SetFace(B_BOLD_FACE); view->SetFontAndColor(0, 6, &font); view->ResizeToPreferred(); - + if (alert->Go() == 0) { - //New game + // New game fButtonClicks = 0; fFinishPairs = 0; fPairsView->CreateGameBoard(); } else { - //Quit game + // Quit game be_app->PostMessage(B_QUIT_REQUESTED); } } break; - + default: BWindow::MessageReceived(message); break; } } - -