Added Pairs game contributed by Ralf Schuelke to the image! (Demo folder)

Thanks a lot, Ralf!


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25114 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2008-04-23 09:06:05 +00:00
parent 291b899af6
commit 69e62e48b9
21 changed files with 8866 additions and 0 deletions

View File

@ -28,6 +28,7 @@ SubInclude HAIKU_TOP src apps mediaplayer ;
SubInclude HAIKU_TOP src apps midiplayer ;
SubInclude HAIKU_TOP src apps networkstatus ;
SubInclude HAIKU_TOP src apps packageinstaller ;
SubInclude HAIKU_TOP src apps pairs ;
SubInclude HAIKU_TOP src apps people ;
SubInclude HAIKU_TOP src apps poorman ;
SubInclude HAIKU_TOP src apps powerstatus ;

15
src/apps/pairs/Jamfile Normal file
View File

@ -0,0 +1,15 @@
SubDir HAIKU_TOP src apps pairs ;
SetSubDirSupportedPlatformsBeOSCompatible ;
UsePrivateHeaders shared ;
Application Pairs :
Pairs.cpp
PairsWindow.cpp
PairsView.cpp
PairsTopButton.cpp
: be $(TARGET_LIBSTDC++)
: Pairs.rdef
;

57
src/apps/pairs/Pairs.cpp Normal file
View File

@ -0,0 +1,57 @@
/*
* Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <stdlib.h>
#include <Application.h>
#include "Pairs.h"
#include "PairsWindow.h"
const char* kSignature = "application/x-vnd.haiku-Pairs";
Pairs::Pairs()
: BApplication(kSignature),
fWindow(NULL)
{
}
Pairs::~Pairs()
{
}
void
Pairs::ReadyToRun()
{
fWindow = new PairsWindow();
fWindow->Show();
}
void
Pairs::RefsReceived(BMessage* message)
{
fWindow->PostMessage(message);
}
void
Pairs::MessageReceived(BMessage* message)
{
BApplication::MessageReceived(message);
}
int
main(void)
{
Pairs pairs;
pairs.Run();
return 0;
}

28
src/apps/pairs/Pairs.h Normal file
View File

@ -0,0 +1,28 @@
/*
* Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef PAIRS_H
#define PAIRS_H
#include <Application.h>
extern const char* kSignature;
class BMessage;
class PairsWindow;
class Pairs : public BApplication {
public:
Pairs();
virtual ~Pairs();
virtual void ReadyToRun();
virtual void RefsReceived(BMessage* message);
virtual void MessageReceived(BMessage* message);
private:
PairsWindow* fWindow;
};
#endif // PAIRS_H

62
src/apps/pairs/Pairs.rdef Normal file
View File

@ -0,0 +1,62 @@
/*
* Pairs.rdef
*/
resource app_signature "application/x-vnd.haiku-Pairs";
resource app_flags B_SINGLE_LAUNCH;
resource app_version {
major = 0,
middle = 1,
minor = 2,
/* 0 = development 1 = alpha 2 = beta
3 = gamma 4 = golden master 5 = final */
variety = 2,
internal = 1,
short_info = "Pairs",
long_info = "Pairs, Copyright 2008 Haiku Inc."
};
resource vector_icon {
$"6E636966110500020006023CADF43D104EBF02CA3EA2224BB02046648D00FFDB"
$"97FFFCAF29020006023A6496BA3F153EBCDD3EEA9147D96A4458AB00FFF7EAFE"
$"FDD17B020006023AB3673751B2BC59783FD2D74AF3F147385900C85805FFF063"
$"06020106033ABB740000000000003AB5E049D1B1471E5B00F2F2F2FFBCA184AB"
$"7D7A7A04016C03A3290402000602388A280000000000004000004A3000C20000"
$"00C85804FFDC952F0500020006023CADF43D104EBF02CA3EA2224BB02046648D"
$"00FFDB97FFFCAF29020006023A6496BA3F153EBCDD3EEA9147D96A4458AB00FF"
$"F7EAFEFDD17B020006023AB3673751B2BC59783FD2D74AF3F147385900C85805"
$"FFF06306020006023AB3673751B2BC59783FD2D74B13F147785900A02646FFD5"
$"3961020106033ABB740000000000003AB5E049D1B1471E5B00F2F2F2FFBCA184"
$"AB7D7A7A04016C03A3290402000602388A280000000000004000004A3000C200"
$"0000C85804FFDC952F140A0D36353646384838593C5B3E5B3E5C425E4858484A"
$"4A484A373D310A0B36353645384838593C5B3E5B3E5C425E424D444D443B0A06"
$"3635443B4A374036C157BAF73D310A08444D424D425E4858484A4A484A37443B"
$"0204402843283D283A2E3A2B3A3140343D344334462E4631462B02043F2AC059"
$"2ABE5B2A3A2F3AB7FB3AB9F93F34BE5B34C05934442F44B9F944B7FB0607FE2B"
$"42604D6048605260CAA3CCB3C8A5CD19CDC4CC12CA93CB3CCBC5CC08C935CA53"
$"CBD5C8A5CC75C9E5CB6FC7D9C39A534A580A04424D4253484A444D0A0340364A"
$"3744340A043C5B3E5B3E4D3C4C0A0D36353646384838593C5B3E5B3E5C425E48"
$"58484A4A484A373D310A0B36353645384838593C5B3E5B3E5C425E424D444D44"
$"3B0A063635443B4A374036C157BAF73D310A08444D424D425E4858484A4A484A"
$"37443B0204402843283D283A2E3A2B3A3140343D344334462E4631462B02043F"
$"2AC0592ABE5B2A3A2F3AB7FB3AB9F93F34BE5B34C05934442F44B9F944B7FB06"
$"07FE2B42604D6048605260C9C760C7C0CCA7CC5ECC4DC9FACA60CB09CB1BC89F"
$"C9715BC83FCB21C97F5AC773C377C90B4A580A04424D4253484A444D0A034036"
$"4A3744340A043C5B3E5B3E4D3C4C260A050106000A00010030101A0117840004"
$"0A01010120101A0A0A010220101A0A03010320101A0A06010720101A0A000108"
$"20101A0A00010530101A01178400040A04010520101A0A00010928101A15FF0A"
$"0001001001178400040A010101000A020102000A030103000A060107000A0701"
$"08000A0001041001178400040A040104000A0301090815FF0A0E0110000A0801"
$"0A30101A01178400040A09010B20101A0A02010C20101A0A0B010D20101A0A0F"
$"011120101A0A00011220101A0A08010F30101A01178400040A0D010F20101A0A"
$"00011328101A15FF0A08010A1001178400040A09010B000A0A010C000A0B010D"
$"000A0F0111000A100112000A08010E1001178400040A0D010E000A0B01130815"
$"FF"
};

