Προς το περιεχόμενο

Αλγόριθμος Τεχνητής Νοημοσύνης για το παιχνίδι της Ναυμαχίας


migf1

Προτεινόμενες αναρτήσεις

  • Απαντ. 35
  • Δημ.
  • Τελ. απάντηση

Συχνή συμμετοχή στο θέμα

Συχνή συμμετοχή στο θέμα

Χαλαρά, δεν έχουμε deadlines :) Κι εγώ με το sink-mode θα δω πότε θα το κάνω :lol:

 

Όταν καταλήξεις με τις διαστάσεις μου λες... με 50άρια κελιά πάντως θέλει 1200px οριζόντια ανάλυση για να χωρέσουν 2 boards πλάι, πλάι... too much νομίζω

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

Χμμ ειναι πιο δυσκολο απο΄σο φανταζομουν confused.png

 

Αντε να φτιαξω και το rotate και θα ειναι gg grin.png

 

 

 

>
#include <Windows.h>
#include <math.h>
#include <tchar.h>


#define SHIP_TYPE_WATER						0
#define SHIP_TYPE_AIRCARRY					1
#define SHIP_TYPE_BATTLE					2
#define SHIP_TYPE_DESTROYER					3
#define SHIP_TYPE_PATROL					4
#define SHIP_TYPE_VENIZELOS					5

#define GFX_FLAG_H							(1)
#define GFX_FLAG_V							(1<<1)
#define GFX_FLAG_HEALTHY					(1<<2)
#define GFX_FLAG_DAMAGE						(1<<3)

#define GRID_FLAG_MUTABLE					(1)
	



#define WM_GRID_EX							(WM_USER + 1)
#define GRID_RESIZEMATRIX					1					//LPARAM HIWORD x LOWORD y
#define GRID_GETMATRIX						2					//LPARAM not used
#define GRID_INSERT_SHIP					3					//LPARAM GRID_INSERT_SHIP_STRUCT ptr


//Grid command notify
#define GRID_CLICK							1					// WPARAM LOWORD grid id HIWORD event LPARAM HIWORD x LOWORD y




#define Grid_ResizeMatrix(hGrid,x,y)		SendMessage(hGrid,WM_GRID_EX,GRID_RESIZEMATRIX,MAKELPARAM(x,y))
#define Grid_GetMatrixPtr(hGrid)			(SHIP**)SendMessage(hGrid,WM_GRID_EX,GRID_GETMATRIX,0)
#define Grid_InsertShip(hGrid,pgis)			SendMessage(hGrid,WM_GRID_EX,GRID_INSERT_SHIP,(LPARAM)pgis);


#define IDC_GRIDPALYER						101
#define IDC_GRIDCOMPUTER					102
#define IDC_SHIPCONTAINER					103
		


#define DIFF(x,y)			(max(x,y) - min(x,y))

typedef struct
{
int		id;
POINT	pLocH;
POINT	pLocV;
int		parts;
int		flags;
}SHIP_RESOURCE;
typedef struct
{
int x,y,cx,cy;
}SPIRIT;
typedef struct
{
int id;
int part;
int flags;
}SHIP;
typedef struct
{
HBITMAP				hBitmap;
SHIP_RESOURCE		*pShips;
int					nShips;
POINT				sz;

}GFX;
typedef struct
{
int		matX,matY;
SHIP	**matrixEx;
GFX		gfx;
int		sel;
POINT	selStart;
int		flags;
}GRID_INFO;
typedef struct
{
POINT p;
int shipId;
}GRID_INSERT_SHIP_STRUCT;




PCTSTR	szClsMain = _T("clsMain");
PCTSTR	szClsGrid = _T("clsGrid");


HRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
HRESULT CALLBACK _GridProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);


BOOL	RegisterClasses(HINSTANCE hInst);

int Gfx_Create(GFX* gfx);
int Gfx_GetSpirit(GFX* gfx,SHIP* ship, SPIRIT* pSpirit);

int Matrix_InsertShip(GRID_INFO* gi,int shipId,int x,int y);
int Matrix_MoveShip(GRID_INFO* gi,int shipId,/*Offsets*/int x,int y);
int Matrix_RotateShip(GRID_INFO* gi,int shipId);




//event handlers
void Grid_Clicked(HWND hGrid, int x, int y);
void Grid_OnInsertShip(HWND hGrid,int x,int y,int shipId);


int WINAPI _tWinMain(HINSTANCE hInst,HINSTANCE hPrev,PTSTR szCmdLine,int nShow)
{

MSG msg;
HWND hWndMain;
if(!RegisterClasses(hInst))
	return 1;
hWndMain = CreateWindowEx(WS_EX_COMPOSITED,szClsMain,_T("Title"),WS_OVERLAPPEDWINDOW,100,100,1040,600,NULL,NULL,hInst,NULL);


ShowWindow(hWndMain,nShow);

while(GetMessage(&msg,NULL,0,0) > 0)
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}
return 0;
}


HRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
POINT p;
PAINTSTRUCT ps;
HDC			hdc;
switch(msg)
{
case WM_MOUSEMOVE:
	break;
case WM_CREATE:
	CreateWindowEx(WS_EX_COMPOSITED,szClsGrid,NULL,WS_VISIBLE | WS_CHILD,520,20,480,480,hWnd,(HMENU)IDC_GRIDPALYER,NULL,NULL);
	CreateWindowEx(WS_EX_COMPOSITED,szClsGrid,NULL,WS_VISIBLE | WS_CHILD,20,20,480,480,hWnd,(HMENU)IDC_GRIDCOMPUTER,NULL,(LPVOID)GRID_FLAG_MUTABLE);
	Grid_ResizeMatrix(GetDlgItem(hWnd,IDC_GRIDPALYER),10,10);
	Grid_ResizeMatrix(GetDlgItem(hWnd,IDC_GRIDCOMPUTER),10,10);
	break;
case  WM_CLOSE:
	PostQuitMessage(0);
case WM_COMMAND:
	switch(LOWORD(wParam))
	{
	case IDC_GRIDPALYER:
		switch(HIWORD(wParam))
		{
		case GRID_CLICK:
			Grid_Clicked(GetDlgItem(hWnd,LOWORD(wParam)),HIWORD(lParam),LOWORD(lParam));
			break;
		}
	default:
		return DefWindowProc(hWnd,msg,wParam,lParam);
	}
	break;
case WM_MBUTTONDOWN:
	{
		GetCursorPos(&p);

	}break;
case WM_PAINT:
	hdc = BeginPaint(hWnd,&ps);
	EndPaint(hWnd,&ps);
	break;
default:
	return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}

HRESULT CALLBACK _GridProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
GRID_INFO		*pInfo;
PAINTSTRUCT		ps;
HDC				hdc,hdcMem;
HGDIOBJ			hOld;
int				i,j;
RECT			rect;
POINT			p;
SPIRIT			spirit;
GRID_INSERT_SHIP_STRUCT gis,*pgis;

static HMENU	hPopup,hPopupRotate;
switch(msg)
{
case WM_RBUTTONDOWN:
	GetCursorPos(&p);
	GetClientRect(hWnd,&rect);
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	if(pInfo->flags & GRID_FLAG_MUTABLE)
		break;
	gis.p.x = LOWORD(lParam) / ((rect.right - rect.left) /pInfo->matX );
	gis.p.y = HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY);
	//tsekare an pataei deksi clck se ena object
	if(pInfo->matrixEx[gis.p.y][gis.p.x].id != SHIP_TYPE_WATER)
	{
		//vgale to rotate menu
		if(TrackPopupMenu(hPopupRotate,TPM_RETURNCMD,p.x,p.y,0,hWnd,NULL) == 1)
		{
			Matrix_RotateShip(pInfo,pInfo->matrixEx[gis.p.y][gis.p.x].id);
			break;
		}
	}
	gis.shipId = TrackPopupMenu(hPopup,TPM_RETURNCMD,p.x,p.y,0,hWnd,NULL);
	Grid_InsertShip(hWnd,&gis);
	break;
case WM_LBUTTONDOWN:
	GetClientRect(hWnd,&rect);
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	if(! (pInfo->flags & GRID_FLAG_MUTABLE))
	{
		//select object
		i = LOWORD(lParam) / ((rect.right - rect.left) /pInfo->matX );
		j = HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY);
		if(pInfo->matrixEx[i][j].id != SHIP_TYPE_WATER)
		{
			pInfo->sel = pInfo->matrixEx[i][j].id;
			pInfo->selStart.x = i;
			pInfo->selStart.y = j;
		}
		break;
	}
	SendMessage(GetParent(hWnd),WM_COMMAND,MAKEWPARAM((WPARAM)GetMenu(hWnd),GRID_CLICK),
				MAKELPARAM
				(
				HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY) ,
				LOWORD(lParam) / ((rect.right - rect.left) /pInfo->matX ) 
				)
			);
	break;
case WM_LBUTTONUP:
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	pInfo->sel = -1;
	//Matrix_MoveShip(pInfo,SHIP_TYPE_AIRCARRY,-1,0);
	break;
case WM_MOUSEMOVE:
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	GetClientRect(hWnd,&rect);
	if(pInfo->sel != -1
		&& wParam & MK_LBUTTON)
	{
		i =  LOWORD(lParam) / ((rect.right - rect.left) / pInfo->matX ) - pInfo->selStart.x ;
		j =  HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY) - pInfo->selStart.y  ;
		if(i || j)
		{
			Matrix_MoveShip(pInfo,pInfo->sel,i,j);
			
			pInfo->selStart.x = LOWORD(lParam) / ((rect.right - rect.left) / pInfo->matX );
			pInfo->selStart.y =  HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY);
			InvalidateRect(hWnd,NULL,TRUE);

		}

	}
	break;
