Simple Memory Game Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MaKiT

    Simple Memory Game Problem

    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(GetTickCo unt());

    // 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(GetTickCo unt());

    // 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.
  • Agent Mulder

    #2
    Re: Simple Memory Game Problem

    Can you send code to me? Thanks. Mail to

    mbmulder@home.n l



    "MaKiT" <makkill@blueyo nder.co.uk> wrote in message
    news:4df0fb28.0 308180502.14107 12b@posting.goo gle.com...[color=blue]
    > 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(GetTickCo unt());
    >
    > // 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(GetTickCo unt());
    >
    > // 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.[/color]


    Comment

    • Thomas Matthews

      #3
      Re: Simple Memory Game Problem

      MaKiT wrote:
      [color=blue]
      > 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(GetTickCo unt());
      >
      > // 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);[/color]
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
      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);
      }
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=

      [color=blue]
      >
      > // 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;
      > }[/color]
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
      for (unsigned int i = 0; i < 4; ++i)
      {
      std::fill(bTile States[i], bTileStates[i] + 4, false);
      std::fill(iTile s[i], iTiles[i] + 4, 0);
      }
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=

      [color=blue]
      >
      > // 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(GetTickCo unt());
      >
      > // 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);[/color]
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
      See comments above.
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=

      [color=blue]
      >
      > // 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++)[/color]
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=
      Are these constants correct?
      =-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=-+-=

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

      [color=blue]
      >
      > // 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.[/color]


      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      • MaKiT

        #4
        Re: Simple Memory Game Problem

        "Howard" <alicebt@hotmai l.com> wrote in message news:<bhqnm9$bn v@dispatch.conc entric.net>...[color=blue]
        > "MaKiT" <makkill@blueyo nder.co.uk> wrote in message
        > news:4df0fb28.0 308180502.14107 12b@posting.goo gle.com...[color=green]
        > > 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:[/color]
        >
        > 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[/color]

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

        Comment

        Working...