Advertisement
Guest User

Untitled

a guest
May 28th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Tictactoe
  12. {
  13.     public partial class tictactoe : Form
  14.     {
  15.  
  16.         // bool turn = true; // = X turn/ false Y
  17.         private Button[,] buttonArray;
  18.         private bool isX = true;
  19.         private bool there_is_a_winner;
  20.         bool against_computer = false;
  21.         int turn_count = 0;
  22.  
  23.         const int Xitems = 3;
  24.         const int Yitems = 3;
  25.         //static String player1, player2;
  26.         private int[,] winPattern =
  27.         {
  28.         {0,1,2},//down horizontal
  29.         {3,4,5},//middle horizontal
  30.         {6,7,8},//top  horizontal
  31.         {0,3,6},//left vertical
  32.         {1,4,7},//middle vertical
  33.         {2,5,8},//right vertical
  34.         {0,4,8},// / diagonal
  35.         {2,4,6}// \ diagonal
  36.         };
  37.  
  38.  
  39.         public tictactoe()
  40.         {
  41.             InitializeComponent();
  42.             buttonArray = new Button[Xitems, Yitems];
  43.             for (int x = 0; x < Xitems; x++)
  44.             {
  45.                 for (int y = 0; y < Yitems; y++)
  46.                 {
  47.                     Button btn = new Button();
  48.                     btn.Font = new System.Drawing.Font("Microsoft Sans Serif", 27.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(177)));
  49.                     btn.Location = new System.Drawing.Point(22 + (x * 81), 40 + (y * 81));
  50.                     btn.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
  51.                     btn.Size = new System.Drawing.Size(75, 75);
  52.                     btn.TabIndex = 2;
  53.                     btn.UseVisualStyleBackColor = true;
  54.                     btn.Click += new System.EventHandler(this.button_click);
  55.                     btn.MouseEnter += new System.EventHandler(this.button_enter);
  56.                     btn.MouseLeave += new System.EventHandler(this.button_leave);
  57.                     buttonArray[x, y] = btn;
  58.                     this.Controls.Add(btn);
  59.                 }
  60.             }
  61.  
  62.             buttonArray[0, 0].BackColor = Color.Red;
  63.         }
  64.         /*
  65.           public static void setPlayersNames(String n1, String n2)
  66.           {
  67.               player1 = n1;
  68.               player2 = n2;
  69.  
  70.           }
  71.           */
  72.  
  73.         private void exirToolStripMenuItem_Click(object sender, EventArgs e)
  74.         {
  75.             Application.Exit();
  76.         }
  77.  
  78.         private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
  79.         {
  80.             MessageBox.Show("By Or", " Tic Tac Toe About");
  81.         }
  82.  
  83.         private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  84.         {
  85.  
  86.         }
  87.  
  88.         private void button_click(object sender, EventArgs e)
  89.         {
  90.             if ((p2.Text == "Player 1") || (p2.Text == "Player 2"))
  91.             {
  92.                 MessageBox.Show("You must specify the players");
  93.             }
  94.             else
  95.             {
  96.                 Button b = (Button)sender;
  97.                 if (isX)
  98.                     b.Text = "X";
  99.                 else
  100.                     b.Text = "O";
  101.                 isX = !isX;
  102.                 b.Enabled = false;
  103.                 turn_count++;
  104.  
  105.                 label3.Focus();
  106.                 checkForWinner();
  107.             }
  108.             //     if ((!isX) && (against_computer))
  109.             {
  110.                 //   computer_make_move();
  111.             }
  112.         }
  113.         /*
  114.         private void computer_make_move()
  115.         {
  116.             //priority 1:  get tick tac toe
  117.             //priority 2:  block x tic tac toe
  118.             //priority 3:  go for corner space
  119.             //priority 4:  pick open space
  120.  
  121.             Button move = null;
  122.  
  123.             //look for tic tac toe opportunities
  124.             move = look_for_win_or_block("O"); //look for win
  125.             if (move == null)
  126.             {
  127.                 move = look_for_win_or_block("X"); //look for block
  128.                 if (move == null)
  129.                 {
  130.                     move = look_for_corner();
  131.                     if (move == null)
  132.                     {
  133.                         move = look_for_open_space();
  134.                     }//end if
  135.                 }//end if
  136.             }//end if
  137.             try
  138.             {
  139.                 move.PerformClick();
  140.             }
  141.             catch
  142.             {
  143.  
  144.             }
  145.  
  146.  
  147.          
  148.            
  149.         }
  150.  
  151.         private Button look_for_open_space()
  152.         {
  153.             Console.WriteLine("Looking for open space");
  154.             Button b = null;
  155.             foreach (Control c in Controls)
  156.             {
  157.                 b = c as Button;
  158.                 if (b != null)
  159.                 {
  160.                     if (b.Text == "")
  161.                         return b;
  162.                 }//end if
  163.             }//end if
  164.  
  165.             return null;
  166.         }
  167.  
  168.         private Button look_for_corner()
  169.         {
  170.             Console.WriteLine("Looking for corner");
  171.             if (A1.Text == "O")
  172.             {
  173.                 if (A3.Text == "")
  174.                     return A3;
  175.                 if (C3.Text == "")
  176.                     return C3;
  177.                 if (C1.Text == "")
  178.                     return C1;
  179.             }
  180.  
  181.             if (A3.Text == "O")
  182.             {
  183.                 if (A1.Text == "")
  184.                     return A1;
  185.                 if (C3.Text == "")
  186.                     return C3;
  187.                 if (C1.Text == "")
  188.                     return C1;
  189.             }
  190.  
  191.             if (C3.Text == "O")
  192.             {
  193.                 if (A1.Text == "")
  194.                     return A3;
  195.                 if (A3.Text == "")
  196.                     return A3;
  197.                 if (C1.Text == "")
  198.                     return C1;
  199.             }
  200.  
  201.             if (C1.Text == "O")
  202.             {
  203.                 if (A1.Text == "")
  204.                     return A3;
  205.                 if (A3.Text == "")
  206.                     return A3;
  207.                 if (C3.Text == "")
  208.                     return C3;
  209.             }
  210.  
  211.             if (A1.Text == "")
  212.                 return A1;
  213.             if (A3.Text == "")
  214.                 return A3;
  215.             if (C1.Text == "")
  216.                 return C1;
  217.             if (C3.Text == "")
  218.                 return C3;
  219.  
  220.             return null;
  221.         }
  222.  
  223.         private Button look_for_win_or_block(string mark)
  224.         {
  225.             Console.WriteLine("Looking for win or block:  " + mark);
  226.             //HORIZONTAL TESTS
  227.             if ((A1.Text == mark) && (A2.Text == mark) && (A3.Text == ""))
  228.                 return A3;
  229.             if ((A2.Text == mark) && (A3.Text == mark) && (A1.Text == ""))
  230.                 return A1;
  231.             if ((A1.Text == mark) && (A3.Text == mark) && (A2.Text == ""))
  232.                 return A2;
  233.  
  234.             if ((B1.Text == mark) && (B2.Text == mark) && (B3.Text == ""))
  235.                 return B3;
  236.             if ((B2.Text == mark) && (B3.Text == mark) && (B1.Text == ""))
  237.                 return B1;
  238.             if ((B1.Text == mark) && (B3.Text == mark) && (B2.Text == ""))
  239.                 return B2;
  240.  
  241.             if ((C1.Text == mark) && (C2.Text == mark) && (C3.Text == ""))
  242.                 return C3;
  243.             if ((C2.Text == mark) && (C3.Text == mark) && (C1.Text == ""))
  244.                 return C1;
  245.             if ((C1.Text == mark) && (C3.Text == mark) && (C2.Text == ""))
  246.                 return C2;
  247.  
  248.             //VERTICAL TESTS
  249.             if ((A1.Text == mark) && (B1.Text == mark) && (C1.Text == ""))
  250.                 return C1;
  251.             if ((B1.Text == mark) && (C1.Text == mark) && (A1.Text == ""))
  252.                 return A1;
  253.             if ((A1.Text == mark) && (C1.Text == mark) && (B1.Text == ""))
  254.                 return B1;
  255.  
  256.             if ((A2.Text == mark) && (B2.Text == mark) && (C2.Text == ""))
  257.                 return C2;
  258.             if ((B2.Text == mark) && (C2.Text == mark) && (A2.Text == ""))
  259.                 return A2;
  260.             if ((A2.Text == mark) && (C2.Text == mark) && (B2.Text == ""))
  261.                 return B2;
  262.  
  263.             if ((A3.Text == mark) && (B3.Text == mark) && (C3.Text == ""))
  264.                 return C3;
  265.             if ((B3.Text == mark) && (C3.Text == mark) && (A3.Text == ""))
  266.                 return A3;
  267.             if ((A3.Text == mark) && (C3.Text == mark) && (B3.Text == ""))
  268.                 return B3;
  269.  
  270.             //DIAGONAL TESTS
  271.             if ((A1.Text == mark) && (B2.Text == mark) && (C3.Text == ""))
  272.                 return C3;
  273.             if ((B2.Text == mark) && (C3.Text == mark) && (A1.Text == ""))
  274.                 return A1;
  275.             if ((A1.Text == mark) && (C3.Text == mark) && (B2.Text == ""))
  276.                 return B2;
  277.  
  278.             if ((A3.Text == mark) && (B2.Text == mark) && (C1.Text == ""))
  279.                 return C1;
  280.             if ((B2.Text == mark) && (C1.Text == mark) && (A3.Text == ""))
  281.                 return A3;
  282.             if ((A3.Text == mark) && (C1.Text == mark) && (B2.Text == ""))
  283.                 return B2;
  284.  
  285.             return null;
  286.         }
  287.         */
  288.         private void checkForWinner()
  289.         {
  290.             for (int i = 0; i < 3; i++)
  291.             {
  292.                 if ((buttonArray[0, i].Text == buttonArray[1, i].Text) && (buttonArray[1, i].Text == buttonArray[2, i].Text) && (!buttonArray[0, i].Enabled))
  293.                     there_is_a_winner = true;
  294.                 if ((buttonArray[i, 0].Text == buttonArray[i, 1].Text) && (buttonArray[i, 1].Text == buttonArray[i, 2].Text) && (!buttonArray[i, 0].Enabled))
  295.                     there_is_a_winner = true;
  296.             }
  297.            
  298.                 if ((buttonArray[0, 0].Text == buttonArray[1, 1].Text) && (buttonArray[1, 1].Text == buttonArray[2, 2].Text) && (!buttonArray[0, 0].Enabled))
  299.                     there_is_a_winner = true;
  300.  
  301.                 if ((buttonArray[0, 2].Text == buttonArray[1, 1].Text) && (buttonArray[1, 1].Text == buttonArray[2, 0].Text) && (!buttonArray[0, 2].Enabled))
  302.                     there_is_a_winner = true;
  303.            
  304.  
  305.            
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.             //Horizontal
  314.             //      if ((buttonArray[0,0].Text == buttonArray[1,0].Text) && (buttonArray[1,0].Text == buttonArray[2,0].Text) && (!buttonArray[0,0].Enabled))
  315.             //          there_is_a_winner = true;
  316.             //     else if ((buttonArray[0, 1].Text == buttonArray[1, 1].Text) && (buttonArray[1, 1].Text == buttonArray[2, 1].Text) && (!buttonArray[0, 1].Enabled))
  317.             //         there_is_a_winner = true;
  318.             //     else if ((buttonArray[0, 2].Text == buttonArray[1, 2].Text) && (buttonArray[1, 2].Text == buttonArray[2, 2].Text) && (!buttonArray[0, 2].Enabled))
  319.             //       there_is_a_winner = true;
  320.             /*     // Verticall
  321.                  else if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
  322.                      there_is_a_winner = true;
  323.                  else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
  324.                      there_is_a_winner = true;
  325.                  else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
  326.                      there_is_a_winner = true;
  327.                  // Diagonal
  328.          
  329.                  else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
  330.                      there_is_a_winner = true;
  331.                  else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
  332.                      there_is_a_winner = true;
  333.                  */
  334.  
  335.  
  336.             if (there_is_a_winner)
  337.             {
  338.                 disableButtons();
  339.  
  340.                 String winner = "";
  341.                 if (isX)
  342.                 {
  343.  
  344.                     winner = p2.Text;
  345.                     o_win_count.Text = (Int32.Parse(o_win_count.Text) + 1).ToString();
  346.                     newGameToolStripMenuItem.PerformClick();
  347.  
  348.                 }
  349.                 else
  350.                 {
  351.                     winner = p1.Text;
  352.                     x_win_count.Text = (Int32.Parse(x_win_count.Text) + 1).ToString();
  353.                 }
  354.                 MessageBox.Show(winner + " Wins", "Yay!");
  355.                 newGameToolStripMenuItem.PerformClick();
  356.             }
  357.             else
  358.             {
  359.                 if (turn_count == 9)
  360.                 {
  361.                     draw_count.Text = (Int32.Parse(draw_count.Text) + 1).ToString();
  362.                     MessageBox.Show("Draw!", " Bummer");
  363.                     newGameToolStripMenuItem.PerformClick();
  364.  
  365.                 }
  366.             }
  367.  
  368.         }//end check
  369.  
  370.         private void disableButtons()
  371.         {
  372.             try
  373.             {
  374.                 foreach (Control c in Controls)
  375.                 {
  376.                     Button b = (Button)c;
  377.                     b.Enabled = false;
  378.                 }
  379.             }
  380.             catch { }
  381.  
  382.         }
  383.  
  384.  
  385.  
  386.         private void tictactoe_Load(object sender, EventArgs e)
  387.         {
  388.  
  389.             foreach (Button ctrlBtn in buttonArray)
  390.             {
  391.                 // ctrlBtn.Click += new System.EventHandler(this.DrawCharacter);
  392.             }
  393.             InitGame();
  394.         }
  395.         private void InitGame()
  396.         {
  397.             foreach (Button btn in buttonArray)
  398.             {
  399.                 btn.Text = "";
  400.                 btn.BackColor = Color.Transparent;
  401.                 btn.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F,
  402.                 System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
  403.             }
  404.             //     isGameOver = false;
  405.             there_is_a_winner = false;
  406.             isX = true;
  407.         }
  408.  
  409.         private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
  410.         {
  411.             isX = true;
  412.             turn_count = 0;
  413.  
  414.             foreach (Control c in Controls)
  415.             {
  416.                 try
  417.                 {
  418.                     Button b = (Button)c;
  419.                     b.Enabled = true;
  420.                     b.Text = "";
  421.                 }
  422.                 catch { }
  423.  
  424.  
  425.             }
  426.  
  427.  
  428.         }
  429.  
  430.         private void button_enter(object sender, EventArgs e)
  431.         {
  432.             Button b = (Button)sender;
  433.             if (b.Enabled)
  434.             {
  435.                 if (isX)
  436.                     b.Text = "X";
  437.                 else
  438.  
  439.                     b.Text = "O";
  440.             }
  441.  
  442.  
  443.         }
  444.  
  445.         private void button_leave(object sender, EventArgs e)
  446.         {
  447.             Button b = (Button)sender;
  448.             if (b.Enabled)
  449.             {
  450.                 b.Text = "";
  451.             }
  452.  
  453.         }
  454.  
  455.         private void label2_Click(object sender, EventArgs e)
  456.         {
  457.  
  458.         }
  459.  
  460.         private void resetWinCountToolStripMenuItem_Click(object sender, EventArgs e)
  461.         {
  462.             o_win_count.Text = "0";
  463.             x_win_count.Text = "0";
  464.             draw_count.Text = "0";
  465.         }
  466.  
  467.         private void tictactoe_Load_1(object sender, EventArgs e)
  468.         {
  469.             /*
  470.             Form f2 = new Form2();
  471.             f2.ShowDialog();
  472.             p1.Text = player1;
  473.             p2.Text = player2; */
  474.             setPlayerDefaultToolStripMenuItem.PerformClick();
  475.  
  476.         }
  477.  
  478.         private void p2_TextChanged(object sender, EventArgs e)
  479.         {
  480.             if (p2.Text.ToUpper() == "COMPUTER")
  481.                 against_computer = true;
  482.             else
  483.                 against_computer = false;
  484.  
  485.  
  486.         }
  487.  
  488.         private void setPlayerDefaultToolStripMenuItem_Click(object sender, EventArgs e)
  489.         {
  490.             p1.Text = "Or";
  491.             p2.Text = "Computer";
  492.  
  493.         }
  494.     }
  495.  
  496. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement