• 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++

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

  • 0
  On 18/04/2013 at 15:37, Lant said:

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.

  • 0
  On 18/04/2013 at 17:07, togermano said:

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.

  • 0
  On 18/04/2013 at 17:20, n_K said:

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.

  • 0
  On 18/04/2013 at 17:20, n_K said:

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

  • 0
  On 18/04/2013 at 16:39, togermano said:

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.

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

    • No registered users viewing this page.
  • Posts

    • The multi tab function is definitely useful. I was constantly asking it to analyze my screen and perform certain functions, but having to do that for each tab was a chore. This is def a welcome improvement!
    • Tablet mode doesn't exist on public releases in Windows 11, but there is a very good option activated with a few registry tweaks they must be testing. I've been using it on an old Surface Pro and love it. Minimised taskbar with no pinned icons or Start button visible, only clock and settings (battery/wifi/sound) icons. When the whole thing is visible, the icons are much larger and easy to tap. Swiping up does shows it and if you continue, opens the Start Menu. Needs more work, like a single finger swipe for multitasking, and perhaps miniaturised widget/s visible on the left, but it works well and I think it even works with mouse & keyboard use (mine's forced always on). Reddit - https://preview.redd.it/what-w...51db8351dd56cbf6bc012397a20
    • A GUI wrapper makes something like this actually accessible for people coming from any other system. I do not want to ever touch the terminal, we are no longer in the 80's, so this is actually a massive help, even if the underlying software isn't actually great. Bullying should be called out, and I believe that's what this post is about. So I'm glad it's happened, and I hope that this dev, or someone else picks it back up and makes the Linux Desktop a touch more relevant in the modern world.
    • the point is you need new PCs with a dedicated co-pilot button lol
    • The problem is that LOUD and AGRESSIVE persons end up getting their way one way or another. In this case, the dodgy user was successful in making the dev quit the project. If the dev continued to work on it, their comments could well have had ramifications on others who may have wanted to try it, also thinking that what was said was true, and the tool may never have got traction. It's just flat out bullying, lets call it what it is, and needs to be addressed. Often bullies are the actual "snowflakes", who have often missed developmental milestones in childhood, or experienced trauma and as a result don't have the intellect or skills to manage their emotions. It often comes across as aggressive, much like this user. So while this user clearly has something going on, I do hope they seek help. As for the developer, I also hope they build some resilience, and experience the opposite of bullying via positive words from this news story. Calling them a "snowflake" is effectively bullying in itself, and that too needs to be called out. So I'm calling you out @noobient. Your point otherwise is valid. But we all need to have support too, and clearly there wasn't during this developers time of bullying. We as part of the tech community need to do better.
  • Recent Achievements

    • First Post
      Gladiattore earned a badge
      First Post
    • Reacting Well
      Gladiattore earned a badge
      Reacting Well
    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      649
    2. 2
      ATLien_0
      256
    3. 3
      Xenon
      165
    4. 4
      neufuse
      143
    5. 5
      +FloatingFatMan
      108
  • Tell a friend

    Love Neowin? Tell a friend!