Hey guys, I'm having a problem with my checkers program; whenever I move the piece, it redraws the whole board and basically puts the piece back into its original position. The code is attached to this post. Thank you for your help!
dankbc1 0 Newbie Poster
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
//Daniel Kaner
//Period 2-3
#pragma comment (lib, "gdiplus.lib")
//-----------------------------------------------------------------
// Skeleton Application
// C++ Source - Skeleton.cpp
//-----------------------------------------------------------------
//-----------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------
#include <windows.h>
#include <iostream>
#include <GdiPlus.h>
//#include <allegro.h>
#include <time.h>
//#include "msoftcon.h"
//-----------------------------------------------------------------
// Global Function Declarations
//-----------------------------------------------------------------
using namespace Gdiplus; //Graphic NAMESPACE
//Defines the function called when Windows messages come through the system.
LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam);
HWND hWindow;
HINSTANCE instance;
POINT pos;
HDC hDC = GetDC(hWindow);
ULONG_PTR gdiPlusToken;
int xCoord, yCoord;
//-----------------------------------------------------------------
// Global Functions
//-----------------------------------------------------------------
int Initialize(HINSTANCE hInstance, int iCmdShow, int width, int height)
{
instance = hInstance;
//Note that this is an array of characters, which is essentially a String.
static char szAppName[] = "Skeleton";
//These are defined structures
WNDCLASSEX wndclass;
//** Create the window class for the main window **
wndclass.cbSize = sizeof(wndclass);
//The simplest and most effective styles tell the computer to redraw the window completely.
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
//Creates an icon for the window. Uses the hInstance of the Window and
//a string with the name of the bitmap that holds the icon.
wndclass.hIcon = LoadIcon(hInstance, "Skeleton.bmp");
wndclass.hIconSm = LoadIcon(hInstance, "Skeleton_sm.bmp");
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
//** End Window Creation **
// Register the window class
if (!RegisterClassEx(&wndclass))
return 0;
// Create the window
/* CreateWindow(window class name
window caption
window style
initial x position
initial y position
initial x size
initial y size
parent window handle
window menu handle
program instance handle
creation parameters)
*/
hWindow = CreateWindow(szAppName, szAppName, WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL,
hInstance, NULL);
// Show and update the window
ShowWindow(hWindow, iCmdShow);
UpdateWindow(hWindow);
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&gdiPlusToken, &gdiplusStartupInput, NULL);
return 1;
}
/*DRAWS A GRID*/
void GamePaint(HDC hdc)
{
// char checkerBoard[8][8];
// int row, column, side, top;
for(int row = 1; row < 8; row++)
{
MoveToEx(hdc, 4075 * row / 50, 0, NULL);
LineTo(hdc, 4075 * row / 50, 600);
MoveToEx(hdc, 0, 2850 * row / 50, NULL);
LineTo(hdc, 815, 2850 * row / 50);
}
// Color c(255,0,0);
Graphics *g = Graphics::FromHWND(hWindow);
Font f(L"Arial",12);
SolidBrush sb(Color::Red);
SolidBrush sb2(Color::Black);
SolidBrush sb3(Color::White);
SolidBrush sb4(Color::Red);
SolidBrush sb9(Color::White);
// DrawRectangle(Pen p, int x, int y, int w, int h);
//column
g->FillRectangle(&sb, 0, 0, 82, 57);
g->FillRectangle(&sb, 82, 57, 82, 57);
g->FillRectangle(&sb, 163, 114, 82, 57);
g->FillRectangle(&sb, 244, 171, 82, 57);
g->FillRectangle(&sb, 326, 228, 82, 57);
g->FillRectangle(&sb, 408, 285, 81, 57);
g->FillRectangle(&sb, 490, 342, 80, 57);
g->FillRectangle(&sb, 570, 399, 82, 57);
//row
g->FillRectangle(&sb2, 82, 0, 82, 57);
g->FillRectangle(&sb, 164, 0, 80, 57);
g->FillRectangle(&sb2, 244, 0, 82, 57);
g->FillRectangle(&sb, 326, 0, 82, 57);
g->FillRectangle(&sb2, 408, 0, 81, 57);
g->FillRectangle(&sb, 490, 0, 80, 57);
g->FillRectangle(&sb2, 570, 0, 82, 57);
//2nd column
g->FillRectangle(&sb2, 82, 114, 82, 57);
g->FillRectangle(&sb, 82, 171, 82, 57);
g->FillRectangle(&sb2, 82, 228, 82, 57);
g->FillRectangle(&sb, 82, 285, 81, 57);
g->FillRectangle(&sb2, 82, 342, 81, 57);
g->FillRectangle(&sb, 82, 399, 82, 57);
//3rd column
g->FillRectangle(&sb2, 163, 57, 82, 57);
g->FillRectangle(&sb, 163, 114, 82, 57);
g->FillRectangle(&sb2, 163, 171, 82, 57);
g->FillRectangle(&sb, 163, 228, 82, 57);
g->FillRectangle(&sb2, 163, 285, 81, 57);
g->FillRectangle(&sb, 163, 342, 81, 57);
g->FillRectangle(&sb2, 163, 399, 82, 57);
//4th column
g->FillRectangle(&sb, 244, 57, 82, 57);
g->FillRectangle(&sb2, 244, 114, 82, 57);
g->FillRectangle(&sb, 244, 171, 82, 57);
g->FillRectangle(&sb2, 244, 228, 82, 57);
g->FillRectangle(&sb, 244, 285, 82, 57);
g->FillRectangle(&sb2, 244, 342, 82, 57);
g->FillRectangle(&sb, 244, 399, 82, 57);
//5th column
g->FillRectangle(&sb2, 326, 57, 82, 57);
g->FillRectangle(&sb, 326, 114, 82, 57);
g->FillRectangle(&sb2, 326, 171, 82, 57);
g->FillRectangle(&sb, 326, 228, 82, 57);
g->FillRectangle(&sb2, 326, 285, 82, 57);
g->FillRectangle(&sb, 326, 342, 82, 57);
g->FillRectangle(&sb2, 326, 399, 82, 57);
//6th column
g->FillRectangle(&sb, 408, 57, 82, 57);
g->FillRectangle(&sb2, 408, 114, 82, 57);
g->FillRectangle(&sb, 408, 171, 82, 57);
g->FillRectangle(&sb2, 408, 228, 82, 57);
g->FillRectangle(&sb, 408, 285, 82, 57);
g->FillRectangle(&sb2, 408, 342, 82, 57);
g->FillRectangle(&sb, 408, 399, 82, 57);
//7th column
g->FillRectangle(&sb2, 490, 57, 81, 57);
g->FillRectangle(&sb, 490, 114, 80, 57);
g->FillRectangle(&sb2, 490, 171, 81, 57);
g->FillRectangle(&sb, 490, 228, 80, 57);
g->FillRectangle(&sb2, 490, 285, 81, 57);
g->FillRectangle(&sb, 490, 342, 80, 57);
g->FillRectangle(&sb2, 490, 399, 81, 57);
//final column
g->FillRectangle(&sb, 570, 57, 82, 57);
g->FillRectangle(&sb2, 570, 114, 82, 57);
g->FillRectangle(&sb, 570, 171, 82, 57);
g->FillRectangle(&sb2, 570, 228, 82, 57);
g->FillRectangle(&sb, 570, 285, 82, 57);
g->FillRectangle(&sb2, 570, 342, 82, 57);
g->FillRectangle(&sb, 570, 399, 82, 57);
//diagnal (mistake -_-)
g->FillRectangle(&sb2, 0, 57, 82, 57);
g->FillRectangle(&sb, 0, 114, 82, 57);
g->FillRectangle(&sb2, 0, 171, 82, 57);
g->FillRectangle(&sb, 0, 228, 82, 57);
g->FillRectangle(&sb2, 0, 285, 81, 57);
g->FillRectangle(&sb, 0, 342, 81, 57);
g->FillRectangle(&sb2, 0, 399, 82, 57);
//White pieces on board
g->FillEllipse(&sb3, 87, 0, 57, 57);
g->FillEllipse(&sb3, 251, 0, 57, 57);
g->FillEllipse(&sb3, 415, 0, 57, 57);
g->FillEllipse(&sb3, 579, 0, 57, 57);
g->FillEllipse(&sb3, 0, 57, 57, 57);
g->FillEllipse(&sb3, 164, 57, 57, 57);
g->FillEllipse(&sb3, 328, 57, 57, 57);
g->FillEllipse(&sb3, 492, 57, 57, 57);
g->FillEllipse(&sb3, 87, 114, 57, 57);
g->FillEllipse(&sb3, 251, 114, 57, 57);
g->FillEllipse(&sb3, 415, 114, 57, 57);
g->FillEllipse(&sb3, 579, 114, 57, 57);
//Red pieces on board
g->FillEllipse(&sb4, 0, 285, 57, 57);
g->FillEllipse(&sb4, 164, 285, 57, 57);
g->FillEllipse(&sb4, 328, 285, 57, 57);
g->FillEllipse(&sb4, 492, 285, 57, 57);
g->FillEllipse(&sb4, 87, 340, 57, 57);
g->FillEllipse(&sb4, 251, 340, 57, 57);
g->FillEllipse(&sb4, 415, 340, 57, 57);
g->FillEllipse(&sb4, 579, 340, 57, 57);
g->FillEllipse(&sb4, 0, 400, 57, 57);
g->FillEllipse(&sb4, 164, 400, 57, 57);
g->FillEllipse(&sb4, 328, 400, 57, 57);
g->FillEllipse(&sb4, 492, 400, 57, 57);
// g->DrawString(L"Hello", -1, &f, PointF(50,50), &sb);
// LinearGradientBrush(PointF(50,50), PointF(100,200), c, Color::AliceBlue);
delete g;
// g->Clear(c);
}
void drawNewPiece()
{
// xPos = GET_X_LPARAM(lParam);
// yPos = GET_Y_LPARAM(lParam);
}
void wait(int seconds)
{
clock_t endwait;
endwait = clock() + seconds * CLK_TCK;
while(clock() < endwait){}
}
//DRAWS AN ELLIPSE
//Shade in the area of the last checker piece and draw a new piece at the place where the user clicked.
void DrawEllipse(HDC hdc, int top, int left, int right, int bottom)
{
/*GetCursorPos(&pos);
ScreenToClient(hWindow, &pos);
//Use MoveToEx and LineTo to draw Rectangle.
for (int i = 1; i < 50; i++)
{
MoveToEx(hdc, top, left, NULL);
LineTo(hdc, bottom, left);
MoveToEx(hdc, top, left, NULL);
LineTo(hdc, top, right);
MoveToEx(hdc, bottom, right, NULL);
LineTo(hdc, bottom, left);
MoveToEx(hdc, bottom, right, NULL);
LineTo(hdc, top, right);
}*/
/* MoveToEx(hdc, left, top, NULL);
LineTo(hdc, left, bottom);
LineTo(hdc, right, bottom);
LineTo(hdc, right, top);
LineTo(hdc, left, top);*/
Graphics *a = Graphics::FromHWND(hWindow);
SolidBrush sb5(Color::Red);
SolidBrush sb6(Color::Black);
a->FillEllipse(&sb5, 87, 230, 57, 57);
a->FillEllipse(&sb6, 0, 285, 57, 57);
delete a;
}
void Clicked(int xCoord, int yCoord)
{
if(xCoord < 174 && xCoord > 87 && yCoord > 230 && yCoord < 276)
{
Graphics *a = Graphics::FromHWND(hWindow);
SolidBrush sb5(Color::Red);
SolidBrush sb6(Color::Black);
a->FillEllipse(&sb5, 87, 230, 57, 57);
a->FillEllipse(&sb6, 0, 285, 57, 57);
delete a;
}
}
void DrawCompEllipse(HDC hdc, int top, int left, int right, int bottom)
{
Graphics *c = Graphics::FromHWND(hWindow);
SolidB
Rashakil Fol 978 Super Senior Demiposter Team Colleague
Ya think? Looking at your code, how could you expect it to do anything different? I don't see any attempt to maintain a board's state, and your program does not even deserve to be called an attempt at a checkers program; except upon the rare click, it just calls GamePaint over and over again, drawing the starting position of a checkers board.
dankbc1 0 Newbie Poster
Ya think? Looking at your code, how could you expect it to do anything different? I don't see any attempt to maintain a board's state, and your program does not even deserve to be called an attempt at a checkers program; except upon the rare click, it just calls GamePaint over and over again, drawing the starting position of a checkers board.
Can you instead tell me how to possibly fix it?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.