case WM_GRID_EX:
	switch(wParam)
	{
	case GRID_INSERT_SHIP:
		pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
		pgis = (GRID_INSERT_SHIP_STRUCT*) lParam;
		Matrix_InsertShip(pInfo,pgis->shipId,pgis->p.x,pgis->p.y);
		InvalidateRect(hWnd,NULL,TRUE);
		break;
	case GRID_RESIZEMATRIX:
		pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
		//todo free matrix if exist
		pInfo->matX = HIWORD(lParam);
		pInfo->matY	= LOWORD(lParam);
		pInfo->matrixEx = malloc(sizeof(void*) * pInfo->matY);
		for(i =0; i< pInfo->matY; i++)
			pInfo->matrixEx[i] = calloc(pInfo->matX,sizeof(SHIP));
		RedrawWindow(hWnd,NULL,NULL,RDW_UPDATENOW);
		break;
	case GRID_GETMATRIX:
		pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
		return (HRESULT)pInfo->matrixEx;
	default:
		return 0;
	}
	break;
case WM_CREATE:
	pInfo = (GRID_INFO*) malloc(sizeof(GRID_INFO));
	if(!pInfo)
		return -1;
	ZeroMemory(pInfo,sizeof(GRID_INFO));
	pInfo->flags = (int)((CREATESTRUCT*)lParam)->lpCreateParams;
	SetWindowLongPtr(hWnd,GWLP_USERDATA,(LONG)pInfo);
	Grid_ResizeMatrix(hWnd,10,10); //defualt
	hPopup = CreatePopupMenu();
	hPopupRotate = CreatePopupMenu();
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_AIRCARRY,_T("Air carry"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_BATTLE,_T("Battle ship"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_DESTROYER,_T("Destroyer ship"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_PATROL,_T("Patrol ship"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_VENIZELOS,_T("Spasmeno ypovrixio"));

	AppendMenu(hPopupRotate,MF_STRING,1,_T("Rotate"));

	Gfx_Create(&pInfo->gfx);
	
	break;
case WM_PAINT:
	//draw grid
	hdc = BeginPaint(hWnd,&ps);
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	GetClientRect(hWnd,&rect);

	hdcMem = CreateCompatibleDC(hdc);
	hOld = SelectObject(hdcMem,pInfo->gfx.hBitmap);
	for(i=0;i < pInfo->matX; i++)
	{
		for(j=0; j < pInfo->matY; j++)
		{
			Gfx_GetSpirit(&pInfo->gfx,&pInfo->matrixEx[i][j],&spirit);
			BitBlt(hdc,
				j * ((rect.right - rect.left) / pInfo->matX),
				i * ((rect.bottom - rect.top) / pInfo->matY),
				spirit.cx,
				spirit.cy,
				hdcMem,
				spirit.x,
				spirit.y,
				SRCCOPY);
		}
	}
	SelectObject(hdc,hOld);
	DeleteDC(hdcMem);
	EndPaint(hWnd,&ps);
	break;
case WM_ERASEBKGND:
	return TRUE;
case WM_DESTROY:
	pInfo = (GRID_INFO*)GetWindowLongPtr(hWnd,GWLP_USERDATA);
	//todo free
	DestroyMenu(hPopup);
	break;
default:
	return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}

BOOL RegisterClasses(HINSTANCE hInst)
{
WNDCLASSEX			wcGrid,wcMain;

ZeroMemory(&wcGrid,sizeof(wcGrid));
ZeroMemory(&wcMain,sizeof(wcMain));

wcGrid.cbSize			= sizeof(wcGrid);
wcGrid.style			= CS_SAVEBITS;
wcGrid.hCursor			= LoadCursor(NULL,IDC_HAND);
wcGrid.lpfnWndProc		= _GridProc;
wcGrid.lpszClassName	= szClsGrid;
wcGrid.hInstance		= hInst;

wcMain.cbSize			= sizeof(wcMain);
wcMain.hCursor			= LoadCursor(NULL,IDC_ARROW);
wcMain.hInstance		= hInst;
wcMain.lpfnWndProc		= WndProc;
wcMain.lpszClassName	= szClsMain;
wcMain.style			= (CS_HREDRAW | CS_VREDRAW);
wcMain.hbrBackground	= (HBRUSH) (COLOR_WINDOW);


return (
	RegisterClassEx(&wcMain) 
	&& RegisterClassEx(&wcGrid)
	);
}




int Gfx_Create(GFX* gfx)
{
gfx->hBitmap = LoadImage(NULL,_T("gfx.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(!gfx->hBitmap)
	return 1;
gfx->nShips = 5;
gfx->pShips = malloc(sizeof(SHIP_RESOURCE) * gfx->nShips);

ZeroMemory(gfx->pShips,sizeof(SHIP_RESOURCE) * gfx->nShips);

gfx->sz.x = 48;
gfx->sz.y = 48;

gfx->pShips[0].id			= SHIP_TYPE_AIRCARRY;
gfx->pShips[0].parts		= 5;
gfx->pShips[0].pLocH.x		= gfx->sz.x * 0;
gfx->pShips[0].pLocH.y		= gfx->sz.y * 1;
gfx->pShips[0].pLocV.x		= gfx->sz.x * 0;
gfx->pShips[0].pLocV.y		= gfx->sz.y * 8;


return 0;
}
int Gfx_GetSpirit(GFX* gfx,SHIP* ship, SPIRIT* pSpirit)
{
int i;
if(ship->id == SHIP_TYPE_WATER)
{
	if(!ship->flags)
	{
		pSpirit->x = 0;
		pSpirit->y = 0;
		pSpirit->cx = 48;
		pSpirit->cy = 48;
	}
	return 0;
}
for(i = 0; i < gfx->nShips; i++)
{
	if(gfx->pShips[i].id == ship->id)
	{
		if(ship->flags & GFX_FLAG_H 
			&& ship->flags & GFX_FLAG_HEALTHY)
		{
			pSpirit->x = gfx->pShips[i].pLocH.x + ship->part * gfx->sz.x;
			pSpirit->y = gfx->pShips[i].pLocH.y;
			pSpirit->cx = gfx->sz.x;
			pSpirit->cy = gfx->sz.y;
		}
		return 0;
	}
}
return 1;
}

int Matrix_InsertShip(GRID_INFO* gi,int shipId,int x,int y)
{
int parts;
int i,j;

//check if exist and erase
for(i = 0; i < gi->matX; i++)
	for(j = 0; j <gi->matY; j++)
		if(gi->matrixEx[j][i].id == shipId)
		{
			gi->matrixEx[j][i].id = SHIP_TYPE_WATER;
			gi->matrixEx[j][i].flags =0;
		}
//get parts count;
for(i = 0; i < gi->gfx.nShips ; i++)
	if(gi->gfx.pShips[i].id == shipId)
		parts = gi->gfx.pShips[i].parts;

if(gi->matX < x + parts)
	return Matrix_InsertShip(gi,shipId,x-1,y);

for(i = 0; i < parts; i++)
{
	gi->matrixEx[y][x+i].part = i;
	gi->matrixEx[y][x+i].id	= shipId;
	gi->matrixEx[y][x+i].flags = GFX_FLAG_H | GFX_FLAG_HEALTHY;
}


return 0;
}
int Matrix_MoveShip(GRID_INFO* gi,int shipId,/*Offsets*/int x,int y)
{
int		i,j;
int		parts;
POINT	shipStart;
SHIP	shipClone[10];
//find ship len
for(i = 0; i < gi->gfx.nShips; i++)
{
	if(gi->gfx.pShips[i].id == shipId)
	{
		parts = gi->gfx.pShips[i].parts;
		break;
	}
}
//find ship head
for(i = 0; i < gi->matX; i++)
{
	for(j = 0; j < gi->matY; j++)
	{
		if(gi->matrixEx[j][i].id == shipId 
			&& gi->matrixEx[j][i].part == 0)
		{
			shipStart.x = i;
			shipStart.y = j;
			break;
		}
	}
}
//chech for overflow
if(gi->matrixEx[shipStart.y][shipStart.x].flags & GFX_FLAG_H)
{
	if(
		shipStart.x + x + parts > gi->matX
		|| shipStart.x + x < 0
		|| shipStart.y + y < 0
		|| shipStart.x + y > gi->matY
		)
		return 0;
}
else
{
	if(
		shipStart.x + x > gi->matX
		|| shipStart.x + x < 0
		|| shipStart.y + y < 0
		|| shipStart.x + y + parts > gi->matY
		)
		return 0;
}

//clone ship & earse
if(gi->matrixEx[shipStart.y][shipStart.x].flags & GFX_FLAG_H)
{
	for(i = 0 ; i < parts; i++)
	{
		memcpy(&shipClone[i],&gi->matrixEx[shipStart.y][shipStart.x + i],sizeof(SHIP));
		gi->matrixEx[shipStart.y][shipStart.x + i].id = SHIP_TYPE_WATER;
		gi->matrixEx[shipStart.y][shipStart.x + i].flags = 0;
	}
}
else
{
	for(i = 0 ; i < parts; i++)
	{
		memcpy(&shipClone[i],&gi->matrixEx[shipStart.y + i][shipStart.x],sizeof(SHIP));
		gi->matrixEx[shipStart.y][shipStart.x + i].id = SHIP_TYPE_WATER;
		gi->matrixEx[shipStart.y][shipStart.x + i].flags = 0;
	}
}

//place ship
if(shipClone[0].flags & GFX_FLAG_H)
{
	for(i = 0; i < parts; i++)
	{
		memcpy(&gi->matrixEx[shipStart.y + y][shipStart.x + x + i],&shipClone[i],sizeof(SHIP));
	}
}
else
{
	for(i = 0; i < parts; i++)
	{
		memcpy(&gi->matrixEx[shipStart.y + y + i][shipStart.x + x ],&shipClone[i],sizeof(SHIP));
	}
}

return 0;
}
int Matrix_RotateShip(GRID_INFO* gi,int shipId)
{

}




void Grid_Clicked(HWND hGrid,int x,int y)
{

}

void Grid_OnInsertShip(HWND hGrid,int x,int y,int shipId)
{

}

 

 

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

Χμμ ειναι πιο δυσκολο απο΄σο φανταζομουν confused.png

Είδες το άτιμο; Κι εγώ όταν το ξεκίνησα λέω οκ, πόσο θα με πάρει μωρέ; Και τελικά αποδείχτηκε πολύ πιο απαιτητικό.

 

Αντε να φτιαξω και το rotate και θα ειναι gg grin.png

 

 

 

>
#include <Windows.h>
#include <math.h>
#include <tchar.h>


#define SHIP_TYPE_WATER						0
#define SHIP_TYPE_AIRCARRY					1
#define SHIP_TYPE_BATTLE					2
#define SHIP_TYPE_DESTROYER					3
#define SHIP_TYPE_PATROL					4
#define SHIP_TYPE_VENIZELOS					5

#define GFX_FLAG_H							(1)
#define GFX_FLAG_V							(1<<1)
#define GFX_FLAG_HEALTHY					(1<<2)
#define GFX_FLAG_DAMAGE						(1<<3)

#define GRID_FLAG_MUTABLE					(1)
	



#define WM_GRID_EX							(WM_USER + 1)
#define GRID_RESIZEMATRIX					1					//LPARAM HIWORD x LOWORD y
#define GRID_GETMATRIX						2					//LPARAM not used
#define GRID_INSERT_SHIP					3					//LPARAM GRID_INSERT_SHIP_STRUCT ptr


//Grid command notify
#define GRID_CLICK							1					// WPARAM LOWORD grid id HIWORD event LPARAM HIWORD x LOWORD y




#define Grid_ResizeMatrix(hGrid,x,y)		SendMessage(hGrid,WM_GRID_EX,GRID_RESIZEMATRIX,MAKELPARAM(x,y))
#define Grid_GetMatrixPtr(hGrid)			(SHIP**)SendMessage(hGrid,WM_GRID_EX,GRID_GETMATRIX,0)
#define Grid_InsertShip(hGrid,pgis)			SendMessage(hGrid,WM_GRID_EX,GRID_INSERT_SHIP,(LPARAM)pgis);


#define IDC_GRIDPALYER						101
#define IDC_GRIDCOMPUTER					102
#define IDC_SHIPCONTAINER					103
		


#define DIFF(x,y)			(max(x,y) - min(x,y))

typedef struct
{
int		id;
POINT	pLocH;
POINT	pLocV;
int		parts;
int		flags;
}SHIP_RESOURCE;
typedef struct
{
int x,y,cx,cy;
}SPIRIT;
typedef struct
{
int id;
int part;
int flags;
}SHIP;
typedef struct
{
HBITMAP				hBitmap;
SHIP_RESOURCE		*pShips;
int					nShips;
POINT				sz;

}GFX;
typedef struct
{
int		matX,matY;
SHIP	**matrixEx;
GFX		gfx;
int		sel;
POINT	selStart;
int		flags;
}GRID_INFO;
typedef struct
{
POINT p;
int shipId;
}GRID_INSERT_SHIP_STRUCT;




PCTSTR	szClsMain = _T("clsMain");
PCTSTR	szClsGrid = _T("clsGrid");


HRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
HRESULT CALLBACK _GridProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);


BOOL	RegisterClasses(HINSTANCE hInst);

int Gfx_Create(GFX* gfx);
int Gfx_GetSpirit(GFX* gfx,SHIP* ship, SPIRIT* pSpirit);

int Matrix_InsertShip(GRID_INFO* gi,int shipId,int x,int y);
int Matrix_MoveShip(GRID_INFO* gi,int shipId,/*Offsets*/int x,int y);
int Matrix_RotateShip(GRID_INFO* gi,int shipId);




//event handlers
void Grid_Clicked(HWND hGrid, int x, int y);
void Grid_OnInsertShip(HWND hGrid,int x,int y,int shipId);


int WINAPI _tWinMain(HINSTANCE hInst,HINSTANCE hPrev,PTSTR szCmdLine,int nShow)
{

MSG msg;
HWND hWndMain;
if(!RegisterClasses(hInst))
	return 1;
hWndMain = CreateWindowEx(WS_EX_COMPOSITED,szClsMain,_T("Title"),WS_OVERLAPPEDWINDOW,100,100,1040,600,NULL,NULL,hInst,NULL);


ShowWindow(hWndMain,nShow);

while(GetMessage(&msg,NULL,0,0) > 0)
{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
}
return 0;
}


HRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
POINT p;
PAINTSTRUCT ps;
HDC			hdc;
switch(msg)
{
case WM_MOUSEMOVE:
	break;
case WM_CREATE:
	CreateWindowEx(WS_EX_COMPOSITED,szClsGrid,NULL,WS_VISIBLE | WS_CHILD,520,20,480,480,hWnd,(HMENU)IDC_GRIDPALYER,NULL,NULL);
	CreateWindowEx(WS_EX_COMPOSITED,szClsGrid,NULL,WS_VISIBLE | WS_CHILD,20,20,480,480,hWnd,(HMENU)IDC_GRIDCOMPUTER,NULL,(LPVOID)GRID_FLAG_MUTABLE);
	Grid_ResizeMatrix(GetDlgItem(hWnd,IDC_GRIDPALYER),10,10);
	Grid_ResizeMatrix(GetDlgItem(hWnd,IDC_GRIDCOMPUTER),10,10);
	break;
case  WM_CLOSE:
	PostQuitMessage(0);
case WM_COMMAND:
	switch(LOWORD(wParam))
	{
	case IDC_GRIDPALYER:
		switch(HIWORD(wParam))
		{
		case GRID_CLICK:
			Grid_Clicked(GetDlgItem(hWnd,LOWORD(wParam)),HIWORD(lParam),LOWORD(lParam));
			break;
		}
	default:
		return DefWindowProc(hWnd,msg,wParam,lParam);
	}
	break;
case WM_MBUTTONDOWN:
	{
		GetCursorPos(&p);

	}break;
case WM_PAINT:
	hdc = BeginPaint(hWnd,&ps);
	EndPaint(hWnd,&ps);
	break;
default:
	return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}

HRESULT CALLBACK _GridProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
GRID_INFO		*pInfo;
PAINTSTRUCT		ps;
HDC				hdc,hdcMem;
HGDIOBJ			hOld;
int				i,j;
RECT			rect;
POINT			p;
SPIRIT			spirit;
GRID_INSERT_SHIP_STRUCT gis,*pgis;

static HMENU	hPopup,hPopupRotate;
switch(msg)
{
case WM_RBUTTONDOWN:
	GetCursorPos(&p);
	GetClientRect(hWnd,&rect);
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	if(pInfo->flags & GRID_FLAG_MUTABLE)
		break;
	gis.p.x = LOWORD(lParam) / ((rect.right - rect.left) /pInfo->matX );
	gis.p.y = HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY);
	//tsekare an pataei deksi clck se ena object
	if(pInfo->matrixEx[gis.p.y][gis.p.x].id != SHIP_TYPE_WATER)
	{
		//vgale to rotate menu
		if(TrackPopupMenu(hPopupRotate,TPM_RETURNCMD,p.x,p.y,0,hWnd,NULL) == 1)
		{
			Matrix_RotateShip(pInfo,pInfo->matrixEx[gis.p.y][gis.p.x].id);
			break;
		}
	}
	gis.shipId = TrackPopupMenu(hPopup,TPM_RETURNCMD,p.x,p.y,0,hWnd,NULL);
	Grid_InsertShip(hWnd,&gis);
	break;
case WM_LBUTTONDOWN:
	GetClientRect(hWnd,&rect);
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	if(! (pInfo->flags & GRID_FLAG_MUTABLE))
	{
		//select object
		i = LOWORD(lParam) / ((rect.right - rect.left) /pInfo->matX );
		j = HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY);
		if(pInfo->matrixEx[i][j].id != SHIP_TYPE_WATER)
		{
			pInfo->sel = pInfo->matrixEx[i][j].id;
			pInfo->selStart.x = i;
			pInfo->selStart.y = j;
		}
		break;
	}
	SendMessage(GetParent(hWnd),WM_COMMAND,MAKEWPARAM((WPARAM)GetMenu(hWnd),GRID_CLICK),
				MAKELPARAM
				(
				HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY) ,
				LOWORD(lParam) / ((rect.right - rect.left) /pInfo->matX ) 
				)
			);
	break;
case WM_LBUTTONUP:
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	pInfo->sel = -1;
	//Matrix_MoveShip(pInfo,SHIP_TYPE_AIRCARRY,-1,0);
	break;
case WM_MOUSEMOVE:
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	GetClientRect(hWnd,&rect);
	if(pInfo->sel != -1
		&& wParam & MK_LBUTTON)
	{
		i =  LOWORD(lParam) / ((rect.right - rect.left) / pInfo->matX ) - pInfo->selStart.x ;
		j =  HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY) - pInfo->selStart.y  ;
		if(i || j)
		{
			Matrix_MoveShip(pInfo,pInfo->sel,i,j);
			
			pInfo->selStart.x = LOWORD(lParam) / ((rect.right - rect.left) / pInfo->matX );
			pInfo->selStart.y =  HIWORD(lParam) / ((rect.bottom - rect.top) / pInfo->matY);
			InvalidateRect(hWnd,NULL,TRUE);

		}

	}
	break;
