我如何才能接受其他坐标以获胜

发布于 2025-02-08 20:57:24 字数 5879 浏览 1 评论 0原文

这是代码,它来自我在YouTube上的这个人,我进行了一些更改=> youtube funer 事情是。我主要更改了董事会的布局。我必须做一个终极的tictactoe,我觉得我没有到达那里。当我尝试播放坐标(1-4)(1-5)(1-6)时,代码并未表明它赢了。错误在哪里?我需要返回什么?

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    
    char tabu[9][9];
    const char PLAYER = 'X';
    const char COMPUTER = 'O';
    
    void resetTabu();
    void printTabu();
    int checkEspacoLivre();
        void JogadaPlayer();
        void JogadaPC();
        char checkWin();
        void printWin(char);

    int main(){
   
    char winner = ' ';
    char response = ' ';
    do{

      winner = ' ';
      response = ' ';
      resetTabu();

      while(winner == ' ' && checkEspacoLivre() != 0){
         printTabu();

         JogadaPlayer();
         winner = checkWin();
         if(winner != ' ' || checkEspacoLivre() == 0){
            break;
         }

         JogadaPC();
         winner = checkWin();
         if(winner != ' ' || checkEspacoLivre() == 0){
            break;
         }
      }

      printTabu();
      printWin(winner);

      printf("\nJOGAR OUTRA VEZ (Y/N): ");
      scanf("%c", &response);
      scanf("%c", &response);
      response = toupper(response);   // /*toupper*/ -> converte minisculas em maiusculas
   } while (response == 'Y');

   printf("\n\nFIM DO JOGO\n\n");

   return 0;
}

