bool playing = true;
public Form1()
{
InitializeComponent();
}
private Button[] Cells => new Button[]
{
//top row
button1,
button2,
button3,
//middle row
button4,
button5,
button6,
//bottom row
button7,
button8,
button9
};
private bool IsBoardFull
{
get
{
bool result = true;
foreach (Button btn in Cells)
{
if (btn.Enabled) { result = false; break; }
}
return result;
}
}
//This tells our CheckForWinner if their is a winner
private bool IsThreeInRow
{
get
{
//rows
if (Cells[0].Text == "X" && Cells[1].Text == "X" && Cells[2].Text == "X" ||
Cells[0].Text == "O" && Cells[1].Text == "O" && Cells[2].Text == "O" ||
Cells[3].Text == "X" && Cells[4].Text == "X" && Cells[5].Text == "X" ||
Cells[3].Text == "O" && Cells[4].Text == "O" && Cells[5].Text == "O" ||
Cells[6].Text == "X" && Cells[7].Text == "X" && Cells[8].Text == "X" ||
Cells[6].Text == "O" && Cells[7].Text == "O" && Cells[8].Text == "O" ||
//columns
Cells[0].Text == "X" && Cells[3].Text == "X" && Cells[6].Text == "X" ||
Cells[0].Text == "O" && Cells[3].Text == "O" && Cells[6].Text == "O" ||
Cells[1].Text == "X" && Cells[4].Text == "X" && Cells[7].Text == "X" ||
Cells[1].Text == "O" && Cells[4].Text == "O" && Cells[7].Text == "O" ||
Cells[2].Text == "X" && Cells[5].Text == "X" && Cells[8].Text == "X" ||
Cells[2].Text == "O" && Cells[5].Text == "O" && Cells[8].Text == "O" ||
//Diagional
Cells[0].Text == "X" && Cells[4].Text == "X" && Cells[8].Text == "X" ||
Cells[0].Text == "O" && Cells[4].Text == "O" && Cells[8].Text == "O" ||
Cells[2].Text == "X" && Cells[4].Text == "X" && Cells[6].Text == "X" ||
Cells[2].Text == "O" && Cells[4].Text == "O" && Cells[6].Text == "O")
return true;
return false;
}
}
//This is for the computer play. It just returns all
//the possible plays that are currently available. It
//chooses one of these at random, better computer play
//logic should be thought out.
private IEnumerable PossibleMoves()
{
return Cells.Where((button) => button.Enabled);
}
private void CheckForWinner()
{
string draw = "Sorry, it was a draw.";
string playAgain = "Another Game?";
string sorry = "Sorry, you los