Ticket #5615: pairs-multiple-board-sizes.diff
File pairs-multiple-board-sizes.diff, 14.9 KB (added by , 14 years ago) |
---|
-
src/apps/pairs/Pairs.cpp
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 … … 16 17 17 18 18 19 Pairs::Pairs() 19 : BApplication(kSignature), 20 fWindow(NULL) 20 : 21 BApplication(kSignature), 22 fWindow(NULL) 21 23 { 22 24 be_locale->GetAppCatalog(&fCatalog); 23 25 } -
src/apps/pairs/PairsView.h
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 … … 15 16 class PairsView : public BView { 16 17 public: 17 18 PairsView(BRect frame, const char* name, 19 int width, int height, 18 20 uint32 resizingMode); 19 21 20 22 virtual ~PairsView(); 21 23 virtual void AttachedToWindow(); 22 24 virtual void Draw(BRect updateRect); 23 25 virtual void CreateGameBoard(); 26 27 int fWidth; 28 int fHeight; 29 int fNumOfCards; 24 30 25 TopButton* fDeckCard[16];31 BList fDeckCard; 26 32 int GetIconFromPos(int pos); 27 33 28 34 private: … … 32 38 bool _HasBitmap(BList& bitmaps, BBitmap* bitmap); 33 39 34 40 BMessage* fButtonMessage; 35 B Bitmap* fCard[8];36 int fRandPos[16];37 int fPosX[16];38 int fPosY[16];41 BList fCardBitmaps; 42 int* fRandPos; 43 int* fPosX; 44 int* fPosY; 39 45 }; 40 46 41 47 -
src/apps/pairs/PairsTopButton.h
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 -
src/apps/pairs/Pairs.h
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 -
src/apps/pairs/PairsGlobal.h
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 … … 12 13 const uint32 kMsgCardButton = 'card'; 13 14 const uint32 kMsgPairComparing = 'pcom'; 14 15 const int kBitmapSize = 64; 16 const int kSpaceSize = 10; 15 17 16 18 17 19 #endif // PAIRS_GLOBAL_H -
src/apps/pairs/PairsWindow.cpp
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 … … 12 13 #include <Button.h> 13 14 #include <Catalog.h> 14 15 #include <Locale.h> 16 #include <Menu.h> 17 #include <MenuBar.h> 18 #include <MenuItem.h> 15 19 #include <MessageRunner.h> 16 20 #include <String.h> 17 21 #include <TextView.h> … … 25 29 #undef TR_CONTEXT 26 30 #define TR_CONTEXT "PairsWindow" 27 31 32 const uint32 MENU_NEW = 'MGnw'; 33 const uint32 MENU_SIZE = 'MGsz'; 34 const uint32 MENU_QUIT = 'MGqu'; 35 28 36 PairsWindow::PairsWindow() 29 : BWindow(BRect(100, 100, 405, 4 05), "Pairs", B_TITLED_WINDOW,37 : BWindow(BRect(100, 100, 405, 423), "Pairs", B_TITLED_WINDOW, 30 38 B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE 31 39 | B_NOT_RESIZABLE | B_NOT_ZOOMABLE), 32 33 34 35 36 37 38 39 40 40 fPairComparing(NULL), 41 fIsFirstClick(true), 42 fIsPairsActive(true), 43 fPairCard(0), 44 fPairCardTmp(0), 45 fButtonTmp(0), 46 fButton(0), 47 fButtonClicks(0), 48 fFinishPairs(0) 41 49 { 42 fPairsView = new PairsView(Bounds().InsetByCopy(0, 0).OffsetToSelf(0, 0), 43 "PairsView", B_FOLLOW_NONE); 44 fPairsView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 45 AddChild(fPairsView); 50 _MakeMenuBar(); 51 52 _MakeGameView(4, 4); 46 53 47 54 CenterOnScreen(); 48 55 } … … 55 62 56 63 57 64 void 65 PairsWindow::_MakeMenuBar() 66 { 67 fMenuBar = new BMenuBar(BRect(0, 0, 0, 0), "menubar"); 68 AddChild(fMenuBar); 69 70 BMenu* menu = new BMenu(TR("Game")); 71 fMenuBar->AddItem(menu); 72 73 BMenuItem* menuItem; 74 menu->AddItem(menuItem = new BMenuItem(TR("New"), 75 new BMessage(MENU_NEW), 'N')); 76 77 menu->AddSeparatorItem(); 78 79 BMenu* sizeMenu = new BMenu(TR("Size")); 80 sizeMenu->SetRadioMode(true); 81 82 BMessage* sizeMessage = new BMessage(MENU_SIZE); 83 sizeMessage->AddInt32("width",4); 84 sizeMessage->AddInt32("height",4); 85 sizeMenu->AddItem(menuItem = new BMenuItem(TR("Beginner (4x4)"), 86 sizeMessage)); 87 menuItem->SetMarked(true); 88 89 sizeMessage = new BMessage(MENU_SIZE); 90 sizeMessage->AddInt32("width",6); 91 sizeMessage->AddInt32("height",6); 92 sizeMenu->AddItem(menuItem = new BMenuItem(TR("Intermediate (6x6)"), 93 sizeMessage)); 94 95 sizeMessage = new BMessage(MENU_SIZE); 96 sizeMessage->AddInt32("width",8); 97 sizeMessage->AddInt32("height",8); 98 sizeMenu->AddItem(menuItem = new BMenuItem(TR("Expert (8x8)"), 99 sizeMessage)); 100 101 menu->AddItem(sizeMenu); 102 103 menu->AddSeparatorItem(); 104 105 menu->AddItem(menuItem = new BMenuItem(TR("Quit"), 106 new BMessage(MENU_QUIT), 'Q')); 107 } 108 109 110 void 111 PairsWindow::_MakeGameView(int width, int height) 112 { 113 BRect viewBounds = Bounds(); 114 viewBounds.top = fMenuBar->Bounds().Height() + 1; 115 116 fPairsView = new PairsView(viewBounds, "PairsView", width, height, 117 B_FOLLOW_NONE); 118 fPairsView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 119 AddChild(fPairsView); 120 } 121 122 123 void 124 PairsWindow::NewGame() 125 { 126 fButtonClicks = 0; 127 fFinishPairs = 0; 128 fPairsView->CreateGameBoard(); 129 } 130 131 132 void 133 PairsWindow::SetGameSize(int width, int height) { 134 ResizeTo((kBitmapSize + kSpaceSize) * width + kSpaceSize, 135 (kBitmapSize + kSpaceSize) * height + kSpaceSize 136 + fMenuBar->Bounds().Height()); 137 RemoveChild(fPairsView); 138 delete fPairsView; 139 _MakeGameView(width, height); 140 NewGame(); 141 } 142 143 144 void 58 145 PairsWindow::MessageReceived(BMessage* message) 59 { 146 { 60 147 switch (message->what) { 148 case MENU_NEW: 149 NewGame(); 150 break; 151 case MENU_SIZE: 152 int32 width; 153 int32 height; 154 message->FindInt32("width", &width); 155 message->FindInt32("height", &height); 156 SetGameSize(width, height); 157 break; 158 case MENU_QUIT: 159 be_app->PostMessage(B_QUIT_REQUESTED); 160 break; 61 161 case kMsgCardButton: 62 162 if (fIsPairsActive) { 63 163 fButtonClicks++; … … 66 166 if (message->FindInt32("ButtonNum", &num) < B_OK) 67 167 break; 68 168 69 // look what Icon is behind a button 70 for (int h = 0; h < 16; h++) {169 // look what Icon is behind a button 170 for (int h = 0; h < fPairsView->fNumOfCards; h++) { 71 171 if (fPairsView->GetIconFromPos(h) == num) { 72 fPairCard = (h % 8);172 fPairCard = (h % fPairsView->fNumOfCards / 2); 73 173 fButton = fPairsView->GetIconFromPos(h); 74 174 break; 75 175 } 76 176 } 77 177 78 178 // gameplay 79 fPairsView->fDeckCard[fButton]->Hide();179 ((TopButton*)fPairsView->fDeckCard.ItemAt(fButton))->Hide(); 80 180 81 181 if (fIsFirstClick) { 82 182 fPairCardTmp = fPairCard; … … 104 204 105 205 if (fPairCard == fPairCardTmp) { 106 206 fFinishPairs++; 107 } else { 108 fPairsView->fDeckCard[fButton]->Show();109 fPairsView->fDeckCard[fButtonTmp]->Show();207 } else { 208 ((TopButton*)fPairsView->fDeckCard.ItemAt(fButton))->Show(); 209 ((TopButton*)fPairsView->fDeckCard.ItemAt(fButtonTmp))->Show(); 110 210 } 111 211 112 212 // game end and results 113 if (fFinishPairs == 8) {213 if (fFinishPairs == fPairsView->fNumOfCards / 2) { 114 214 BString strAbout; 115 215 strAbout 116 216 << "Pairs\n" … … 136 236 137 237 if (alert->Go() == 0) { 138 238 // New game 139 fButtonClicks = 0; 140 fFinishPairs = 0; 141 fPairsView->CreateGameBoard(); 239 NewGame(); 142 240 } else { 143 241 // Quit game 144 242 be_app->PostMessage(B_QUIT_REQUESTED); -
src/apps/pairs/PairsView.cpp
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 … … 26 27 #include "PairsGlobal.h" 27 28 #include "PairsTopButton.h" 28 29 29 // TODO: support custom board sizes 30 31 PairsView::PairsView(BRect frame, const char* name, uint32 resizingMode) 32 : BView(frame, name, resizingMode, B_WILL_DRAW) 30 PairsView::PairsView(BRect frame, const char* name, 31 int width, int height, 32 uint32 resizingMode) 33 : 34 BView(frame, name, resizingMode, B_WILL_DRAW), 35 fWidth(width), 36 fHeight(height), 37 fNumOfCards(width * height), 38 fRandPos(new int[fNumOfCards]), 39 fPosX(new int[fNumOfCards]), 40 fPosY(new int[fNumOfCards]) 33 41 { 34 // init bitmap pointers35 for (int i = 0; i < 8; i++)36 fCard[i] = NULL;37 38 42 CreateGameBoard(); 39 43 _SetPairsBoard(); 40 44 } … … 55 59 56 60 PairsView::~PairsView() 57 61 { 58 for (int i = 0; i < 8; i++)59 delete fCard[i];62 for (int i = 0; i < fCardBitmaps.CountItems(); i++) 63 delete ((BBitmap*)fCardBitmaps.ItemAt(i)); 60 64 61 for (int i = 0; i < 16; i++) 62 delete fDeckCard[i]; 65 for (int i = 0; i < fDeckCard.CountItems(); i++) 66 delete ((TopButton*)fDeckCard.ItemAt(i)); 67 68 delete fRandPos; 69 delete fPosX; 70 delete fPosY; 63 71 } 64 72 65 73 … … 91 99 // TODO: maybe read the icons only once at startup 92 100 93 101 // clean out any previous icons 94 for (int i = 0; i < 8; i++) { 95 delete fCard[i]; 96 fCard[i] = NULL; 102 for (int i = 0; i < fCardBitmaps.CountItems(); i++) { 103 delete ((BBitmap*)fCardBitmaps.ItemAt(i)); 97 104 } 105 fCardBitmaps.MakeEmpty(); 98 106 99 107 BDirectory appsDirectory; 100 108 BDirectory prefsDirectory; … … 152 160 // pick eight random bitmaps from the ones we got in the list 153 161 srand((unsigned)time(0)); 154 162 155 for (int i = 0; i < 8; i++) {163 for (int i = 0; i < fNumOfCards / 2; i++) { 156 164 int32 index = rand() % bitmaps.CountItems(); 157 fCard[i] = (BBitmap*)bitmaps.RemoveItem(index); 158 if (fCard[i] == NULL) { 159 BAlert* alert = new BAlert("fatal", "Pairs did not find enough " 160 "vector icons in the system, it needs at least eight.", 165 BBitmap* bitmap = ((BBitmap*)bitmaps.RemoveItem(index)); 166 if (bitmap == NULL) { 167 BString strErr; 168 strErr 169 << "Pairs did not find enough vector icons in the system; it " 170 << "needs at least " << fNumOfCards / 2; 171 BAlert* alert = new BAlert("fatal", strErr, 161 172 "OK", NULL, NULL, B_WIDTH_FROM_WIDEST, B_STOP_ALERT); 162 173 alert->Go(); 163 174 exit(1); 164 175 } 176 fCardBitmaps.AddItem(bitmap); 165 177 } 166 178 167 179 // delete the remaining bitmaps from the list … … 173 185 void 174 186 PairsView::_SetPairsBoard() 175 187 { 176 for (int i = 0; i < 16; i++) {188 for (int i = 0; i < fNumOfCards; i++) { 177 189 fButtonMessage = new BMessage(kMsgCardButton); 178 190 fButtonMessage->AddInt32("ButtonNum", i); 179 191 180 int x = i % 4 * (kBitmapSize + 10) + 10; 181 int y = i / 4 * (kBitmapSize + 10) + 10; 182 183 fDeckCard[i] = new TopButton(x, y, fButtonMessage); 184 AddChild(fDeckCard[i]); 192 int x = i % fWidth * (kBitmapSize + kSpaceSize) + kSpaceSize; 193 int y = i / fHeight * (kBitmapSize + kSpaceSize) + kSpaceSize; 194 195 TopButton* button = new TopButton(x, y, fButtonMessage); 196 fDeckCard.AddItem(button); 197 AddChild(button); 185 198 } 186 199 } 187 200 … … 191 204 { 192 205 _ReadRandomIcons(); 193 206 194 srand((unsigned)time(0)); 207 srand((unsigned)time(0)); 195 208 196 int positions[16];197 for (int i = 0; i < 16; i++)209 int* positions = new int[fNumOfCards]; 210 for (int i = 0; i < fNumOfCards; i++) 198 211 positions[i] = i; 199 212 200 for (int i = 16; i >= 1; i--) {213 for (int i = fNumOfCards; i >= 1; i--) { 201 214 int index = rand() % i; 202 215 203 fRandPos[ 16-i] = positions[index];216 fRandPos[fNumOfCards - i] = positions[index]; 204 217 205 218 for (int j = index; j < i - 1; j++) 206 219 positions[j] = positions[j + 1]; 207 220 } 208 221 209 for (int i = 0; i < 16; i++) { 210 fPosX[i] = (fRandPos[i]) % 4 * (kBitmapSize + 10) + 10; 211 fPosY[i] = (fRandPos[i]) / 4 * (kBitmapSize + 10) + 10; 222 for (int i = 0; i < fNumOfCards; i++) { 223 fPosX[i] = (fRandPos[i]) % fWidth * (kBitmapSize + kSpaceSize) 224 + kSpaceSize; 225 fPosY[i] = (fRandPos[i]) / fHeight * (kBitmapSize + kSpaceSize) 226 + kSpaceSize; 212 227 } 228 229 delete [] positions; 213 230 } 214 231 215 232 … … 219 236 SetDrawingMode(B_OP_ALPHA); 220 237 221 238 // draw rand pair 1 & 2 222 for (int i = 0; i < 16; i++) 223 DrawBitmap(fCard[i % 8], BPoint(fPosX[i], fPosY[i])); 239 for (int i = 0; i < fNumOfCards; i++) { 240 BBitmap* bitmap = ((BBitmap*)fCardBitmaps.ItemAt(i % fNumOfCards / 2)); 241 DrawBitmap(bitmap, BPoint(fPosX[i], fPosY[i])); 242 } 224 243 } 225 244 226 245 -
src/apps/pairs/PairsWindow.h
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6 … … 18 19 virtual ~PairsWindow(); 19 20 20 21 virtual void MessageReceived(BMessage* message); 22 23 void NewGame(); 24 void SetGameSize(int width, int height); 21 25 22 26 private: 27 void _MakeGameView(int width, int height); 28 void _MakeMenuBar(); 29 23 30 BView* fBackgroundView; 24 31 PairsView* fPairsView; 32 BMenuBar* fMenuBar; 25 33 BMessageRunner* fPairComparing; 26 34 bool fIsFirstClick; 27 35 bool fIsPairsActive; -
src/apps/pairs/PairsTopButton.cpp
1 1 /* 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. All rights reserved. 2 * Copyright 2008 Ralf Schülke, ralf.schuelke@googlemail.com. 3 * All rights reserved. 3 4 * Distributed under the terms of the MIT License. 4 5 */ 5 6