I am making a basic Player vs. Computer Tic-Tac-Toe in Visual Studios Express 2008. My Form.cs is

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

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;
        }
    }
}

Line 3 gets your random number, but you never get another one if the square picked is already used. Your while loop will never end thus you get your 'lock up'. Move line 3 to between line 5 and 6.

Wow thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.