Code:
#include "TicTacToe.h"
#include "resource.h"
#define X (1)
#define O (2)
//Global Windows
HWND BUTTON_1, BUTTON_2, BUTTON_3, BUTTON_4,
BUTTON_5, BUTTON_6, BUTTON_7, BUTTON_8, BUTTON_9; //Main buttons
HWND BUTTON_RESTART;
HWND STATIC_TURN;
HWND STATIC_TITLE;
HWND STATIC_XSCORE, STATIC_OSCORE, STATIC_CATSCORE;
HWND STATIC_XVALUE, STATIC_OVALUE, STATIC_CATVALUE;
//Fonts
HBRUSH _ctl_ctrl = NULL; //For global text
HDC fHdc = NULL;
long lfHeight = 0;
HFONT hf = NULL;
HFONT g_hfFont = NULL;
HDC hdc2 = NULL;
long lfHeight2 = NULL;
HFONT hf2 = NULL;
HFONT g_hfFont2 = NULL;
HWND _ctl;
HDC _hdc;
//Game control variables
float xScore = 0, oScore = 0, catScore = 0;
bool over = false, UseAI = true, draw = false;
short turnColor = 1;
int iColor1 = 0, iColor2 = 0, iColor3 = 0;
int AI_Level = 3;
short turn = 0, counter = 0;
int bStatus[9] = {0};
//Icons
HICON xIcon = NULL;
HICON oIcon = NULL;
HICON catIcon = NULL;
enum{
IDB_1, IDB_2, IDB_3, IDB_4, IDB_5, IDB_6, IDB_7, IDB_8, IDB_9,
IDS_TURN,
IDB_RESTART,
IDS_TITLE,
IDS_XSCORE, IDS_OSCORE, IDS_CATSCORE,
IDS_XVALUE, IDS_OVALUE, IDS_CATVALUE,
ID_DUMMY
};
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
void InitGUI(const HWND hWnd, CREATESTRUCT *cs);
LRESULT SetBox(int, HWND);
void SetTurn();
void CheckWinner();
void ShowWinner(LPCSTR);
void UpdateScores(LPCSTR);
void RestartGame();
void AI();
/* Make the class name into a global variable */
char szClassName[] = "TicTacToeCls";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hWnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_HREDRAW | CS_VREDRAW; /* Redraw */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON));
wincl.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 0, 0, 0);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); /* Menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hWnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Tic-Tac-Toe", /* Title Text */
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
546, /* The programs width */
625, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hWnd, nCmdShow);
UpdateWindow(hWnd);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CTLCOLOREDIT:
SendMessage(STATIC_TITLE, WM_SETFONT, reinterpret_cast<WPARAM>(g_hfFont), 0);
hdc2 = GetDC(NULL);
lfHeight2 = -MulDiv(14, GetDeviceCaps(hdc2, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc2);
hf2 = CreateFont(lfHeight2, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Poor Richard");
if(hf2)
{
DeleteObject(g_hfFont2);
g_hfFont2 = hf2;
}
//SendMessage(STATIC_TITLE, WM_SETFONT, reinterpret_cast<WPARAM>(g_hfFont2), 0);
break;
case WM_CTLCOLORSTATIC:
_ctl = (HWND)lParam;
_hdc = (HDC)wParam;
if(_ctl == GetDlgItem(hWnd, IDS_TITLE))
{
if(UseAI == true)
{
SetTextColor(_hdc, RGB(34, 139, 34));
}
else
{
SetTextColor(_hdc, RGB(255, 165, 0));
}
SetBkColor(_hdc, RGB(0, 0, 0));
return (LONG)_ctl_ctrl;
}
if(_ctl == GetDlgItem(hWnd, IDS_TURN))
{
if(turnColor == 1) //x turn
{
SetTextColor(_hdc, RGB(15, 30, 230));
}
else if(turnColor == 2) //o turn
{
SetTextColor(_hdc, RGB(230, 30, 15));
}
else if(turnColor == 3) //draw/cat wins
{
SetTextColor(_hdc, RGB(255, 255, 0));
}
SetBkColor(_hdc, COLOR_BACKGROUND);
return (LONG)_ctl_ctrl;
}
//Set X score colors
if(_ctl == GetDlgItem(hWnd, IDS_XSCORE))
{
SetTextColor(_hdc, RGB(15, 30, 230));
SetBkMode(_hdc, TRANSPARENT);
return (LONG)_ctl_ctrl;
}
if(_ctl == GetDlgItem(hWnd, IDS_XVALUE))
{
SetTextColor(_hdc, RGB(0, 204, 0));
SetBkColor(_hdc, COLOR_BACKGROUND);
return (LONG)_ctl_ctrl;
}
//Set Cat score colors
if(_ctl == GetDlgItem(hWnd, IDS_CATSCORE))
{
SetTextColor(_hdc, RGB(102, 68, 0));
SetBkMode(_hdc, TRANSPARENT);
return (LONG)_ctl_ctrl;
}
if(_ctl == GetDlgItem(hWnd, IDS_CATVALUE))
{
SetTextColor(_hdc, RGB(0, 204, 0));
SetBkColor(_hdc, COLOR_BACKGROUND);
return (LONG)_ctl_ctrl;
}
//Set O score colors
if(_ctl == GetDlgItem(hWnd, IDS_OSCORE))
{
SetTextColor(_hdc, RGB(230, 30, 15));
SetBkMode(_hdc, TRANSPARENT);
return (LONG)_ctl_ctrl;
}
if(_ctl == GetDlgItem(hWnd, IDS_OVALUE))
{
SetTextColor(_hdc, RGB(0, 204, 0));
SetBkColor(_hdc, COLOR_BACKGROUND);
return (LONG)_ctl_ctrl;
}
break;
case WM_CREATE:
fHdc = GetDC(NULL);
lfHeight = -MulDiv(21, GetDeviceCaps(fHdc, LOGPIXELSY), 72);
ReleaseDC(NULL, fHdc);
hf = CreateFont(lfHeight, 0, 0, 0, FW_BOLD, FALSE, 0, 0, 0, 0, 0, 0, 0, "Times New Roman");
if(hf)
{
DeleteObject(g_hfFont);
g_hfFont = hf;
}
_ctl_ctrl = CreateSolidBrush(RGB(0, 0, 0));
xIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_X));
oIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_O));
catIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_CAT));
InitGUI(hWnd, reinterpret_cast<CREATESTRUCT*>(lParam));
for(int i=0;i<9;i++)
{
bStatus[i] = 0;
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_1: SetBox(0, BUTTON_1); break;
case IDB_2: SetBox(1, BUTTON_2); break;
case IDB_3: SetBox(2, BUTTON_3); break;
case IDB_4: SetBox(3, BUTTON_4); break;
case IDB_5: SetBox(4, BUTTON_5); break;
case IDB_6: SetBox(5, BUTTON_6); break;
case IDB_7: SetBox(6, BUTTON_7); break;
case IDB_8: SetBox(7, BUTTON_8); break;
case IDB_9: SetBox(8, BUTTON_9); break;
case IDB_RESTART:
RestartGame();
ShowWindow(BUTTON_RESTART, false);
break;
//Menu items
case IDR_FILE_QUIT:
PostQuitMessage(0);
break;
case IDR_OPTIONS_1PLAY:
UseAI = true;
SetWindowText(STATIC_TITLE, "One Player Game");
break;
case IDR_OPTIONS_2PLAY:
UseAI = false;
SetWindowText(STATIC_TITLE, "Two Player Game");
break;
case IDR_AI_CHILD:
AI_Level = 1;
MessageBox(NULL,"AI now has the brain of a small child.", "AI", MB_OK | MB_ICONINFORMATION);
break;
case IDR_AI_DECENT:
AI_Level = 2;
MessageBox(NULL,"AI now has the brain of an average human being.", "AI", MB_OK | MB_ICONINFORMATION);
break;
case IDR_AI_EXPERT:
AI_Level = 3;
MessageBox(NULL,"AI now has the brain of a chessmaster.", "AI", MB_OK | MB_ICONINFORMATION);
break;
case IDR_HELP_ABOUT:
DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_ABOUT), hWnd, AboutDlgProc);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hWnd, message, wParam, lParam);
}
return 0;
}
BOOL CALLBACK AboutDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hWnd, IDOK);
break;
case IDCANCEL:
EndDialog(hWnd, IDCANCEL);
break;
}
break;
case WM_CREATE:
break;
case WM_DESTROY:
break;
default:
return FALSE;
break;
}
return TRUE;
}
LRESULT SetBox(int b, HWND hWnd)
{
if(bStatus[b] == 0)
{
ShowWindow(BUTTON_RESTART, false);
counter++;
if(turn == 0) // if X's turn
{
bStatus[b] = X;
turn = 1;
SetTurn();
CheckWinner();
if(UseAI == true && over == false)
AI();
if(draw == false)
return SendMessage(hWnd, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(xIcon));;
}
else if(turn == 1) // if O's turn
{
bStatus[b] = O;
turn = 0;
SetTurn();
CheckWinner();
if(UseAI == true && over == false)
AI();
if(draw == false)
return SendMessage(hWnd, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(oIcon));
}
}
}
void SetTurn()
{
if(turn == 0)
{
SetWindowText(STATIC_TURN, "X's Turn ");
turnColor = 1;
}
else if(turn == 1)
{
SetWindowText(STATIC_TURN, "O's Turn ");
turnColor = 2;
}
}
void CheckWinner()
{
/* Check X Victory */
//Check Rows
if(bStatus[0] == X && bStatus[1] == X && bStatus[2] == X)
{
ShowWinner("x");
}
if(bStatus[3] == X && bStatus[4] == X && bStatus[5] == X)
{
ShowWinner("x");
}
if(bStatus[6] == X && bStatus[7] == X && bStatus[8] == X)
{
ShowWinner("x");
}
//Check Collums
if(bStatus[0] == X && bStatus[3] == X && bStatus[6] == X)
{
ShowWinner("x");
}
if(bStatus[1] == X && bStatus[4] == X && bStatus[7] == X)
{
ShowWinner("x");
}
if(bStatus[2] == X && bStatus[5] == X && bStatus[8] == X)
{
ShowWinner("x");
}
//Check Diagonals
if(bStatus[0] == X && bStatus[4] == X && bStatus[8] == X)
{
ShowWinner("x");
}
if(bStatus[2] == X && bStatus[4] == X && bStatus[6] == X)
{
ShowWinner("x");
}
//-------------------------------------------------------
/* Check O Victory */
//Check Rows
if(bStatus[0] == O && bStatus[1] == O && bStatus[2] == O)
{
ShowWinner("o");
}
if(bStatus[3] == O && bStatus[4] == O && bStatus[5] == O)
{
ShowWinner("o");
}
if(bStatus[6] == O && bStatus[7] == O && bStatus[8] == O)
{
ShowWinner("o");
}
//Check Collums
if(bStatus[0] == O && bStatus[3] == O && bStatus[6] == O)
{
ShowWinner("o");
}
if(bStatus[1] == O && bStatus[4] == O && bStatus[7] == O)
{
ShowWinner("o");
}
if(bStatus[2] == O && bStatus[5] == O && bStatus[8] == O)
{
ShowWinner("o");
}
//Check Diagonals
if(bStatus[0] == O && bStatus[4] == O && bStatus[8] == O)
{
ShowWinner("o");
}
if(bStatus[2] == O && bStatus[4] == O && bStatus[6] == O)
{
ShowWinner("o");
}
else if(counter >= 9 && over == false)
{
ShowWinner("draw");
}
/*else
{
int ctr = 0;
for(int i=0;i<9;i++)
{
if(bStatus[i] != 0)
{
ctr++;
}
}
if(ctr >= 9 && over == false)
{
ShowWinner("draw");
}
}*/
}
void ShowWinner(LPCSTR winner)
{
if(winner == "x")
{
SetWindowText(STATIC_TURN, "X Wins! ");
}
else if(winner == "o")
{
SetWindowText(STATIC_TURN, "O Wins! ");
}
else if(winner == "draw")
{
turnColor = 3;
SetWindowText(STATIC_TURN, "Cat Wins!");
SendMessage(BUTTON_1, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_2, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_3, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_4, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_5, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_6, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_7, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_8, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
SendMessage(BUTTON_9, BM_SETIMAGE, IMAGE_ICON, reinterpret_cast<LPARAM>(catIcon));
draw = true;
}
for(int i=0;i<9;i++)
{
bStatus[i] = 3; //Lock every tile
}
UpdateScores(winner);
ShowWindow(BUTTON_RESTART, true);
over = true;
}
void UpdateScores(LPCSTR winner)
{
char *buf = new char;
if(winner == "x")
{
xScore++;
sprintf(buf, "%g", xScore);
SetWindowText(STATIC_XVALUE, buf);
}
if(winner == "o")
{
oScore++;
sprintf(buf, "%g", oScore);
SetWindowText(STATIC_OVALUE, buf);
}
if(winner == "draw")
{
catScore++;
sprintf(buf, "%g", catScore);
SetWindowText(STATIC_CATVALUE, buf);
if(catScore > 15)
{
MessageBox(NULL, "The cat has got game.", "Message from Cat", MB_OK | MB_ICONINFORMATION);
}
}
delete buf;
buf = 0;
}
void RestartGame()
{
for(int i=0;i<9;i++)
{
bStatus[i] = 0;
}
SendMessage(BUTTON_1, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_2, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_3, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_4, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_5, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_6, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_7, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_8, BM_SETIMAGE, IMAGE_ICON, 0);
SendMessage(BUTTON_9, BM_SETIMAGE, IMAGE_ICON, 0);
turn = 0;
SetTurn();
over = false;
draw = false;
counter = 0;
}
void AI()
{
if(turn == 1) // Make sure this is O
{
if(AI_Level == 3)
Sleep(rand()%90+10);
else if(AI_Level == 2)
Sleep(rand()%140+40);
else if(AI_Level == 1)
Sleep(rand()%120+5);
//Check for possible victory moves
//Rows
if(bStatus[0] == O && bStatus[1] == O && bStatus[2] == 0)
{
SetBox(2, BUTTON_3);
}
else if(bStatus[3] == O && bStatus[4] == O && bStatus[5] == 0)
{
SetBox(5, BUTTON_6);
}
else if(bStatus[6] == O && bStatus[7] == O && bStatus[8] == 0)
{
SetBox(8, BUTTON_9);
}
else if(bStatus[0] == 0 && bStatus[1] == O && bStatus[2] == O)
{
SetBox(0, BUTTON_1);
}
else if(bStatus[3] == 0 && bStatus[4] == O && bStatus[5] == O)
{
SetBox(3, BUTTON_4);
}
else if(bStatus[6] == 0 && bStatus[7] == O && bStatus[8] == O)
{
SetBox(6, BUTTON_7);
}
else if(bStatus[0] == O && bStatus[1] == 0 && bStatus[2] == O)
{
SetBox(1, BUTTON_2);
}
else if(bStatus[3] == O && bStatus[4] == 0 && bStatus[5] == O)
{
SetBox(4, BUTTON_5);
}
else if(bStatus[6] == O && bStatus[7] == 0 && bStatus[8] == O)
{
SetBox(7, BUTTON_8);
}
//Collums
else if(bStatus[0] == O && bStatus[3] == O && bStatus[6] == 0)
{
SetBox(6, BUTTON_7);
}
else if(bStatus[1] == O && bStatus[4] == O && bStatus[7] == 0)
{
SetBox(7, BUTTON_8);
}
else if(bStatus[2] == O && bStatus[5] == O && bStatus[8] == 0)
{
SetBox(8, BUTTON_9);
}
else if(bStatus[0] == 0 && bStatus[3] == O && bStatus[6] == O)
{
SetBox(0, BUTTON_1);
}
else if(bStatus[1] == 0 && bStatus[4] == O && bStatus[7] == O)
{
SetBox(1, BUTTON_2);
}
else if(bStatus[2] == 0 && bStatus[5] == O && bStatus[8] == O)
{
SetBox(2, BUTTON_3);
}
else if(bStatus[0] == O && bStatus[3] == 0 && bStatus[6] == O)
{
SetBox(3, BUTTON_4);
}
else if(bStatus[1] == O && bStatus[4] == 0 && bStatus[7] == O)
{
SetBox(4, BUTTON_5);
}
else if(bStatus[2] == O && bStatus[5] == 0 && bStatus[8] == O)
{
SetBox(5, BUTTON_6);
}
//Diagonals
else if(bStatus[0] == O && bStatus[4] == O && bStatus[8] == 0)
{
SetBox(8, BUTTON_9);
}
else if(bStatus[0] == 0 && bStatus[4] == O && bStatus[8] == O)
{
SetBox(0, BUTTON_1);
}
else if(bStatus[0] == O && bStatus[4] == 0 && bStatus[8] == O)
{
SetBox(4, BUTTON_5);
}
else if(bStatus[2] == O && bStatus[4] == O && bStatus[6] == 0)
{
SetBox(6, BUTTON_7);
}
else if(bStatus[2] == 0 && bStatus[4] == O && bStatus[6] == O)
{
SetBox(2, BUTTON_3);
}
else if(bStatus[2] == O && bStatus[4] == 0 && bStatus[6] == O)
{
SetBox(4, BUTTON_5);
}
//------------------------------------------------------------
//Check for possible enemy victory
//Rows
else if(bStatus[0] == X && bStatus[1] == X && bStatus[2] == 0)
{
SetBox(2, BUTTON_3);
}
else if(bStatus[3] == X && bStatus[4] == X && bStatus[5] == 0)
{
SetBox(5, BUTTON_6);
}
else if(bStatus[6] == X && bStatus[7] == X && bStatus[8] == 0)
{
SetBox(8, BUTTON_9);
}
else if(bStatus[0] == 0 && bStatus[1] == X && bStatus[2] == X)
{
SetBox(0, BUTTON_1);
}
else if(bStatus[3] == 0 && bStatus[4] == X && bStatus[5] == X)
{
SetBox(3, BUTTON_4);
}
else if(bStatus[6] == 0 && bStatus[7] == X && bStatus[8] == X)
{
SetBox(6, BUTTON_7);
}
else if(bStatus[0] == X && bStatus[1] == 0 && bStatus[2] == X)
{
SetBox(1, BUTTON_2);
}
else if(bStatus[3] == X && bStatus[4] == 0 && bStatus[5] == X)
{
SetBox(4, BUTTON_5);
}
else if(bStatus[6] == X && bStatus[7] == 0 && bStatus[8] == X)
{
SetBox(7, BUTTON_8);
}
//Collums
else if(bStatus[0] == X && bStatus[3] == X && bStatus[6] == 0)
{
SetBox(6, BUTTON_7);
}
else if(bStatus[1] == X && bStatus[4] == X && bStatus[7] == 0)
{
SetBox(7, BUTTON_8);
}
else if(bStatus[2] == X && bStatus[5] == X && bStatus[8] == 0)
{
SetBox(8, BUTTON_9);
}
else if(bStatus[0] == 0 && bStatus[3] == X && bStatus[6] == X)
{
SetBox(0, BUTTON_1);
}
else if(bStatus[1] == 0 && bStatus[4] == X && bStatus[7] == X)
{
SetBox(1, BUTTON_2);
}
else if(bStatus[2] == 0 && bStatus[5] == X && bStatus[8] == X)
{
SetBox(2, BUTTON_3);
}
else if(bStatus[0] == X && bStatus[3] == 0 && bStatus[6] == X)
{
SetBox(3, BUTTON_4);
}
else if(bStatus[1] == X && bStatus[4] == 0 && bStatus[7] == X)
{
SetBox(4, BUTTON_5);
}
else if(bStatus[2] == X && bStatus[5] == 0 && bStatus[8] == X)
{
SetBox(5, BUTTON_6);
}
//Diagonals
else if(bStatus[0] == X && bStatus[4] == X && bStatus[8] == 0 && AI_Level > 1)
{
SetBox(8, BUTTON_9);
}
else if(bStatus[0] == 0 && bStatus[4] == X && bStatus[8] == X && AI_Level > 1)
{
SetBox(0, BUTTON_1);
}
else if(bStatus[0] == X && bStatus[4] == 0 && bStatus[8] == X && AI_Level > 1)
{
SetBox(4, BUTTON_5);
}
else if(bStatus[2] == X && bStatus[4] == X && bStatus[6] == 0 && AI_Level > 1)
{
SetBox(6, BUTTON_7);
}
else if(bStatus[2] == 0 && bStatus[4] == X && bStatus[6] == X && AI_Level > 1)
{
SetBox(2, BUTTON_3);
}
else if(bStatus[2] == X && bStatus[4] == 0 && bStatus[6] == X && AI_Level > 1)
{
SetBox(4, BUTTON_5);
}
//Check Kitty Corners
else if(bStatus[1] == X && bStatus[3] == X && bStatus[0] == 0 && AI_Level == 3)
{
SetBox(0, BUTTON_1);
}
else if(bStatus[1] == X && bStatus[5] == X && bStatus[2] == 0 && AI_Level == 3)
{
SetBox(2, BUTTON_3);
}
else if(bStatus[3] == X && bStatus[7] == X && bStatus[6] == 0 && AI_Level == 3)
{
SetBox(6, BUTTON_7);
}
else if(bStatus[5] == X && bStatus[7] == X && bStatus[8] == 0 && AI_Level == 3)
{
SetBox(8, BUTTON_9);
}
//-------------------------------------------------------
//Regular Moves
//Check for middle square
else if(bStatus[4] == 0 && turn == 1 && AI_Level > 1)
{
SetBox(4, BUTTON_5);
}
//Rows
//Row 1
else if(bStatus[0] == O && bStatus[1] == 0 && bStatus[2] == 0)
{
if(bStatus[4] == 0 && bStatus[7] == 0 && AI_Level > 1)
{
SetBox(1, BUTTON_2);
}
else
{
SetBox(2, BUTTON_3);
}
}
else if(bStatus[0] == 0 && bStatus[1] == 0 && bStatus[2] == O)
{
if(bStatus[3] == 0 && bStatus[6] == 0 && AI_Level > 1)
{
SetBox(0, BUTTON_1);
}
else
{
SetBox(1, BUTTON_2);
}
}
else if(bStatus[0] == 0 && bStatus[1] == O && bStatus[2] == 0)
{
if(bStatus[3] == 0 && bStatus[6] == 0 && AI_Level > 1)
{
SetBox(0, BUTTON_1);
}
else
{
SetBox(2, BUTTON_3);
}
}
//Row 2
else if(bStatus[3] == O && bStatus[4] == 0 && bStatus[5] == 0)
{
if(bStatus[1] == 0 && bStatus[7] == 0 && AI_Level > 1)
{
SetBox(4, BUTTON_5);
}
else
{
SetBox(5, BUTTON_6);
}
}
else if(bStatus[3] == 0 && bStatus[4] == 0 && bStatus[5] == O)
{
if(bStatus[0] == 0 && bStatus[6] == 0 && AI_Level > 1)
{
SetBox(3, BUTTON_4);
}
else
{
SetBox(4, BUTTON_5);
}
}
else if(bStatus[3] == 0 && bStatus[4] == O && bStatus[5] == 0)
{
if(bStatus[0] == 0 && bStatus[6] == 0 && AI_Level > 1)
{
SetBox(3, BUTTON_4);
}
else
{
SetBox(5, BUTTON_6);
}
}
//Row 3
else if(bStatus[6] == O && bStatus[7] == 0 && bStatus[8] == 0)
{
if(bStatus[4] == 0 && bStatus[1] == 0 && AI_Level > 1)
{
SetBox(7, BUTTON_8);
}
else
{
SetBox(8, BUTTON_9);
}
}
else if(bStatus[6] == 0 && bStatus[7] == 0 && bStatus[8] == O)
{
if(bStatus[0] == 0 && bStatus[3] == 0 && AI_Level > 1)
{
SetBox(6, BUTTON_7);
}
else
{
SetBox(7, BUTTON_8);
}
}
else if(bStatus[6] == 0 && bStatus[7] == O && bStatus[8] == 0)
{
if(bStatus[0] == 0 && bStatus[3] == 0 && AI_Level > 1)
{
SetBox(6, BUTTON_7);
}
else
{
SetBox(8, BUTTON_9);
}
}
//Check collums
//Collum 1
else if(bStatus[0] == O && bStatus[3] == 0 && bStatus[6] == 0)
{
if(bStatus[4] == 0 && bStatus[5] == 0 && AI_Level == 3)
{
SetBox(3, BUTTON_4);
}
else
{
SetBox(6, BUTTON_7);
}
}
else if(bStatus[0] == 0 && bStatus[3] == 0 && bStatus[6] == O)
{
if(bStatus[1] == 0 && bStatus[2] == 0 && AI_Level == 3)
{
SetBox(0, BUTTON_1);
}
else
{
SetBox(3, BUTTON_4);
}
}
else if(bStatus[0] == 0 && bStatus[3] == O && bStatus[6] == 0)
{
if(bStatus[1] == 0 && bStatus[2] == 0 && AI_Level == 3)
{
SetBox(0, BUTTON_1);
}
else
{
SetBox(6, BUTTON_7);
}
}
//Collum 2
else if(bStatus[1] == O && bStatus[4] == 0 && bStatus[7] == 0)
{
if(bStatus[3] == 0 && bStatus[5] == 0 && AI_Level == 3)
{
SetBox(4, BUTTON_5);
}
else
{
SetBox(7, BUTTON_8);
}
}
else if(bStatus[1] == 0 && bStatus[4] == 0 && bStatus[7] == O)
{
if(bStatus[0] == 0 && bStatus[2] == 0 && AI_Level == 3)
{
SetBox(1, BUTTON_2);
}
else
{
SetBox(4, BUTTON_5);
}
}
else if(bStatus[1] == 0 && bStatus[4] == O && bStatus[7] == 0)
{
if(bStatus[0] == 0 && bStatus[2] == 0 && AI_Level == 3)
{
SetBox(1, BUTTON_2);
}
else
{
SetBox(7, BUTTON_8);
}
}
//Collum 3
else if(bStatus[2] == O && bStatus[5] == 0 && bStatus[8] == 0)
{
if(bStatus[3] == 0 && bStatus[4] == 0 && AI_Level == 3)
{
SetBox(5, BUTTON_6);
}
else
{
SetBox(8, BUTTON_9);
}
}
else if(bStatus[2] == 0 && bStatus[5] == 0 && bStatus[8] == O)
{
if(bStatus[0] == 0 && bStatus[1] == 0 && AI_Level == 3)
{
SetBox(2, BUTTON_3);
}
else
{
SetBox(5, BUTTON_6);
}
}
else if(bStatus[2] == 0 && bStatus[5] == O && bStatus[8] == 0)
{
if(bStatus[0] == 0 && bStatus[1] == 0 && AI_Level == 3)
{
SetBox(2, BUTTON_3);
}
else
{
SetBox(8, BUTTON_9);
}
}
//Diagonals
//Left to right
else if(bStatus[0] == O && bStatus[4] == 0 && bStatus[8] == 0 && AI_Level > 1)
{
if((bStatus[3] == 0 && bStatus[5] == 0 && AI_Level == 3) || (bStatus[1] == 0 && bStatus[7] == 0 && AI_Level == 3))
{
SetBox(4, BUTTON_5);
}
else
{
SetBox(8, BUTTON_9);
}
}
else if(bStatus[0] == 0 && bStatus[4] == 0 && bStatus[8] == O && AI_Level > 1)
{
if((bStatus[1] == 0 && bStatus[2] == 0 && AI_Level == 3) || (bStatus[3] == 0 && bStatus[6] == 0 && AI_Level == 3))
{
SetBox(0, BUTTON_1);
}
else
{
SetBox(4, BUTTON_5);
}
}
else if(bStatus[0] == 0 && bStatus[4] == O && bStatus[8] == 0 && AI_Level > 1)
{
if((bStatus[1] == 0 && bStatus[2] == 0 && AI_Level == 3) || (bStatus[3] == 0 && bStatus[6] == 0 && AI_Level == 3))
{
SetBox(0, BUTTON_1);
}
else
{
SetBox(8, BUTTON_9);
}
}
//Right to left
else if(bStatus[2] == O && bStatus[4] == 0 && bStatus[6] == 0 && AI_Level > 1)
{
if((bStatus[3] == 0 && bStatus[5] == 0 && AI_Level == 3) || (bStatus[1] == 0 && bStatus[7] == 0 && AI_Level == 3))
{
SetBox(4, BUTTON_5);
}
else
{
SetBox(6, BUTTON_7);
}
}
else if(bStatus[2] == 0 && bStatus[4] == 0 && bStatus[6] == O && AI_Level > 1)
{
if((bStatus[0] == 0 && bStatus[1] == 0 && AI_Level == 3) || (bStatus[5] == 0 && bStatus[8] == 0 && AI_Level == 3))
{
SetBox(2, BUTTON_3);
}
else
{
SetBox(4, BUTTON_5);
}
}
else if(bStatus[2] == 0 && bStatus[4] == O && bStatus[6] == 0 && AI_Level > 1)
{
if((bStatus[0] == 0 && bStatus[1] == 0 && AI_Level == 3) || (bStatus[5] == 0 && bStatus[8] == 0 && AI_Level == 3))
{
SetBox(2, BUTTON_3);
}
else
{
SetBox(6, BUTTON_7);
}
}
//------------------------------------------------------------
//Find a square to fill
else
{
if(bStatus[0] == 0)
SetBox(0, BUTTON_1);
else if(bStatus[1] == 0)
SetBox(1, BUTTON_2);
else if(bStatus[2] == 0)
SetBox(2, BUTTON_3);
else if(bStatus[3] == 0)
SetBox(3, BUTTON_4);
else if(bStatus[4] == 0)
SetBox(4, BUTTON_5);
else if(bStatus[5] == 0)
SetBox(5, BUTTON_6);
else if(bStatus[6] == 0)
SetBox(6, BUTTON_7);
else if(bStatus[7] == 0)
SetBox(7, BUTTON_8);
else if(bStatus[8] == 0)
SetBox(8, BUTTON_9);
}
}
}
void InitGUI(const HWND hWnd, CREATESTRUCT *cs)
{
RECT rc;
SetRect(&rc, 45, 70, 150, 150);
BUTTON_1 = CreateControl("BUTTON", //Type of control
"", //Caption for control
hWnd, //Parent window
cs->hInstance, //HINSTANCE
BS_ICON, //dwStyle
0, //dwExStyle
rc, //RECT struct
IDB_1 //ID
);
rc.left += 150;
BUTTON_2 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_2
);
rc.left += 150;
BUTTON_3 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_3
);
rc.left -= 300;
rc.top += 150;
BUTTON_4 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_4
);
rc.left += 150;
BUTTON_5 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_5
);
rc.left += 150;
BUTTON_6 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_6
);
rc.left -= 300;
rc.top += 150;
BUTTON_7 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_7
);
rc.left += 150;
BUTTON_8 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_8
);
rc.left += 150;
BUTTON_9 = CreateControl("BUTTON",
"",
hWnd,
cs->hInstance,
BS_ICON,
0,
rc,
IDB_9
);
SetRect(&rc, 214, 20, 150, 40);
STATIC_TITLE = CreateControl("STATIC",
"One Player Game",
hWnd,
cs->hInstance,
SS_SIMPLE,
WS_EX_STATICEDGE,
rc,
IDS_TITLE
);
rc.left -= 195;
STATIC_TURN = CreateControl("STATIC",
"X's turn",
hWnd,
cs->hInstance,
SS_SIMPLE,
0,
rc,
IDS_TURN
);
SetRect(&rc, 430, 13, 70, 30);
BUTTON_RESTART = CreateControl("BUTTON",
"Restart",
hWnd,
cs->hInstance,
BS_PUSHBUTTON,
WS_EX_CLIENTEDGE,
rc,
IDB_RESTART
);
ShowWindow(BUTTON_RESTART, false);
SetRect(&rc, 41, 540, 90, 19);
STATIC_XSCORE = CreateControl("STATIC",
"X Score:",
hWnd,
cs->hInstance,
SS_SIMPLE,
0,
rc,
IDS_XSCORE
);
rc.left += 61;
STATIC_XVALUE = CreateControl("STATIC",
"0",
hWnd,
cs->hInstance,
SS_SIMPLE,
0,
rc,
IDS_XVALUE
);
rc.left += 127;
STATIC_CATSCORE = CreateControl("STATIC",
"Cat Score:",
hWnd,
cs->hInstance,
SS_SIMPLE,
0,
rc,
IDS_CATSCORE
);
rc.left += 74;
STATIC_CATVALUE = CreateControl("STATIC",
"0",
hWnd,
cs->hInstance,
SS_SIMPLE,
0,
rc,
IDS_CATVALUE
);
rc.left += 126;
STATIC_OSCORE = CreateControl("STATIC",
"0 Score:",
hWnd,
cs->hInstance,
SS_SIMPLE,
0,
rc,
IDS_OSCORE
);
rc.left += 61;
STATIC_OVALUE = CreateControl("STATIC",
"0",
hWnd,
cs->hInstance,
SS_SIMPLE,
0,
rc,
IDS_OVALUE
);
}