By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,046 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,046 IT Pros & Developers. It's quick & easy.

Simple Memory Game Problem

P: n/a
I have a program which on start makes a 4x4 grid of tiles. These tiles
are all blank. Two Array variables hold the position of 8 pairs of
images. Then when you click a tile it is uncovered and you try to
match up.

I attempted to modify this so it is a 8x8 grid with 16 pairs of
images. The only problem is that only some random tiles seem to be
placed and not all of them.

Heres the original start part of the code:

************************************************** **
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());

// Create and load the tile bitmaps
HDC hDC = GetDC(hWindow);
_pTiles[0] = new Bitmap(hDC, IDB_TILEBLANK, _hInstance);
_pTiles[1] = new Bitmap(hDC, IDB_TILE1, _hInstance);
_pTiles[2] = new Bitmap(hDC, IDB_TILE2, _hInstance);
_pTiles[3] = new Bitmap(hDC, IDB_TILE3, _hInstance);
_pTiles[4] = new Bitmap(hDC, IDB_TILE4, _hInstance);
_pTiles[5] = new Bitmap(hDC, IDB_TILE5, _hInstance);
_pTiles[6] = new Bitmap(hDC, IDB_TILE6, _hInstance);
_pTiles[7] = new Bitmap(hDC, IDB_TILE7, _hInstance);
_pTiles[8] = new Bitmap(hDC, IDB_TILE8, _hInstance);

// Clear the tile states and images
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
_bTileStates[i][j] = FALSE;
_iTiles[i][j] = 0;
}

// Initialize the tile images randomly
for (int i = 0; i < 2; i++)
for (int j = 1; j < 9; j++)
{
int x = rand() % 4;
int y = rand() % 4;
while (_iTiles[x][y] != 0)
{
x = rand() % 4;
y = rand() % 4;
}
_iTiles[x][y] = j;
}

// Initialize the tile selections and match/try count
_ptTile1.x = _ptTile1.y = -1;
_ptTile2.x = _ptTile2.y = -1;
_iMatches = _iTries = 0;
}
************************************************** **

Heres what I modified:

************************************************** **
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());

// Create and load the tile bitmaps
HDC hDC = GetDC(hWindow);
_pTiles[0] = new Bitmap(hDC, IDB_TILEBLANK, _hInstance);
_pTiles[1] = new Bitmap(hDC, IDB_TILE1, _hInstance);
_pTiles[2] = new Bitmap(hDC, IDB_TILE2, _hInstance);
_pTiles[3] = new Bitmap(hDC, IDB_TILE3, _hInstance);
_pTiles[4] = new Bitmap(hDC, IDB_TILE4, _hInstance);
_pTiles[5] = new Bitmap(hDC, IDB_TILE5, _hInstance);
_pTiles[6] = new Bitmap(hDC, IDB_TILE6, _hInstance);
_pTiles[7] = new Bitmap(hDC, IDB_TILE7, _hInstance);
_pTiles[8] = new Bitmap(hDC, IDB_TILE8, _hInstance);
_pTiles[9] = new Bitmap(hDC, IDB_TILE9, _hInstance);
_pTiles[10] = new Bitmap(hDC, IDB_TILE10, _hInstance);
_pTiles[11] = new Bitmap(hDC, IDB_TILE11, _hInstance);
_pTiles[12] = new Bitmap(hDC, IDB_TILE12, _hInstance);
_pTiles[13] = new Bitmap(hDC, IDB_TILE13, _hInstance);
_pTiles[14] = new Bitmap(hDC, IDB_TILE14, _hInstance);
_pTiles[15] = new Bitmap(hDC, IDB_TILE15, _hInstance);
_pTiles[16] = new Bitmap(hDC, IDB_TILE16, _hInstance);

// Clear the tile states and images
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
_bTileStates[i][j] = FALSE;
_iTiles[i][j] = 0;
}

// Initialize the tile images randomly
for (i = 0; i < 2; i++)
for (int j = 1; j < 17; j++)
{
int x = rand() % 8;
int y = rand() % 8;
while (_iTiles[x][y] != 0)
{
x = rand() % 8;
y = rand() % 8;
}
_iTiles[x][y] = j;
}

// Initialize the tile selections and match/try count
_ptTile1.x = _ptTile1.y = -1;
_ptTile2.x = _ptTile2.y = -1;
_iMatches = _iTries = 0;
}
************************************************** **

If you need the pait/click part of the codes just ask.
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Can you send code to me? Thanks. Mail to

mb******@home.nl

"MaKiT" <ma*****@blueyonder.co.uk> wrote in message
news:4d**************************@posting.google.c om...
I have a program which on start makes a 4x4 grid of tiles. These tiles
are all blank. Two Array variables hold the position of 8 pairs of
images. Then when you click a tile it is uncovered and you try to
match up.

I attempted to modify this so it is a 8x8 grid with 16 pairs of
images. The only problem is that only some random tiles seem to be
placed and not all of them.

Heres the original start part of the code:

************************************************** **
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());

