简体   繁体   English

分段错误:11 c++ 错误

[英]Segmentation fault: 11 c++ Error

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!我正在创建一个用于练习的井字游戏,并且正在使用枚举器和递归,因为我从来没有真正做过枚举,并且总是可以进行一些递归练习。好吧,无论如何我刚刚完成了为 player2 进行随机移动的编码,然后大约 3 圈它给出了分段错误,我不知道为什么......我希望你们能弄清楚,谢谢!

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

You're running out of stack space in your recursion because you call srand(time(NULL)) every time.由于每次都调用srand(time(NULL))递归中的堆栈空间不足。 The random number generator should only be seeded once, in main, and not in NPC .随机数生成器只应在 main 中播种一次,而不应在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. time(NULL)返回秒数,因此它很少更改(与递归函数调用发生的速度相比),这将消耗所有可用的堆栈空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM