ASDV-C-Sharp/MP5/TTT/Form1.cs

135 lines
3.2 KiB
C#
Raw Normal View History

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();
}
private void label10_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
populateArray();
displayBoard();
while (determineWinner() == -1)
;//relaod table
displayBoard();
}
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;
if (this.board[0, 0] == 1 &&
this.board[0, 1] == 1 &&
this.board[0, 2] == 1)
winsOfX++;
if (this.board[1, 0] == 1 &&
this.board[1, 1] == 1 &&
this.board[1, 2] == 1)
winsOfX++;
//count all wins of X
//count all the wins of 0
if (winsOfX == 1 && winsOfO == 0)
;//x wins
else if (winsOfX == 0 && winsOfO == 1)
;//o wins
else if (winsOfX == 1 && winsOfO == 1)
;//draw
else
;//invalid combination , rload the table with 0's and 1's
return 1;
}
void displayBoard()
{
if (board[0, 0] == 0)
label1.Text = "O";
else
label1.Text = "X";
if (board[0, 1] == 0)
label2.Text = "O";
else
label2.Text = "X";
if (board[0, 2] == 0)
label3.Text = "O";
else
label3.Text = "X";
if (board[1, 0] == 0)
label4.Text = "O";
else
label4.Text = "X";
if (board[1, 1] == 0)
label5.Text = "O";
else
label5.Text = "X";
if (board[1, 2] == 0)
label6.Text = "O";
else
label6.Text = "X";
if (board[2, 0] == 0)
label7.Text = "O";
else
label7.Text = "X";
if (board[2, 1] == 0)
label8.Text = "O";
else
label8.Text = "X";
if (board[2, 2] == 0)
label9.Text = "O";
else
label9.Text = "X";
}
}
}