Simple Memory Game Problem

M

MaKiT

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[j] = FALSE;
_iTiles[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[j] = FALSE;
_iTiles[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.
 
A

Agent Mulder

Can you send code to me? Thanks. Mail to

(e-mail address removed)



MaKiT said:
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[j] = FALSE;
_iTiles[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[j] = FALSE;
_iTiles[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.
 
T

Thomas Matthews

MaKiT said:
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 = 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[j] = FALSE;
_iTiles[j] = 0;
}

=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
for (unsigned int i = 0; i < 4; ++i)
{
std::fill(bTileStates, bTileStates + 4, false);
std::fill(iTiles, iTiles + 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[j] = FALSE;
_iTiles[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
 
M

MaKiT

Howard said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,802
Messages
2,569,662
Members
45,433
Latest member
andrewartemow

Latest Threads

Top