// Create and load the tile bitmaps
HDC hDC = GetDC(hWindow);
_pTiles[0] = new Bitmap(hDC, IDB_TILEBLANK, _hInstance);
_pTiles[1] = new Bitmap(hDC, IDB_TILE1, _hInstance);
_pTiles[2] = new Bitmap(hDC, IDB_TILE2, _hInstance);
_pTiles[3] = new Bitmap(hDC, IDB_TILE3, _hInstance);
_pTiles[4] = new Bitmap(hDC, IDB_TILE4, _hInstance);
_pTiles[5] = new Bitmap(hDC, IDB_TILE5, _hInstance);
_pTiles[6] = new Bitmap(hDC, IDB_TILE6, _hInstance);
_pTiles[7] = new Bitmap(hDC, IDB_TILE7, _hInstance);
_pTiles[8] = new Bitmap(hDC, IDB_TILE8, _hInstance);

// Clear the tile states and images
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
_bTileStates[i][j] = FALSE;
_iTiles[i][j] = 0;
}

// Initialize the tile images randomly
for (int i = 0; i < 2; i++)
for (int j = 1; j < 9; j++)
{
int x = rand() % 4;
int y = rand() % 4;
while (_iTiles[x][y] != 0)
{
x = rand() % 4;
y = rand() % 4;
}
_iTiles[x][y] = j;
}

// Initialize the tile selections and match/try count
_ptTile1.x = _ptTile1.y = -1;
_ptTile2.x = _ptTile2.y = -1;
_iMatches = _iTries = 0;
}
************************************************** **

Heres what I modified:

************************************************** **
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());

// Create and load the tile bitmaps
HDC hDC = GetDC(hWindow);
_pTiles[0] = new Bitmap(hDC, IDB_TILEBLANK, _hInstance);
_pTiles[1] = new Bitmap(hDC, IDB_TILE1, _hInstance);
_pTiles[2] = new Bitmap(hDC, IDB_TILE2, _hInstance);
_pTiles[3] = new Bitmap(hDC, IDB_TILE3, _hInstance);
_pTiles[4] = new Bitmap(hDC, IDB_TILE4, _hInstance);
_pTiles[5] = new Bitmap(hDC, IDB_TILE5, _hInstance);
_pTiles[6] = new Bitmap(hDC, IDB_TILE6, _hInstance);
_pTiles[7] = new Bitmap(hDC, IDB_TILE7, _hInstance);
_pTiles[8] = new Bitmap(hDC, IDB_TILE8, _hInstance);
_pTiles[9] = new Bitmap(hDC, IDB_TILE9, _hInstance);
_pTiles[10] = new Bitmap(hDC, IDB_TILE10, _hInstance);
_pTiles[11] = new Bitmap(hDC, IDB_TILE11, _hInstance);
_pTiles[12] = new Bitmap(hDC, IDB_TILE12, _hInstance);
_pTiles[13] = new Bitmap(hDC, IDB_TILE13, _hInstance);
_pTiles[14] = new Bitmap(hDC, IDB_TILE14, _hInstance);
_pTiles[15] = new Bitmap(hDC, IDB_TILE15, _hInstance);
_pTiles[16] = new Bitmap(hDC, IDB_TILE16, _hInstance);

// Clear the tile states and images
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
_bTileStates[i][j] = FALSE;
_iTiles[i][j] = 0;
}

// Initialize the tile images randomly
for (i = 0; i < 2; i++)
for (int j = 1; j < 17; j++)
{
int x = rand() % 8;
int y = rand() % 8;
while (_iTiles[x][y] != 0)
{
x = rand() % 8;
y = rand() % 8;
}
_iTiles[x][y] = j;
}

// Initialize the tile selections and match/try count
_ptTile1.x = _ptTile1.y = -1;
_ptTile2.x = _ptTile2.y = -1;
_iMatches = _iTries = 0;
}
************************************************** **

If you need the pait/click part of the codes just ask.

Jul 19 '05 #2

P: n/a
MaKiT wrote:
I have a program which on start makes a 4x4 grid of tiles. These tiles
are all blank. Two Array variables hold the position of 8 pairs of
images. Then when you click a tile it is uncovered and you try to
match up.

I attempted to modify this so it is a 8x8 grid with 16 pairs of
images. The only problem is that only some random tiles seem to be
placed and not all of them.

Heres the original start part of the code:

************************************************** **
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());

