• 0

Now my c++ tictactoe has some AI to it and will play against a computer


Question

I am also taking an Artificial Intelligence class so I decided to add AI to my c++ project.Unfort I used abunch of IF statements to it.... Is there a better way to do this?

download here:

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


#include <windows.h>
//#include <stdlib.h>
#include <time.h>
#include <stdio.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
#define BUTTON10 1000
int v1;
int compturnstop;
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 enablebuttons(HWND hwnd){
EnableWindow(GetDlgItem(hwnd, BUTTON1), true);
EnableWindow(GetDlgItem(hwnd, BUTTON2), true);
EnableWindow(GetDlgItem(hwnd, BUTTON3), true);
EnableWindow(GetDlgItem(hwnd, BUTTON4), true);
EnableWindow(GetDlgItem(hwnd, BUTTON5), true);
EnableWindow(GetDlgItem(hwnd, BUTTON6), true);
EnableWindow(GetDlgItem(hwnd, BUTTON7), true);
EnableWindow(GetDlgItem(hwnd, BUTTON8), true);
EnableWindow(GetDlgItem(hwnd, BUTTON9), true);

}

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"){

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


}
void computerturn(HWND hwnd){
int compturnstop = 0;
if (winner == "N"){

if (b1 == b2 && t3 != "U" && compturnstop == 0){
compturnstop = 3;
b3 = tic;
t3 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON3), false);
SetDlgItemText(hwnd, BUTTON3, tic);
checkwin(hwnd);
changetic();

}
if (b2 == b3 && t1 != "U" && compturnstop == 0){
compturnstop = 1;
b1 = tic;
t1 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON1), false);
SetDlgItemText(hwnd, BUTTON1, tic);
checkwin(hwnd);
changetic();

}
if (b4 == b5 && t6 != "U" && compturnstop == 0){
compturnstop = 6;
b6 = tic;
t6 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON6), false);
SetDlgItemText(hwnd, BUTTON6, tic);
checkwin(hwnd);
changetic();

}
if (b6 == b5 && t4 != "U" && compturnstop == 0){
compturnstop = 4;
b4 = tic;
t4 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON4), false);
SetDlgItemText(hwnd, BUTTON4, tic);
checkwin(hwnd);
changetic();

}
if (b6 == b8 && t9 != "U" && compturnstop == 0){
compturnstop = 9;
b9 = tic;
t9 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON9), false);
SetDlgItemText(hwnd, BUTTON9, tic);
checkwin(hwnd);
changetic();

}
if (b9 == b8 && t7 != "U" && compturnstop == 0){
compturnstop = 7;
b7 = tic;
t7 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON7), false);
SetDlgItemText(hwnd, BUTTON7, tic);
checkwin(hwnd);
changetic();

}
if (b1 == b4 && t7 != "U" && compturnstop == 0){
compturnstop = 7;
b7 = tic;
t7 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON7), false);
SetDlgItemText(hwnd, BUTTON7, tic);
checkwin(hwnd);
changetic();

}
if (b7 == b3 && t1 != "U" && compturnstop == 0){
compturnstop = 1;
b1 = tic;
t1 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON1), false);
SetDlgItemText(hwnd, BUTTON1, tic);
checkwin(hwnd);
changetic();

}

if (b2 == b5 && t8 != "U" && compturnstop == 0){
compturnstop = 8;
b8 = tic;
t8 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON8), false);
SetDlgItemText(hwnd, BUTTON8, tic);
checkwin(hwnd);
changetic();

}
if (b8 == b5 && t2 != "U" && compturnstop == 0){
compturnstop = 2;
b2 = tic;
t2 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON2), false);
SetDlgItemText(hwnd, BUTTON2, tic);
checkwin(hwnd);
changetic();

}
if (b3 == b6 && t9 != "U" && compturnstop == 0){
compturnstop = 9;
b9 = tic;
t9 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON9), false);
SetDlgItemText(hwnd, BUTTON9, tic);
checkwin(hwnd);
changetic();

}
if (b9 == b6 && t3 != "U" && compturnstop == 0){
compturnstop = 3;
b3 = tic;
t3 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON3), false);
SetDlgItemText(hwnd, BUTTON3, tic);
checkwin(hwnd);
changetic();

}
if (b1 == b3 && t2 != "U" && compturnstop == 0){
compturnstop = 2;
b2 = tic;
t2 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON2), false);
SetDlgItemText(hwnd, BUTTON2, tic);
checkwin(hwnd);
changetic();

}
if (b4 == b6 && t5 != "U" && compturnstop == 0){
compturnstop = 5;
b5 = tic;
t5 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON5), false);
SetDlgItemText(hwnd, BUTTON5, tic);
checkwin(hwnd);
changetic();

}
if (b7 == b9 && t8 != "U" && compturnstop == 0){
compturnstop = 8;
b8 = tic;
t8 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON8), false);
SetDlgItemText(hwnd, BUTTON8, tic);
checkwin(hwnd);
changetic();

}
if (b1 == b7 && t4 != "U" && compturnstop == 0){
compturnstop = 4;
b4 = tic;
t4 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON4), false);
SetDlgItemText(hwnd, BUTTON4, tic);
checkwin(hwnd);
changetic();

}
if (b2 == b8 && t5 != "U" && compturnstop == 0){
compturnstop = 5;
b5 = tic;
t5 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON5), false);
SetDlgItemText(hwnd, BUTTON5, tic);
checkwin(hwnd);
changetic();

}
if (b3 == b9 && t6 != "U" && compturnstop == 0){
compturnstop = 6;
b6 = tic;
t6 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON6), false);
SetDlgItemText(hwnd, BUTTON6, tic);
checkwin(hwnd);
changetic();

}
if (b3 == b5 && t7 != "U" && compturnstop == 0){
compturnstop = 6;
b7 = tic;
t7 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON7), false);
SetDlgItemText(hwnd, BUTTON7, tic);
checkwin(hwnd);
changetic();

}
if (b7 == b5 && t3 != "U" && compturnstop == 0){
compturnstop = 3;
b3 = tic;
t3 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON3), false);
SetDlgItemText(hwnd, BUTTON3, tic);
checkwin(hwnd);
changetic();

}
if (b1 == b5 && t9 != "U" && compturnstop == 0){
compturnstop = 9;
b9 = tic;
t9 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON9), false);
SetDlgItemText(hwnd, BUTTON9, tic);
checkwin(hwnd);
changetic();

}
if (b9 == b5 && t1 != "U" && compturnstop == 0){
compturnstop = 1;
b1 = tic;
t1 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON1), false);
SetDlgItemText(hwnd, BUTTON1, tic);
checkwin(hwnd);
changetic();

}

