OKat so here is the deal The follwing progrman needs to be done in C# and in vis
ID: 3834539 • Letter: O
Question
OKat so here is the deal The follwing progrman needs to be done in C# and in visual studio and it is due by May 12th. Game Logic There are three possible outcomes to the game. 1. The player wins. a. When the player wins, the computer should not be able to play. Whether the computer can play or not is tracked by the GoodMove Boolean variable. This variable tracks the state of the computer’s ability to play. b. All labels should be disabled so the player can’t play. c. The only option is to click New Game. 2. The computer wins. a. All labels should be disabled so the player can’t play. b. The computer should not be able to play. c. The only option is to click New Game. 3. There is a tie. a. A tie is tracked by the PlayerClicks variable. After the player has clicked 5x’s without winning, the computer shouldn’t be able to play, and all labels should be disabled so the player can’t play. b. The only option is to click New Game. 4. New Game a. Allow the computer to play. Set the GoodMove variable to false. b. Allow the user to play, clear the labels and the gameBoard. Requirements Create a global variable named PlayerClicks to keep track of how many times the Player has clicked. The game is a tie if the Player clicks 5x’s without winning. Create a CheckWinner() method that checks first to see if the player won, then checks to see if the computer won. o Test the gameBoard array for each possible combination of winning: 012, 345, 678, 036, 147, 258, 048, 246 for X, then O. o Determine the winner of X or O. o Display the result of the game. o If the winner is X (player), use the global goodMove Boolean variable to determine that the computer can’t keep playing after the player wins. goodMove = true; stops computer play. o Use the global PlayerClicks variable to track how many times the player plays until a tie. Tic Tac Toe 3 Page 2 of 2 Revised: 5/4/2017 o Disable all the labels when either player wins or there is a tie. o Display the game results. o Call the CheckWinner() method at the beginning and the end of the GameBoard_Click method. This tests first if the player won, then at the end it tests to see if the computer won. The new game button resets the PlayerClicks variable. Add a MenuStrip. Add a File menu, then a New Game, and Exit command. Add a Help menu with an About command. The About command can call a form that your built, or a messagedialogbox with an OK button.
Here is what i have so far:
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 Tic_Tac_Toe
{
public partial class ticticatoeFrm : Form
{ // Global level varaibles.
int Rounds; // To hold the number of rounds played.
public ticticatoeFrm()
{
InitializeComponent();
}
// Form level constants.
const int SIZE = 9; // The size of the array
const int MIN = 0; // Set the minimum range of the random numbers, inclusive.
const int MAX = 9; // Set the maximum range of ther random numbers, exclusive.
// Form level variables.
char[] gameboard = new char[SIZE]; // An array to store the xs and os
Label[] _labels = new Label[SIZE]; // some label objects to access the labels.
Random rand = new Random(); // A new random number generator object.
Label lblLabel; // To track which lable the player clicks.
// Create a method to check who wins and loses.
private void Newgamebtn_Click(object sender, EventArgs e)
{
for (int x = 0; x < SIZE; x++) // Go through the rows.
{ // Clear the board and start a new game
// Renable the labels.
_labels[x].Enabled = true;
// Set the gameboard array to spaces.
gameboard[x] = ' ';
// Method for showing the computer and players moves
displayGameLabels();
}
}
// Create a method to display the results to the labels and have them set to disable if used.
private void displayGameLabels()
{ // Display the labels and set labels to displayed if they have been played.
// Go through the entire array.
for (int x = 0; x < SIZE; x++)
{
// if the label has been clicked disable it.
if (gameboard[x].ToString() == "X" || gameboard[x].ToString() == "O")
{
_labels[x].Enabled = false; // Disable the played lable.
}
// Update the labels text from the game array
_labels[x].Text = gameboard[x].ToString();
}
}
private void exitbtn_Click(object sender, EventArgs e)
{
// Close the form
this.Close();
}
private void ticticatoeFrm_Load(object sender, EventArgs e)
{
_labels[0] = this.label1;
_labels[1] = this.label2;
_labels[2] = this.label3;
_labels[3] = this.label4;
_labels[4] = this.label5;
_labels[5] = this.label6;
_labels[6] = this.label7;
_labels[7] = this.label8;
_labels[8] = this.label9;
}
private void GameBoard_Click(object sender, EventArgs e)
{
// If the user has clicked any label.
string labelName; // Store the label name.
int labelNumber; // Store the label number.
int randomLabel; // Random number for computer choice.
lblLabel = (Label)sender; // use the label object variable to hold the label that was just clicked
lblLabel.Text = "X"; // Mark the players label choice.
labelName = lblLabel.Name; // Pull the name of the label that was clicked.
labelNumber = int.Parse(labelName.Substring(5, 1)); // Pull the number out of the clicked lablel.
gameboard[labelNumber - 1] = 'X'; // Put an x in the label that was clicked by the player.
lblLabel.Enabled = false; // Display the label that was clicked by the player.
Boolean goodMove = false; // Set a flag for played lable.
// Computer moves.
while (goodMove == false)
{
randomLabel = rand.Next(MIN, MAX); // PIck a random label for the computers move.
// Check to see if the player has already chosen that label
if (gameboard[randomLabel].ToString() == "X" || gameboard[randomLabel].ToString() == "O")
{
goodMove = false; // Already been played, try another label
}
// COmputer plays label as it has not been used.
else
{
goodMove = true; // The lable has not been played, mark as good play.
gameboard[randomLabel] = 'O'; // Set O for the computer move.
}
}
displayGameLabels();
}
}
}
Also here is the form i have so far:
Thanks for the help
Tic tac toe New Game Exit XExplanation / Answer
Please find the code below for tic tac toe game using c#
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 Tictactoegame
{
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;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.