Novak
Novak

Reputation: 2768

C# Tic Tac Toe Console App

I created a simple Tic Tac Toe game in c#, which can played in 1 players mode, and in 2 player mode.

I programmed the computer to react correctly (in the computer vs. player mode), but I think that my code is much bigger than it should be since what I did is that I checked every possible combination manually, and can't seems to find how to minimize the code.

Here is the makeComputerMove() func: https://gist.github.com/2192374

Explanation of the variables:

char[,] Board: 2D array, contains the 'X' and 'O'

char shape: that's the shape I'm using to determine the priority move, first I set it to 'O' so that the computer will try to win. If it can't win, it will try to block. If there is nothing to block, it will try to set a 'O' near another 'O' on the board.

Position P: an object that contains the values Row and Col.

Thanks.

EDIT: Now I see that people are thinking I want you to rewrite my code. I just added it so you could see what I did. I'm only asking for tips (by approaching the problem in a different way).

Upvotes: 0

Views: 3539

Answers (1)

YoryeNathan
YoryeNathan

Reputation: 14502

Read about MinMax algorithms here to get the general idea. It's basically backtracking through possible moves, and foreach possible move to all possible moves, and so in, into a certain number of moves (ai depth). This uses recursion. Since this is TicTacToe, you can lose the "depth" and have "end" as your stop condition.

Upvotes: 1

Related Questions