This commit is contained in:
maxwes08
2025-10-15 09:07:52 +02:00
parent 6662b92891
commit 3fe7224aaf

64
Game.cs
View File

@@ -58,26 +58,59 @@ namespace TicTacToe
SetInputEnabled(false);
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);
box = boxes[randomIndex];
boxCache[randomIndex] = true;
if (!box.Filled)
b.BotOwned = true;
b.Filled = true;
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);
RoundEnded();
}
public bool CheckWins()
public bool CheckWins(bool bot) // check if bot won, or player if bot == true
{
int[][] lines =
{
@@ -96,9 +129,9 @@ namespace TicTacToe
if (!boxes[line[0]].Filled || !boxes[line[1]].Filled || !boxes[line[2]].Filled)
continue;
if (boxes[line[0]].BotOwned == BotRound &&
boxes[line[1]].BotOwned == BotRound &&
boxes[line[2]].BotOwned == BotRound)
if (boxes[line[0]].BotOwned == bot &&
boxes[line[1]].BotOwned == bot &&
boxes[line[2]].BotOwned == bot)
{
return true;
}
@@ -112,14 +145,9 @@ namespace TicTacToe
SetInputEnabled(true);
}
public void CheckWin()
{
}
public void RoundEnded()
{
if (CheckWins())
if (CheckWins(BotRound))
{
if (BotRound)
{