void TicTacToeGame::PlayGame(int index, HWND hWnd, HDC hdc) { if (index != -1) { RECT rc; if ((arrayGameBoard[index] == 0) && GetCellRect(hWnd, index, &rc)) { //FillRect(hdc, &rc, (intPlayerTurn == 1) ? hbPlayerOne : hbPlayerTwo); DrawIconCentered(hdc, &rc, (intPlayerTurn == 1) ? hiPlayerOne : hiPlayerTwo); arrayGameBoard[index] = intPlayerTurn; winner = GetWinner(wins); if (winner == 1 || winner == 2) { ShowWinner(hWnd, hdc); MessageBox(hWnd, (winner == 1) ? L"Player 1 is the winner" : L"Player 2 is the winner", L"You Win", MB_OK | MB_ICONINFORMATION); intPlayerTurn = 0; } else if (winner == 3) { MessageBox(hWnd, L"it's a draw", L"Draw", MB_OK | MB_ICONINFORMATION); intPlayerTurn = 0; } else { intPlayerTurn = (intPlayerTurn == 1) ? 2 : 1; } ShowTurn(hWnd, hdc); } } }
void TicTacToeGame::ShowWinner(HWND hWnd, HDC hdc) { RECT rcClient; for (int i = 0; i < ARRAYSIZE(wins); i++) { if (GetCellRect(hWnd, wins[i], &rcClient)) { FillRect(hdc, &rcClient, CreateSolidBrush(RGB(255, 0, 0))); DrawIconCentered(hdc, &rcClient, (winner == 1) ? hiPlayerOne : hiPlayerTwo); } } }
void TicTacToeGame::DrawGame(HDC hdc, RECT rc, HWND hWnd) { for (int i = 0; i < 4; i++) { DrawLine(hdc, rc.left + TicTacToeGame::CELL_SIZE * i, rc.top, rc.left + TicTacToeGame::CELL_SIZE * i, rc.bottom); DrawLine(hdc, rc.left, rc.top + TicTacToeGame::CELL_SIZE * i, rc.right, rc.top + TicTacToeGame::CELL_SIZE * i); } RECT rcCell; for (int i = 0; i < ARRAYSIZE(arrayGameBoard); i++) { if ((arrayGameBoard[i] != 0) && GetCellRect(hWnd, i, &rcCell)) { DrawIconCentered(hdc, &rcCell, (arrayGameBoard[i] == 1) ? hiPlayerOne : hiPlayerTwo); } } if (winner != 0 && winner != 3) { ShowWinner(hWnd, hdc); } ShowTurn(hWnd, hdc); }
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: { hbr1 = CreateSolidBrush(RGB(255, 0, 0)); hbr2 = CreateSolidBrush(RGB(0, 0, 255)); hIcon1 = LoadIcon(hInst, MAKEINTRESOURCE(ID_PLAYER1)); hIcon2 = LoadIcon(hInst, MAKEINTRESOURCE(ID_PLAYER2)); } break; case WM_COMMAND: { int wmId = LOWORD(wParam); // Parse the menu selections: switch (wmId) { case ID_FILE_NEWGAME: { int ret = MessageBox(hWnd, L"Are you sure you want to start a new game?", L"New Game", MB_YESNO | MB_ICONQUESTION); if (IDYES == ret) { //Reset and start a new game playerTurn = 1; winner = 0; ZeroMemory(gameBoard, sizeof(gameBoard)); InvalidateRect(hWnd, NULL, TRUE); UpdateWindow(hWnd); } } break; case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_LBUTTONDOWN: { int xPos = GET_X_LPARAM(lParam); int yPos = GET_Y_LPARAM(lParam); if (0 == playerTurn) break; int index = GetCellNumberFromPoint(hWnd, xPos, yPos); HDC hdc = GetDC(hWnd); if (NULL != hdc) { // WCHAR temp[100]; //wsprintf(temp, L"Index = %d", index); // TextOut(hdc, xPos, yPos, temp, lstrlen(temp)); if (index != -1) { RECT rcCell; if ((0 == gameBoard[index]) && GetCellRect(hWnd, index, &rcCell)) { gameBoard[index] = playerTurn; //FillRect(hdc, &rcCell, (playerTurn==1) ? hbr1 : hbr2); DrawIconCentered(hdc, &rcCell, (playerTurn == 1) ? hIcon1 : hIcon2); winner = GetWinner(wins); if (winner == 1 || winner == 2) { MessageBox(hWnd, (winner == 1) ? L"Player 1 wins" : L"Player 2 wins", L"You win!",MB_OK | MB_ICONINFORMATION); playerTurn = 0; } else if (3==winner) { MessageBox(hWnd, L"No one wins!", L"It's a draw", MB_OK | MB_ICONEXCLAMATION); playerTurn = 0; } else if (winner == 0) { playerTurn = (playerTurn == 1) ? 2 : 1; } ShowTurn(hWnd, hdc); } } ReleaseDC(hWnd, hdc); } } break; case WM_GETMINMAXINFO: { MINMAXINFO * pMinMax = (MINMAXINFO*)lParam; pMinMax->ptMinTrackSize.x = CELL_SIZE * 5; pMinMax->ptMinTrackSize.y = CELL_SIZE * 5; } case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); RECT rc; if (GetGameBoardRect(hWnd, &rc)) { RECT rcClient; if (GetClientRect(hWnd, &rcClient)) { const WCHAR szPlayer1 [] = L"Player 1"; const WCHAR szPlayer2 []= L"Player 2"; SetBkMode(hdc, TRANSPARENT); SetTextColor(hdc, RGB(255, 255, 0)); TextOut(hdc, 16, 16, szPlayer1, 8); SetTextColor(hdc, RGB(0, 0, 255)); TextOut(hdc, rcClient.right-72, 16, szPlayer2, 8); ShowTurn(hWnd, hdc); } FillRect(hdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH)); //Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom); } for (int i = 0; i < 4; i++) { DrawLine(hdc, rc.left+CELL_SIZE*i , rc.top, rc.left + CELL_SIZE*i, rc.bottom); DrawLine(hdc, rc.left, rc.top + CELL_SIZE*i, rc.right, rc.top+CELL_SIZE*i); } RECT rcCell; for (int i = 0; i < 9; i++) { if ((0!=gameBoard[i]) && GetCellRect(hWnd, i, &rcCell)) { //FillRect(hdc, &rcCell, (gameBoard[i]==2) ? hbr2 : hbr1); DrawIconCentered(hdc, &rcCell, (gameBoard[i] == 1) ? hIcon1 : hIcon2); } } EndPaint(hWnd, &ps); } break; case WM_DESTROY: DeleteObject(hbr1); DeleteObject(hbr2); DestroyIcon(hIcon1); DestroyIcon(hIcon2); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }