Hello I need help finishing the code for this Tic Tac Toe pr
Hello I need help finishing the code for this Tic Tac Toe problem. I\'m using C# Visual Studio 2015 windows form. I need help in \"Reseting the Squares\", and also \"Making the Computer Move using a random number generator. The areas i need help in are in BOLD. Once again this is for C# Visual Studio 2015 Windows Form, thank you
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 TicTacToe
{
public partial class TTTForm : Form
{
public TTTForm()
{
InitializeComponent();
}
const string USER_SYMBOL = \"X\";
const string COMPUTER_SYMBOL = \"O\";
const string EMPTY = \"\";
const int SIZE = 5;
// constants for the 2 diagonals
const int TOP_LEFT_TO_BOTTOM_RIGHT = 1;
const int TOP_RIGHT_TO_BOTTOM_LEFT = 2;
// constants for IsWinner
const int NONE = -1;
const int ROW = 1;
const int COLUMN = 2;
const int DIAGONAL = 3;
// This method takes a row and column as parameters and
// returns a reference to a label on the form in that position
private Label GetSquare(int row, int column)
{
int labelNumber = row * SIZE + column + 1;
return (Label)(this.Controls[\"label\" + labelNumber.ToString()]);
}
// This method does the \"reverse\" process of GetSquare
// It takes a label on the form as it\'s parameter and
// returns the row and column of that square as output parameters
private void GetRowAndColumn(Label l, out int row, out int column)
{
int position = int.Parse(l.Name.Substring(5));
row = (position - 1) / SIZE;
column = (position - 1) % SIZE;
}
// This method takes a row (in the range of 0 - 4) and returns true if
// the row on the form contains 5 Xs or 5 Os.
// Use it as a model for writing IsColumnWinner
private bool IsRowWinner(int row)
{
Label square = GetSquare(row, 0);
string symbol = square.Text;
for (int col = 1; col < SIZE; col++)
{
square = GetSquare(row, col);
if (symbol == EMPTY || square.Text != symbol)
return false;
}
return true;
}
//* TODO: finish all of these that return true
private bool IsAnyRowWinner()
{
//UPDATED CODE
for (int row = 1; row < SIZE; row++)
{
if (IsRowWinner(row))
{
return true;
}
}
return false;
}
private bool IsColumnWinner(int col)
{
//UPDATED CODE
Label square = GetSquare(0, col);
string symbol = square.Text;
for (int row = 1; row < SIZE; row++)
{
square = GetSquare(col, row);
if (symbol == EMPTY || square.Text != symbol)
return false;
}
return true;
}
private bool IsAnyColumnWinner()
{
//UPDATED CODE
for (int col = 1; col < SIZE; col++)
{
if (IsColumnWinner(col))
{
return true;
}
}
return false;
}
private bool IsDiagonal1Winner()
{
//UPDATED CODE
Label square = GetSquare((SIZE - 1), 0);
string symbol = square.Text;
for (int diag = 1; diag < SIZE; diag++)
{
square = GetSquare(diag, diag);
if (symbol == EMPTY || square.Text != symbol)
return false;
}
return true;
}
private bool IsDiagonal2Winner()
{
//Original code inputed with form
Label square = GetSquare(0, (SIZE - 1));
string symbol = square.Text;
for (int row = 1, col = SIZE - 2; row < SIZE; row++, col--)
{
square = GetSquare(row, col);
if (symbol == EMPTY || square.Text != symbol)
return false;
}
return true;
}
private bool IsAnyDiagonalWinner()
{
//UPDATED CODE
Label square = GetSquare(0, (SIZE - 1));
string symbol = square.Text;
for (int row = 1, col = SIZE - 2; row < SIZE; row++, col--)
{
square = GetSquare(row, col);
if (symbol == EMPTY || square.Text != symbol)
return false;
}
return true;
}
private bool IsFull()
{
return true;
}
// This method determines if any row, column or diagonal on the board is a winner.
// It returns true or false and the output parameters will contain appropriate values
// when the method returns true. See constant definitions at top of form.
private bool IsWinner(out int whichDimension, out int whichOne)
{
// rows
for (int row = 0; row < SIZE; row++)
{
if (IsRowWinner(row))
{
whichDimension = ROW;
whichOne = row;
return true;
}
}
// columns
for (int column = 0; column < SIZE; column++)
{
if (IsColumnWinner(column))
{
whichDimension = COLUMN;
whichOne = column;
return true;
}
}
// diagonals
if (IsDiagonal1Winner())
{
whichDimension = DIAGONAL;
whichOne = TOP_LEFT_TO_BOTTOM_RIGHT;
return true;
}
if (IsDiagonal2Winner())
{
whichDimension = DIAGONAL;
whichOne = TOP_RIGHT_TO_BOTTOM_LEFT;
return true;
}
whichDimension = NONE;
whichOne = NONE;
return false;
}
// I wrote this method to show you how to call IsWinner
private bool IsTie()
{
int winningDimension, winningValue;
return (IsFull() && !IsWinner(out winningDimension, out winningValue));
}
// This method takes an integer in the range 0 - 4 that represents a column
// as it\'s parameter and changes the font color of that cell to red.
private void HighlightColumn(int col)
{
for (int row = 0; row < SIZE; row++)
{
Label square = GetSquare(row, col);
square.Enabled = true;
square.ForeColor = Color.Red;
}
}
// This method changes the font color of the top right to bottom left diagonal to red
// I did this diagonal because it\'s harder than the other one
private void HighlightDiagonal2()
{
for (int row = 0, col = SIZE - 1; row < SIZE; row++, col--)
{
Label square = GetSquare(row, col);
square.Enabled = true;
square.ForeColor = Color.Red;
}
}
// This method will highlight either diagonal, depending on the parameter that you pass
private void HighlightDiagonal(int whichDiagonal)
{
if (whichDiagonal == TOP_LEFT_TO_BOTTOM_RIGHT)
HighlightDiagonal1();
else
HighlightDiagonal2();
}
//* TODO: finish these 2
private void HighlightRow(int row)
{
//UPDATED CODE 1
for (int col = 0; col < SIZE; col++)
{
Label square = GetSquare(row, col);
square.Enabled = true;
square.ForeColor = Color.Red;
}
}
private void HighlightDiagonal1()
{
//UPDATED CODE 2
for (int row = SIZE - 1, col = 0; row < SIZE; row++, col--)
{
Label square = GetSquare(row, col);
square.Enabled = true;
square.ForeColor = Color.Red;
}
}
//* TODO: finish this
private void HighlightWinner(string player, int winningDimension, int winningValue)
{
switch (winningDimension)
{
//UPDATED CODE
case ROW:
HighlightRow(winningValue);
resultLabel.Text = (player + \" wins!\");
break;
//UPDATED CODE
case COLUMN:
HighlightColumn(winningValue);
resultLabel.Text = (player + \" wins!\");
break;
case DIAGONAL:
HighlightDiagonal(winningValue);
resultLabel.Text = (player + \" wins!\");
break;
}
}
//* TODO: finish these 2
private void ResetSquares()
{
//This is where im needing help witht he code to reset the squares
}
private void MakeComputerMove()
{
//This is where im needing the code to make computer move using a random number generator
}
// Setting the enabled property changes the look and feel of the cell.
// Instead, this code removes the event handler from each square.
// Use it when someone wins or the board is full to prevent clicking a square.
private void DisableAllSquares()
{
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
Label square = GetSquare(row, col);
DisableSquare(square);
}
}
}
// Inside the click event handler you have a reference to the label that was clicked
// Use this method (and pass that label as a parameter) to disable just that one square
private void DisableSquare(Label square)
{
square.Click -= new System.EventHandler(this.label_Click);
}
// You\'ll need this method to allow the user to start a new game
private void EnableAllSquares()
{
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
Label square = GetSquare(row, col);
square.Click += new System.EventHandler(this.label_Click);
}
}
}
//* TODO: finish the event handlers
private void label_Click(object sender, EventArgs e)
{
//UPDATED CODE
int winningDimension = NONE;
int winningValue = NONE;
Label clickedLabel = (Label)sender;
clickedLabel.Text = USER_SYMBOL;
DisableSquare(clickedLabel);
if (IsWinner(out winningDimension, out winningValue))
{
HighlightWinner(\"The User\", winningDimension, winningValue);
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Solution
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 TicTacToe
{
public partial class Form1 : Form
{
//Variable to store player, 0 is X, 1 is O.
int counter = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if(counter == 0)
{
button1.Text = \"X\";
counter++;
}else if(counter == 1)
{
button1.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button1.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button2_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button2.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button2.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button2.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button3_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button3.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button3.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button3.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button4_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button4.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button4.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button4.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button5_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button5.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button5.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button5.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button6_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button6.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button6.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button6.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button7_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button7.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button7.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button7.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button8_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button8.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button8.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button8.Enabled = false;
//Check if anyone won, lose, tie
check();
}
private void button9_Click(object sender, EventArgs e)
{
//Check who\'s turn it is
if (counter == 0)
{
button9.Text = \"X\";
counter++;
}
else if (counter == 1)
{
button9.Text = \"O\";
counter--;
}
//Disable button so it cannot be changed
button9.Enabled = false;
//Check if anyone won, lose, tie
check();
}
void check()
{
//Check if tie
if (button1.Text != \"\" && button2.Text != \"\" && button3.Text != \"\" &&
button4.Text != \"\" && button5.Text != \"\" && button6.Text != \"\" &&
button7.Text != \"\" && button8.Text != \"\" && button9.Text != \"\")
{
textBox1.Text = \"Tied\";
}
//Check diagonal for X
if (button1.Text == \"X\" && button5.Text == \"X\" && button9.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if(button3.Text == \"X\" && button5.Text == \"X\" && button7.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
//Check rows for X
if(button1.Text == \"X\" && button2.Text == \"X\" && button3.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button4.Text == \"X\" && button5.Text == \"X\" && button6.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button7.Text == \"X\" && button8.Text == \"X\" && button9.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
//Check columns for X
if(button1.Text == \"X\" && button4.Text == \"X\" && button7.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button2.Text == \"X\" && button5.Text == \"X\" && button8.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button3.Text == \"X\" && button6.Text == \"X\" && button9.Text == \"X\")
{
textBox1.Text = \"Player X wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
//Check diagonal for O
if (button1.Text == \"O\" && button5.Text == \"O\" && button9.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button3.Text == \"O\" && button5.Text == \"O\" && button7.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
//Check rows for O
if (button1.Text == \"O\" && button2.Text == \"O\" && button3.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button4.Text == \"O\" && button5.Text == \"O\" && button6.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button7.Text == \"O\" && button8.Text == \"O\" && button9.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
//Check columns for O
if (button1.Text == \"O\" && button4.Text == \"O\" && button7.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button2.Text == \"O\" && button5.Text == \"O\" && button8.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
if (button3.Text == \"O\" && button6.Text == \"O\" && button9.Text == \"O\")
{
textBox1.Text = \"Player O wins\";
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
}
}
private void button10_Click(object sender, EventArgs e)
{
button1.Text = \"\";
button1.Enabled = true;
button2.Text = \"\";
button2.Enabled = true;
button3.Text = \"\";
button3.Enabled = true;
button4.Text = \"\";
button4.Enabled = true;
button5.Text = \"\";
button5.Enabled = true;
button6.Text = \"\";
button6.Enabled = true;
button7.Text = \"\";
button7.Enabled = true;
button8.Text = \"\";
button8.Enabled = true;
button9.Text = \"\";
button9.Enabled = true;
textBox1.Text = \"\";
counter = 0;
}
}
}


