if (t5 == "5" && compturnstop == 0){
compturnstop = 5;
b5 = tic;
t5 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON5), false);
SetDlgItemText(hwnd, BUTTON5, tic);
checkwin(hwnd);
changetic();

}
if (t1 == "1" && compturnstop == 0){
compturnstop = 1;
b1 = tic;
t1 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON1), false);
SetDlgItemText(hwnd, BUTTON1, tic);
checkwin(hwnd);
changetic();

}
if (t3 == "3" && compturnstop == 0){
compturnstop = 3;
b3 = tic;
t3 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON3), false);
SetDlgItemText(hwnd, BUTTON3, tic);
checkwin(hwnd);
changetic();

}
if (t5 == "5" && compturnstop == 0){
compturnstop = 5;
b5 = tic;
t5 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON5), false);
SetDlgItemText(hwnd, BUTTON5, tic);
checkwin(hwnd);
changetic();

}
if (t7 == "7" && compturnstop == 0){
compturnstop = 7;
b7 = tic;
t7 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON7), false);
SetDlgItemText(hwnd, BUTTON7, tic);
checkwin(hwnd);
changetic();

}
if (t9 == "9" && compturnstop == 0){
compturnstop = 5;
b9 = tic;
t9 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON9), false);
SetDlgItemText(hwnd, BUTTON9, tic);
checkwin(hwnd);
changetic();

}
if (compturnstop == 0){
// MessageBoxA(hwnd,"Computer never took a turn","TicTacToe",MB_OK|MB_ICONINFORMATION);}

if (t1 != "U"){

compturnstop = 1;
b1 = tic;
t1 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON1), false);
SetDlgItemText(hwnd, BUTTON1, tic);
checkwin(hwnd);
changetic();
}
if (t2 != "U"){

compturnstop = 2;
b2 = tic;
t2 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON2), false);
SetDlgItemText(hwnd, BUTTON2, tic);
checkwin(hwnd);
changetic();
}
if (t3 != "U"){

compturnstop = 3;
b3 = tic;
t3 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON3), false);
SetDlgItemText(hwnd, BUTTON3, tic);
checkwin(hwnd);
changetic();
}
if (t4 != "U"){

compturnstop = 4;
b4 = tic;
t4 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON4), false);
SetDlgItemText(hwnd, BUTTON4, tic);
checkwin(hwnd);
changetic();
}
if (t5 != "U"){

compturnstop = 5;
b5 = tic;
t5 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON5), false);
SetDlgItemText(hwnd, BUTTON5, tic);
checkwin(hwnd);
changetic();
}
if (t6 != "U"){

compturnstop = 6;
b6 = tic;
t6 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON6), false);
SetDlgItemText(hwnd, BUTTON6, tic);
checkwin(hwnd);
changetic();
}
if (t6 != "U"){

compturnstop = 7;
b7 = tic;
t7 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON7), false);
SetDlgItemText(hwnd, BUTTON7, tic);
checkwin(hwnd);
changetic();
}
if (t6 != "U"){

compturnstop = 8;
b8 = tic;
t8 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON8), false);
SetDlgItemText(hwnd, BUTTON8, tic);
checkwin(hwnd);
changetic();
}
if (t9 != "U"){

compturnstop = 9;
b9 = tic;
t9 = "U";
EnableWindow(GetDlgItem(hwnd, BUTTON9), false);
SetDlgItemText(hwnd, BUTTON9, tic);
checkwin(hwnd);
changetic();
}
}

}
}
/* 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 */
380, /* The programs width */
395, /* 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, 50, 10, 50, 50, hwnd, (HMENU)BUTTON1, GetModuleHandle (NULL), NULL);
CreateWindow("button", "2", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 150, 10, 50, 50, hwnd, (HMENU)BUTTON2, GetModuleHandle (NULL), NULL);
CreateWindow("button", "3", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 250, 10, 50, 50, hwnd, (HMENU)BUTTON3, GetModuleHandle (NULL), NULL);
CreateWindow("button", "4", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 50, 110, 50, 50, hwnd, (HMENU)BUTTON4, GetModuleHandle (NULL), NULL);
CreateWindow("button", "5", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 150, 110, 50, 50, hwnd, (HMENU)BUTTON5, GetModuleHandle (NULL), NULL);
CreateWindow("button", "6", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 250, 110, 50, 50, hwnd, (HMENU)BUTTON6, GetModuleHandle (NULL), NULL);
CreateWindow("button", "7", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 50, 210, 50, 50, hwnd, (HMENU)BUTTON7, GetModuleHandle (NULL), NULL);
CreateWindow("button", "8", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 150, 210, 50, 50, hwnd, (HMENU)BUTTON8, GetModuleHandle (NULL), NULL);
CreateWindow("button", "9", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 250, 210, 50, 50, hwnd, (HMENU)BUTTON9, GetModuleHandle (NULL), NULL);
CreateWindow("button","Start Game!", WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 130, 300, 100, 50, hwnd, (HMENU)BUTTON10, GetModuleHandle (NULL), NULL);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
disablebuttons(hwnd);
/* 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();
computerturn(hwnd);

}


}


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

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

}

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

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

}

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

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

}

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

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

}

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

}

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

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


}
switch(LOWORD(wParam)) {
case BUTTON10:{
srand (time(NULL));
/* generate secret number between 1 and 10: */
v1 = rand() % 10 + 1;
enablebuttons(hwnd);
if (v1 <= 4){
MessageBoxA(hwnd,"computer goes first!","TicTacToe",MB_OK|MB_ICONINFORMATION);
computerturn(hwnd);
}

if (v1 >= 5){
MessageBoxA(hwnd,"Person goes first!","TicTacToe",MB_OK|MB_ICONINFORMATION);}

}
}




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]

13 answers to this question

Recommended Posts

  • 0

Congrats man! I do not know of a better way.. but congrats on getting that far :)

Thanks Id like to change the windows api and see if I can change it around to run on linux or maybe try Haiku first (beos clone)

  • 0

A lot of this code is repetitive, so you could at least make functions that take the parts that vary as parameters.

A tic-tac-toe AI is basically a function that takes a game position as a parameter and returns the best move. One way to do this is pre-encode all possible positions along with their respective best moves in some kind of map structure. This is easy to do by hand for a 3x3 tic-tac-toe, but quickly becomes impractical if the board gets larger (the number of possible positions increases exponentially). Implementing the AI is then as easy as a look up in a table.

If you want to code a real AI that actually finds the best move dynamically, I suggest looking into the minimax algorithm. This is the basis of AI engines for chess, checkers, reverso and all those turn-based positional games.

  • Like 2
  • 0

A lot of this code is repetitive, so you could at least make functions that take the parts that vary as parameters.

A tic-tac-toe AI is basically a function that takes a game position as a parameter and returns the best move. One way to do this is pre-encode all possible positions along with their respective best moves in some kind of map structure. This is easy to do by hand for a 3x3 tic-tac-toe, but quickly becomes impractical if the board gets larger (the number of possible positions increases exponentially). Implementing the AI is then as easy as a look up in a table.

If you want to code a real AI that actually finds the best move dynamically, I suggest looking into the minimax algorithm. This is the basis of AI engines for chess, checkers, reverso and all those turn-based positional games.

Yes your right we learned about minimax but wasn't sure how to do in c++

  • 0

Before you attempt to optimize the AI algorithm, definitely clean the code up first. I think you can reduce the amount of code to maybe a third of what it is now by making helper functions to eliminate the repetition as suggested. Less code, less potential bugs.

And if you are up for a redesign, consider going the OOP approach as that is the main strength of C++ over C.

  • 0

wow what a mess :p anyways im sure you'll work on cleaning up your code as you mature as a programmer. just something i noticed right off the bat,i dont think you're using switches correctly.

from your code

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();

computerturn(hwnd);

}

}

switch(LOWORD(wParam)) {

case BUTTON2:{

b2 = tic;

t2 = "U";

EnableWindow(GetDlgItem(hwnd, BUTTON2), false);

SetDlgItemText(hwnd, BUTTON2, tic);

checkwin(hwnd);

changetic();

computerturn(hwnd);

}

}

why do you keep using switch(LOWORD(wParam)) for every check of a button?

it should be

switch(LOWORD(wParam))

{

case BUTTON1:

yadda yadda

break;

case BUTTON2:

yadda yadda

break;

case BUTTON3:

break;

case BUTTON4:

break;

}

  • 0

Before you attempt to optimize the AI algorithm, definitely clean the code up first. I think you can reduce the amount of code to maybe a third of what it is now by making helper functions to eliminate the repetition as suggested. Less code, less potential bugs.

And if you are up for a redesign, consider going the OOP approach as that is the main strength of C++ over C.

Thank you I know oop but isn't this only useful if your program is about keeping data?

wow what a mess :p anyways im sure you'll work on cleaning up your code as you mature as a programmer. just something i noticed right off the bat,i dont think you're using switches correctly.

from your code

why do you keep using switch(LOWORD(wParam)) for every check of a button?

it should be

switch(LOWORD(wParam))

{

case BUTTON1:

yadda yadda

break;

case BUTTON2:

yadda yadda

break;

case BUTTON3:

break;

case BUTTON4:

break;

}

I thought I tried that and it didn't work.... I'll try it again later thou thanks

If you understand the algorithm then it's just a matter of learning the language. That's a much easier problem thankfully. :)

