Win32 Battleship game enigma?

This is a discussion on Win32 Battleship game enigma? within the Windows Programming forums, part of the Platform Specific Boards category; I'm writing a program at the moment that is a windows battleship game, where you play against the computer. The ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question Win32 Battleship game enigma?

    I'm writing a program at the moment that is a windows battleship game, where you play against the computer. The problem is that I can't think of a way to get the co-ordinates of the "grid", so that the computer can understand the user input. Is the best way to simply make lots of buttons and then assign them all identifiers, so that the user can click where they want to "shoot". By the way, at the moment, I'm not using any special libs, just the win32 api.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You could just set it up as a 2D array. Then when the user clicks on the screen you could translate the mouse coordinates to array indices. You might do something like, when the user clicks and holds, highlight whichever grid he is about to select and when he releases, select it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    I understand what you mean, but I'm not too sure about how to do what you say. Could you post a little example code please so that I can see how to do it

  4. #4
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Caption bar will display the (column,row) of the cell clicked.

    Code:
    #include <windows.h>
    
    #define MATRIX_WIDTH  5
    #define MATRIX_HEIGHT 4
    #define CELL_WIDTH    100
    #define CELL_HEIGHT   100
    
    LRESULT CALLBACK WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam )
    {
      static char   matrix[MATRIX_WIDTH][MATRIX_HEIGHT];
      static HBRUSH brush = (HBRUSH)GetStockObject(BLACK_BRUSH);
      
      switch(msg)  
      {
        case WM_PAINT:
        {
          PAINTSTRUCT ps;
          RECT        rc;
          HDC         hdc;
          
          hdc = BeginPaint( hwnd, &ps );    
          
          for( int row = 0; row < MATRIX_HEIGHT; ++row )
          {
            for( int column = 0; column < MATRIX_WIDTH; ++column )
            {
              rc.left   = column  * CELL_WIDTH;
              rc.top    = row     * CELL_HEIGHT;
              rc.right  = rc.left + CELL_WIDTH;
              rc.bottom = rc.top  + CELL_HEIGHT;
    
              FrameRect( hdc, &rc, brush );
            }
          }
          
          EndPaint ( hwnd, &ps );
          
          return 0;        
        }
        
        case WM_LBUTTONDOWN:
        {
          int x = LOWORD(lParam);
          int y = HIWORD(lParam);
          
          char buffer[100];
          wsprintf( buffer, "You clicked in column %d, row %d", 
                    x/CELL_WIDTH, y/CELL_HEIGHT );
          SetWindowText( hwnd, buffer );
          
          return 0; 
        }
    
        case WM_DESTROY:
        {
          PostQuitMessage ( 0 ); 
        }
    
        default: return DefWindowProc(hwnd,msg,wParam,lParam);
      }
    }
    
    int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow )
    {
      WNDCLASS wc = {0};
      wc.lpszClassName = TEXT( "Minimum" );
      wc.hInstance     = hInst ;
      wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
      wc.lpfnWndProc   = WndProc ;
      wc.hCursor       = LoadCursor(0,IDC_ARROW);
      
      RegisterClass(&wc);
    
      RECT rc = { 0, 0, MATRIX_WIDTH * CELL_WIDTH, MATRIX_HEIGHT * CELL_HEIGHT };
      AdjustWindowRect( &rc, WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU, FALSE );    
      
      HWND hwnd = CreateWindow(wc.lpszClassName,TEXT("Click on a cell."),
                               WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU ,
                               0,0,rc.right-rc.left,rc.bottom-rc.top,0,0,hInst,0);
                               
      
      MSG  msg ;  
      while( GetMessage(&msg,0,0,0) > 0 ) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int)msg.wParam;
    }
    Attached Images Attached Images  

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    the simplest way is two comboboxes (One for X and one for Y) and a button to tell that the user has finished selecting from the combos.

    You can stack both combos easily (at the start of the app) with a loop and they will retain the last selection.

    But I (as a user) would prefer the grid. I would try to move the code from the WM_PAINT handler though. Try looking at 'double buffering', should find many code snippets here.

    Code:
    HBRUSH hbrSelectedCell=CreateSolidBrush(COLORREF(255,0,0));//red brush
    for( int row = 0; row < MATRIX_HEIGHT; ++row )
          {
            for( int column = 0; column < MATRIX_WIDTH; ++column )
            {
              rc.left   = column  * CELL_WIDTH;
              rc.top    = row     * CELL_HEIGHT;
              rc.right  = rc.left + CELL_WIDTH;
              rc.bottom = rc.top  + CELL_HEIGHT;
              //highlight the selected cell
              if((row==Selected_Row)&&(column==Selected_Col))
                    FrameRect(hdc,&rc,hbrSelectedCell)
              else
                    FrameRect( hdc, &rc, brush );
            }
          }
    DeleteObject(hbrSelectedCell);//clean up GDI. Not selected into DC so can just delete
    Last edited by novacain; 03-18-2005 at 09:36 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    That code is just what I wanted, but how would you limit the area that the grid fills so that it only fills a part of the window, not the entire window? Thanks for the code, much appriciated

  7. #7
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    but how would you limit the area that the grid fills so that it only fills a part of the window, not the entire window?
    Change the following lines in WinMain.

    Code:
      RECT rc = { 0, 0, MATRIX_WIDTH * CELL_WIDTH, MATRIX_HEIGHT * CELL_HEIGHT };
      AdjustWindowRect( &rc, WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU, FALSE );    
      
      HWND hwnd = CreateWindow(wc.lpszClassName,TEXT("Click on a cell."),
                               WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU ,
                               0,0,rc.right-rc.left,rc.bottom-rc.top,0,0,hInst,0);
    I used AdjustWindowRect to make it so that the size of the window's client area is the same as the matrix. If you do not wish this to happen, remove the AdjustWindowRect line and give your own width and height values to CreateWindow (bolded).

    And to change the size of the matrix, adjust the #defines at the top of the source code.
    Last edited by Dante Shamest; 03-19-2005 at 12:59 PM.

  8. #8
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Thank you very much for your help, I'll look at the source code and learn from it. At least I know how to make a grid now, so I don't have to program in a few hundred little buttons on the screen, that would take ages

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 06:39 AM
  2. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM
  4. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  5. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 11:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21