// Create and load the tile bitmaps
HDC hDC = GetDC(hWindow);
_pTiles[0] = new Bitmap(hDC, IDB_TILEBLANK, _hInstance);
_pTiles[1] = new Bitmap(hDC, IDB_TILE1, _hInstance);
_pTiles[2] = new Bitmap(hDC, IDB_TILE2, _hInstance);
_pTiles[3] = new Bitmap(hDC, IDB_TILE3, _hInstance);
_pTiles[4] = new Bitmap(hDC, IDB_TILE4, _hInstance);
_pTiles[5] = new Bitmap(hDC, IDB_TILE5, _hInstance);
_pTiles[6] = new Bitmap(hDC, IDB_TILE6, _hInstance);
_pTiles[7] = new Bitmap(hDC, IDB_TILE7, _hInstance);
_pTiles[8] = new Bitmap(hDC, IDB_TILE8, _hInstance); =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
Notes:
1. Identifiers with leading underscores are reserved for the
compiler and implementation (i.e. _pTiles should be pTiles).
2. If the constants IDB_TILE1 through IDB_TILE8 are sequential,
you could simplify the above code:
for (int i = 1; i < 9; ++i)
{
pTiles[i] = new Bitmap(hDC, IDB_TILE1 + (i-1), _hInstance);
}
=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=


// Clear the tile states and images
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
_bTileStates[i][j] = FALSE;
_iTiles[i][j] = 0;
} =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
for (unsigned int i = 0; i < 4; ++i)
{
std::fill(bTileStates[i], bTileStates[i] + 4, false);
std::fill(iTiles[i], iTiles[i] + 4, 0);
}
=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=


// Initialize the tile images randomly
for (int i = 0; i < 2; i++)
for (int j = 1; j < 9; j++)
{
int x = rand() % 4;
int y = rand() % 4;
while (_iTiles[x][y] != 0)
{
x = rand() % 4;
y = rand() % 4;
_iTiles[x][y] = j;
}

// Initialize the tile selections and match/try count
_ptTile1.x = _ptTile1.y = -1;
_ptTile2.x = _ptTile2.y = -1;
_iMatches = _iTries = 0;
}
************************************************** **

Heres what I modified:

************************************************** **
void GameStart(HWND hWindow)
{
// Seed the random number generator
srand(GetTickCount());

// Create and load the tile bitmaps
HDC hDC = GetDC(hWindow);
_pTiles[0] = new Bitmap(hDC, IDB_TILEBLANK, _hInstance);
_pTiles[1] = new Bitmap(hDC, IDB_TILE1, _hInstance);
_pTiles[2] = new Bitmap(hDC, IDB_TILE2, _hInstance);
_pTiles[3] = new Bitmap(hDC, IDB_TILE3, _hInstance);
_pTiles[4] = new Bitmap(hDC, IDB_TILE4, _hInstance);
_pTiles[5] = new Bitmap(hDC, IDB_TILE5, _hInstance);
_pTiles[6] = new Bitmap(hDC, IDB_TILE6, _hInstance);
_pTiles[7] = new Bitmap(hDC, IDB_TILE7, _hInstance);
_pTiles[8] = new Bitmap(hDC, IDB_TILE8, _hInstance);
_pTiles[9] = new Bitmap(hDC, IDB_TILE9, _hInstance);
_pTiles[10] = new Bitmap(hDC, IDB_TILE10, _hInstance);
_pTiles[11] = new Bitmap(hDC, IDB_TILE11, _hInstance);
_pTiles[12] = new Bitmap(hDC, IDB_TILE12, _hInstance);
_pTiles[13] = new Bitmap(hDC, IDB_TILE13, _hInstance);
_pTiles[14] = new Bitmap(hDC, IDB_TILE14, _hInstance);
_pTiles[15] = new Bitmap(hDC, IDB_TILE15, _hInstance);
_pTiles[16] = new Bitmap(hDC, IDB_TILE16, _hInstance); =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
See comments above.
=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=


// Clear the tile states and images
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
{
_bTileStates[i][j] = FALSE;
_iTiles[i][j] = 0;
}

// Initialize the tile images randomly
for (i = 0; i < 2; i++)
for (int j = 1; j < 17; j++) =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
Are these constants correct?
=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=

{
int x = rand() % 8;
int y = rand() % 8;
while (_iTiles[x][y] != 0)
{
x = rand() % 8;
y = rand() % 8;
}
_iTiles[x][y] = j;
} =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
See comments above.
=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=


// Initialize the tile selections and match/try count
_ptTile1.x = _ptTile1.y = -1;
_ptTile2.x = _ptTile2.y = -1;
_iMatches = _iTries = 0;
}
************************************************** **

If you need the pait/click part of the codes just ask.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #3

P: n/a
"Howard" <al*****@hotmail.com> wrote in message news:<bh********@dispatch.concentric.net>...
"MaKiT" <ma*****@blueyonder.co.uk> wrote in message
news:4d**************************@posting.google.c om...
I have a program which on start makes a 4x4 grid of tiles. These tiles
are all blank. Two Array variables hold the position of 8 pairs of
images. Then when you click a tile it is uncovered and you try to
match up.

I attempted to modify this so it is a 8x8 grid with 16 pairs of
images. The only problem is that only some random tiles seem to be
placed and not all of them.

Heres the original start part of the code:


Without looking at the code, I *can* tell you one problem: on an 8x8 grid,
there are 64 squares, which requires 32 pairs, not 16! So placing only 16
pairs will leave half the board empty.

-Howard


Thanks, I just forgot that doubling the sides wouldn't double the
area. It now works after modifing it.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.