I think minmax will require alot more code?

I'd look at refactoring a lot of your code into function(s) as a first step - remove the repetition.

Thanks

  • 0
I think minmax will require alot more code?
If you know what you're doing it's not that much code... I get the feeling you're pretty new to programming with a general-purpose language, so perhaps coding a minimax-based AI (which is a lot more than just the algorithm itself) is outside of your grasp for now. Brush up on your data structures and algorithms and it should be fairly obvious how to do this.
  • 0

I actually wrote a (console based) Tic Tac Toe game in C++ two days ago. For my AI player implementation I wanted to avoid "Googling it" and implementing an algorithm, but as an experiment I just threw together whatever came to mind. Please note I am very much not an AI person (did a module on Machine Learning at Uni, it sucked the life out of me). My AI player doesn't try to block the human player because I couldn't be especially bothered to write that, but it wouldn't be hard to implement a test against the human players last move following the same ideas as the current code (i.e, checking which line the player's trying to fill and blocking it). If you're interested this was my code for the AI decision...

(Where char map[] is a 1D array of each square on the board left-to-right top-to-bottom, and int size is the width/height of the board (i.e, standard game of 3x3, 9 cells, size = 3))


int AIPlayer::YourMove(char map[], int size){
int move;
cout << "Computer player's move!" << endl;
if (lastMove == -1){
move = ((size*size)-1) / 2;
if (isFree(map,move)){
map[move] = _thischar;
}else{
if (isFree(map, 0)){
move = 0;
}else if (isFree(map, size-1)){
move = size-1;
}else if (isFree(map, size * (size-1))){
move = size * (size-1);
}else if (isFree(map, (size*size)-1)){
move = (size*size)-1;
}
}
}else{
int tmp = lastMove;
int iters = 0;
while(true){
move = testNeighbours(tmp, size, map);
if (move == -1 && iters < 30){
tmp = (rand() % (size*size));
iters++;
}else if (iters >= 30){
move = 0;
while (!isFree(map, move)) move = (rand() % (size*size));
break;
}else break;
}
}
map[move] = _thischar;
lastMove = move;
return move;
}
int AIPlayer::testNeighbours(int target, int size, char map[]){
/*
So... the way we're going to run this...
Take the target, scale it back to the start of it's horiz, vert, and diag lines (if rlvnt)
and run a test with testSequence which will check each element. If the whole path is clear
it's worth trying to fill up, so go for it.
Otherwise return a -1 and wait for a different target value.
*/
int chosen;
int vert = target;
while (vert >= size) vert -= size;
chosen = testSequence(vert, size, size, map);
if (chosen != -1) return chosen;
int horiz = target;
while (horiz % size != 0) horiz -= 1;
chosen = testSequence(horiz, 1, size, map);
if (chosen != -1) return chosen;
int diag = target;
if (target % (size+1) == 0){ //top-left to btm-right
while (diag != 0) diag -= size+1;
chosen = testSequence(diag, size+1, size, map);
if (chosen != -1) return chosen;
}
diag = target;
if (target % (size-1) == 0){ //top-right to btm-left
while (diag != (size-1)) diag -= size-1;
chosen = testSequence(diag, size-1, size, map);
if (chosen != -1) return chosen;
}
return -1;
}
int AIPlayer::testSequence(int start, int step, int size, char map[]){
int lfree;
for (int i=start;i<start+(size*step);i+=step){
if ( isFree(map, i) ) {lfree=i;continue;}
if ( map[i] != _thischar ) return -1;
}
return lfree;
}
[/CODE]

And to quote my own readme...

On it's first move it merely checks the "highly desirable" squares in the middle and corners, taking the first available. Subsequently, it uses it's previous move to decide it's next by checking the vertical, horizontal and diagonal (if applicable) paths which cross through the previous move. If it finds an opponents piece in one of these paths it is discounted as being "unwinnable", and it moves on to the next checking direction. If all directions are obstructed it picks a random cell and starts again from there. It is therefore trivial to beat the AI by forming your own line parallel to the one it's creating - it all just depends who makes the first move!

