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 7515005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:39:52+00:00 2026-05-30T00:39:52+00:00

I’m creating a little tic tac toe game and I generate the computer’s move

  • 0

I’m creating a little tic tac toe game and I generate the computer’s move randomly using this function:

//Generates the computer's move randomly
void tictactoe::getComputerMove( char** b )
{
    int rand1,rand2;

    //Generate two random numbers
    rand1 = rand() % 2;//0-2
    rand2 = rand() % 2;//0-2

    //Check if that spot is taken yet.
    if( b[rand1][rand2] == ' ' )//Empty
            b[rand1][rand2] = 'O';

    else //Already filled.
        getComputerMove( b );
}

That function is called in a loop immediately after the human user makes their move. The reason I think this is the issue is because the message I get is dealing with randoms and this is the only place I use them. I have these files included:

#include "tictactoe.h" //Homemade header file
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>

The error message generated by valgrind looks like this:

==31280== Stack overflow in thread 1: can't grow stack to 0xbe398ff8
==31280== 
==31280== Process terminating with default action of signal 11 (SIGSEGV)
==31280==  Access not within mapped region at address 0xBE398FF8
==31280==    at 0x41AB4BF: random_r (random_r.c:364)
==31280==  If you believe this happened as a result of a stack
==31280==  overflow in your program's main thread (unlikely but
==31280==  possible), you can try to increase the size of the
==31280==  main thread stack using the --main-stacksize= flag.
==31280==  The main thread stack size used in this run was 8388608.
==31280== Stack overflow in thread 1: can't grow stack to 0xbe398ff4
==31280== 
==31280== Process terminating with default action of signal 11 (SIGSEGV)
==31280==  Access not within mapped region at address 0xBE398FF4
==31280==    at 0x4021430: _vgnU_freeres (vg_preloaded.c:58)
==31280==  If you believe this happened as a result of a stack
==31280==  overflow in your program's main thread (unlikely but
==31280==  possible), you can try to increase the size of the
==31280==  main thread stack using the --main-stacksize= flag.
==31280==  The main thread stack size used in this run was 8388608.
==31280== 
==31280== HEAP SUMMARY:
==31280==     in use at exit: 21 bytes in 4 blocks
==31280==   total heap usage: 4 allocs, 0 frees, 21 bytes allocated
==31280== 
==31280== LEAK SUMMARY:
==31280==    definitely lost: 0 bytes in 0 blocks
==31280==    indirectly lost: 0 bytes in 0 blocks
==31280==      possibly lost: 0 bytes in 0 blocks
==31280==    still reachable: 21 bytes in 4 blocks
==31280==         suppressed: 0 bytes in 0 blocks
==31280== Rerun with --leak-check=full to see details of leaked memory
==31280== 
==31280== For counts of detected and suppressed errors, rerun with: -v
==31280== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 20 from 9)
Segmentation fault

Code to create the 2d array:

char** tictactoe::createBoard()
{
//Initialize it to something
char **board;

    //Allocate memory for the array
    board = new char* [3];

    for( int i = 0; i < 3; i++ )
    {
        board[i] = new char[3];
    }

    //Now fill each slot with a dummy ' '
    for( int i = 0; i < 3; i++ )
    {
        for( int j = 0; j < 3; j++ )
            board[i][j] = ' ';
    }

    return board;
}
  • 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-05-30T00:39:54+00:00Added an answer on May 30, 2026 at 12:39 am

    Your mod operation rand() % 2 will only output a 0 or a 1, you should use rand() % 3. What is probably happening is that you are only randomly exploring a 2×2 sub-matrix of the whole board.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.