Как я могу преобразовать мою функцию winnerCheck в рекурсивную работу?- С - PullRequest
0 голосов
/ 21 ноября 2018

ВЫПУСК: У меня трудные времена, когда я строю свое понимание рекурсии (медленный ученик), и всякий раз, когда мне поручается создать функцию recursive, я всегда создаю ее через iterationпервый.

Тогда я делаю все возможное, чтобы выполнить некоторые из основных правил рекурсии, но обычно в конце концов чувствую себя немного измотанным.

Все, что я пытаюсь сделать, это преобразовать мою winnerCheck функцию в работу recursively.

Кроме того, я слышал, что использование global variables не одобряется.Это правда?Должен ли я переместить мой массив square для локальной работы?

Спасибо за любые советы и предложения, которые вы можете предложить.

КОД:

#include<stdio.h>
#include<conio.h>

char square[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

int winnerCheck();

void board();

int main() {

    /*
    char board[3][3] = {
    };

    printf("\t|\t|\t\n");
    printf("\t|\t|\t\n");
    printf("________|_______|________\n");
    printf("\t|\t|\t\n");
    printf("\t|\t|\t\n");
    printf("________|_______|________\n");
    printf("\t|\t|\t\n");
    printf("\t|\t|\t\n");
    printf("\t|\t|\t\n");

*/

    int player = 1, i, choice;
    char mark;
    do {
        board();
        player = player % 2 ? 1 : 2;

        printf("Player %d, enter a number: ", player);
        scanf("%d", &choice);

        //mark = (player == 1) ? 'X' : 'O';

        if (player == 1) {
            mark = 'X';
        } else {
            mark = '0';
        }

        if (choice == 1)
            square[1] = mark;
        else if (choice == 2)
            square[2] = mark;
        else if (choice == 3)
            square[3] = mark;
        else if (choice == 4)
            square[4] = mark;
        else if (choice == 5)
            square[5] = mark;
        else if (choice == 6)
            square[6] = mark;
        else if (choice == 7)
            square[7] = mark;
        else if (choice == 8)
            square[8] = mark;
        else if (choice == 9)
            square[9] = mark;


        i = winnerCheck();
        player++;
    } while (i == -1);
    board();

    if (i == 1)
        printf("==>\aPlayer %d win ", --player);
    else
        printf("==>\aGame draw");
    getch();

    return 0;
}

int winnerCheck() {
    if (square[1] == square[2] && square[2] == square[3])
        return 1;
    else if (square[4] == square[5] && square[5] == square[6])
        return 1;
    else if (square[7] == square[8] && square[8] == square[9])
        return 1;
    else if (square[1] == square[4] && square[4] == square[7])
        return 1;
    else if (square[2] == square[5] && square[5] == square[8])
        return 1;
    else if (square[3] == square[6] && square[6] == square[9])
        return 1;
    else if (square[1] == square[5] && square[5] == square[9])
        return 1;
    else if (square[3] == square[5] && square[5] == square[7])
        return 1;
    else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4]
                                                                         != '4' && square[5] != '5' &&
             square[6] != '6' && square[7] != '7' && square[8]
                                                     != '8' && square[9] != '9')
        return 0;
    else
        return -1;

}

void board() {
    printf("\n\n\tTic Tac Toe\n\n");
    printf("Player 1 (X) - Player 2 (O)\n\n\n");

//prints the board after every input

    printf("    |     |    \n");
    printf("  %c |  %c  |  %c  \n", square[1], square[2],square[3]);
    printf("____|_____|____\n");
    printf("    |     |    \n");
    printf("  %c |  %c  |  %c  \n", square[4], square[5],square[6]);
    printf("____|_____|____\n");
    printf("    |     |    \n");
    printf("  %c |  %c  |  %c  \n", square[7], square[8],square[9]);
    printf("    |     |    \n");
}

1 Ответ

0 голосов
/ 22 ноября 2018

