* 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
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
* Copyright 2008, Ralf Schülke, [email protected]. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include "PairsView.h"
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,31 +2,32 @@
|
||||
* Copyright 2008, Ralf Schülke, [email protected]. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#ifndef PAIRS_VIEW_H
|
||||
#define PAIRS_VIEW_H
|
||||
|
||||
|
||||
#include <View.h>
|
||||
|
||||
|
||||
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];
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include "PairsWindow.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <Application.h>
|
||||
#include <MessageRunner.h>
|
||||
#include <MessageRunner.h>
|
||||
#include <Button.h>
|
||||
#include <Alert.h>
|
||||
#include <TextView.h>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user