CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Posts
    0

    i have errors with tic tac toe game

    i am trying to learn c# looking examples.
    there is one game i am looking to code i understand everything on it.But when i write it exactly same thing there errors .how can i fix them.

    this is code
    Code:
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace tictactoe
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            int fon;
    
            Random r = new Random();
            private void Form1_Load(object sender, EventArgs e)
            {
                fon = r.Next(1, 4);
    
                timer1.Start(); 
                timer2.Start();
                button5.Text = "O"; 
                button5.Enabled = false; 
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button1.Text = "X";
                    button1.Enabled = false;
                    button4.Text = "0";
                    button4.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button1.Text = "X";
                    button1.Enabled = false;
                    button3.Text = "0";
                    button3.Enabled = false;
                }
                else if (fon == 3)
                {
                    button1.Text = "X";
                    button1.Enabled = false;
                    button6.Text = "0";
                    button6.Enabled = false;
                }
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button2.Text = "X";
                    button2.Enabled = false;
                    button6.Text = "0";
                    button6.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button2.Text = "X";
                    button2.Enabled = false;
                    button4.Text = "0";
                    button4.Enabled = false;
                }
                else if (fon == 3)
                {
                    button2.Text = "X";
                    button2.Enabled = false;
                    button7.Text = "0";
                    button7.Enabled = false;
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button3.Text = "X";
                    button3.Enabled = false;
                    button8.Text = "0";
                    button8.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button3.Text = "X";
                    button3.Enabled = false;
                    button1.Text = "0";
                    button1.Enabled = false;
                }
                else if (fon == 3)
                {
                    button3.Text = "X";
                    button3.Enabled = false;
                    button9.Text = "0";
                    button9.Enabled = false;
    
                }
    
    
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button4.Text = "X";
                    button4.Enabled = false;
                    button1.Text = "0";
                    button1.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button4.Text = "X";
                    button4.Enabled = false;
                    button2.Text = "0";
                    button2.Enabled = false;
                }
                else if (fon == 3)
                {
                    button4.Text = "X";
                    button4.Enabled = false;
                    button8.Text = "0";
                    button8.Enabled = false;
                }
    
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
            }
            private void button6_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button6.Text = "X";
                    button6.Enabled = false;
                    button2.Text = "0";
                    button2.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button6.Text = "X";
                    button6.Enabled = false;
                    button9.Text = "0";
                    button9.Enabled = false;
                }
                else if (fon == 3)
                {
                    button6.Text = "X";
                    button6.Enabled = false;
                    button1.Text = "0";
                    button1.Enabled = false;
                }
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button7.Text = "X";
                    button7.Enabled = false;
                    button9.Text = "0";
                    button9.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button7.Text = "X";
                    button7.Enabled = false;
                    button8.Text = "0";
                    button8.Enabled = false;
                }
                else if (fon == 3)
                {
                    button7.Text = "X";
                    button7.Enabled = false;
                    button2.Text = "0";
                    button2.Enabled = false;
                }
    
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button8.Text = "X";
                    button8.Enabled = false;
                    button3.Text = "0";
                    button3.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button8.Text = "X";
                    button8.Enabled = false;
                    button7.Text = "0";
                    button7.Enabled = false;
                }
                else if (fon == 3)
                {
                    button8.Text = "X";
                    button8.Enabled = false;
                    button4.Text = "0";
                    button4.Enabled = false;
                }
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button9.Text = "X";
                    button9.Enabled = false;
                    button7.Text = "0";
                    button7.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button9.Text = "X";
                    button9.Enabled = false;
                    button6.Text = "0";
                    button6.Enabled = false;
                }
                else if (fon == 3)
                {
                    button9.Text = "X";
                    button9.Enabled = false;
                    button3.Text = "0";
                    button3.Enabled = false;
                }
            }
    
            bool kazan = false;
            bool kazan2 = false;
            private void timer1_Tick(object sender, EventArgs e)
            {
                
    
                if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button1.Text == "X" && button4.Text == "X" && button7.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button4.Text == "X" && button5.Text == "X" && button6.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button3.Text == "X" && button6.Text == "X" && button9.Text == "X")
                {
                    kazan = true;
    
                else if (button2.Text == "X" && button5.Text == "X" && button8.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button7.Text == "X" && button8.Text == "X" && button9.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button3.Text == "X" && button5.Text == "X" && button7.Text == "X")
                {
                    kazan = true;
                }
    
    
    
    
                else if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button1.Text == "O" && button5.Text == "O" && button9.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button1.Text == "O" && button4.Text == "O" && button7.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button3.Text == "O" && button6.Text == "O" && button9.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button2.Text == "O" && button5.Text == "O" && button8.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button7.Text == "O" && button8.Text == "O" && button9.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O")
                {
                    kazan2 = true;
                }
    
            }}
    
    
    
            private void timer2_Tick(object sender, EventArgs e)
            {
                
    
                if (kazan == true)
                {
                    timer2.Stop();
                    MessageBox.Show(" you win");
                    
                    timer2.Start();
                    fon = r.Next(1, 4); ;
                    button1.Enabled = true;
                    button1.Text = " ";
                    button2.Enabled = true;
                    button2.Text = " ";
                    button3.Enabled = true;
                    button3.Text = " ";
                    button4.Enabled = true;
                    button4.Text = " ";
                    button5.Enabled = false;
                    button5.Text = "O";
                    button6.Enabled = true;
                    button6.Text = " ";
                    button7.Enabled = true;
                    button7.Text = " ";
                    button8.Enabled = true;
                    button8.Text = " ";
                    button9.Enabled = true;
                    button9.Text = " ";
                    kazan = false;
                    kazan2 = false;
                }
    
                else if (kazan2 == true)
                {
                    timer2.Stop();
                    MessageBox.Show(" pc won");
                    
                    timer2.Start();
                    fon = r.Next(1, 4);
                    button1.Enabled = true;
                    button1.Text = " ";
                    button2.Enabled = true;
                    button2.Text = " ";
                    button3.Enabled = true;
                    button3.Text = " ";
                    button4.Enabled = true;
                    button4.Text = " ";
                    button5.Enabled = false;
                    button5.Text = "O";
                    button6.Enabled = true;
                    button6.Text = " ";
                    button7.Enabled = true;
                    button7.Text = " ";
                    button8.Enabled = true;
                    button8.Text = " ";
                    button9.Enabled = true;
                    button9.Text = " ";
                    kazan = false;
                    kazan2 = false;
                }
    
                else if (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 && kazan == false && kazan2 == false)
                {
                    
    
                    timer2.Stop();
                    MessageBox.Show("no winner");
                    button1.Enabled = true;
                    button1.Text = " ";
                    button2.Enabled = true;
                    button2.Text = " ";
                    button3.Enabled = true;
                    button3.Text = " ";
                    button4.Enabled = true;
                    button4.Text = " ";
                    button5.Enabled = false;
                    button5.Text = "O";
                    button6.Enabled = true;
                    button6.Text = " ";
                    button7.Enabled = true;
                    button7.Text = " ";
                    button8.Enabled = true;
                    button8.Text = " ";
                    button9.Enabled = true;
                    button9.Text = " ";
                    kazan = false;
                    kazan2 = false;
                    timer2.Start();
                    fon = r.Next(1, 4);
                }
    
            }
    
        }
    
    
    }
    this is game
    Code:
    http://tamamsin.com/xox.rar

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: i have errors with tic tac toe game

    What errors do you get?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: i have errors with tic tac toe game

    ^^ and where does the error occur?
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: i have errors with tic tac toe game

    Quote Originally Posted by zeybekli View Post
    i am trying to learn c# looking examples.
    there is one game i am looking to code i understand everything on it.But when i write it exactly same thing there errors .how can i fix them.

    this is code
    Code:
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace tictactoe
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            int fon;
    
            Random r = new Random();
            private void Form1_Load(object sender, EventArgs e)
            {
                fon = r.Next(1, 4);
    
                timer1.Start(); 
                timer2.Start();
                button5.Text = "O"; 
                button5.Enabled = false; 
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button1.Text = "X";
                    button1.Enabled = false;
                    button4.Text = "0";
                    button4.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button1.Text = "X";
                    button1.Enabled = false;
                    button3.Text = "0";
                    button3.Enabled = false;
                }
                else if (fon == 3)
                {
                    button1.Text = "X";
                    button1.Enabled = false;
                    button6.Text = "0";
                    button6.Enabled = false;
                }
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button2.Text = "X";
                    button2.Enabled = false;
                    button6.Text = "0";
                    button6.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button2.Text = "X";
                    button2.Enabled = false;
                    button4.Text = "0";
                    button4.Enabled = false;
                }
                else if (fon == 3)
                {
                    button2.Text = "X";
                    button2.Enabled = false;
                    button7.Text = "0";
                    button7.Enabled = false;
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button3.Text = "X";
                    button3.Enabled = false;
                    button8.Text = "0";
                    button8.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button3.Text = "X";
                    button3.Enabled = false;
                    button1.Text = "0";
                    button1.Enabled = false;
                }
                else if (fon == 3)
                {
                    button3.Text = "X";
                    button3.Enabled = false;
                    button9.Text = "0";
                    button9.Enabled = false;
    
                }
    
    
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button4.Text = "X";
                    button4.Enabled = false;
                    button1.Text = "0";
                    button1.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button4.Text = "X";
                    button4.Enabled = false;
                    button2.Text = "0";
                    button2.Enabled = false;
                }
                else if (fon == 3)
                {
                    button4.Text = "X";
                    button4.Enabled = false;
                    button8.Text = "0";
                    button8.Enabled = false;
                }
    
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
            }
            private void button6_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button6.Text = "X";
                    button6.Enabled = false;
                    button2.Text = "0";
                    button2.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button6.Text = "X";
                    button6.Enabled = false;
                    button9.Text = "0";
                    button9.Enabled = false;
                }
                else if (fon == 3)
                {
                    button6.Text = "X";
                    button6.Enabled = false;
                    button1.Text = "0";
                    button1.Enabled = false;
                }
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button7.Text = "X";
                    button7.Enabled = false;
                    button9.Text = "0";
                    button9.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button7.Text = "X";
                    button7.Enabled = false;
                    button8.Text = "0";
                    button8.Enabled = false;
                }
                else if (fon == 3)
                {
                    button7.Text = "X";
                    button7.Enabled = false;
                    button2.Text = "0";
                    button2.Enabled = false;
                }
    
            }
    
            private void button8_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button8.Text = "X";
                    button8.Enabled = false;
                    button3.Text = "0";
                    button3.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button8.Text = "X";
                    button8.Enabled = false;
                    button7.Text = "0";
                    button7.Enabled = false;
                }
                else if (fon == 3)
                {
                    button8.Text = "X";
                    button8.Enabled = false;
                    button4.Text = "0";
                    button4.Enabled = false;
                }
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
                if (fon == 1)
                {
                    button9.Text = "X";
                    button9.Enabled = false;
                    button7.Text = "0";
                    button7.Enabled = false;
    
                }
                else if (fon == 2)
                {
                    button9.Text = "X";
                    button9.Enabled = false;
                    button6.Text = "0";
                    button6.Enabled = false;
                }
                else if (fon == 3)
                {
                    button9.Text = "X";
                    button9.Enabled = false;
                    button3.Text = "0";
                    button3.Enabled = false;
                }
            }
    
            bool kazan = false;
            bool kazan2 = false;
            private void timer1_Tick(object sender, EventArgs e)
            {
                
    
                if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button1.Text == "X" && button4.Text == "X" && button7.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button4.Text == "X" && button5.Text == "X" && button6.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button3.Text == "X" && button6.Text == "X" && button9.Text == "X")
                {
                    kazan = true;
    
                else if (button2.Text == "X" && button5.Text == "X" && button8.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button7.Text == "X" && button8.Text == "X" && button9.Text == "X")
                {
                    kazan = true;
                }
    
                else if (button3.Text == "X" && button5.Text == "X" && button7.Text == "X")
                {
                    kazan = true;
                }
    
    
    
    
                else if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button1.Text == "O" && button5.Text == "O" && button9.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button1.Text == "O" && button4.Text == "O" && button7.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button3.Text == "O" && button6.Text == "O" && button9.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button2.Text == "O" && button5.Text == "O" && button8.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button7.Text == "O" && button8.Text == "O" && button9.Text == "O")
                {
                    kazan2 = true;
                }
    
                else if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O")
                {
                    kazan2 = true;
                }
    
            }}
    
    
    
            private void timer2_Tick(object sender, EventArgs e)
            {
                
    
                if (kazan == true)
                {
                    timer2.Stop();
                    MessageBox.Show(" you win");
                    
                    timer2.Start();
                    fon = r.Next(1, 4); ;
                    button1.Enabled = true;
                    button1.Text = " ";
                    button2.Enabled = true;
                    button2.Text = " ";
                    button3.Enabled = true;
                    button3.Text = " ";
                    button4.Enabled = true;
                    button4.Text = " ";
                    button5.Enabled = false;
                    button5.Text = "O";
                    button6.Enabled = true;
                    button6.Text = " ";
                    button7.Enabled = true;
                    button7.Text = " ";
                    button8.Enabled = true;
                    button8.Text = " ";
                    button9.Enabled = true;
                    button9.Text = " ";
                    kazan = false;
                    kazan2 = false;
                }
    
                else if (kazan2 == true)
                {
                    timer2.Stop();
                    MessageBox.Show(" pc won");
                    
                    timer2.Start();
                    fon = r.Next(1, 4);
                    button1.Enabled = true;
                    button1.Text = " ";
                    button2.Enabled = true;
                    button2.Text = " ";
                    button3.Enabled = true;
                    button3.Text = " ";
                    button4.Enabled = true;
                    button4.Text = " ";
                    button5.Enabled = false;
                    button5.Text = "O";
                    button6.Enabled = true;
                    button6.Text = " ";
                    button7.Enabled = true;
                    button7.Text = " ";
                    button8.Enabled = true;
                    button8.Text = " ";
                    button9.Enabled = true;
                    button9.Text = " ";
                    kazan = false;
                    kazan2 = false;
                }
    
                else if (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 && kazan == false && kazan2 == false)
                {
                    
    
                    timer2.Stop();
                    MessageBox.Show("no winner");
                    button1.Enabled = true;
                    button1.Text = " ";
                    button2.Enabled = true;
                    button2.Text = " ";
                    button3.Enabled = true;
                    button3.Text = " ";
                    button4.Enabled = true;
                    button4.Text = " ";
                    button5.Enabled = false;
                    button5.Text = "O";
                    button6.Enabled = true;
                    button6.Text = " ";
                    button7.Enabled = true;
                    button7.Text = " ";
                    button8.Enabled = true;
                    button8.Text = " ";
                    button9.Enabled = true;
                    button9.Text = " ";
                    kazan = false;
                    kazan2 = false;
                    timer2.Start();
                    fon = r.Next(1, 4);
                }
    
            }
    
        }
    
    
    }
    this is game
    Code:
    http://tamamsin.com/xox.rar


    Looks like some formatting errors in timer1 code
    1st error
    Code:
                else if (button3.Text == "X" && button6.Text == "X" && button9.Text == "X")
                {
                    kazan = true;
                    // Needs a closing bracket before calling next else if statement
                else if (button2.Text == "X" && button5.Text == "X" && button8.Text == "X")
                {
                    kazan = true;
                }
    Code:
                else if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O")
                {
                    kazan2 = true;
                }
    
            }} < ---- Here
    By doing that your code becomes this
    Code:
                else if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O")
                {
                    kazan2 = true;
                }
    
            }
    } // You close your class code here and timer 2 gets excluded
    To fix the second error just remove one of the two } unless that is suppose to be the end of your class code.

    Also quick question which is possible but unlikely. Is it possible you forgot to create the timers? Without you mentioning what errors you are getting we can only guess the problems you are having but I found at least those two formatting errors I mentioned.

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: i have errors with tic tac toe game

    Outdoing us lazy folk, eh Ubiquitous? :-)

    Good work as usual.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: i have errors with tic tac toe game

    Well since I do not have as much experience as most of you guys whenever a simpler problem like this arises I try to jump on it and help out

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured