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...AItictactoe.exe

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.

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

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.

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 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 yaddabreak;

case BUTTON2:yadda yaddabreak;

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.

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.

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

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!

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.