case WM_GRID_EX:
	switch(wParam)
	{
	case GRID_INSERT_SHIP:
		pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
		pgis = (GRID_INSERT_SHIP_STRUCT*) lParam;
		Matrix_InsertShip(pInfo,pgis->shipId,pgis->p.x,pgis->p.y);
		InvalidateRect(hWnd,NULL,TRUE);
		break;
	case GRID_RESIZEMATRIX:
		pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
		//todo free matrix if exist
		pInfo->matX = HIWORD(lParam);
		pInfo->matY	= LOWORD(lParam);
		pInfo->matrixEx = malloc(sizeof(void*) * pInfo->matY);
		for(i =0; i< pInfo->matY; i++)
			pInfo->matrixEx[i] = calloc(pInfo->matX,sizeof(SHIP));
		RedrawWindow(hWnd,NULL,NULL,RDW_UPDATENOW);
		break;
	case GRID_GETMATRIX:
		pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
		return (HRESULT)pInfo->matrixEx;
	default:
		return 0;
	}
	break;
case WM_CREATE:
	pInfo = (GRID_INFO*) malloc(sizeof(GRID_INFO));
	if(!pInfo)
		return -1;
	ZeroMemory(pInfo,sizeof(GRID_INFO));
	pInfo->flags = (int)((CREATESTRUCT*)lParam)->lpCreateParams;
	SetWindowLongPtr(hWnd,GWLP_USERDATA,(LONG)pInfo);
	Grid_ResizeMatrix(hWnd,10,10); //defualt
	hPopup = CreatePopupMenu();
	hPopupRotate = CreatePopupMenu();
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_AIRCARRY,_T("Air carry"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_BATTLE,_T("Battle ship"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_DESTROYER,_T("Destroyer ship"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_PATROL,_T("Patrol ship"));
	AppendMenu(hPopup,MF_STRING,SHIP_TYPE_VENIZELOS,_T("Spasmeno ypovrixio"));

	AppendMenu(hPopupRotate,MF_STRING,1,_T("Rotate"));

	Gfx_Create(&pInfo->gfx);
	
	break;
case WM_PAINT:
	//draw grid
	hdc = BeginPaint(hWnd,&ps);
	pInfo = (GRID_INFO*) GetWindowLongPtr(hWnd,GWLP_USERDATA);
	GetClientRect(hWnd,&rect);

	hdcMem = CreateCompatibleDC(hdc);
	hOld = SelectObject(hdcMem,pInfo->gfx.hBitmap);
	for(i=0;i < pInfo->matX; i++)
	{
		for(j=0; j < pInfo->matY; j++)
		{
			Gfx_GetSpirit(&pInfo->gfx,&pInfo->matrixEx[i][j],&spirit);
			BitBlt(hdc,
				j * ((rect.right - rect.left) / pInfo->matX),
				i * ((rect.bottom - rect.top) / pInfo->matY),
				spirit.cx,
				spirit.cy,
				hdcMem,
				spirit.x,
				spirit.y,
				SRCCOPY);
		}
	}
	SelectObject(hdc,hOld);
	DeleteDC(hdcMem);
	EndPaint(hWnd,&ps);
	break;
case WM_ERASEBKGND:
	return TRUE;
case WM_DESTROY:
	pInfo = (GRID_INFO*)GetWindowLongPtr(hWnd,GWLP_USERDATA);
	//todo free
	DestroyMenu(hPopup);
	break;
default:
	return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}

BOOL RegisterClasses(HINSTANCE hInst)
{
WNDCLASSEX			wcGrid,wcMain;

ZeroMemory(&wcGrid,sizeof(wcGrid));
ZeroMemory(&wcMain,sizeof(wcMain));

wcGrid.cbSize			= sizeof(wcGrid);
wcGrid.style			= CS_SAVEBITS;
wcGrid.hCursor			= LoadCursor(NULL,IDC_HAND);
wcGrid.lpfnWndProc		= _GridProc;
wcGrid.lpszClassName	= szClsGrid;
wcGrid.hInstance		= hInst;

wcMain.cbSize			= sizeof(wcMain);
wcMain.hCursor			= LoadCursor(NULL,IDC_ARROW);
wcMain.hInstance		= hInst;
wcMain.lpfnWndProc		= WndProc;
wcMain.lpszClassName	= szClsMain;
wcMain.style			= (CS_HREDRAW | CS_VREDRAW);
wcMain.hbrBackground	= (HBRUSH) (COLOR_WINDOW);


return (
	RegisterClassEx(&wcMain) 
	&& RegisterClassEx(&wcGrid)
	);
}




int Gfx_Create(GFX* gfx)
{
gfx->hBitmap = LoadImage(NULL,_T("gfx.bmp"),IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(!gfx->hBitmap)
	return 1;
gfx->nShips = 5;
gfx->pShips = malloc(sizeof(SHIP_RESOURCE) * gfx->nShips);

ZeroMemory(gfx->pShips,sizeof(SHIP_RESOURCE) * gfx->nShips);

gfx->sz.x = 48;
gfx->sz.y = 48;

gfx->pShips[0].id			= SHIP_TYPE_AIRCARRY;
gfx->pShips[0].parts		= 5;
gfx->pShips[0].pLocH.x		= gfx->sz.x * 0;
gfx->pShips[0].pLocH.y		= gfx->sz.y * 1;
gfx->pShips[0].pLocV.x		= gfx->sz.x * 0;
gfx->pShips[0].pLocV.y		= gfx->sz.y * 8;


return 0;
}
int Gfx_GetSpirit(GFX* gfx,SHIP* ship, SPIRIT* pSpirit)
{
int i;
if(ship->id == SHIP_TYPE_WATER)
{
	if(!ship->flags)
	{
		pSpirit->x = 0;
		pSpirit->y = 0;
		pSpirit->cx = 48;
		pSpirit->cy = 48;
	}
	return 0;
}
for(i = 0; i < gfx->nShips; i++)
{
	if(gfx->pShips[i].id == ship->id)
	{
		if(ship->flags & GFX_FLAG_H 
			&& ship->flags & GFX_FLAG_HEALTHY)
		{
			pSpirit->x = gfx->pShips[i].pLocH.x + ship->part * gfx->sz.x;
			pSpirit->y = gfx->pShips[i].pLocH.y;
			pSpirit->cx = gfx->sz.x;
			pSpirit->cy = gfx->sz.y;
		}
		return 0;
	}
}
return 1;
}

int Matrix_InsertShip(GRID_INFO* gi,int shipId,int x,int y)
{
int parts;
int i,j;

//check if exist and erase
for(i = 0; i < gi->matX; i++)
	for(j = 0; j <gi->matY; j++)
		if(gi->matrixEx[j][i].id == shipId)
		{
			gi->matrixEx[j][i].id = SHIP_TYPE_WATER;
			gi->matrixEx[j][i].flags =0;
		}
//get parts count;
for(i = 0; i < gi->gfx.nShips ; i++)
	if(gi->gfx.pShips[i].id == shipId)
		parts = gi->gfx.pShips[i].parts;

if(gi->matX < x + parts)
	return Matrix_InsertShip(gi,shipId,x-1,y);

for(i = 0; i < parts; i++)
{
	gi->matrixEx[y][x+i].part = i;
	gi->matrixEx[y][x+i].id	= shipId;
	gi->matrixEx[y][x+i].flags = GFX_FLAG_H | GFX_FLAG_HEALTHY;
}


return 0;
}
int Matrix_MoveShip(GRID_INFO* gi,int shipId,/*Offsets*/int x,int y)
{
int		i,j;
int		parts;
POINT	shipStart;
SHIP	shipClone[10];
//find ship len
for(i = 0; i < gi->gfx.nShips; i++)
{
	if(gi->gfx.pShips[i].id == shipId)
	{
		parts = gi->gfx.pShips[i].parts;
		break;
	}
}
//find ship head
for(i = 0; i < gi->matX; i++)
{
	for(j = 0; j < gi->matY; j++)
	{
		if(gi->matrixEx[j][i].id == shipId 
			&& gi->matrixEx[j][i].part == 0)
		{
			shipStart.x = i;
			shipStart.y = j;
			break;
		}
	}
}
//chech for overflow
if(gi->matrixEx[shipStart.y][shipStart.x].flags & GFX_FLAG_H)
{
	if(
		shipStart.x + x + parts > gi->matX
		|| shipStart.x + x < 0
		|| shipStart.y + y < 0
		|| shipStart.x + y > gi->matY
		)
		return 0;
}
else
{
	if(
		shipStart.x + x > gi->matX
		|| shipStart.x + x < 0
		|| shipStart.y + y < 0
		|| shipStart.x + y + parts > gi->matY
		)
		return 0;
}

//clone ship & earse
if(gi->matrixEx[shipStart.y][shipStart.x].flags & GFX_FLAG_H)
{
	for(i = 0 ; i < parts; i++)
	{
		memcpy(&shipClone[i],&gi->matrixEx[shipStart.y][shipStart.x + i],sizeof(SHIP));
		gi->matrixEx[shipStart.y][shipStart.x + i].id = SHIP_TYPE_WATER;
		gi->matrixEx[shipStart.y][shipStart.x + i].flags = 0;
	}
}
else
{
	for(i = 0 ; i < parts; i++)
	{
		memcpy(&shipClone[i],&gi->matrixEx[shipStart.y + i][shipStart.x],sizeof(SHIP));
		gi->matrixEx[shipStart.y][shipStart.x + i].id = SHIP_TYPE_WATER;
		gi->matrixEx[shipStart.y][shipStart.x + i].flags = 0;
	}
}

//place ship
if(shipClone[0].flags & GFX_FLAG_H)
{
	for(i = 0; i < parts; i++)
	{
		memcpy(&gi->matrixEx[shipStart.y + y][shipStart.x + x + i],&shipClone[i],sizeof(SHIP));
	}
}
else
{
	for(i = 0; i < parts; i++)
	{
		memcpy(&gi->matrixEx[shipStart.y + y + i][shipStart.x + x ],&shipClone[i],sizeof(SHIP));
	}
}

return 0;
}
int Matrix_RotateShip(GRID_INFO* gi,int shipId)
{

}




void Grid_Clicked(HWND hGrid,int x,int y)
{

}

void Grid_OnInsertShip(HWND hGrid,int x,int y,int shipId)
{

}

 

 

:) :) :)

 

ΥΓ.Εγώ ασχολούμαι λιγάκι με εκείνη την βιβλιοθήκη για c-strings που λέγαμε παλαιότερα ;)

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

  • 2 εβδομάδες αργότερα...

Τα σέεεεεβη μου στον δασκαλο της C migf1 !!!

 

Ειναι ενας απο τους καλυτερους προγραμματιστες που εχω γνωρισει εστω και ηλεκτρονικα.

 

p.s Το φτιαξατε τελικα το παιχνιδακι??? :D

Συνδέστε για να σχολιάσετε
Κοινοποίηση σε άλλες σελίδες

Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε

Πρέπει να είστε μέλος για να αφήσετε σχόλιο

Δημιουργία λογαριασμού

Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!

Δημιουργία νέου λογαριασμού

Σύνδεση

Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.

Συνδεθείτε τώρα

  • Δημιουργία νέου...