#include <windows.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
typedef void (WINAPI *pfcdtInit)(int *, int *);
typedef void (WINAPI *pfcdtDraw)(HDC, int x, int y, int card, int type, DWORD color);
typedef void (WINAPI *pfcdtDrawEx)(HDC, int x, int y, int dx, int dy, int card, int type, DWORD color);
typedef void (WINAPI *pfcdtAnimate)(HDC hdc, int cardback, int x, int y, int state);
typedef void (WINAPI *pfcdtTerm) (void);
pfcdtInit cdtInit;
pfcdtDraw cdtDraw;
pfcdtDrawEx cdtDrawEx;
pfcdtTerm cdtTerm;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HDC hdc;
HWND hwnd;
HWND button;
int const COMPUTER = 0;
int const PLAYER = 1;
int computercard = 0;
int playercard = 0;
int numbercomputercards = 0;
int numberplayercards = 0;
int globalcomputercards = 26;
int globalplayercards = 26;
int cardsboard = 1;
int gamestate = 0;
int arraycard[52];
int arrayplayercards[52];
int arraycomputercards[52];
int playercardvisibility[26] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int computercardvisibility[26] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int getsuit(int cardnumber);
int getrank(int cardnumber);
int generate_card();
void sorteqsuitcards();
void sortcards();
void RenderCards();
int ProcessCards(int iter);
int Nextcard();
int uniquecomputercards(int ncards);
int uniqueplayercards();
int getsuit(int cardnumber)
{
if (cardnumber > 51 || cardnumber < 0 ) return -1;
for (int i = 0; i < 14; i++)
{
if ((i*4) == cardnumber ) return 0; //CLUBS
if ((i*4 + 1) == cardnumber ) return 1; //DIAMOND
if ((i*4 + 2) == cardnumber ) return 2; //HEARTS
if ((i*4 + 3) == cardnumber ) return 3; //SPADES
}
}
int getrank(int cardnumber)
{
if (cardnumber > 51 || cardnumber < 0 ) return -1;
for (int iteration = 4;iteration <=51; iteration+=4 )
{
if (cardnumber >= iteration && cardnumber <= iteration + 3)
{
return iteration/4;
}
}
return 13;
}
void sorteqsuitcards()
{
int copyarraycard[52];
for (int i = 0; i<=51;i++)
{
copyarraycard[i] = arraycard[i];
}
int nc = 0;
for (int suit = 0; suit<=3;suit++)
{
for (int i = 0; i<=25;i++)
{
if (getsuit(arraycard[i]) == suit)
{
copyarraycard[nc] = arraycard[i];
nc++;
}
}
}
for (int suit = 0; suit<=3;suit++)
{
for (int i = 26; i<=51;i++)
{
if (getsuit(arraycard[i]) == suit)
{
copyarraycard[nc] = arraycard[i];
nc++;
}
}
}
for (int i = 0; i<=51;i++)
{
arraycard[i] = copyarraycard[i];
}
}
void sortcards()
{
int z, i,swapvar;
for (i = 25; i > 0; i--)
{
for (z = 0; z < i; z++)
{
if (arraycard[z] > arraycard[z + 1])
{
swapvar = arraycard[z];
arraycard[z] = arraycard[z + 1];
arraycard[z + 1] = swapvar;
}
}
}
for (i = 51; i > 0; i--)
{
for (z = 26; z < i; z++)
{
if (arraycard[z] > arraycard[z + 1])
{
swapvar = arraycard[z];
arraycard[z] = arraycard[z + 1];
arraycard[z + 1] = swapvar;
}
}
}
sorteqsuitcards();
}
int generate_card()
{
srand(time(0));
int i,a;
for(i=0;i<=51;i++){
arraycard[i]=rand()%52;
for(a=0;a<i;a++){
if(arraycard[a]==arraycard[i]) {
i--;
break;
}
}
}
}
void RenderCards()
{
HBITMAP memBM = CreateCompatibleBitmap(hdc,900,500);
HDC memDC = CreateCompatibleDC(hdc);
SelectObject(memDC,memBM);
int iteration;
static int n = 26;
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = 900;
rc.bottom = 900;
FillRect(memDC,&rc, (HBRUSH)(COLOR_BACKGROUND));
for (iteration = 0; iteration <= 25; iteration++)
{
if (computercardvisibility[iteration] == 1)
{
cdtDrawEx(memDC,iteration*30,0,90,90,arraycard[iteration],0,0);
}
}
for (iteration = 0; iteration < 26; iteration++)
{
if (playercardvisibility[iteration] == 1)
{
cdtDrawEx(memDC,iteration*30,400,90,90,arraycard[iteration + 26],0,0);
}
}
cdtDrawEx(memDC,27*30,200,90,90,arraycard[51],0,0);
for (iteration = 0; iteration < numberplayercards; iteration++)
{
cdtDrawEx(memDC,iteration*50,250,90,90,arrayplayercards[iteration],0,0);
}
for (iteration = 0; iteration < numbercomputercards; iteration++)
{
cdtDrawEx(memDC,iteration*50,150,90,90,arraycomputercards[iteration],0,0);
}
BitBlt(hdc,0,0,900,900, memDC, 0, 0, SRCCOPY);
}
void Clear()
{
HBITMAP memBM = CreateCompatibleBitmap(hdc,900,500);
HDC memDC = CreateCompatibleDC(hdc);
SelectObject(memDC,memBM);
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = 900;
rc.bottom = 900;
FillRect(memDC,&rc, (HBRUSH)(COLOR_BACKGROUND));
numbercomputercards = 0;
numberplayercards = 0;
RenderCards();
}
int uniquecomputercards(int ncards)
{
for (int iteration = 0; iteration < numbercomputercards; iteration++)
{
if (arraycomputercards[iteration] == arraycard[ncards]) return false;
}
return true;
}
int uniqueplayercards()
{
for (int iteration = 0; iteration < numberplayercards; iteration++)
{
if (arrayplayercards[iteration] == arraycard[playercard]) return false;
}
return true;
}
int ProcessCards(int iter)
{
Nextcard();
int static indexarray = 0;
if (gamestate == COMPUTER)
{
if (getsuit(arraycard[playercard]) == getsuit(arraycomputercards[indexarray]) && getrank(arraycard[playercard]) > getrank(arraycomputercards[indexarray]) )
{
indexarray++;
if (uniqueplayercards())
{
arrayplayercards[numberplayercards] = arraycard[playercard];
numberplayercards++;
playercardvisibility[iter] = 0;
}
}
}
}
int Nextcard()
{
int static n = 0;
for (int indexcard = 0; indexcard < globalcomputercards; indexcard++)
{
if (getrank(arraycard[indexcard]) == getrank(arraycard[computercard]))
{
if (uniquecomputercards(indexcard))
{
arraycomputercards[numbercomputercards] = arraycard[indexcard];
numbercomputercards++;
n++;
}
}
}
for (int indexcard = 0; indexcard < globalcomputercards; indexcard++)
{
if (getrank(arraycard[indexcard]) == getrank(arraycard[playercard]))
{
if (uniquecomputercards(indexcard))
{
arraycomputercards[numbercomputercards] = arraycard[indexcard];
numbercomputercards++;
n++;
}
}
}
return n;
}
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
"Windows App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
900,
900,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
HMODULE hCardDll;
hCardDll = LoadLibrary("cards.dll");
cdtInit = (pfcdtInit) GetProcAddress(hCardDll, "cdtInit");
cdtDraw = (pfcdtDraw) GetProcAddress(hCardDll, "cdtDraw");
cdtDrawEx = (pfcdtDrawEx) GetProcAddress(hCardDll, "cdtDrawExt");
cdtTerm = (pfcdtTerm) GetProcAddress(hCardDll, "cdtTerm");
int w;
int h;
cdtInit(&w,&h);
hdc = GetDC(hwnd);
generate_card();
sortcards();
computercard = rand()%25;
arraycomputercards[numbercomputercards] = arraycard[computercard];
numbercomputercards++;
button=CreateWindow("button","Бито",WS_VISIBLE|WS_CHILD,0,500,35,35,hwnd,NULL,NULL,NULL);
RenderCards();
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
{
POINT p;
RECT r1;
RECT r2;
GetCursorPos(&p);
GetWindowRect(hwnd,&r1);
int mousex = p.x;
int mousey = p.y;
for (int iter = 0;iter <= 25;iter++)
{
if (abs(r1.left - mousex) > iter*30 && abs(r1.left - mousex) < iter*30 + 30 && abs(r1.top - mousey) > 400 )
{
playercard = iter + 26;
ProcessCards(iter);
}
}
RenderCards();
}
break;
case WM_COMMAND:
if ((HWND)lParam == button)
{
Clear();
computercard = rand()%25;
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
|