done
This commit is contained in:
64
Game.cs
64
Game.cs
@@ -58,26 +58,59 @@ namespace TicTacToe
|
|||||||
SetInputEnabled(false);
|
SetInputEnabled(false);
|
||||||
|
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
Box box;
|
|
||||||
bool[] boxCache = new bool[9];
|
|
||||||
|
|
||||||
while (true)
|
var empty = boxes.Where(b => !b.Filled).ToList();
|
||||||
|
|
||||||
|
// try to win
|
||||||
|
foreach (Box b in empty)
|
||||||
{
|
{
|
||||||
int randomIndex = random.Next(0, 8);
|
b.BotOwned = true;
|
||||||
box = boxes[randomIndex];
|
b.Filled = true;
|
||||||
boxCache[randomIndex] = true;
|
|
||||||
if (!box.Filled)
|
if (CheckWins(true))
|
||||||
{
|
{
|
||||||
break;
|
b.PlayerClaim(true);
|
||||||
|
RoundEnded();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b.BotOwned = false;
|
||||||
|
b.Filled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// try to block
|
||||||
|
foreach (Box b in empty)
|
||||||
|
{
|
||||||
|
b.BotOwned = false;
|
||||||
|
b.Filled = true;
|
||||||
|
|
||||||
|
if (CheckWins(false))
|
||||||
|
{
|
||||||
|
b.PlayerClaim(true);
|
||||||
|
RoundEnded();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Filled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// pick middle
|
||||||
|
if (!boxes[4].Filled)
|
||||||
|
{
|
||||||
|
boxes[4].PlayerClaim(true);
|
||||||
|
RoundEnded();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// else pick random
|
||||||
|
int index = random.Next(0, empty.Count);
|
||||||
|
Box box = empty[index];
|
||||||
box.PlayerClaim(true);
|
box.PlayerClaim(true);
|
||||||
|
|
||||||
RoundEnded();
|
RoundEnded();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CheckWins()
|
public bool CheckWins(bool bot) // check if bot won, or player if bot == true
|
||||||
{
|
{
|
||||||
int[][] lines =
|
int[][] lines =
|
||||||
{
|
{
|
||||||
@@ -96,9 +129,9 @@ namespace TicTacToe
|
|||||||
if (!boxes[line[0]].Filled || !boxes[line[1]].Filled || !boxes[line[2]].Filled)
|
if (!boxes[line[0]].Filled || !boxes[line[1]].Filled || !boxes[line[2]].Filled)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (boxes[line[0]].BotOwned == BotRound &&
|
if (boxes[line[0]].BotOwned == bot &&
|
||||||
boxes[line[1]].BotOwned == BotRound &&
|
boxes[line[1]].BotOwned == bot &&
|
||||||
boxes[line[2]].BotOwned == BotRound)
|
boxes[line[2]].BotOwned == bot)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -112,14 +145,9 @@ namespace TicTacToe
|
|||||||
SetInputEnabled(true);
|
SetInputEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CheckWin()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RoundEnded()
|
public void RoundEnded()
|
||||||
{
|
{
|
||||||
if (CheckWins())
|
if (CheckWins(BotRound))
|
||||||
{
|
{
|
||||||
if (BotRound)
|
if (BotRound)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user