View File

@ -0,0 +1,16 @@
/*
* Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef PAIRS_GLOBAL_H
#define PAIRS_GLOBAL_H
#include <SupportDefs.h>
const uint32 kMsgCardButton = 'card';
const uint32 kMsgPairComparing = 'pcom';
const int kBitmapSize = 64;
#endif // PAIRS_GLOBAL_H

View File

@ -0,0 +1,25 @@
/*
* Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <stdio.h>
#include <unistd.h>
#include <Button.h>
#include "PairsTopButton.h"
#include "PairsGlobal.h"
TopButton::TopButton(int x, int y, BMessage* message)
: BButton(BRect(x, y, x + kBitmapSize, y + kBitmapSize), "top_button",
"?", message)
{
SetFontSize(54);
}
TopButton::~TopButton()
{
}

View File

@ -0,0 +1,18 @@
/*
* Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef PAIRS_TOP_BUTTON_H
#define PAIRS_TOP_BUTTON_H
#include <OS.h>
class BButton;
class TopButton : public BButton {
public:
TopButton(int x, int y, BMessage* message);
virtual ~TopButton();
};
#endif // PAIRS_TOP_BUTTON_H

View File

@ -0,0 +1,158 @@
/*
* Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <Application.h>
#include <Bitmap.h>
#include <Button.h>
#include "PairsTopButton.h"
#include "PairsView.h"
#include "PairsGlobal.h"
#include "Pairs.h"
#include "bitmaps/appearance.h"
#include "bitmaps/cortex.h"
#include "bitmaps/joystick.h"
#include "bitmaps/kernel.h"
#include "bitmaps/launchbox.h"
#include "bitmaps/people.h"
#include "bitmaps/teapot.h"
#include "bitmaps/tracker.h"
// TODO: support custom board sizes
PairsView::PairsView(BRect frame, const char* name, uint32 resizingMode)
: BView(frame, name, resizingMode, B_WILL_DRAW)
{
_PairsCards();
CreateGameBoard();
_SetPairsBoard();
}
void
PairsView::CreateGameBoard()
{
// Show hidden buttons
for (int32 i = 0; i < CountChildren(); i++) {
BView* child = ChildAt(i);
if (child->IsHidden())
child->Show();
}
_GenarateCardPos();
}
PairsView::~PairsView()
{
for (int i = 0; i < 8; i++)
delete fCard[i];
for (int i = 0; i < 16; i++)
delete fDeckCard[i];
}
void
PairsView::AttachedToWindow()
{
MakeFocus(true);
}
void
PairsView::_PairsCards()
{
for (int i = 0; i < 8; i++) {
fCard[i] = new BBitmap(BRect(0, 0, kBitmapSize - 1, kBitmapSize - 1),
B_RGBA32);
}
// TODO: read random icons from the files in /boot/beos/apps
// and /boot/beos/preferences...
memcpy(fCard[0]->Bits(), kappearanceBits, fCard[0]->BitsLength());
memcpy(fCard[1]->Bits(), kcortexBits, fCard[1]->BitsLength());
memcpy(fCard[2]->Bits(), kjoystickBits, fCard[2]->BitsLength());
memcpy(fCard[3]->Bits(), kkernelBits, fCard[3]->BitsLength());
memcpy(fCard[4]->Bits(), klaunchboxBits, fCard[4]->BitsLength());
memcpy(fCard[5]->Bits(), kpeopleBits, fCard[5]->BitsLength());
memcpy(fCard[6]->Bits(), kteapotBits, fCard[6]->BitsLength());
memcpy(fCard[7]->Bits(), ktrackerBits, fCard[7]->BitsLength());
}
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()
{
// TODO: when loading random icons, the would also have to be
// loaded here (or at least somewhere in the code path that creates
// a new game after one finished)
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;
}
}
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]));
}
}
int
PairsView::GetIconFromPos(int pos)
{
return fRandPos[pos];
}

View File

@ -0,0 +1,39 @@
/*
* 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 <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 _PairsCards();
void _GenarateCardPos();
BMessage* fButtonMessage;
BBitmap* fCard[8];
int fRandPos[16];
int fPosX[16];
int fPosY[16];
};
#endif // PAIRS_VIEW_H

View File

@ -0,0 +1,148 @@
/*
* Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <stdio.h>
#include <Application.h>
#include <MessageRunner.h>
#include <Button.h>
#include <Alert.h>
#include <TextView.h>
#include <String.h>
#include "Pairs.h"
#include "PairsGlobal.h"
#include "PairsWindow.h"
#include "PairsView.h"
#include "PairsTopButton.h"
PairsWindow::PairsWindow()
: BWindow(BRect(100, 100, 405, 405), "Pairs", B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE
| B_NOT_RESIZABLE | B_NOT_ZOOMABLE),
fPairComparing(NULL),
fIsFirstClick(true),
fIsPairsActive(true),
fPairCard(0),
fPairCardTmp(0),
fButtonTmp(0),
fButton(0),
fButtonClicks(0),
fFinishPairs(0)
{
fPairsView = new PairsView(Bounds().InsetByCopy(0, 0).OffsetToSelf(0, 0),
"PairsView", B_FOLLOW_NONE);
fPairsView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
AddChild(fPairsView);
}
PairsWindow::~PairsWindow()
{
delete fPairComparing;
}
void
PairsWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgCardButton:
if (fIsPairsActive) {
fButtonClicks = fButtonClicks + 1;
int32 num;
if (message->FindInt32("ButtonNum", &num) < B_OK)
break;
//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
fPairsView->fDeckCard[fButton]->Hide();
if (fIsFirstClick) {
fPairCardTmp = fPairCard;
fButtonTmp = fButton;
} else {
delete fPairComparing;
// message of message runner might not have arrived
// yet, so it is deleted here to prevent any leaking
// just in case
fPairComparing = new BMessageRunner(BMessenger(this),
new BMessage(kMsgPairComparing), 5 * 100000L, 1);
fIsPairsActive = false;
}
fIsFirstClick = !fIsFirstClick;
}
break;
case kMsgPairComparing:
delete fPairComparing;
fPairComparing = NULL;
fIsPairsActive = true;
if (fPairCard == fPairCardTmp) {
fFinishPairs++;
} else {
fPairsView->fDeckCard[fButton]->Show();
fPairsView->fDeckCard[fButtonTmp]->Show();
// TODO: remove when app_server is fixed: (Could this be the Firefox problem?)
fPairsView->Invalidate();
}
//game end and results
if (fFinishPairs == 8) {
BString strAbout;
strAbout
<< "Pairs\n"
<< "\twritten by Ralf Schülke\n"
<< "\tCopyright 2008, Haiku Inc.\n"
<< "\n"
<< "You completed the game in " << fButtonClicks + 1
<< " clicks.\n";
BAlert* alert = new BAlert("about", strAbout.String(), "New game",
"Quit game");
BTextView* view = alert->TextView();
BFont font;
view->SetStylable(true);
view->GetFont(&font);
font.SetSize(18);
font.SetFace(B_BOLD_FACE);
view->SetFontAndColor(0, 6, &font);
view->ResizeToPreferred();
if (alert->Go() == 0) {
//New game
fButtonClicks = 0;
fFinishPairs = 0;
fPairsView->CreateGameBoard();
} else {
//Quit game
be_app->PostMessage(B_QUIT_REQUESTED);
}
}
break;
default:
BWindow::MessageReceived(message);
break;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2007, Ralf Schülke, teammaui@web.de. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef PAIRS_WINDOW_H
#define PAIRS_WINDOW_H
#include <Window.h>
class PairsView;
class BMessageRunner;
class PairsWindow : public BWindow {
public:
PairsWindow();
virtual ~PairsWindow();
virtual void MessageReceived(BMessage* message);
private:
BView* fBackgroundView;
PairsView* fPairsView;
BMessageRunner* fPairComparing;
bool fIsFirstClick;
bool fIsPairsActive;
int fPairCard;
int fPairCardTmp;
int fButtonTmp;
int fButton;
int fButtonClicks;
int fFinishPairs;
};
#endif // PAIRS_WINDOW_H

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff