Example #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();
    }
}
Example #2
0
void doisjogadores(char **board) {
    system("clear");
    print_usage2();
    print_board(board);
    while ((!game_over(board) && (!someone_wins(board, 'O')))) {
        while(1) {
            printf("\n\t\t\tVez de jogador 1:\n");
            if (jogada(board,'X')) {
                break;
                //print_board(board)
            }
        }
        if(game_over(board) || someone_wins(board, 'X'))
            break;
        system("clear");
        print_board(board);

        while (1) {
            printf("\n\t\t\tVez de jogador 2:\n");
            if(jogada(board,0)) {
                break;
            }
        }
        system("clear");
        print_board(board);
    }
    system("clear");
    print_board(board);
    if (someone_wins(board, 'X')) {
        printf("\t\t\t\t\t\t\t\tJogador 1 ganhou!\n");

        enter();

        getchar();
        ultimos(board, 1);
    } else if (someone_wins(board, 'O'))  {
        printf("\t\t\t\t\t\t\t\tJogador 2 ganhou!\n");

        enter();


        getchar();
        ultimos(board, 2);
    } else {
        printf("\t\t\t\t\t\t\t\tFoi um empate!\n");

        enter();
        getchar();
        getchar();
    }


}
Example #3
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
}