1

this is my first time asking a question on this forum so here it goes. I am creating a tic-tac-toe game for practice and am using enumerators and recursion because I have never really done enumeration and could always get some recursion practice in. Well anyway I just finished coding for the player2 to take a random move and after about 3 turns it gives a segmentation fault and I cannot figure out why... I hope you guys can figure it out and thank you!

#include <iostream>
#include <string>
#include <cstdlib>

const int size = 3;

enum play {none,X,O};

void NPC(play (&board)[size][size],play player2) {
  srand(time(NULL));
  int tempx = rand() % 3;
  int tempy = rand() % 3;
  if(board[tempx][tempy] == none)
    board[tempx][tempy] = player2;
  else
    NPC(board,player2);
}

void getBoardState(play (&board)[size][size],int y,int x) {
  if(board[x][y] == none) std::cout << " ";
  else if(board[x][y] == X) std::cout << "X";
  else std::cout << "O";
}

void printboard(play (&board)[size][size]){
  int length = 4 * size - 1;
  for(int i = 1; i <= length; i++) {
    for(int j = 1; j <= length; j++) {
      if(i % 4 == 0 && j % 4 == 0) std::cout << "+";
      else if(i % 4 == 0) std::cout << "-";
      else if(j % 4 == 0) std::cout << "|";
      else if(i % 2 == 0 && j % 2 == 0) getBoardState(board,(i - 2)/4,(j - 2)/4);
      else std::cout << " ";
    }
    std::cout << std::endl;
  }
}

int main() {
  play player = O, player2 = X;
  bool over = false;
  play board[size][size];
  for(int i = 0; i < size; i++) {
    for(int j = 0; j < size; j++) {
      board[i][j] = none;
    }
  }
  std::string player1 = "";
  std::cout << "What would You like to be? An X or an O?" << std::endl;
  while(((player1 != "X") + (player1 != "O")) == 2) {
    std::cin >> player1;
    if(((player1 != "X") + (player1 != "O")) == 2)
      std::cout << "Invalid entry! Please enter X or an 0!" << std::endl;
    }
  if(player1 == "X") {
    player2 = O;
    player = X;}
  int tempx,tempy;
  while(!over) {
    std::cout << "Please enter an x and then a y (1 to " << size << ")" << std::endl;
    std::cin >> tempx;
    std::cin >> tempy;
    while(tempx > size || tempy > size || board[tempx-1][tempy-1] != none) {
      std::cout << "Invalid entry! Try again!" << std::endl;
      std::cin >> tempx;
      std::cin >> tempy;
    }
    board[tempx-1][tempy-1] = player;
    NPC(board,player2);
    printboard(board);
  }
  return 0;
}
Brandon
  • 37
  • 4
  • You need to quit when the board is full otherwise the NPC function will recurse forever. – Retired Ninja Jan 07 '17 at 06:00
  • Possible duplicate of [rand function returns same values when called within a single function c++](http://stackoverflow.com/questions/6729214/rand-function-returns-same-values-when-called-within-a-single-function-c) – Retired Ninja Jan 07 '17 at 06:03

1 Answers1

1

You're running out of stack space in your recursion because you call srand(time(NULL)) every time. The random number generator should only be seeded once, in main, and not in NPC. time(NULL) returns a number of seconds, so it changes infrequently (compared to how fast your recursive function calls will occur) which will consume all available stack space.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • Even with your change it crashes when the board fills up. There's no terminating condition. – Retired Ninja Jan 07 '17 at 05:58
  • And one more thing. I know it crashes when the board fills up. I still was in the process of finishing up the function, but noticed this problem and wanted to fix it before continuing. Thanks again guys. – Brandon Jan 07 '17 at 06:01