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 newGameButton_Click(object sender, EventArgs e) { populateArray(); //displayBoard(); while (determineWinner() == -1) { //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"; } 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; //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) winsOfX++; if (this.board[1, 0] == 1 && this.board[1, 1] == 1 && this.board[1, 2] == 1) winsOfX++; 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; } } void displayBoard() { if (board[0, 0] == 0) label1.Text = "O"; else if(board[0, 0] == 1) label1.Text = "X"; else label1.Text = ""; if (board[0, 1] == 0) label2.Text = "O"; else if (board[0,1] == 1) label2.Text = "X"; else label2.Text = ""; if (board[0, 2] == 0) label3.Text = "O"; else if (board[0,2] == 1) label3.Text = "X"; else label3.Text = ""; if (board[1, 0] == 0) label4.Text = "O"; else if (board[1,0] == 1) label4.Text = "X"; else label4.Text = ""; if (board[1, 1] == 0) label5.Text = "O"; else if (board[1,1] == 1) label5.Text = "X"; else label5.Text = ""; if (board[1, 2] == 0) label6.Text = "O"; else if (board[1,2] == 1) label6.Text = "X"; else label6.Text = ""; if (board[2, 0] == 0) label7.Text = "O"; else if (board[2,0] == 1) label7.Text = "X"; else label7.Text = ""; if (board[2, 1] == 0) label8.Text = "O"; else if (board[2,1] == 1) label8.Text = "X"; else label8.Text = ""; if (board[2, 2] == 0) label9.Text = "O"; else if (board[2,2] == 1) label9.Text = "X"; 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 = ""; } } }