1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
#include <windows.h>
#include <iostream>
#include <time.h>
#define BUTTON_SIZE 25
#define SPACING 25
HINSTANCE g_hInst;
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
TCHAR szAppName[] = TEXT("Minesweeper");
TCHAR szWinClass[] = TEXT("WIN_CLASS");
DWORD WINAPI cLine( LPVOID param )
{
int *stay = (int*)param;
while (*stay==2)
{
std::cout << "Type 1 to reset, 0 to quit.\n";
std::cin >> *stay;
if (*stay > 2 || *stay < 0)
*stay = 2;
}
/////////////
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
WNDCLASS wc, bc;
HWND hwnd;
g_hInst = hInstance;
HANDLE prompt;
wc.style = 0;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szWinClass;
//Get rid of this Button class class - it clashes with
// the default windows button style.
/*bc.style = 0;
bc.lpfnWndProc = WinProc;
bc.cbClsExtra = 0;
bc.cbWndExtra = 0;
bc.hInstance = hInstance;
bc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
bc.hCursor = LoadCursor(NULL, IDC_ARROW);
bc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
bc.lpszMenuName = NULL;
bc.lpszClassName = _T("Button");*/
RegisterClass(&wc);
//RegisterClass(&bc);
int stay=1;
while(stay)
{
srand(time(NULL));
bool ** grid;
HWND ** buttons;
//Got rid of the sonsole cout and cin stuff and made it fixed size for
//testing purposes
int sizes[4]={4,4,4,4};
stay = 2;
//Got rid of cout and cin stuff and fixed the number of bombs
long long int numBombs = 10;
bool invalid = 1;
grid = new bool*[sizes[0]*sizes[1]];
buttons = new HWND*[sizes[0]*sizes[1]];
for (int i=0;i<sizes[0];i++)
for (int j=0;j<sizes[1];j++)
{
grid[i*sizes[1]+j] = new bool[sizes[2]*sizes[3]];
buttons[i*sizes[1]+j] = new HWND[sizes[2]*sizes[3]];
}
invalid = 1;
bool initBombs;
if (numBombs < (sizes[0]*sizes[1]*sizes[2]*sizes[3])/2)
initBombs = 0;
else
initBombs = 1;
for (int x=0;x<sizes[0];x++)
for (int y=0;y<sizes[1];y++)
for (int z=0;z<sizes[2];z++)
for (int t=0;t<sizes[3];t++)
(grid[x*sizes[1]+y])[z*sizes[3]+t] = initBombs;
while(numBombs)
if ((grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] == initBombs)
{
(grid[(rand()%sizes[0])*sizes[1]+rand()%sizes[1]])
[(rand()%sizes[2])*sizes[3]+rand()%sizes[3]] = !initBombs;
numBombs--;
}
hwnd = CreateWindow(szWinClass,
szAppName,
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPCHILDREN,
300,
300,
(sizes[0]*BUTTON_SIZE+SPACING)*sizes[2]-SPACING,
(sizes[1]*BUTTON_SIZE+SPACING)*sizes[3]-SPACING,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
for (int x=0;x<sizes[0];x++)
for (int y=0;y<sizes[1];y++)
for (int z=0;z<sizes[2];z++)
for (int t=0;t<sizes[3];t++)
{
//made some temp variables for easier debugging
int z1, z2, z3;
z1 = z*(SPACING+BUTTON_SIZE*sizes[0])+x*BUTTON_SIZE;
z2 = t*(SPACING+BUTTON_SIZE*sizes[1])+y*BUTTON_SIZE;
z3 = (sizes[2]*(sizes[1]*(sizes[0]*x+y)+z)+t);
(buttons[x*sizes[1]+y])[z*sizes[3]+t] =
CreateWindow(_T("BUTTON"), //szWinClass <- you had the wrong window class here!!!!
_T(""),
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
z1, //z*(SPACING+BUTTON_SIZE*sizes[0])+x*BUTTON_SIZE,
z2, //t*(SPACING+BUTTON_SIZE*sizes[1])+y*BUTTON_SIZE,
BUTTON_SIZE,
BUTTON_SIZE,
hwnd,
(HMENU)z3, //(HMENU)(sizes[2]*(sizes[1]*(sizes[0]*x+y)+z)+t),
hInstance,
NULL);
}
//Don't need this anymore
/*HWND lalz = CreateWindow(
_T("BUTTON"),_T("BUTTON"),BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
50,50,100,25,hwnd,(HMENU)156,NULL,NULL
);
ShowWindow(lalz, nCmdShow);
UpdateWindow(lalz);*/
//Temporarily removed thread for testing purposes
//prompt = CreateThread( NULL, 0, cLine, &stay, 0, NULL);
while (stay==2)
{
if (PeekMessage (&msg, hwnd, 0, 0, PM_REMOVE))
{
if (msg.message == WM_COMMAND)
{
for (int x=0;x<sizes[0];x++)
for (int y=0;y<sizes[1];y++)
for (int z=0;z<sizes[2];z++)
for (int t=0;t<sizes[3];t++)
if ((HWND)msg.wParam == (buttons[x*sizes[1]+y])[z*sizes[3]+t])
{
if ((grid[x*sizes[1]+y])[z*sizes[3]+t])
{
stay = 2;
std::cout << "You lose, sorry! :(\n";
}
int adjBombs=0;//////////////////////
for (int i=-1;i<2;i++)
for (int j=-1;j<2;j++)
for (int k=-1;k<2;k++)
for (int u=-1;u<2;u++)
if (x+i>=0&&x+i<sizes[0]&&y+j>=0&&y+j<sizes[1]&&z+k>=0&&z+k<sizes[2]&&t+u>=0&&t+u<sizes[3])
if ((grid[(x+i)*sizes[1]+(y+j)])[(z+k)*sizes[3]+(t+u)])
adjBombs++;
SetWindowText((buttons[x*sizes[1]+y])[z*sizes[3]+t],_T("HARR"));
}
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
//do nothing??
}
}
for (int i=0;i<sizes[0];i++)
for (int j=0;j<sizes[1];j++)
{
delete grid[i*sizes[1]+j];
delete buttons[i*sizes[1]+j];
}
delete grid;
delete buttons;
DestroyWindow(hwnd);
}
return msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
break;
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break; // pass to DefWindowProc(...) as well
case WM_CLOSE:
DestroyWindow(hwnd);
break;
default:
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
|