You can download my full code here: http://nerdshack.info/?p=730

  • 0

You are doing a lot of potentially dodgy string comparisons in your code.


// This is bad
char const * a = "a";
char const * b = "a";
bool eq1 = a == b;

// Slightly better
bool eq1 = strcmp(a, b) == 0;

// This is much better
std::string a = "a", b = "a";
bool eq2 = a == b;
[/CODE]

The C string comparisons will work if your compiler stores duplicate C strings in the same location in memory (which MSVC does do).

However, you cannot rely on this behaviour. So either use std::string to store the strings and make use of its equality operator,

or use strncmp on the two character arrays.

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

    • No registered users viewing this page.
  • Posts

    • Removed the blue and underline as you did not post a link. This would also  be considered spamming.
    • Why it's almost impossible to produce a smartphone in the United States by Hamid Ganji If you look at the back of some Apple products, you can see the famous phrase “Designed by Apple in California, Assembled in China.” This phrase appears on products from one of the largest smartphone brands in the United States. These products are designed in the U.S., but their manufacturing takes place in China, India, Vietnam, or even Brazil. But why can’t Apple, as one of the largest American tech companies, produce its iPhones on U.S. soil? The idea for this topic came to me after the Trump Foundation launched a smartphone called the T1 and claimed that it was designed and built with American values in mind. However, this claim did not last long, as it was revealed that Trump’s phone was actually a rebranded HTC U24 Pro, with only a gold case and minor internal component changes. You see? Even a phone that is supposed to represent American values is manufactured in China. With a gross domestic product (GDP) exceeding $32 trillion, the United States is currently the world’s largest economy, while China ranks second with around $20 trillion. On the other hand, the United States is by a wide margin the global leader in various technological fields, and American companies spend hundreds of billions of dollars annually on research and development. From Apple and Google to Microsoft, Lockheed Martin, Boeing, and others, American tech and industrial giants lead their foreign competitors in many sectors. The United States also has no shortage of smartphone brands. Apple, Google, and Motorola are among the major brands in the smartphone market, collectively holding a significant share. However, the vast majority of their products are manufactured outside the United States. So why is it that the world’s largest economy, home to the most advanced technology companies and industrial powers, cannot produce a smartphone on its own soil? Let’s explore this question together. Even threats to impose tariffs won’t work After Trump entered the White House as the 47th President of the United States, his administration adopted strict tariff policies. One of these policies was the imposition of a 25% tariff on smartphones manufactured outside the United States. Trump said he “had a little problem” with Apple CEO Tim Cook over producing smartphones outside the U.S. So he thought that threatening a 25% tax on imported phones might force Apple to bring manufacturing back to the United States. “I have long ago informed Tim Cook of Apple that I expect their iPhones that will be sold in the United States of America will be manufactured and built in the United States, not India, or anyplace else,” Trump wrote on Truth Social. Image via The White House Although Apple currently manufactures some of the iPhone’s chips in the United States with TSMC's help, it still shows no willingness to shift full iPhone production to the country. At the time, renowned Apple supply chain analyst Ming-Chi Kuo wrote on X, “In terms of profitability, it’s way better for Apple to take the hit of a 25% tariff on iPhones sold in the US market than to move iPhone assembly lines back to the US.” However, manufacturing a smartphone in the United States is not as easy as it might seem, and many technical and economic barriers are involved. The lack of necessary manufacturing hubs There is a clear reason why many companies prefer to manufacture their products in China. China has established itself as the main global manufacturing hub for international companies, and over the past few decades, large contract manufacturers have emerged there, allowing companies like Apple to outsource production. One such example is Foxconn, which also manufactures some Apple products in India. Building the infrastructure required to produce smartphones in the United States would require tens of billions of dollars in new investment. Factories would need to be built, essential manufacturing equipment would have to be installed, and, most importantly, a skilled workforce capable of operating these systems would need to be recruited and trained. The United States currently lacks the core infrastructure needed to manufacture smartphones, and for this reason, many companies prefer to outsource production to Chinese contractors rather than spend tens of billions of dollars to build that infrastructure, which is significantly more economically efficient. Additionally, building such infrastructure in the United States could take up to a decade, ultimately leading to a significant increase in the product's final price for consumers. Shortage of trained labor in the U.S. compared to China Decades of serving as a global manufacturing hub have allowed China to build a massive talent pool in the production sector that is almost unmatched worldwide. Today, if a company chooses to manufacture its products in China, it can be confident that the workers involved in production have years of experience in their respective roles and are capable of producing high-quality goods with minimal errors. Even if we assume that tens of billions of dollars were invested in building smartphone manufacturing infrastructure in the United States, finding skilled workers would remain highly challenging. Apple CEO Tim Cook visiting the iPhone 6 assembly line in China in 2014. Image: Tim Cook on X In a 2015 interview on CBS’s 60 Minutes, Tim Cook said the main reason Apple isn’t producing in the US is a lack of skills. "China put an enormous focus on manufacturing, in what you and I would call vocational kind of skills. The US over time began to stop having as many vocational kinds of skills. I mean you could take every tool and die maker in the United States and probably put them in the room that we're currently sitting in. In China you would have to have multiple football fields,” Cook said. Also, in 2017, at the Fortune Global Forum in Guangzhou, Cook once again emphasized the importance of highly skilled Chinese workers. “China has moved into very advanced manufacturing, so you find in China the intersection of craftsman kind of skill, and sophisticated robotics and the computer science world. That intersection, which is very rare to find anywhere, that kind of skill, is very important to our business because of the precision and quality level that we like. The thing that most people focus on if they’re a foreigner coming to China is the size of the market, and obviously, it’s the biggest market in the world in so many areas. But for us, the number one attraction is the quality of the people,” Apple CEO said. Higher labor costs in the United States Producing almost any product in the United States is more expensive than in many other countries, and one of the main reasons is the higher cost of labor in the U.S. According to the Bureau of Labor Statistics, median weekly earnings of full-time workers in the United States were $1,235 in the first quarter of 2026. Meanwhile, the average annual salary in China's private sector in 2025 was RMB 71,590 (US$9,961). In many parts of the world, the weekly wage of an American worker is equivalent to several months of income. Another important factor to consider is that in the United States, the workforce capable of working on a smartphone assembly line is highly specialized and therefore commands higher-than-average wages. According to an estimate by Bank of America, producing an iPhone in the U.S. is technically possible, but “iPhone cost can increase 25% purely on higher labor cost in the U.S.” However, this 25% increase applies only if final assembly is performed in the United States while components are still sourced from China or elsewhere. In this case, the price of a base iPhone would rise from $799 to around $1,000. But in another scenario, if Apple were to produce the required components for the iPhone within the United States, production costs could increase by more than 90%. Trump’s dream for a “Made in the USA” iPhone might never come true In a free-market capitalist economy, one of the primary responsibilities of any CEO is to maximize profit. Using Apple as an example, Tim Cook’s role is to maximize the company’s profits so that it can fund research and development for new products and invest in areas such as artificial intelligence, while also keeping shareholders satisfied. Therefore, it is entirely understandable that Apple would choose not to bring its manufacturing back to the United States and instead keep production in countries where labor is cheaper, and products can be manufactured at a lower cost, thereby maximizing its profit margins. What is your opinion about manufacturing smartphones in the United States? If you are an American citizen, would you be willing to pay hundreds of dollars more for a smartphone made domestically in the USA? Let us know in the comments.
    • Cheers everyone for the replies. It's been very useful. 👍
  • Recent Achievements

    • Conversation Starter
      jessse3334 earned a badge
      Conversation Starter
    • Reacting Well
      JuvenileDelinquent earned a badge
      Reacting Well
    • One Month Later
      Excellence2025 earned a badge
      One Month Later
    • Week One Done
      Excellence2025 earned a badge
      Week One Done
    • Week One Done
      flexorcist earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      505
    2. 2
      +Edouard
      195
    3. 3
      PsYcHoKiLLa
      153
    4. 4
      Steven P.
      71
    5. 5
      FloatingFatMan
      66
  • Tell a friend

    Love Neowin? Tell a friend!