complete, step before doing ai
This commit is contained in:
176
Game.cs
Normal file
176
Game.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
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();
|
||||
Box box;
|
||||
bool[] boxCache = new bool[9];
|
||||
|
||||
while (true)
|
||||
{
|
||||
int randomIndex = random.Next(0, 8);
|
||||
box = boxes[randomIndex];
|
||||
boxCache[randomIndex] = true;
|
||||
if (!box.Filled)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
box.PlayerClaim(true);
|
||||
|
||||
RoundEnded();
|
||||
}
|
||||
|
||||
public bool CheckWins()
|
||||
{
|
||||
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 == BotRound &&
|
||||
boxes[line[1]].BotOwned == BotRound &&
|
||||
boxes[line[2]].BotOwned == BotRound)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void PlayerTurn()
|
||||
{
|
||||
SetInputEnabled(true);
|
||||
}
|
||||
|
||||
public void CheckWin()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void RoundEnded()
|
||||
{
|
||||
if (CheckWins())
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user