void resetTabu(){
   for(int i = 0; i < 9; i++){
      for(int j = 0; j < 9; j++){
         tabu[i][j] = ' ';
      }
   }
}
void printTabu(){

    printf("\n  || 1 | 2 | 3 || 4 | 5 | 6 || 7 | 8 | 9\n");
    printf("=========================================\n");
    printf("1 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[0][0], tabu[0][1], tabu[0][2], tabu[0][3], tabu[0][4], tabu[0][5], tabu[0][6], tabu[0][7], tabu[0][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("2 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[1][0], tabu[1][1], tabu[1][2], tabu[1][3], tabu[1][4], tabu[1][5], tabu[1][6], tabu[1][7], tabu[1][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("3 || %c | %c | %c || %c | %c | %c || %c | %c | %c \n", tabu[2][0], tabu[2][1], tabu[2][2], tabu[2][3], tabu[2][4], tabu[2][5], tabu[2][6], tabu[2][7], tabu[2][8]);
    printf("==||=====================================\n");
    /*======================================================================*/

    printf("4 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[3][0], tabu[3][1], tabu[3][2], tabu[3][3], tabu[3][4], tabu[3][5], tabu[3][6], tabu[3][7], tabu[3][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("5 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[4][0], tabu[4][1], tabu[4][2], tabu[4][3], tabu[4][4], tabu[4][5], tabu[4][6], tabu[4][7], tabu[4][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("6 || %c | %c | %c || %c | %c | %c || %c | %c | %c \n", tabu[5][0], tabu[5][1], tabu[5][2], tabu[5][3], tabu[5][4], tabu[5][5], tabu[5][6], tabu[5][7], tabu[5][8]);
    printf("==||=====================================\n");
   /*======================================================================*/

    printf("7 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[6][0], tabu[6][1], tabu[6][2], tabu[6][3], tabu[6][4], tabu[6][5], tabu[6][6], tabu[6][7], tabu[6][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("8 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[7][0], tabu[7][1], tabu[7][2], tabu[7][3], tabu[7][4], tabu[7][5], tabu[7][6], tabu[7][7], tabu[7][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("9 || %c | %c | %c || %c | %c | %c || %c | %c | %c \n\n\n", tabu[8][0], tabu[8][1], tabu[8][2], tabu[8][3], tabu[8][4], tabu[8][5], tabu[8][6], tabu[8][7], tabu[8][8]);
}
int checkEspacoLivre(){
   int freeSpaces = 81;

   for(int i = 0; i < 9; i++){
      for(int j = 0; j < 9; j++){
         if(tabu[i][j] != ' '){
            freeSpaces--;
         }
      }
   }
   return freeSpaces;
}

void JogadaPlayer(){
   int x;
   int y;

   do{
      printf("Linha <1-9>: ");
      scanf("%d", &x);
      x--;
      printf("Coluna <1-9>: ");
      scanf("%d", &y);
      y--;

      if(tabu[x][y] != ' '){
         printf("JOGADA INVALIDA\n");
      }
      else{
         tabu[x][y] = PLAYER;
         break;
      }
   } while (tabu[x][y] != ' ');

}
void JogadaPC(){

   //Cria uma seed baseada no tempo atual
   srand(time(0));
   int x;
   int y;

   if(checkEspacoLivre() > 0){
      do{
         x = rand() % 9;
         y = rand() % 9;
      } while (tabu[x][y] != ' ');

      tabu[x][y] = COMPUTER;
   }
   else{
      printWin(' ');
   }
}
char checkWin(){

   //verifica linhas
    for(int i = 0; i < 3; i++){
        if(tabu[i][0] == tabu[i][1] && tabu[i][0] == tabu[i][2]){
            return tabu[i][0];
      }

    }

   //verifica colunas
   for(int i = 0; i < 3; i++){
      if(tabu[0][i] == tabu[1][i] && tabu[0][i] == tabu[2][i] ){
         return tabu[0][i];
      }

   //verifica diagonais
    if(tabu[0][0] == tabu[1][1] && tabu[0][0] == tabu[2][2]){
            return tabu[0][0];
    }

    if(tabu[0][2] == tabu[1][1] && tabu[0][2] == tabu[2][0]){
            return tabu[0][2];
    }
   }
   return ' ';
}

void printWin(char winner){

   if(winner == PLAYER){
      printf("Jogador GANHOU!");
   }
   else if(winner == COMPUTER){
       printf("Jogador PERDEU!");
    }
   else{
      printf("EMPATE");
   }
}

/*====================================*/
/*========== FIM DO CODIGO ===========*/
/*====================================*/




/**/

Here is the code, its from this guy on youtube that I took and made some changes =>
Youtube Author
The thing is. I mostly just changed the layout of the board. I gotta do an Ultimate tictactoe and I feel like im not getting there. When I try to play the coordinates (1-4)(1-5)(1-6) the code doesnt show that it won. Where is the mistake? what do I need to return?

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    
    char tabu[9][9];
    const char PLAYER = 'X';
    const char COMPUTER = 'O';
    
    void resetTabu();
    void printTabu();
    int checkEspacoLivre();
        void JogadaPlayer();
        void JogadaPC();
        char checkWin();
        void printWin(char);

    int main(){
   
    char winner = ' ';
    char response = ' ';
    do{

      winner = ' ';
      response = ' ';
      resetTabu();

      while(winner == ' ' && checkEspacoLivre() != 0){
         printTabu();

         JogadaPlayer();
         winner = checkWin();
         if(winner != ' ' || checkEspacoLivre() == 0){
            break;
         }

         JogadaPC();
         winner = checkWin();
         if(winner != ' ' || checkEspacoLivre() == 0){
            break;
         }
      }

      printTabu();
      printWin(winner);

      printf("\nJOGAR OUTRA VEZ (Y/N): ");
      scanf("%c", &response);
      scanf("%c", &response);
      response = toupper(response);   // /*toupper*/ -> converte minisculas em maiusculas
   } while (response == 'Y');

   printf("\n\nFIM DO JOGO\n\n");

   return 0;
}

void resetTabu(){
   for(int i = 0; i < 9; i++){
      for(int j = 0; j < 9; j++){
         tabu[i][j] = ' ';
      }
   }
}
void printTabu(){

    printf("\n  || 1 | 2 | 3 || 4 | 5 | 6 || 7 | 8 | 9\n");
    printf("=========================================\n");
    printf("1 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[0][0], tabu[0][1], tabu[0][2], tabu[0][3], tabu[0][4], tabu[0][5], tabu[0][6], tabu[0][7], tabu[0][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("2 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[1][0], tabu[1][1], tabu[1][2], tabu[1][3], tabu[1][4], tabu[1][5], tabu[1][6], tabu[1][7], tabu[1][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("3 || %c | %c | %c || %c | %c | %c || %c | %c | %c \n", tabu[2][0], tabu[2][1], tabu[2][2], tabu[2][3], tabu[2][4], tabu[2][5], tabu[2][6], tabu[2][7], tabu[2][8]);
    printf("==||=====================================\n");
    /*======================================================================*/

    printf("4 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[3][0], tabu[3][1], tabu[3][2], tabu[3][3], tabu[3][4], tabu[3][5], tabu[3][6], tabu[3][7], tabu[3][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("5 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[4][0], tabu[4][1], tabu[4][2], tabu[4][3], tabu[4][4], tabu[4][5], tabu[4][6], tabu[4][7], tabu[4][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("6 || %c | %c | %c || %c | %c | %c || %c | %c | %c \n", tabu[5][0], tabu[5][1], tabu[5][2], tabu[5][3], tabu[5][4], tabu[5][5], tabu[5][6], tabu[5][7], tabu[5][8]);
    printf("==||=====================================\n");
   /*======================================================================*/

    printf("7 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[6][0], tabu[6][1], tabu[6][2], tabu[6][3], tabu[6][4], tabu[6][5], tabu[6][6], tabu[6][7], tabu[6][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("8 || %c | %c | %c || %c | %c | %c || %c | %c | %c ", tabu[7][0], tabu[7][1], tabu[7][2], tabu[7][3], tabu[7][4], tabu[7][5], tabu[7][6], tabu[7][7], tabu[7][8]);
    printf("\n--||---|---|---||---|---|---||---|---|---\n");
    printf("9 || %c | %c | %c || %c | %c | %c || %c | %c | %c \n\n\n", tabu[8][0], tabu[8][1], tabu[8][2], tabu[8][3], tabu[8][4], tabu[8][5], tabu[8][6], tabu[8][7], tabu[8][8]);
}
int checkEspacoLivre(){
   int freeSpaces = 81;

   for(int i = 0; i < 9; i++){
      for(int j = 0; j < 9; j++){
         if(tabu[i][j] != ' '){
            freeSpaces--;
         }
      }
   }
   return freeSpaces;
}

void JogadaPlayer(){
   int x;
   int y;

   do{
      printf("Linha <1-9>: ");
      scanf("%d", &x);
      x--;
      printf("Coluna <1-9>: ");
      scanf("%d", &y);
      y--;

      if(tabu[x][y] != ' '){
         printf("JOGADA INVALIDA\n");
      }
      else{
         tabu[x][y] = PLAYER;
         break;
      }
   } while (tabu[x][y] != ' ');

}
void JogadaPC(){

   //Cria uma seed baseada no tempo atual
   srand(time(0));
   int x;
   int y;

   if(checkEspacoLivre() > 0){
      do{
         x = rand() % 9;
         y = rand() % 9;
      } while (tabu[x][y] != ' ');

      tabu[x][y] = COMPUTER;
   }
   else{
      printWin(' ');
   }
}
char checkWin(){

   //verifica linhas
    for(int i = 0; i < 3; i++){
        if(tabu[i][0] == tabu[i][1] && tabu[i][0] == tabu[i][2]){
            return tabu[i][0];
      }

    }

   //verifica colunas
   for(int i = 0; i < 3; i++){
      if(tabu[0][i] == tabu[1][i] && tabu[0][i] == tabu[2][i] ){
         return tabu[0][i];
      }

   //verifica diagonais
    if(tabu[0][0] == tabu[1][1] && tabu[0][0] == tabu[2][2]){
            return tabu[0][0];
    }

    if(tabu[0][2] == tabu[1][1] && tabu[0][2] == tabu[2][0]){
            return tabu[0][2];
    }
   }
   return ' ';
}

void printWin(char winner){

   if(winner == PLAYER){
      printf("Jogador GANHOU!");
   }
   else if(winner == COMPUTER){
       printf("Jogador PERDEU!");
    }
   else{
      printf("EMPATE");
   }
}

/*====================================*/
/*========== FIM DO CODIGO ===========*/
/*====================================*/




/**/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2025-02-15 20:57:24

您说您正在播放坐标”(1-4)(1-5)(1-6),“对吗?教程版本看起来最容易,

    char checkWin(){

   //verifica linhas
    for(int i = 0; i < 3; i++){ //needs to be updated to check whole board
        if(tabu[i][0] == tabu[i][1] && tabu[i][0] == tabu[i][2]){
            return tabu[i][0];
      }

    }

   //needs to be updated
   for(int i = 0; i < 3; i++){ //you are not checking 4,5,6
      if(tabu[0][i] == tabu[1][i] && tabu[0][i] == tabu[2][i] ){
         return tabu[0][i];
      }

   //verifica diagonais
    if(tabu[0][0] == tabu[1][1] && tabu[0][0] == tabu[2][2]){
            return tabu[0][0];
    }
    //update these too
    if(tabu[0][2] == tabu[1][1] && tabu[0][2] == tabu[2][0]){
            return tabu[0][2];
    }
   }
   return ' ';
}

当前的列,

for(int x = 0; x < 9; x++) {
    for(int y = 0; y <= 6; y++) {
         if(tabu[x][y] == tabu[x][y + 1] && tabu[x][y] == tabu[x][y + 2]){
            return tabu[x][y];
      }
    }
}

就像您对“自由空间”功能所做的那样。

而不是修改

    //This should check all of the columns in a 9x9 grid board
    for(int x = 0; x < 9; x++) {
            for(int y = 0; y <= 6; y++) {
                 if(tabu[x][y] == tabu[x][y + 1] && tabu[x][y] == tabu[x][y + 2]){
                    return tabu[x][y];
              }
            }
        }
    
   //to check rows you just need to swap which 3 grid spaces you are checking
   for(int x = 0; x <= 6; x++) {
            for(int y = 0; y < 9; y++) {
                 if(tabu[x][y] == tabu[x + 1][y] && tabu[x][y] == tabu[x + 2][y]){
                    return tabu[x][y];
              }
            }
        }
//Checking diagonals should be pretty straightforward but it’s 3am and I am going to go to sleep.

You say you are playing the coordinates "(1-4)(1-5)(1-6),” correct? Because if so this is a very easy solution. Look at you checkWin function, you haven’t updated it from the tutorials version it looks like.

    char checkWin(){

   //verifica linhas
    for(int i = 0; i < 3; i++){ //needs to be updated to check whole board
        if(tabu[i][0] == tabu[i][1] && tabu[i][0] == tabu[i][2]){
            return tabu[i][0];
      }

    }

   //needs to be updated
   for(int i = 0; i < 3; i++){ //you are not checking 4,5,6
      if(tabu[0][i] == tabu[1][i] && tabu[0][i] == tabu[2][i] ){
         return tabu[0][i];
      }

   //verifica diagonais
    if(tabu[0][0] == tabu[1][1] && tabu[0][0] == tabu[2][2]){
            return tabu[0][0];
    }
    //update these too
    if(tabu[0][2] == tabu[1][1] && tabu[0][2] == tabu[2][0]){
            return tabu[0][2];
    }
   }
   return ' ';
}

It will probably be easiest to make a new check win function rather than modifying the current one what about for columns:

for(int x = 0; x < 9; x++) {
    for(int y = 0; y <= 6; y++) {
         if(tabu[x][y] == tabu[x][y + 1] && tabu[x][y] == tabu[x][y + 2]){
            return tabu[x][y];
      }
    }
}

like you did for the check free space function.

Edit:

    //This should check all of the columns in a 9x9 grid board
    for(int x = 0; x < 9; x++) {
            for(int y = 0; y <= 6; y++) {
                 if(tabu[x][y] == tabu[x][y + 1] && tabu[x][y] == tabu[x][y + 2]){
                    return tabu[x][y];
              }
            }
        }
    
   //to check rows you just need to swap which 3 grid spaces you are checking
   for(int x = 0; x <= 6; x++) {
            for(int y = 0; y < 9; y++) {
                 if(tabu[x][y] == tabu[x + 1][y] && tabu[x][y] == tabu[x + 2][y]){
                    return tabu[x][y];
              }
            }
        }
//Checking diagonals should be pretty straightforward but it’s 3am and I am going to go to sleep.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文