Files
TicTacToe/Game.cs
maxwes08 3fe7224aaf done
2025-10-15 09:07:52 +02:00

205 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace TicTacToe
{
public class Game
{
Form1 form;
Box[] boxes = new Box[9];
bool BotRound = false;
public Game()
{
form = Program.form;
boxes[0] = new Box(form.grid_0, 0); boxes[0].Pressed += ButtonClicked;
boxes[1] = new Box(form.grid_1, 1); boxes[1].Pressed += ButtonClicked;
boxes[2] = new Box(form.grid_2, 2); boxes[2].Pressed += ButtonClicked;
boxes[3] = new Box(form.grid_3, 3); boxes[3].Pressed += ButtonClicked;
boxes[4] = new Box(form.grid_4, 4); boxes[4].Pressed += ButtonClicked;
boxes[5] = new Box(form.grid_5, 5); boxes[5].Pressed += ButtonClicked;
boxes[6] = new Box(form.grid_6, 6); boxes[6].Pressed += ButtonClicked;
boxes[7] = new Box(form.grid_7, 7); boxes[7].Pressed += ButtonClicked;
boxes[8] = new Box(form.grid_8, 8); boxes[8].Pressed += ButtonClicked;
}
public void Start()
{
Round();
}
public void ButtonClicked(int index)
{
Box box = boxes[index];
if (box.Filled) return;
box.PlayerClaim(BotRound);
RoundEnded();
}
public void Round()
{
if (BotRound)
{
BotTurn();
}
else
{
PlayerTurn();
}
}
public void BotTurn()
{
SetInputEnabled(false);
Random random = new Random();
var empty = boxes.Where(b => !b.Filled).ToList();
// try to win
foreach (Box b in empty)
{
b.BotOwned = true;
b.Filled = true;
if (CheckWins(true))
{
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(bool bot) // check if bot won, or player if bot == true
{
int[][] lines =
{
new[] {0, 1, 2},
new[] {3, 4, 5},
new[] {6, 7, 8},
new[] {0, 3, 6},
new[] {1, 4, 7},
new[] {2, 5, 8},
new[] {0, 4, 8},
new[] {2, 4, 6}
};
foreach (var line in lines)
{
if (!boxes[line[0]].Filled || !boxes[line[1]].Filled || !boxes[line[2]].Filled)
continue;
if (boxes[line[0]].BotOwned == bot &&
boxes[line[1]].BotOwned == bot &&
boxes[line[2]].BotOwned == bot)
{
return true;
}
}
return false;
}
public void PlayerTurn()
{
SetInputEnabled(true);
}
public void RoundEnded()
{
if (CheckWins(BotRound))
{
if (BotRound)
{
MessageBox.Show("You lost");
Program.AddLose();
}
else
{
MessageBox.Show("You won");
Program.AddWin();
}
return;
}
BotRound = !BotRound;
if (IsAllBoxesFilled())
{
MessageBox.Show("Tie");
}
else
{
Round();
}
}
public bool IsAllBoxesFilled()
{
foreach (Box box in boxes)
{
if (!box.Filled)
return false;
}
return true;
}
public void SetInputEnabled(bool value)
{
foreach (Box box in boxes)
{
box.SetEnabled(value);
}
}
public void Destroy()
{
foreach (Box box in boxes)
{
box.Destroy();
}
}
}
}