2022-11-25 11:30:44 -06:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace TTT
|
|
|
|
|
{
|
|
|
|
|
public partial class Form1 : Form
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int[,] board = new int [3,3];
|
|
|
|
|
Random r = new Random();
|
|
|
|
|
public Form1()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 17:11:02 -06:00
|
|
|
|
private void newGameButton_Click(object sender, EventArgs e)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
{
|
|
|
|
|
populateArray();
|
2022-11-27 17:11:02 -06:00
|
|
|
|
//displayBoard();
|
2022-11-25 11:30:44 -06:00
|
|
|
|
while (determineWinner() == -1)
|
2022-11-27 17:11:02 -06:00
|
|
|
|
{
|
|
|
|
|
//reload table
|
|
|
|
|
resultLabel.Text = "Undetermined";
|
|
|
|
|
populateArray();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (determineWinner() == 0)
|
|
|
|
|
{
|
|
|
|
|
resultLabel.Text = "O Wins!";
|
|
|
|
|
}
|
|
|
|
|
else if (determineWinner() == 1)
|
|
|
|
|
{
|
|
|
|
|
resultLabel.Text = "X Wins!";
|
|
|
|
|
}
|
|
|
|
|
else if (determineWinner() == 2)
|
|
|
|
|
{
|
|
|
|
|
resultLabel.Text = "Draw";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resultLabel.Text = "Undetermined";
|
|
|
|
|
}
|
2022-11-25 11:30:44 -06:00
|
|
|
|
displayBoard();
|
2022-11-27 17:11:02 -06:00
|
|
|
|
|
2022-11-25 11:30:44 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void populateArray()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
|
for (int j = 0; j < 3; ++j)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
this.board[i,j] = (int) r.Next(9999) % 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* returns 0 if "O" is winner
|
|
|
|
|
* 1 if "X" is winner
|
|
|
|
|
* 2 if draw
|
|
|
|
|
* -1 if there exist invalid combinations
|
|
|
|
|
both X's and O's winning
|
|
|
|
|
*/
|
|
|
|
|
int determineWinner()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int winsOfX = 0;
|
|
|
|
|
int winsOfO = 0;
|
|
|
|
|
|
2022-11-27 17:11:02 -06:00
|
|
|
|
//count all wins of X
|
|
|
|
|
// Wins on x axis
|
|
|
|
|
if (this.board[0, 0] == 1 &&
|
|
|
|
|
this.board[0, 1] == 1 &&
|
|
|
|
|
this.board[0, 2] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
winsOfX++;
|
|
|
|
|
|
|
|
|
|
if (this.board[1, 0] == 1 &&
|
2022-11-27 17:11:02 -06:00
|
|
|
|
this.board[1, 1] == 1 &&
|
|
|
|
|
this.board[1, 2] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
winsOfX++;
|
|
|
|
|
|
2022-11-27 17:11:02 -06:00
|
|
|
|
if (this.board[2, 0] == 1 &&
|
|
|
|
|
this.board[2, 1] == 1 &&
|
|
|
|
|
this.board[2, 2] == 1)
|
|
|
|
|
winsOfX++;
|
|
|
|
|
// Count all wins for O
|
|
|
|
|
//wins on y axis
|
|
|
|
|
if (this.board[0, 0] == 0 &&
|
|
|
|
|
this.board[1, 0] == 0 &&
|
|
|
|
|
this.board[2, 0] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
if (this.board[0, 1] == 0 &&
|
|
|
|
|
this.board[1, 1] == 0 &&
|
|
|
|
|
this.board[2, 1] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
if (this.board[0, 2] == 0 &&
|
|
|
|
|
this.board[1, 2] == 0 &&
|
|
|
|
|
this.board[2, 2] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
if (this.board[0, 0] == 0 &&
|
|
|
|
|
this.board[0, 1] == 0 &&
|
|
|
|
|
this.board[0, 2] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
if (this.board[1, 0] == 0 &&
|
|
|
|
|
this.board[1, 1] == 0 &&
|
|
|
|
|
this.board[1, 2] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
if (this.board[2, 0] == 0 &&
|
|
|
|
|
this.board[2, 1] == 0 &&
|
|
|
|
|
this.board[2, 2] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
//wins on y axis
|
|
|
|
|
if (this.board[0, 0] == 0 &&
|
|
|
|
|
this.board[1, 0] == 0 &&
|
|
|
|
|
this.board[2, 0] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
if (this.board[0, 1] == 0 &&
|
|
|
|
|
this.board[1, 1] == 0 &&
|
|
|
|
|
this.board[2, 1] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
if (this.board[0, 2] == 0 &&
|
|
|
|
|
this.board[1, 2] == 0 &&
|
|
|
|
|
this.board[2, 2] == 0)
|
|
|
|
|
winsOfO++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (winsOfX == 1 && winsOfO == 0) //x wins
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (winsOfX == 0 && winsOfO == 1) //o wins
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (winsOfX == 1 && winsOfO == 1) // Draw
|
|
|
|
|
{
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
else // Invalid combination
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
}
|
2022-11-27 17:11:02 -06:00
|
|
|
|
|
|
|
|
|
void displayBoard()
|
2022-11-25 11:30:44 -06:00
|
|
|
|
{
|
|
|
|
|
if (board[0, 0] == 0)
|
|
|
|
|
label1.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if(board[0, 0] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label1.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label1.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
if (board[0, 1] == 0)
|
|
|
|
|
label2.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[0,1] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label2.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label2.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
if (board[0, 2] == 0)
|
|
|
|
|
label3.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[0,2] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label3.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label3.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
if (board[1, 0] == 0)
|
|
|
|
|
label4.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[1,0] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label4.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label4.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (board[1, 1] == 0)
|
|
|
|
|
label5.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[1,1] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label5.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label5.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (board[1, 2] == 0)
|
|
|
|
|
label6.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[1,2] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label6.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label6.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (board[2, 0] == 0)
|
|
|
|
|
label7.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[2,0] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label7.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label7.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (board[2, 1] == 0)
|
|
|
|
|
label8.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[2,1] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label8.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label8.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
|
|
|
|
|
if (board[2, 2] == 0)
|
|
|
|
|
label9.Text = "O";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else if (board[2,2] == 1)
|
2022-11-25 11:30:44 -06:00
|
|
|
|
label9.Text = "X";
|
2022-11-27 17:11:02 -06:00
|
|
|
|
else
|
|
|
|
|
label9.Text = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void resetButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < 3; x++)
|
|
|
|
|
{
|
|
|
|
|
for (int y = 0; y < 3; y++)
|
|
|
|
|
board[x, y] = -1;
|
|
|
|
|
}
|
|
|
|
|
displayBoard();
|
|
|
|
|
resultLabel.Text = "";
|
2022-11-25 11:30:44 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|