Функция winnerCheck проверяет, выполняется ли какое-либо из возможных условий выигрыша.И это состоит в проверке, поданы ли некоторые тройки одним и тем же игроком.

Вы можете жестко закодировать массив со всеми тройками, которые гарантируют выигрыш.Тогда проверка на выигрыш подразумевает обход этого массива, и это легко сделать рекурсивно.

РЕДАКТИРОВАТЬ

Таким образом, есть 8 возможных тройок "выигрышных позиций", которые вы можете объявить, например, как

const int numwinpos = 8;

const int winpos[8][3] = {{1,2,3},
                          {4,5,6},
                          {7,8,9},
                          {1,4,7},
                          {2,5,8},
                          {3,6,9},
                          {1,5,9},
                          {3,5,7}};

Относительно рекурсивной проверкиЗдесь процедура является примером того, как проверить, удовлетворяет ли какой-либо элемент вектора v размера n свойству p.

// returns -1 if no v[i] satisfies p
// returns  i if v[i] satisfies p (picks largest i)
// (i>=0) and (i<n)

int recTest(const int v[], const int n){

  if( (n>0) && (!p(v[n-1])) )
    return recTest(v,n-1);
  else
    return n-1;
}

EDIT2:

Таким образом, winnersCheck может иметь форму,

int winnerCheck(char square[], int winseq[][3], int n){
  // there's n winning sequences to test, 0 .. n-1
  // let's check sequence n-1
  int pos0 = winseq[n-1][0];
  int pos1 = winseq[n-1][1];
  int pos2 = winseq[n-1][2];
  if( (n>0) && !(squares in positions pos1, pos2 and pos2 marked by same player) )

    // game does not contain winning sequence n-1
    // so test the other n-1 positions, i.e. 0 .. n-2 
    return winnerCheck(square,winseq,n-1);  
  else
    // either n = 0 and there's no more positions to test 
    // or game contains winning sequence n-1 
    return n-1;
}

Конечно, состояние, которое вам нужноЗаполнение может быть немного большим, чтобы вставить в это, если.Вы можете организовать это по-другому или определить вспомогательную функцию для выполнения этой работы.

Также вот некоторые модификации вашего основного кода, которые показывают, как некоторые из ваших действий могут быть более понятными.

int main() {

  // Game Board: there's 9 squares in the game
  // 0 1 2
  // 3 4 5
  // 6 7 8

  // ' ' represents "nothing" in a square
  char square[9]={' ',' ',' ',' ',' ',' ',' ',' ',' '}; 

  // There are 8 possible ways of winning the game
  int nwinseq = 8;
  int winseq[8][3] = {{0,1,2},
                      {3,4,5},
                      {6,7,8},
                      {0,3,6},
                      {1,4,7},
                      {2,5,8},
                      {0,4,8},
                      {2,4,6}};

   // there's player 1 and player 2
   // player 1 goes first  
   int player = 1;   

   // current play
   // at most 9 plays in a game
   int i = 1;

   // winning sequence
   // 8 possibilities (0 to 7)
   // no winning sequence found when game starts
   int w = -1;

   // execute game until it's over
   bool gameOver = false;

   while(!gameOver){    
    // print board  
    board(square);

    // ask player for his move of choice
    int choice; 
    printf("Player %d, enter a number: ", player);
    scanf("%d", &choice);

    // change the square according to player's move
    move(player, square, choice);

    // check for win
    w = winnerCheck(square, winseq, nwinseq);

    gameOver = (w >= 0) // found winning sequence i
            || (i == 9); // or made 9 plays already

    // update play number and player
    // obs: maybe update it only if game is not over
    // matter of taste
    i++;

    if (player == 1)
      player = 2;
    else // player == 2 
      player = 1;
  }

  // Game is over 
  // Print the end result 
  // ...
}

и,

void move(int player, char square[], int choice){
   if (player == 1)
    square[choice] = 'X';
  else
    square[choice] = 'O';
}
...