3

I am a university student (1st year) and I have an assignment to make a Tic Tac Toe console game using the ACM Library. I managed to make it working Player VS Player. Another part of this assignment requires me to make it work like Player VS Computer. The teacher told us not to create an AI but to make the computer ALWAYS win or tie. How can I implement this? I don't think that it is possible to implement that using a lot of if's and else's. Is there any smarter way?

I've created one constructor called Board which has all the methods needed to create, display, update the game board, to check if X's or O's win (or tie), and to check the string that the user inputs (In order to play user must type something like (row_space_column ---> example: "3 1")). I also created another file ("TicTacToe.java") which is the game itself.

So, what are your opinions? (The language i use is JAVA) (Sorry for my English, i am Greek) Thanks!

Mikescher
  • 875
  • 2
  • 16
  • 35
kostas7gr
  • 33
  • 5
  • 1
    ```I don't think that it is possible to implement that using a lot of if's and else's``` It is possible. Tic tac toe is a primitive game(I woulnd't call it a game because the non-losing strategy is). The easiest option would be to "study" a non-losing strategy and then create a lookup table with the moves you should do. Like empty board and opponent's piece in the middle -> make a move in the corner. After that you only have to check if opponent has two in a row. Check this: http://www.wikihow.com/Win-at-Tic-Tac-Toe – NeplatnyUdaj Feb 04 '14 at 11:49
  • "The teacher told us not to create an AI but to make the computer ALWAYS win or tie". AI is not a "component" but a full discipline like Mathematics. So you should define what do you intend with "create an AI", specifying what is allowed and what is not allowed. – HAL9000 Feb 04 '14 at 11:50
  • @NeplatnyUdaj Tic-Tac-Toe IS a game. http://en.wikipedia.org/wiki/Game_theory – HAL9000 Feb 04 '14 at 11:53
  • Relevant xkcd: http://xkcd.com/832/ – Michael Madsen Feb 04 '14 at 11:57
  • @MichaelMadsen I would like to be able to vote on comments ! – Ricardo Rivaldo Feb 04 '14 at 12:41

2 Answers2

1

Tic-Tac-Toe is a nice example for AI programming because there are only a relative small number of possible moves.

Also if you play perfectly (what your AI hopefully does) you will always win or make a tie.

You can quite easily test every possible move until someone wins - and then you take the optimal move.

Perhaps you should take a look at the MinMax Algorithm

There are also a lot of example Implementations of Tic-Tac-Toe Algorithms. (If you want i could give you one in Pascal :D )

Edit: I found a really nice tutorial on making a Tic-Tac-Toe AI.

Mikescher
  • 875
  • 2
  • 16
  • 35
1

Premise: each algorithm that solves a generic problem by adapting its behavior to the specific instance of the problem in order to maximizes the chances of success is considered an intelligent agent, even if it's composed by one single if statement.

Given this, your assignment is not well defined, anyway, I try to give you some hints:

Community
  • 1
  • 1
HAL9000
  • 3,562
  • 3
  • 25
  • 47