Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,914 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,575 people online right now. Registration is fast and FREE... Join Now!




tic tac toe help

 
Reply to this topicStart new topic

tic tac toe help

Mugglecaster
17 May, 2008 - 04:14 AM
Post #1

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 16


My Contributions
im trying to make a simple tic tac toe application but i cant seem to stop it from puting in a o or x when there is already one there i know all the if statments are terribly messy but im still intrigued as to why it wont work any way here the code
CODE
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

void drawFigure (int, int, HWND);
int boardArray [3][3] = {{0,0,0}, {0,0,0}};

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";
//mouse positions
int mouseX;
int mouseY;

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    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 = L"WinApp";
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color 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 */
           L"WinApp",         /* Classname */
           L"Tic Tac Toe",       /* Title Text */
           WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           307,                 /* The programs width */
           327,                 /* 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, nFunsterStil);

    /* 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()  */

HDC hdc;
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    
    PAINTSTRUCT ps;
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            MoveToEx(hdc,100,0,NULL);
            LineTo(hdc,100,300);
            MoveToEx(hdc,200,0,NULL);
            LineTo(hdc,200,300);
            MoveToEx(hdc,0,100,NULL);
            LineTo(hdc,300,100);
            MoveToEx(hdc,0,200,NULL);
            LineTo(hdc,300,200);
            EndPaint(hwnd, &ps);
            break;
        case WM_LBUTTONDOWN:
            mouseX = LOWORD(lParam);
            mouseY = HIWORD(lParam);
            drawFigure(mouseX,mouseY,hwnd);
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


int xOrO=1;
void drawFigure(int xPosition, int yPosition, HWND hwnd) {
    int xStart,yStart,xFinish,yFinish;
    if (xPosition <= 100) {
        xStart = 0;
        xFinish = 100;
    }
    else if (xPosition <= 200) {
        xStart = 100;
        xFinish = 200;
    }
    else if (xPosition <=300) {
        xStart = 200;
        xFinish = 300;
    }
    if (yPosition <= 100) {
        yStart = 0;
        yFinish = 100;
    }
    else if (yPosition <= 200) {
        yStart = 100;
        yFinish = 200;
    }
    else if (xPosition <=300) {
        yStart = 200;
        yFinish = 300;
    }
    if (xStart == 0 && yStart == 0) {
        if (boardArray[0][0] = 0)
        boardArray[0][0] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 100 && yStart == 0) {
        if (boardArray[1][0] = 0)
        boardArray[1][0] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 200 && yStart == 0) {
        if (boardArray[2][0] = 0)
        boardArray[2][0] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 0 && yStart == 100) {
        if (boardArray[0][1] = 0)
        boardArray[0][1] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 100 && yStart == 100) {
        if (boardArray[1][1] = 0)
        boardArray[1][1] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 200 && yStart == 100) {
        if (boardArray[2][1] = 0)
        boardArray[2][1] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 0 && yStart == 200) {
        if (boardArray[0][2] = 0)
        boardArray[0][2] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 100 && yStart == 200) {
        if (boardArray[1][2] = 0)
        boardArray[1][2] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    else if (xStart == 200 && yStart == 200) {
        if (boardArray[2][2] = 0)
        boardArray[2][2] = xOrO;
        else if (boardArray[0][0] != 0)
            return;
    }
    hdc = GetDC(hwnd);
    if(xOrO == 1) {
        MoveToEx(hdc,xStart,yStart,NULL);
        LineTo(hdc,xFinish,yFinish);
        MoveToEx(hdc,xStart+100,yStart,NULL);
        LineTo(hdc,xFinish-100,yFinish);
        xOrO = 2;
    }
    else if (xOrO == 2) {
        HBRUSH hBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
        SelectObject(hdc,hBrush);
        Ellipse(hdc,xStart,yStart,xFinish,yFinish);
        xOrO = 1;
    }
    ReleaseDC(hwnd,hdc);
}

User is offlineProfile CardPM
+Quote Post

Ambercroft
RE: Tic Tac Toe Help
18 May, 2008 - 02:31 PM
Post #2

D.I.C Head
Group Icon

Joined: 5 Jan, 2007
Posts: 107



Thanked: 3 times
Dream Kudos: 25
My Contributions
I see two problems (a)
QUOTE

int boardArray [3][3] = {{0,0,0}, {0,0,0}};

try:
CODE

int boardArray [3][3] = {{0,0,0},{0,0,0},{0,0,0}};


(cool.gif
QUOTE

else if (xStart == 100 && yStart == 0) {
if (boardArray[1][0] = 0)
boardArray[1][0] = xOrO;
else if (boardArray[0][0] != 0)
return;


If boardArray is not 0 then you check [0][0]. So only if [0][0] is zero will it not draw and return.

try:
CODE

} else if (xStart == 100 && yStart == 0) {
           if (boardArray[1][0] = 0){
              boardArray[1][0] = xOrO;
           } else {
              return;
           }
}


No you don't need the {} for the single lines or the indenting but it makes things easier to read and thus debug. cool.gif

User is offlineProfile CardPM
+Quote Post

skater_00
RE: Tic Tac Toe Help
18 May, 2008 - 02:45 PM
Post #3

D.I.C Head
Group Icon

Joined: 30 Apr, 2008
Posts: 173



Thanked: 4 times
Dream Kudos: 50
My Contributions
QUOTE(Ambercroft @ 19 May, 2008 - 12:31 AM) *

CODE

} else if (xStart == 100 && yStart == 0) {
           if (boardArray[1][0] = 0){
              boardArray[1][0] = xOrO;
           } else {
              return;
           }
}



Shouldn't it be: if (boardArray[1][0] == 0) ?

Just noticed Mugglecaster used the assignment operator (=) all the time instead of the equation operator (==). Very common programming error. This may cause bad program functionality too.

This post has been edited by skater_00: 18 May, 2008 - 02:49 PM
User is offlineProfile CardPM
+Quote Post

Ambercroft
RE: Tic Tac Toe Help
18 May, 2008 - 03:52 PM
Post #4

D.I.C Head
Group Icon

Joined: 5 Jan, 2007
Posts: 107



Thanked: 3 times
Dream Kudos: 25
My Contributions
QUOTE
skater_00 Posted Today, 03:45 PM
Shouldn't it be: if (boardArray[1][0] == 0) ?


Thanks skater_00. I was staring right at that. The if will always evaluate to 0 and always return.

CODE

} else if (xStart == 100 && yStart == 0) {
           if (boardArray[1][0] == 0){
              boardArray[1][0] = xOrO;
           } else {
              return;
           }
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:53AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month