Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If(), else if() alternative in c++(Is this AI?)

First off, I am a noob. I am also a Janitor that has never made a dime writing code. This is just something that I love doing. It is for fun:) That being said, I wrote this console based tic-tak-toe game that has enough ai to not lose every game. (I guess ai is what it should be called.) It has something like 70 if/else if statements for the computers turn. I used 3 int arrays like so:

int L[2], M[2], R[2];

0 = blank; 1 = X; 2 = O;
The board then 'Looks' like
L[0] | M[0] | R[0]
L[1] | M[1] | R[1]
L[2] | M[2] | R[2]

So I basically wrote out every possible scenario I could think something like:

if(M[0]==1 & M[1]==1 & M[2]==0){M[2] = 2;}//here the computer prevents a win 
else if(L[0] ==2&M[1]==2&R[2]==0){R[2]=2;}//here the computer wins
//and so on....68 more times!

I guess my question(s) is(are):
Is there a better way?
Is there a way to achieve the same result with less lines of code?
Is this considered Artificial Intelligence?

like image 383
Nick P. Avatar asked Apr 22 '11 21:04

Nick P.


People also ask

Is IF ELSE statement an AI?

So a simple if/else is an intelligent agent considered as AI by the pioneers of AI whose tremendous work & years of research been open sourced resulted in building complex intelligent agents faster and simpler.

What can I use instead of if else in C?

One of my favourite alternatives to if...else is the ternary operator. Here expressionIfTrue will be evaluated if condition evaluates to true ; otherwise expressionIfFalse will be evaluated. The beauty of ternary operators is they can be used on the right-hand side of an assignment.


2 Answers

The Wikipedia page on Tic-Tac-Toe has a very good algorithm outline for winning (or tying) every game: http://en.wikipedia.org/wiki/Tic-tac-toe which is what I used to make a Tic-Tac-Toe game several years ago.

After you understand the algorithm, one of the cleverest ways to implement a Tic-Tac-Toe computer player is with a magic square. The method is discussed here. As far as size goes, I've seen this implemented in about 50 lines of code, I'll post the code if I find it :)

This isn't technically artificial intelligence, as AI usually refers to artificial neurons, neuron layers, gradient descent, support vector machines, solving complex polynomials, and the like. Solving Tic-Tac-Toe

like image 37
David Titarenco Avatar answered Sep 21 '22 23:09

David Titarenco


The standard algorithm for this is called Minimax. It basically builds a tree, where the beginning of the game is the root, and then the children represent every possible move X can make on the first turn, then the children of each of those nodes are all the moves O can make in response, etc. Once the entire tree is filled (which is possible for Tic-Tac-Toe, but for games like Chess computers still don't have enough memory), you work your way back up, assuming both players are smart enough to make the best move, and arrive at the optimal move. Here is another explanation of Minimax specifically using Tic Tac Toe as an example.

like image 161
Colin Avatar answered Sep 17 '22 23:09

Colin