I was designing the strategy for Tic Tac Toe:
CHOOSE THE MOVE WITH THE HIGHEST PRIORITY -
if you have 2 in a row, get 3 in a row;
if the opponent has 2 in a row, get 3 to block him;
play the center;
if the opponent is in the corner, get the opposite corner;
get an empty corner;
get an empty side.

Sure, there are a lot of repetitive tasks.

After coding the first 3 conditions for COPUTER moves I've notice it never executes:

``````if (opponentHuman == false && i % 2 != 0 )
{
if (array2[0] == 'X' && array2[1] == 'X' && array2[2] == 3)
{
Console.SetCursorPosition(11, 2);
Console.WriteLine("O");
array2[2] = 'O';
}
}
if (opponentHuman == false && i % 2 != 0)
{
if (array2[0] == 'X' && array2[2] == 'X' && array2[1] == 2)
{
Console.SetCursorPosition(7, 2);
Console.WriteLine("O");
array2[1] = 'O';
}
}
if (opponentHuman == false && i % 2 != 0)
{
if (array2[1] == 'X' && array2[2] == 'X' && array2[0] == 1 )
{
Console.SetCursorPosition(3, 2);
Console.WriteLine("O");
array2[i] = 'O';
}
}``````

The program written so far :

``````using System;
using System.Collections.Generic;
using System.Text;

namespace TTT
{
class Program
{
static void Main(string[] args)
{

int i = 0;
int n = 0;

bool opponentHuman = false;

ConsoleKeyInfo opponent = new ConsoleKeyInfo();

string input = "";
bool validInput = false;
bool exitGame = false;
const int ARRAY1_LENGHT = 10;
int[] array1 = new int[ARRAY1_LENGHT];
array1[n] = 0;
char[] array2 = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

Console.SetCursorPosition(2, 3);

Console.WriteLine(" NOUGHTS & CROSSES ");
Console.WriteLine();

Console.WriteLine(" Human v Human(h) or v Computer(c) ? h/c ");

if ((opponent.KeyChar == 'h') || (opponent.KeyChar == 'H'))
opponentHuman = true;
else if ((opponent.KeyChar == 'c') || (opponent.KeyChar == 'C'))
opponentHuman = false;

int[,] board = new int[3, 3];
Console.SetCursorPosition(0, 0);

Console.WriteLine();
Console.WriteLine("    |   |                                           ");
Console.WriteLine("    |   |                                           ");
Console.WriteLine("   1|  2|  3                                        ");
Console.WriteLine("------------                                        ");
Console.WriteLine("    |   |                                           ");
Console.WriteLine("    |   |                                           ");
Console.WriteLine("   4|  5|  6                                        ");
Console.WriteLine("------------                                        ");
Console.WriteLine("    |   |                                           ");
Console.WriteLine("    |   |                                           ");
Console.WriteLine("   7|  8|  9                                        ");
Console.WriteLine();
Console.WriteLine();

while (!exitGame)
{
for (i = 0; i < 9; i++)
{
if (opponentHuman == false)
{
Console.SetCursorPosition(7, 6);
Console.WriteLine("O");
array2[i] = 'O';
}
Console.SetCursorPosition(0, 13);
Console.WriteLine(" Please enter a number from 1 to 9 : ");

while (!validInput)
{

if (!int.TryParse(input, out n) || n < 0 || n > 10)
{
Console.WriteLine("Please enter a valid number(number from 1 to 9)");
}
else
{
if (array1[n] == n)
{
Console.WriteLine(" Please enter only number what hasn't been entered before : ");
}
else
{
validInput = true;
}
}
}

array1[n] = n;
validInput = false;

if (n == 1)
{
Console.SetCursorPosition(3, 2);
}
if (n == 2)
{
Console.SetCursorPosition(7, 2);
}
if (n == 3)
{
Console.SetCursorPosition(11, 2);
}
if (n == 4)
{
Console.SetCursorPosition(3, 6);
}
if (n == 5)
{
Console.SetCursorPosition(7, 6);
}
if (n == 6)
{
Console.SetCursorPosition(11, 6);
}
if (n == 7)
{
Console.SetCursorPosition(3, 10);
}
if (n == 8)
{
Console.SetCursorPosition(7, 10);
}
if (n == 9)
{
Console.SetCursorPosition(11, 10);
}

if (i % 2 == 0)
{
Console.WriteLine("X");
array2[n - 1] = 'X';
}
else
{
Console.WriteLine("0");
array2[n - 1] = '0';
}

if (opponentHuman == false && i % 2 != 0)
{
if (array2[0] == 'X' && array2[1] == 'X' && array2[2] == 3)
{
Console.SetCursorPosition(11, 2);
Console.WriteLine("O");
array2[2] = 'O';
}
}
if (opponentHuman == false && i % 2 != 0)
{
if (array2[0] == 'X' && array2[2] == 'X' && array2[1] == 2)
{
Console.SetCursorPosition(7, 2);
Console.WriteLine("O");
array2[1] = 'O';
}
}
if (opponentHuman == false && i % 2 != 0)
{
if (array2[1] == 'X' && array2[2] == 'X' && array2[0] == 1)
{
Console.SetCursorPosition(3, 2);
Console.WriteLine("O");
array2[i] = 'O';
}
}

if (
(array2[0] == array2[2] && array2[0] == array2[1]) ||
(array2[0] == array2[4] && array2[0] == array2[8]) ||
(array2[0] == array2[3] && array2[0] == array2[6]) ||

(array2[3] == array2[4] && array2[3] == array2[5]) ||
(array2[6] == array2[7] && array2[6] == array2[8]) ||
(array2[2] == array2[4] && array2[2] == array2[6]) ||

(array2[2] == array2[5] && array2[2] == array2[8]) ||
(array2[1] == array2[4] && array2[1] == array2[7])
)
{
exitGame = true;
}

if (exitGame == true)
{
Console.SetCursorPosition(0, 20);
Console.WriteLine("WINNER IS {0}", i % 2 == 0 ? "CROSS X" : "NOUGHT O");
break;
}
}
}
Console.WriteLine("Press any key to exit");
}
}
}``````

What is the way to combat my problem ?

It's a much simpler and better solution just to have a lookup table of all the nonobvious board positions modulo rotation and reflection.

Can you tell me more about this idea ? What do you mean by saying " a lookup table" ? How can I implement it ? Should I use classes ?

Bump because this has piqued my interest. :P

commented: You're the first person on the internet to have spelled "piqued" correctly. +9

Can you tell me more about this idea ? What do you mean by saying " a lookup table" ? How can I implement it ? Should I use classes ?

A lookup table as in something that contains a small set of standard board positions and tells you what to do in those cases.

You can implement it by programming.

You can define your own datatypes if that's appropriate.

Wow - This is extremely clever stuff! Could you add me on skype or msn and teach me some of this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.