Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 tictactoe : Form
- {
- // bool turn = true; // = X turn/ false Y
- private Button[,] buttonArray;
- private bool isX = true;
- private bool there_is_a_winner;
- bool against_computer = false;
- int turn_count = 0;
- const int Xitems = 3;
- const int Yitems = 3;
- //static String player1, player2;
- private int[,] winPattern =
- {
- {0,1,2},//down horizontal
- {3,4,5},//middle horizontal
- {6,7,8},//top horizontal
- {0,3,6},//left vertical
- {1,4,7},//middle vertical
- {2,5,8},//right vertical
- {0,4,8},// / diagonal
- {2,4,6}// \ diagonal
- };
- public tictactoe()
- {
- InitializeComponent();
- buttonArray = new Button[Xitems, Yitems];
- for (int x = 0; x < Xitems; x++)
- {
- for (int y = 0; y < Yitems; y++)
- {
- Button btn = new Button();
- btn.Font = new System.Drawing.Font("Microsoft Sans Serif", 27.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
- btn.Location = new System.Drawing.Point(22 + (x * 81), 40 + (y * 81));
- btn.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
- btn.Size = new System.Drawing.Size(75, 75);
- btn.TabIndex = 2;
- btn.UseVisualStyleBackColor = true;
- btn.Click += new System.EventHandler(this.button_click);
- btn.MouseEnter += new System.EventHandler(this.button_enter);
- btn.MouseLeave += new System.EventHandler(this.button_leave);
- buttonArray[x, y] = btn;
- this.Controls.Add(btn);
- }
- }
- buttonArray[0, 0].BackColor = Color.Red;
- }
- /*
- public static void setPlayersNames(String n1, String n2)
- {
- player1 = n1;
- player2 = n2;
- }
- */
- private void exirToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
- {
- MessageBox.Show("By Or", " Tic Tac Toe About");
- }
- private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
- {
- }
- private void button_click(object sender, EventArgs e)
- {
- if ((p2.Text == "Player 1") || (p2.Text == "Player 2"))
- {
- MessageBox.Show("You must specify the players");
- }
- else
- {
- Button b = (Button)sender;
- if (isX)
- b.Text = "X";
- else
- b.Text = "O";
- isX = !isX;
- b.Enabled = false;
- turn_count++;
- label3.Focus();
- checkForWinner();
- }
- // if ((!isX) && (against_computer))
- {
- // computer_make_move();
- }
- }
- /*
- private void computer_make_move()
- {
- //priority 1: get tick tac toe
- //priority 2: block x tic tac toe
- //priority 3: go for corner space
- //priority 4: pick open space
- Button move = null;
- //look for tic tac toe opportunities
- move = look_for_win_or_block("O"); //look for win
- if (move == null)
- {
- move = look_for_win_or_block("X"); //look for block
- if (move == null)
- {
- move = look_for_corner();
- if (move == null)
- {
- move = look_for_open_space();
- }//end if
- }//end if
- }//end if
- try
- {
- move.PerformClick();
- }
- catch
- {
- }
- }
- private Button look_for_open_space()
- {
- Console.WriteLine("Looking for open space");
- Button b = null;
- foreach (Control c in Controls)
- {
- b = c as Button;
- if (b != null)
- {
- if (b.Text == "")
- return b;
- }//end if
- }//end if
- return null;
- }
- private Button look_for_corner()
- {
- Console.WriteLine("Looking for corner");
- if (A1.Text == "O")
- {
- if (A3.Text == "")
- return A3;
- if (C3.Text == "")
- return C3;
- if (C1.Text == "")
- return C1;
- }
- if (A3.Text == "O")
- {
- if (A1.Text == "")
- return A1;
- if (C3.Text == "")
- return C3;
- if (C1.Text == "")
- return C1;
- }
- if (C3.Text == "O")
- {
- if (A1.Text == "")
- return A3;
- if (A3.Text == "")
- return A3;
- if (C1.Text == "")
- return C1;
- }
- if (C1.Text == "O")
- {
- if (A1.Text == "")
- return A3;
- if (A3.Text == "")
- return A3;
- if (C3.Text == "")
- return C3;
- }
- if (A1.Text == "")
- return A1;
- if (A3.Text == "")
- return A3;
- if (C1.Text == "")
- return C1;
- if (C3.Text == "")
- return C3;
- return null;
- }
- private Button look_for_win_or_block(string mark)
- {
- Console.WriteLine("Looking for win or block: " + mark);
- //HORIZONTAL TESTS
- if ((A1.Text == mark) && (A2.Text == mark) && (A3.Text == ""))
- return A3;
- if ((A2.Text == mark) && (A3.Text == mark) && (A1.Text == ""))
- return A1;
- if ((A1.Text == mark) && (A3.Text == mark) && (A2.Text == ""))
- return A2;
- if ((B1.Text == mark) && (B2.Text == mark) && (B3.Text == ""))
- return B3;
- if ((B2.Text == mark) && (B3.Text == mark) && (B1.Text == ""))
- return B1;
- if ((B1.Text == mark) && (B3.Text == mark) && (B2.Text == ""))
- return B2;
- if ((C1.Text == mark) && (C2.Text == mark) && (C3.Text == ""))
- return C3;
- if ((C2.Text == mark) && (C3.Text == mark) && (C1.Text == ""))
- return C1;
- if ((C1.Text == mark) && (C3.Text == mark) && (C2.Text == ""))
- return C2;
- //VERTICAL TESTS
- if ((A1.Text == mark) && (B1.Text == mark) && (C1.Text == ""))
- return C1;
- if ((B1.Text == mark) && (C1.Text == mark) && (A1.Text == ""))
- return A1;
- if ((A1.Text == mark) && (C1.Text == mark) && (B1.Text == ""))
- return B1;
- if ((A2.Text == mark) && (B2.Text == mark) && (C2.Text == ""))
- return C2;
- if ((B2.Text == mark) && (C2.Text == mark) && (A2.Text == ""))
- return A2;
- if ((A2.Text == mark) && (C2.Text == mark) && (B2.Text == ""))
- return B2;
- if ((A3.Text == mark) && (B3.Text == mark) && (C3.Text == ""))
- return C3;
- if ((B3.Text == mark) && (C3.Text == mark) && (A3.Text == ""))
- return A3;
- if ((A3.Text == mark) && (C3.Text == mark) && (B3.Text == ""))
- return B3;
- //DIAGONAL TESTS
- if ((A1.Text == mark) && (B2.Text == mark) && (C3.Text == ""))
- return C3;
- if ((B2.Text == mark) && (C3.Text == mark) && (A1.Text == ""))
- return A1;
- if ((A1.Text == mark) && (C3.Text == mark) && (B2.Text == ""))
- return B2;
- if ((A3.Text == mark) && (B2.Text == mark) && (C1.Text == ""))
- return C1;
- if ((B2.Text == mark) && (C1.Text == mark) && (A3.Text == ""))
- return A3;
- if ((A3.Text == mark) && (C1.Text == mark) && (B2.Text == ""))
- return B2;
- return null;
- }
- */
- private void checkForWinner()
- {
- for (int i = 0; i < 3; i++)
- {
- if ((buttonArray[0, i].Text == buttonArray[1, i].Text) && (buttonArray[1, i].Text == buttonArray[2, i].Text) && (!buttonArray[0, i].Enabled))
- there_is_a_winner = true;
- if ((buttonArray[i, 0].Text == buttonArray[i, 1].Text) && (buttonArray[i, 1].Text == buttonArray[i, 2].Text) && (!buttonArray[i, 0].Enabled))
- there_is_a_winner = true;
- }
- if ((buttonArray[0, 0].Text == buttonArray[1, 1].Text) && (buttonArray[1, 1].Text == buttonArray[2, 2].Text) && (!buttonArray[0, 0].Enabled))
- there_is_a_winner = true;
- if ((buttonArray[0, 2].Text == buttonArray[1, 1].Text) && (buttonArray[1, 1].Text == buttonArray[2, 0].Text) && (!buttonArray[0, 2].Enabled))
- there_is_a_winner = true;
- //Horizontal
- // if ((buttonArray[0,0].Text == buttonArray[1,0].Text) && (buttonArray[1,0].Text == buttonArray[2,0].Text) && (!buttonArray[0,0].Enabled))
- // there_is_a_winner = true;
- // else if ((buttonArray[0, 1].Text == buttonArray[1, 1].Text) && (buttonArray[1, 1].Text == buttonArray[2, 1].Text) && (!buttonArray[0, 1].Enabled))
- // there_is_a_winner = true;
- // else if ((buttonArray[0, 2].Text == buttonArray[1, 2].Text) && (buttonArray[1, 2].Text == buttonArray[2, 2].Text) && (!buttonArray[0, 2].Enabled))
- // there_is_a_winner = true;
- /* // Verticall
- else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
- there_is_a_winner = true;
- else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
- there_is_a_winner = true;
- else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
- there_is_a_winner = true;
- // Diagonal
- else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
- there_is_a_winner = true;
- else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
- there_is_a_winner = true;
- */
- if (there_is_a_winner)
- {
- disableButtons();
- String winner = "";
- if (isX)
- {
- winner = p2.Text;
- o_win_count.Text = (Int32.Parse(o_win_count.Text) + 1).ToString();
- newGameToolStripMenuItem.PerformClick();
- }
- else
- {
- winner = p1.Text;
- x_win_count.Text = (Int32.Parse(x_win_count.Text) + 1).ToString();
- }
- MessageBox.Show(winner + " Wins", "Yay!");
- newGameToolStripMenuItem.PerformClick();
- }
- else
- {
- if (turn_count == 9)
- {
- draw_count.Text = (Int32.Parse(draw_count.Text) + 1).ToString();
- MessageBox.Show("Draw!", " Bummer");
- newGameToolStripMenuItem.PerformClick();
- }
- }
- }//end check
- private void disableButtons()
- {
- try
- {
- foreach (Control c in Controls)
- {
- Button b = (Button)c;
- b.Enabled = false;
- }
- }
- catch { }
- }
- private void tictactoe_Load(object sender, EventArgs e)
- {
- foreach (Button ctrlBtn in buttonArray)
- {
- // ctrlBtn.Click += new System.EventHandler(this.DrawCharacter);
- }
- InitGame();
- }
- private void InitGame()
- {
- foreach (Button btn in buttonArray)
- {
- btn.Text = "";
- btn.BackColor = Color.Transparent;
- btn.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F,
- System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
- }
- // isGameOver = false;
- there_is_a_winner = false;
- isX = true;
- }
- private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
- {
- isX = true;
- turn_count = 0;
- foreach (Control c in Controls)
- {
- try
- {
- Button b = (Button)c;
- b.Enabled = true;
- b.Text = "";
- }
- catch { }
- }
- }
- private void button_enter(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (b.Enabled)
- {
- if (isX)
- b.Text = "X";
- else
- b.Text = "O";
- }
- }
- private void button_leave(object sender, EventArgs e)
- {
- Button b = (Button)sender;
- if (b.Enabled)
- {
- b.Text = "";
- }
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void resetWinCountToolStripMenuItem_Click(object sender, EventArgs e)
- {
- o_win_count.Text = "0";
- x_win_count.Text = "0";
- draw_count.Text = "0";
- }
- private void tictactoe_Load_1(object sender, EventArgs e)
- {
- /*
- Form f2 = new Form2();
- f2.ShowDialog();
- p1.Text = player1;
- p2.Text = player2; */
- setPlayerDefaultToolStripMenuItem.PerformClick();
- }
- private void p2_TextChanged(object sender, EventArgs e)
- {
- if (p2.Text.ToUpper() == "COMPUTER")
- against_computer = true;
- else
- against_computer = false;
- }
- private void setPlayerDefaultToolStripMenuItem_Click(object sender, EventArgs e)
- {
- p1.Text = "Or";
- p2.Text = "Computer";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement