קוד:
using System;
public class ticTacToeClass
{
private int[,] gameMatrix;
public ticTacToeClass()
{
gameMatrix = new int[3,3];
int i, j;
for (i = 0; i < gameMatrix.GetLength(0); i++)
for (j = 0; j < gameMatrix.GetLength(1); j++)
gameMatrix[i, j] = 0;
}
public int whoWins()
{
Boolean rowWin;
Boolean columnWin;
Boolean primaryDiagonal = (gameMatrix[0,0] != 0);
Boolean secondaryDiagonal = (gameMatrix[0,gameMatrix.GetLength(1)-1] != 0);
int i, j;
for (i = 0; i < gameMatrix.GetLength(0); i++)
{
rowWin = (gameMatrix[i,0] != 0);
columnWin = (gameMatrix[0,i] != 0);
if (gameMatrix[0, 0] != gameMatrix[i, i])
primaryDiagonal = false;
if (gameMatrix[0, gameMatrix.GetLength(1) - 1] != gameMatrix[i, gameMatrix.GetLength(1) - 1 - i])
secondaryDiagonal = false;
for (j = 1; j < gameMatrix.GetLength(1); j++)
{
if (gameMatrix[i, 0] != gameMatrix[i, j])
rowWin = false;
if (gameMatrix[0, i] != gameMatrix[j, i])
columnWin = false;
}
if (rowWin)
return gameMatrix[i, 0];
if (columnWin)
return gameMatrix[0, i];
}
if (primaryDiagonal)
return gameMatrix[0, 0];
else if (secondaryDiagonal)
return gameMatrix[0, gameMatrix.GetLength(1) - 1];
if (NumEmptyPlaces() == 0)
return 3;
return 0;
}
public Boolean write(int row, int column, int player)
{
if (row >= gameMatrix.GetLength(0) || column >= gameMatrix.GetLength(1))
return false;
if (gameMatrix[row, column] == 0)
{
gameMatrix[row, column] = player;
return true;
}
return false;
}
public void randomWrite(int player)
{
int[,] randomOptions = new int[NumEmptyPlaces(), 2];
int count = 0, i, j;
int currentOption;
Random randomMachine = new Random();
for (i = 0; i < gameMatrix.GetLength(0); i++)
for (j = 0; j < gameMatrix.GetLength(1); j++)
if (gameMatrix[i, j] == 0)
{
randomOptions[count, 0] = i;
randomOptions[count, 1] = j;
count++;
}
currentOption = randomMachine.Next(0, count - 1);
write(randomOptions[currentOption, 0], randomOptions[currentOption, 1], player);
}
private int NumEmptyPlaces()
{
int i, j, count = 0;
for (i = 0; i < gameMatrix.GetLength(0); i++)
for (j = 0; j < gameMatrix.GetLength(1); j++)
if (gameMatrix[i, j] == 0)
count++;
return count;
}
public void drawGame()
{
Console.Clear();
Char[] chars = new Char[3];
int i, j;
chars[0] = ' ';
chars[1] = 'X';
chars[2] = 'O';
for (i = 0; i < gameMatrix.GetLength(0); i++)
{
Console.Write("{0}", chars[gameMatrix[i, 0]]);
for (j = 1; j < gameMatrix.GetLength(1); j++)
Console.Write("|{0}", chars[gameMatrix[i, j]]);
Console.WriteLine();
for (j = 0; j < gameMatrix.GetLength(0) * 2 - 1; j++)
Console.Write("-");
Console.WriteLine();
}
}
}
public class myProject
{
private static string szSeperator = "------------";
public static int Main(string[] args)
{
ConsoleKeyInfo mainCh;
Console.Clear();
Console.SetWindowSize(40, 15);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Tic Tac Toe");
Console.WriteLine(myProject.szSeperator);
Console.WriteLine("1. Start a new game");
Console.WriteLine("2. Exit");
do
{
mainCh = Console.ReadKey();
}
while (mainCh.KeyChar != '1' && mainCh.KeyChar != '2');
if (mainCh.KeyChar == '1')
game();
return 0;
}
public static void game()
{
ConsoleKeyInfo myChar;
int row,column;
Console.Clear();
ticTacToeClass ticTacToe = new ticTacToeClass();
ticTacToe.drawGame();
while (ticTacToe.whoWins() == 0)
{
do
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
Console.WriteLine("Enter Row,Column (Char After Char) => ");
myChar = Console.ReadKey();
row = int.Parse(myChar.KeyChar.ToString()) - 1;
myChar = Console.ReadKey();
column = int.Parse(myChar.KeyChar.ToString()) - 1;
}
while (!ticTacToe.write(row,column,1));
ticTacToe.drawGame();
if (ticTacToe.whoWins() != 0)
break;
ticTacToe.randomWrite(2);
ticTacToe.drawGame();
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
Console.WriteLine("Player {0} Is The Winner!", ticTacToe.whoWins());
Console.WriteLine("Press Any Key To Continue.");
Console.ReadKey();
Main(new string[0]);
}
}