Helpmeplz

Hey guys there is some code that i need to finish my program, well at least the easy way, but I dont understand it. Im writting a tic tac toe program where the computer must never be beaten. This code is supposed to find how many x's there are in a row. But I dont know how to impliment it. Can any one help. Here it is.

Basically im trying to write a algorithm that will check all the differant squares for an 'x'. If there are 2 x's in a row, colum, or dialgnal then move to the square to block it. Thats all i need. After that I know how to program the win. Any ideas?

Originally posted by master5001 <snip>Perfect AI on a game like tic-tac-toe is impossible.

Wow...that's saying quite a bit. I guess it'd depend on what you call 'perfect AI', but I'd sure hesitate a lot before I said it was impossible.

If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

The only way I know to do it is to just check each possible combination. There are eight ways to win. I suppose you could make an algorithm by checking each perimeter square and checking it's two immediate neighbors, one horizontal, one vertical, and only check the third if a match is made in the first check, and add a similar diagonal check at the corner, but that would be more work. Might be useful on a larger grid board, 10 x 10 or so.

I agree...this was an assignment I had for one of my classes. Programming it so that the computer never loses isn't too hard; I doubt you could program it so that the computer always wins. Maybe that's the "perfect AI"?

If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

That would have made an interesting end to 'War Games'.
"Sir, the computer just launched a full first strike package at the Russians!"
"My god, but why?"
"Well, the kid was playing Tic-Tac-Toe, trying to prove that with two smart opponents, in some games, there can be no winnerr; the computer decided in order for there to be a winner, one player has to not finish."

Demonographic rhinology is not the only possible outcome, but why take the chance