Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:22:52+00:00 2026-06-14T11:22:52+00:00

I have created a tic tac toe program that works in that it allows

  • 0

I have created a tic tac toe program that works in that it allows a player to select a move in a 2d array by selecting the cords, then the computer makes a move.

The program works, but the computer’s move is sequential rather then random and will always select the next available space in the array. Now that I have the game working, I want to improve the program into three levels of difficulty:

Easy = Randomly generated move by computer
Medium = Randomly generated move by computer that checks to block player and complete line
Hard = Selection of optimal move every time through recursion

How I can get the computer to randomly select a set of cords in my array?

(My current basic for loop for computers move)

static void Computermove(char[,] gamegrid, char fin)
    {

        Console.WriteLine("\nComputer's turn");
        Console.ReadKey();

        int x = 0;
        int y = 0;
        for (x = 0; x < 2; x++)
        {
            for (y = 0; y < 2; y++)
                if (gamegrid[x, y] == ' ') break;
            if (gamegrid[x, y] == ' ') break;
        }

        if (x * y == 9)
        {
            fin = 'X';
        }
        else
            gamegrid[x, y] = 'O';

    } // End Computermove
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:22:53+00:00Added an answer on June 14, 2026 at 11:22 am

    Create a new instance of the Random class and use the Next() method to create the two random numbers representing the coordinates.

    Here’s the code:

    var r = new Random();
    
    var randomX = r.Next(3);
    var randomY = r.Next(3);
    

    Update:

    Here’s how the method could look like:

    static void Computermove(char[,] gamegrid, char fin)
    {
        if (!ContainsEmptyCells(gamegrid))
        {
            // table is full; probably nobody won
            return;
        }
    
        bool goodRandomCoordinateFound = false;
    
        int row = 0, column = 0;
    
        var random = new Random();
    
        while (!goodRandomCoordinateFound)
        {
            row = random.Next(3);
            column = random.Next(3);
    
            if(gamegrid[row, column] == ' ')
                goodRandomCoordinateFound = true;
        }
    
        gamegrid[row, column] = 'O';
    }
    

    I found the problem that was causing the StackOverflowException. It is a pretty subtle one, but the conclusion is that when retrying to generate random coordinates, the same instance of Random should be used, instead of creating a new one.

    This is because Random does not really generate truly random numbers. When a new Random() instance is created, it is initialized with a seed value that is based on the current time.

    If you create multiple instances of Random with the same seed value, they will create the same random numbers stream.

    In our example, in case we needed new random coordinates to be generated, a new instance of Random was created without specifying a seed, so the seed was using the current time. Due to the fact that the random instances were created extremely quickly, the seed value was the same, therefore the random values were the same, causing infinite recursion.

    I’ve rewritten the method to reuse the random instance, which causes subsequent calls to Next(3) to yield other values than the ones we currently have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a notepad, a Tic Tac Toe and a scientific calculator. All
I have just created a java tic-tac-toe game i would like to figure out
I am making a Tic Tac Toe game and i created a function that
I have created a tic-tac-toe game in php. I have created the following code
I have created a little program that reads in a Java file and feeds
I'm making a Tic-tac-toe game and i have a board class which contains 9
I am working on a Tic-Tac-Toe Game. I have added the 9 JButtons with
For my c++ homework assignment, I have to write a Tic-Tac-Toe Win32 console application.
Currently I have a tic tac toe board tttBoard with a constructor tttBoard::tttBoard() {
I have created some JQuery that will expand a div 'popup' on hover and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.