• 0

wrote a simple c++ tic tac toe app (no ai or tcp/ip yet ) for final project


Question

I did this in visual basic and used sockets to make it tcp/ip but we are learning c++ this semester unfort mostly using text based apps thou so I had to do some research on how to do it

http://togermano.com/homework/cpp/tonystictactoebeta1.exe


#include <windows.h>
// Made By Anthony Germano For Elms College Final Project
#define BUTTON1 100
#define BUTTON2 200
#define BUTTON3 300
#define BUTTON4 400
#define BUTTON5 500
#define BUTTON6 600
#define BUTTON7 700
#define BUTTON8 800
#define BUTTON9 900
char const* b1 = "1";
char const* b2 = "2";
char const* b3 = "3";
char const* b4 = "4";
char const* b5 = "5";
char const* b6 = "6";
char const* b7 = "7";
char const* b8 = "8";
char const* b9 = "9";
char const* t1 = "1";
char const* t2 = "2";
char const* t3 = "3";
char const* t4 = "4";
char const* t5 = "5";
char const* t6 = "6";
char const* t7 = "7";
char const* t8 = "8";
char const* t9 = "9";
char win[16] = " wins"; //for msgbox
char out1[16] = ""; //for msgbox
char const* winner = "N"; //variable for if the last move is a winner and uses all buttons it doesnt say winner and tie
char const* tic = "X";
void changetic(){
if (tic == "X"){
tic = "O";}
else if (tic == "O"){
tic = "X";
}
}
void disablebuttons(HWND hwnd){
EnableWindow(GetDlgItem(hwnd, BUTTON1), false);
EnableWindow(GetDlgItem(hwnd, BUTTON2), false);
EnableWindow(GetDlgItem(hwnd, BUTTON3), false);
EnableWindow(GetDlgItem(hwnd, BUTTON4), false);
EnableWindow(GetDlgItem(hwnd, BUTTON5), false);
EnableWindow(GetDlgItem(hwnd, BUTTON6), false);
EnableWindow(GetDlgItem(hwnd, BUTTON7), false);
EnableWindow(GetDlgItem(hwnd, BUTTON8), false);
EnableWindow(GetDlgItem(hwnd, BUTTON9), false);

}
void checkwin(HWND hwnd){
if (b1 == b2 && b1 == b3){
winner = "Y";
strcat(out1, b1);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}

if (b4 == b5 && b4 == b6){
winner = "Y";
strcat(out1, b4);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}
if (b7 == b8 && b7 == b9){
winner = "Y";
strcat(out1, b7);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}

if (b1 == b4 && b1 == b7){
winner = "Y";
strcat(out1, b1);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}
if (b2 == b5 && b2 == b8){
winner = "Y";
strcat(out1, b2);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}
if (b3 == b6 && b3 == b9){
winner = "Y";
strcat(out1, b3);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}
if (b1 == b5 && b1 == b9){
winner = "Y";
strcat(out1, b1);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}
if (b3 == b5 && b3 == b7){
winner = "Y";
strcat(out1, b3);
strcat(out1, win);
disablebuttons(hwnd);
MessageBoxA(hwnd,out1,"TicTacToe",MB_OK|MB_ICONINFORMATION);}
if (t1 == t2 && t3 == t4 && t5 == t6 && t7 == t8 && t7 == t9 && winner == "N"){


MessageBoxA(hwnd,"It is a Tie!!!","TicTacToe",MB_OK|MB_ICONINFORMATION);}


}

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
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 = szClassName;
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)CreateSolidBrush(RGB(255,3,3));
//http://cboard.cprogramming.com/windows-programming/113200-window-background-color.html
/* 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 */
"Tony's Amazing Tic Tac Toe", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* 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 */
);


CreateWindow("button", "1", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 100, 50, 50, 50, hwnd, (HMENU)BUTTON1, GetModuleHandle (NULL), NULL);
CreateWindow("button", "2", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 200, 50, 50, 50, hwnd, (HMENU)BUTTON2, GetModuleHandle (NULL), NULL);
CreateWindow("button", "3", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 300, 50, 50, 50, hwnd, (HMENU)BUTTON3, GetModuleHandle (NULL), NULL);
CreateWindow("button", "4", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 100, 150, 50, 50, hwnd, (HMENU)BUTTON4, GetModuleHandle (NULL), NULL);
CreateWindow("button", "5", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 200, 150, 50, 50, hwnd, (HMENU)BUTTON5, GetModuleHandle (NULL), NULL);
CreateWindow("button", "6", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 300, 150, 50, 50, hwnd, (HMENU)BUTTON6, GetModuleHandle (NULL), NULL);
CreateWindow("button", "7", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 100, 250, 50, 50, hwnd, (HMENU)BUTTON7, GetModuleHandle (NULL), NULL);
CreateWindow("button", "8", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 200, 250, 50, 50, hwnd, (HMENU)BUTTON8, GetModuleHandle (NULL), NULL);
CreateWindow("button", "9", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 300, 250, 50, 50, hwnd, (HMENU)BUTTON9, GetModuleHandle (NULL), NULL);
/* 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() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_COMMAND:
switch(LOWORD(wParam)) {
case BUTTON1:{
b1 = tic;
t1 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON1), false);
SetDlgItemText(hwnd, BUTTON1, tic);
checkwin(hwnd);
changetic();

}


}


switch(LOWORD(wParam)) {
case BUTTON2:{
b2 = tic;
t2 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON2), false);

SetDlgItemText(hwnd, BUTTON2, tic);
checkwin(hwnd);
changetic();
}

}

switch(LOWORD(wParam)) {
case BUTTON3:{
b3 = tic;
t3 = "U";

EnableWindow(GetDlgItem(hwnd, BUTTON3), false);
SetDlgItemText(hwnd, BUTTON3, tic);
checkwin(hwnd);
changetic();
}

}

switch(LOWORD(wParam)) {
case BUTTON4:{
b4 = tic;
t4 = "U";

EnableWindow(GetDlgItem(hwnd, BUTTON4), false);
SetDlgItemText(hwnd, BUTTON4, tic);
checkwin(hwnd);
changetic();
}

}

switch(LOWORD(wParam)) {
case BUTTON5:{
b5 = tic;
t5 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON5), false);
SetDlgItemText(hwnd, BUTTON5, tic);
checkwin(hwnd);
changetic();
}

}
switch(LOWORD(wParam)) {
case BUTTON6:{
b6 = tic;
t6 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON6), false);
SetDlgItemText(hwnd, BUTTON6, tic);
changetic();
}

}

switch(LOWORD(wParam)) {
case BUTTON7:{
b7 = tic;
t7 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON7), false);
SetDlgItemText(hwnd, BUTTON7, tic);
checkwin(hwnd);
changetic();
}

}

switch(LOWORD(wParam)) {
case BUTTON8:{
b8 = tic;
t8 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON8), false);
SetDlgItemText(hwnd, BUTTON8, tic);
checkwin(hwnd);
changetic();
}

}
switch(LOWORD(wParam)) {
case BUTTON9:{
b9 = tic;
t9 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON9), false);
SetDlgItemText(hwnd, BUTTON9, tic);
checkwin(hwnd);
changetic();
}

}




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;
}
[/CODE]

copy and paste code in to a windows project in devc++

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Why this obsession with devc++? Visual Studio Express is free to download!

Some notes:

1. If you don't care about unicode then keep using the *A variants of the win32 APIs. If you do care then look into the T("string") macro, TCHAR, LPCTSTR and so on.

2. As this is C++ you may want to encapsulate the creation and deletion of the window inside a class with CreateWindowEx in the constructor and DestroyWindow in the destructor (showing good RAII practice)

3. Make sure you get const correctness right with variables and class functions

eg. char s[] = "string"; should be char const s[] = "string";

As string literals are constants.

4. It may be better to express the program as a 3x3 array of classes representing a single tile, this class could be used to wrap up some of the functionality you have to reduce code duplication

5. In my experience the C++ way to define constants would be:

For C++03

static const unsigned int c = 100;

For C++11

constexpr unsigned int c = 100;

rather than the C way

#define c 100

Link to comment
Share on other sites

  • 0

Why this obsession with devc++? Visual Studio Express is free to download!

Some notes:

1. If you don't care about unicode then keep using the *A variants of the win32 APIs. If you do care then look into the T("string") macro, TCHAR, LPCTSTR and so on.

2. As this is C++ you may want to encapsulate the creation and deletion of the window inside a class with CreateWindowEx in the constructor and DestroyWindow in the destructor (showing good RAII practice)

3. Make sure you get const correctness right with variables and class functions

eg. char s[] = "string"; should be char const s[] = "string";

As string literals are constants.

4. It may be better to express the program as a 3x3 array of classes representing a single tile, this class could be used to wrap up some of the functionality you have to reduce code duplication

5. In my experience the C++ way to define constants would be:

For C++03

static const unsigned int c = 100;

For C++11

constexpr unsigned int c = 100;

rather than the C way

#define c 100

Thanks for the better programming tips :D I don't care about studio express because of the big ass runtime users need for apps to run and also for compatibility reasons the code I run,runs pefectly on windows 95 and higher.

Link to comment
Share on other sites

  • 0

Id love to learn assembly because would be cool to make a original nintendo game (NES)

You do not 'learn assembly', you learn about specific hardware and sets of hardware, assembly for the NES CPU will not work or look anything like assembly for x86, etc. And cool though it might be, I don't think you'll ever get around to doing that in assembly.

Link to comment
Share on other sites

  • 0

You do not 'learn assembly', you learn about specific hardware and sets of hardware, assembly for the NES CPU will not work or look anything like assembly for x86, etc. And cool though it might be, I don't think you'll ever get around to doing that in assembly.

I know it's a relatively minor nitpick (since anyone who seriously starts to "learn assembly" will quickly figure out that an assembly language is specific to its ISA), but it is a common misconception that I have seen posted here and elsewhere around the Internet many times (presumably by others who do not know an assembly language). It's nice to know I'm not the only one who has this pet peeve.

Link to comment
Share on other sites

  • 0

You do not 'learn assembly', you learn about specific hardware and sets of hardware, assembly for the NES CPU will not work or look anything like assembly for x86, etc. And cool though it might be, I don't think you'll ever get around to doing that in assembly.

Sure that works xx

Link to comment
Share on other sites

  • 0

Thanks for the better programming tips :D I don't care about studio express because of the big ass runtime users need for apps to run and also for compatibility reasons the code I run,runs pefectly on windows 95 and higher.

Some things are just worth it, I would never want to go back and program in C++ without lambdas and unique_ptr. Visual Studio 2012 supports deploying applications on Windows XP and higher so that's enough for me.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.