|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication2
{
class ZeroCross
{
public bool winner_comp = false;
public bool winner_you = false;
public bool tie = false;
private int[] zeros = { 0, 0, 0, 0, 0 };
private int[] crosses = { 0, 0, 0, 0, 0 };
private int countero;
private int counterx;
public int MakeAMove(int MyMove)
{
zeros[countero] = MyMove;
countero++;
if (counterx == 0)
{
int rand_move = RandomMove();
crosses[counterx] = rand_move;
counterx++;
return rand_move;
}
else
{
int[,] Pattern = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 1, 4, 7 }, { 2, 5, 8 }, { 3, 6, 9 }, { 1, 5, 9 }, { 7, 5, 3 } };
int Compmove = 0;
int FinalMove = 0;
int winning_Move = 0;
for (int i = 0; i < 8; i++)
{
int oCounter = 0;
int xCounter = 0;
int Incase = 0;
for (int j = 0; j < 3; j++)
{
if (CrossSeek(Pattern[i, j]))
{
xCounter++;
}
else if (ZeroSeek(Pattern[i, j]))
{
oCounter++;
}
else
{
Incase = Pattern[i, j];
}
}
if (oCounter == 3)
{
winner_you = true;
return 0;
}
else if (xCounter == 2 && Incase != 0)
{
winning_Move = Incase;
}
else if (oCounter == 2 && Incase != 0)
{
Compmove = Incase;
}
else if (oCounter == 0 && xCounter == 1)
{
if (Compmove == 0)
{
Compmove = Incase;
}
}
else if (Incase != 0)
{
FinalMove = Incase;
}
}
if (winning_Move != 0)
{
crosses[counterx] = winning_Move;
counterx++;
winner_comp = true;
return winning_Move;
}
if (Compmove != 0)
{
crosses[counterx] = Compmove;
counterx++;
if (counterx == 5 || countero == 5)
{
winner_you = false;
winner_comp = false;
tie = true;
}
return Compmove;
}
if (FinalMove != 0)
{
crosses[counterx] = FinalMove;
counterx++;
if (counterx == 5 || countero == 5)
{
tie = true;
}
return FinalMove;
}
tie = true;
return 0;
}
}
public int CompFirstTurn()
{
int CompMove = RandomMove();
crosses[counterx] = CompMove;
counterx++;
return CompMove;
}
public bool GetWinner()
{
if (winner_comp || winner_you || tie)
{
return true;
}
return false;
}
public bool ZeroSeek(int zero)
{
for (int i = 0; i < countero; i++)
{
if (zeros[i] == zero)
{
return true;
}
}
return false;
}
public bool CrossSeek(int cross)
{
for (int i = 0; i < counterx; i++)
{
if (crosses[i] == cross)
{
return true;
}
}
return false;
}
private int RandomMove()
{
Random random = new Random();
int CompMove;
bool truth = false;
do
{
CompMove = random.Next(1, 10);
if (zeros[0] == CompMove || crosses[0] == CompMove)
{
truth = true;
}
else
{
truth = false;
}
}
while (truth);
return CompMove;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.