Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace TicTacToe
- {
- public partial class Form1 : Form
- {
- //global variables
- static string strPlayerFace;
- static string strCompFace;
- static string strMultiplayerFace;
- int intCounter;
- int intPlayerScore = 0;
- int intCompScore = 0;
- bool blnClicked1;
- bool blnClicked2;
- bool blnClicked3;
- bool blnClicked4;
- bool blnClicked5;
- bool blnClicked6;
- bool blnClicked7;
- bool blnClicked8;
- bool blnClicked9;
- bool blnSelection1;
- bool blnSelection2;
- bool blnSelection3;
- bool blnSelection4;
- bool blnSelection5;
- bool blnSelection6;
- bool blnSelection7;
- bool blnSelection8;
- bool blnSelection9;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //Output
- MessageBox.Show("Set Your Difficulty and Mode");
- declareVariable();
- }
- public void declareVariable()
- {
- //initialization
- strPlayerFace = "X";
- strCompFace = "O";
- strMultiplayerFace = "O";
- intCounter = 0;
- //Initialize the variabled
- blnClicked1 = false;
- blnClicked2 = false;
- blnClicked3 = false;
- blnClicked4 = false;
- blnClicked5 = false;
- blnClicked6 = false;
- blnClicked7 = false;
- blnClicked8 = false;
- blnClicked9 = false;
- blnSelection1 = false;
- blnSelection2 = false;
- blnSelection3 = false;
- blnSelection4 = false;
- blnSelection5 = false;
- blnSelection6 = false;
- blnSelection7 = false;
- blnSelection8 = false;
- blnSelection9 = false;
- //Reset Buttons
- btnTopLeft.Enabled = true;
- btnTopMid.Enabled = true;
- btnTopRight.Enabled = true;
- btnMidLeft.Enabled = true;
- btnMidCenter.Enabled = true;
- btnMidRight.Enabled = true;
- btnBottomLeft.Enabled = true;
- btnBottomMid.Enabled = true;
- btnBottomRight.Enabled = true;
- //Reset Button Text
- btnTopLeft.Text = "";
- btnTopMid.Text = "";
- btnTopRight.Text = "";
- btnMidLeft.Text = "";
- btnMidCenter.Text = "";
- btnMidRight.Text = "";
- btnBottomLeft.Text = "";
- btnBottomMid.Text = "";
- btnBottomRight.Text = "";
- }
- private void btnTopLeft_Click(object sender, EventArgs e)
- {
- if (blnClicked1 == false)
- {
- this.btnTopLeft.Text = strPlayerFace;
- blnClicked1 = true;
- this.btnTopLeft.Enabled = false;
- blnSelection1 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnTopMid_Click(object sender, EventArgs e)
- {
- if (blnClicked2 == false)
- {
- this.btnTopMid.Text = strPlayerFace;
- blnClicked2 = true;
- this.btnTopMid.Enabled = false;
- blnSelection2 = true;
- }
- checkWinner();
- intCounter++;if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnTopRight_Click(object sender, EventArgs e)
- {
- if (blnClicked3 == false)
- {
- this.btnTopRight.Text = strPlayerFace;
- blnClicked3 = true;
- this.btnTopRight.Enabled = false;
- blnSelection3 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnMidLeft_Click(object sender, EventArgs e)
- {
- if (blnClicked4 == false)
- {
- this.btnMidLeft.Text = strPlayerFace;
- blnClicked4 = true;
- this.btnMidLeft.Enabled = false;
- blnSelection4 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnMidCenter_Click(object sender, EventArgs e)
- {
- if (blnClicked5 == false)
- {
- this.btnMidCenter.Text = strPlayerFace;
- blnClicked5 = true;
- this.btnMidCenter.Enabled = false;
- blnSelection5 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnMidRight_Click(object sender, EventArgs e)
- {
- if (blnClicked6 == false)
- {
- this.btnMidRight.Text = strPlayerFace;
- blnClicked6 = true;
- this.btnMidRight.Enabled = false;
- blnSelection6 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnBottomLeft_Click(object sender, EventArgs e)
- {
- if (blnClicked7 == false)
- {
- this.btnBottomLeft.Text = strPlayerFace;
- blnClicked7 = true;
- this.btnBottomLeft.Enabled = false;
- blnSelection7 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnBottomMid_Click(object sender, EventArgs e)
- {
- if (blnClicked8 == false)
- {
- this.btnBottomMid.Text = strPlayerFace;
- blnClicked8 = true;
- this.btnBottomMid.Enabled = false;
- blnSelection8 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- private void btnBottomRight_Click(object sender, EventArgs e)
- {
- if (blnClicked9 == false)
- {
- this.btnBottomRight.Text = strPlayerFace;
- blnClicked9 = true;
- this.btnBottomRight.Enabled = false;
- blnSelection9 = true;
- }
- checkWinner();
- intCounter++;
- if (radMultiplayer.Checked == true)
- {
- MultiplayerMode();
- }
- else
- {
- if (radEasy.Checked == true)
- {
- compMoveEasy();
- }
- else if (radMedium.Checked == true)
- {
- compMoveMedium();
- }
- else if (radHard.Checked == true)
- {
- compMoveHard();
- }
- }
- }
- public void compMoveEasy()
- {
- Random rndClass = new Random();
- int rndSelection = rndClass.Next(1,10);
- {
- if (rndSelection == 1)
- {
- if (blnClicked1 == false)
- {
- this.btnTopLeft.Text = strCompFace;
- blnClicked1 = true;
- blnSelection1 = false;
- btnTopLeft.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 2)
- {
- if (blnClicked2 == false)
- {
- this.btnTopMid.Text = strCompFace;
- blnClicked2 = true;
- blnSelection2 = false;
- btnTopMid.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 3)
- {
- if (blnClicked3 == false)
- {
- this.btnTopRight.Text = strCompFace;
- blnClicked3 = true;
- blnSelection3 = false;
- btnTopRight.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 4)
- {
- if (blnClicked4 == false)
- {
- this.btnMidLeft.Text = strCompFace;
- blnClicked4 = true;
- blnSelection4 = false;
- btnMidLeft.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 5)
- {
- if (blnClicked5 == false)
- {
- this.btnMidCenter.Text = strCompFace;
- blnClicked5 = true;
- blnSelection5 = false;
- btnMidCenter.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 6)
- {
- if (blnClicked6 == false)
- {
- this.btnMidRight.Text = strCompFace;
- blnClicked6 = true;
- blnSelection6 = false;
- btnMidRight.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 7)
- {
- if (blnClicked7 == false)
- {
- this.btnBottomLeft.Text = strCompFace;
- blnClicked7 = true;
- blnSelection7 = false;
- btnBottomLeft.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 8)
- {
- if (blnClicked8 == false)
- {
- this.btnBottomMid.Text = strCompFace;
- blnClicked8 = true;
- blnSelection8 = false;
- btnBottomMid.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- if (rndSelection == 9)
- {
- if (blnClicked9 == false)
- {
- this.btnBottomRight.Text = strCompFace;
- blnClicked9 = true;
- blnSelection9 = false;
- btnBottomRight.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveEasy();
- }
- checkWinner();
- }
- }
- }
- public void compMoveMedium()
- {
- Random rndClass = new Random();
- int rndSelectionEven = rndClass.Next(1, 5);
- int rndSelectionOdd = rndClass.Next(1, 5);
- for (int intA = 0; intA < 1; intA++)
- {
- {
- if (blnClicked5 == false)
- {
- blnClicked5 = true;
- this.btnMidCenter.Text = strCompFace;
- btnMidCenter.Enabled = false;
- blnSelection5 = false;
- intCounter++;
- checkWinner();
- }
- else if (rndSelectionEven == 1)
- {
- if (blnClicked2 == false)
- {
- this.btnTopMid.Text = strCompFace;
- blnClicked2 = true;
- blnSelection2 = false;
- btnTopMid.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- else if (rndSelectionEven == 2)
- {
- if (blnClicked4 == false)
- {
- this.btnMidLeft.Text = strCompFace;
- blnClicked4 = true;
- blnSelection4 = false;
- btnMidLeft.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- else if (rndSelectionEven == 3)
- {
- if (blnClicked6 == false)
- {
- this.btnMidRight.Text = strCompFace;
- blnClicked6 = true;
- blnSelection6 = false;
- btnMidRight.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- else if (rndSelectionEven == 4)
- {
- if (blnClicked8 == false)
- {
- this.btnBottomMid.Text = strCompFace;
- blnClicked8 = true;
- blnSelection8 = false;
- btnBottomMid.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- else if (rndSelectionOdd == 1)
- {
- if (blnClicked1 == false)
- {
- this.btnTopLeft.Text = strCompFace;
- blnClicked1 = true;
- blnSelection1 = false;
- btnTopLeft.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- else if (rndSelectionOdd == 2)
- {
- if (blnClicked3 == false)
- {
- this.btnTopRight.Text = strCompFace;
- blnClicked3 = true;
- blnSelection3 = false;
- btnTopRight.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- else if (rndSelectionOdd == 3)
- {
- if (blnClicked7 == false)
- {
- this.btnBottomLeft.Text = strCompFace;
- blnClicked7 = true;
- blnSelection7 = false;
- btnBottomLeft.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- else if (rndSelectionOdd == 4)
- {
- if (blnClicked9 == false)
- {
- this.btnBottomRight.Text = strCompFace;
- blnClicked9 = true;
- blnSelection9 = false;
- btnBottomRight.Enabled = false;
- intCounter++;
- }
- else
- {
- compMoveMedium();
- }
- checkWinner();
- }
- }
- }
- }
- public void compMoveHard()
- {
- }
- public void checkWinner()
- {
- if (blnClicked1 == true && blnClicked2 == true && blnClicked3 == true)
- {
- if (blnSelection1 == true && blnSelection2 == true && blnSelection3 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection1 == false && blnSelection2 == false && blnSelection3 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked4 == true && blnClicked5 == true && blnClicked6 == true)
- {
- if (blnSelection4 == true && blnSelection5 == true && blnSelection6 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection4 == false && blnSelection5 == false && blnSelection6 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked7 == true && blnClicked8 == true && blnClicked9 == true)
- {
- if (blnSelection7 == true && blnSelection8 == true && blnSelection9 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection7 == false && blnSelection8 == false && blnSelection9 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked1 == true && blnClicked4 == true && blnClicked7 == true)
- {
- if (blnSelection1 == true && blnSelection4 == true && blnSelection7 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection1 == false && blnSelection4 == false && blnSelection7 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked2 == true && blnClicked5 == true && blnClicked8 == true)
- {
- if (blnSelection2 == true && blnSelection5 == true && blnSelection8 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection2 == false && blnSelection5 == false && blnSelection8 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked3 == true && blnClicked6 == true && blnClicked9 == true)
- {
- if (blnSelection3 == true && blnSelection6 == true && blnSelection9 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection3 == false && blnSelection6 == false && blnSelection9 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked1 == true && blnClicked5 == true && blnClicked9 == true)
- {
- if (blnSelection1 == true && blnSelection5 == true && blnSelection9 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection1 == false && blnSelection5 == false && blnSelection9 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked3 == true && blnClicked5 == true && blnClicked7 == true)
- {
- if (blnSelection3 == true && blnSelection5 == true && blnSelection7 == true)
- {
- MessageBox.Show("User Wins", "Winning Message");
- intPlayerScore++;
- lblPlayerScoreNum.Text = intPlayerScore.ToString();
- declareVariable();
- }
- else if (blnSelection3 == false && blnSelection5 == false && blnSelection7 == false)
- {
- MessageBox.Show("Computer Wins", "Winning Message");
- intCompScore++;
- lblCompScoreNum.Text = intCompScore.ToString();
- declareVariable();
- }
- }
- else if (blnClicked1 == true && blnClicked2 == true && blnClicked3 == true && blnClicked4 == true && blnClicked5 == true && blnClicked6 == true && blnClicked7 == true && blnClicked8 == true && blnClicked9 == true && intCounter == 9)
- {
- MessageBox.Show("It's a Draw", "Winning Messaage");
- declareVariable();
- }
- }
- private void btnReset_Click(object sender, EventArgs e)
- {
- declareVariable();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement