Beispiel #1
0
void umjogador(char **board) {
    system("clear");
    print_usage1();
    int hor, vert, alpha;
    move best_move = (move) malloc (sizeof(struct legal_move));
    print_board(board);

    while (!game_over(board) && (!someone_wins(board, 'X'))) {
        if (jogada(board,0)) {
            system("clear");
            printf("A posição depois de sua última jogada:\n");
            print_board(board);
            alpha = minimax(board, 1, best_move);
            update_board(board, best_move -> hor, best_move -> vert, 1);
            printf("A posição depois da última jogada do computador:\n");
            print_board(board);
        }
    }


    if (someone_wins(board, 'X')) {
        ai_wins();
    } else {
        cats_game();
    }
}
Beispiel #2
0
int main()
{
  /* Set up global variables: board and best move */
  int i;
  char **board = (char **) malloc (sizeof(char *) * 3);
  for (i=0; i<3; i++) {
    board[i] = (char *) malloc (sizeof(char) * 3);
  }

  /* Begin the game by printing usage instructions and 
     initializing the board */
  print_usage();
  init_board(board);
  
  /* Play the game */
  int hor, vert, alpha;
  move best_move = (move) malloc (sizeof(struct legal_move));

  print_board(board);

  while (!game_over(board) && (!someone_wins(board, 'X'))) {
    scanf("%d %d", &hor, &vert); 
    hor -= 1; // adjust input values to match array indeces
    vert -= 1;
    if ((hor > 2) || (vert > 2) || (hor < 0) || (vert < 0)) { 
      printf("Sorry, that square does not exist.\n");
    } else if (board[hor][vert] != '-') { 
      printf("Sorry, that square is already occupied.\n");
    } else {
      update_board(board, hor, vert, 0);
      printf("The position after your last move:\n");
      print_board(board);
      alpha = minimax(board, 1, best_move);
      update_board(board, best_move -> hor, best_move -> vert, 1);
      printf("The position after the computer's last move:\n");
      print_board(board);
    }
  }

  if (someone_wins(board, 'X')) {
    ai_wins();
  } else {
    cats_game();
  }

  return 0; // control should never reach here
}