Tic-Tac-Toe help

Discussion in 'C#' started by Romil797, Aug 29, 2010.

  1. Romil797

    Romil797 New Member

    Joined:
    Aug 29, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am making a basic Player vs. Computer Tic-Tac-Toe in Visual Studios Express 2008. My Form.cs is
    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 Tic_Tac_Toe
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public Image x = Image.FromFile("X.png");
            public Image y = Image.FromFile("Y.jpg");
            public Boolean pTur = true;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                int n;
                pictureBox1 = new PictureBox[9];
                pictureBox1[0] = pictureBox2;
                pictureBox1[1] = pictureBox3;
                pictureBox1[2] = pictureBox4;
                pictureBox1[3] = pictureBox5;
                pictureBox1[4] = pictureBox6;
                pictureBox1[5] = pictureBox7;
                pictureBox1[6] = pictureBox8;
                pictureBox1[7] = pictureBox9;
                pictureBox1[8] = pictureBox10;
                for (n = 0; n < 9; n++)
                {
                    this.pictureBox1[n].Click += new System.EventHandler(this.oPc);
                }
    
    
            }
            private void oPc(object sender, EventArgs e)
            {
                PictureBox sen = (PictureBox)sender;
                if (pTur == true)
                {
                    if (sen.BackgroundImage == null)
                    {
                        sen.BackgroundImage = x;
                        pTur = false;
                        //xOne();
                        cPc();
                    }
                }
            }
            void cPc()
            {
                Random trytg = new Random();
                int trytg1 = trytg.Next(0, 9);
                Boolean r = true;
                while (r == true)
                {
                    if (pictureBox1[trytg1].BackgroundImage == null)
                    {
                        pictureBox1[trytg1].BackgroundImage = y;
                        pTur = true;
                        r = false;
                    }
    
                }
            }
    
            public Boolean hasX(PictureBox sende)
            {
    
                if (sende.BackgroundImage == x)
                {
                    return true;
                }
                else
                {
                    return false;
                }
    
                
            }
            public Boolean hasY(PictureBox sende)
            {
    
                if (sende.BackgroundImage == y)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    
    Sometimes, it just freezes when I click on a cell and "thinks" forever. My computer is exceptionally fast, so what is the problem... maybe random numbers? Please help quickly
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice