/** * This function read a position by clicking in * the HTML interface's board * * @v info_s when the HTML interface is chosen * @return void * @date 2014-05-13 * @author PL Team **/ void PL_HTMLread_move(position_t *pos) { /* Maximum size for the buffer to read a move: 3 characters + 1 \n + 1 \0 => 5 */ #define READ_MOVE_MAX_BUFF_LEN (5) //The move_in_board (being the move read from the user) is 5 to verify the length of the input //char move_in_board[READ_MOVE_MAX_BUFF_LEN]; char move_in_board[READ_MOVE_MAX_BUFF_LEN]; // Maximum number of characters we want to read with fgets (includes '\n' and '\0') int maxLength = READ_MOVE_MAX_BUFF_LEN; //This allows one more character in order to verify the length of the input FILE *mov; startServer(); printf("Write your move [3 characters max]: "); //fgets(move_in_board, maxLength, stdin); //gets in the string move_in_board a maximum of maxLength characters coming from the STDIN mov = fopen("move.mv","r+"); fgets(move_in_board, maxLength, mov); fclose(mov); /* Terminate the string at the first \n */ terminate_string_at_first_slash_n(move_in_board); pos->X = input_is_digit(move_in_board); pos->Y = input_is_char(move_in_board); pos->Y = toupper(pos->Y); pos->Y_int = board_col_to_matrix_idx(pos->Y); }
void read_move1(position_t *pos) { /* Maximum size for the buffer to read a move: 3 characters + 1 \n + 1 \0 => 5 */ #define READ_MOVE_MAX_BUFF_LEN (5) int dimension = board_get_size(); //The move_in_board (being the move read from the user) is 5 to verify the length of the input // char move_in_board[READ_MOVE_MAX_BUFF_LEN]; char move_in_board[READ_MOVE_MAX_BUFF_LEN]; // Maximum number of characters we want to read with fgets (includes '\n' and '\0') int maxLength = READ_MOVE_MAX_BUFF_LEN; //This allows one more character in order to verify the length of the input // Keep the number of char in the move_in_board string size_t move_in_board_strlen; do { do { printf("Write your move [3 characters max]: "); fgets(move_in_board, maxLength, stdin); //gets in the string move_in_board a maximum of maxLength characters coming from the STDIN /* Terminate the string at the first \n */ terminate_string_at_first_slash_n(move_in_board); /* Compute # of chars in move_in_board */ move_in_board_strlen = strlen(move_in_board); if( move_in_board_strlen > 3) { clean_buffer_keyboard(); printf("The move has a maximum of three characters!\n"); } } while(move_in_board_strlen > 3); pos->X = input_is_digit(move_in_board); pos->Y = input_is_char(move_in_board); pos->Y = toupper(pos->Y); pos->Y_int = board_col_to_matrix_idx(pos->Y); //extern int board_col_to_matrix_idx(char col); if(pos->X < 1 || pos->X > dimension) { printf("Invalid number!\nThe number has to be between 1 - %d\n\n", dimension); } if(pos->Y_int == -1 || pos->Y_int >= dimension) { printf("Invalid letter!\nIt has to be between A - %c\n\n", 64+dimension); } } while(pos->Y_int == -1 || pos->Y_int >= dimension || pos->X < 1 || pos->X > dimension); printf("Part of the board! \n"); }
/** * This function shows the main menu * * @v info_s * @return none * @date 2014-04-14 * @author PL Team **/ void show_menu() { int menu_choose; int control; // to verify if it is not a char! char player1nameAux[MAX_PLAYERNAME_LENGTH]=""; char player2nameAux[MAX_PLAYERNAME_LENGTH]=""; do { clearscr(); printf("Triplets Game\n\n"); printf("1. Play : Player vs Computer (PvC)\n"); printf("2. Play : Player vs Player (PvP)\n"); printf("3. Game rules\n"); printf("4. High scores\n"); printf("5. Replay game\n"); printf("6. Credits\n\n"); printf("7. Exit Game.\n\n"); printf("(Choose an option and press enter): "); control=scanf("%d",&menu_choose); clean_buffer_keyboard(); } while (menu_choose<1 || menu_choose>7 || control == 0); switch(menu_choose) { case 1: G_current_game.game_mode=pvc; G_current_game.pvp_mode = notpvp; show_difficulty(); choose_interface(); clearscr(); printf("Triplets - Player vs Computer \n\n"); do { printf("Enter your name: "); fgets(G_players[0].name, MAX_PLAYERNAME_LENGTH, stdin); terminate_string_at_first_slash_n(G_players[0].name); strcpy(player1nameAux,G_players[0].name); string_to_lower(player1nameAux); if (!strcmp(player1nameAux,"cpu")) { printf("Invalid name.\n"); } } while (!strcmp(player1nameAux,"cpu")); strcpy(G_players[1].name, "CPU");// G_players[1] is cpu player choose_board(); show_who_first(); break; case 2: G_current_game.game_mode=pvp; G_current_game.cpu_mode = none; clearscr(); printf("Triplets - Player vs Player\n"); do { printf("Enter the name of player 1: "); fgets(G_players[0].name, MAX_PLAYERNAME_LENGTH, stdin); terminate_string_at_first_slash_n(G_players[0].name); strcpy(player1nameAux,G_players[0].name); string_to_lower(player1nameAux); if (!(strcmp(player1nameAux,"cpu"))) { printf("Invalid name.\n"); } } while (!(strcmp(player1nameAux,"cpu"))); do { printf("\nEnter the name of player 2: "); fgets(G_players[1].name, MAX_PLAYERNAME_LENGTH, stdin); terminate_string_at_first_slash_n(G_players[1].name); strcpy(player2nameAux,G_players[1].name); string_to_lower(player2nameAux); if (!(strcmp(player2nameAux,"cpu")) || !(strcmp(player2nameAux,player1nameAux))) { printf("Invalid name.\n"); } } while (!(strcmp(player2nameAux,"cpu")) || !(strcmp(player2nameAux,player1nameAux))); show_pvp_mode(); if (G_current_game.pvp_mode == normal) { choose_interface(); } else { G_current_game.interface_mode = console; } choose_board(); show_who_first(); break; case 3: show_game_rules(); show_menu(); break; case 4: //show High scores// show_highscores(); show_menu(); break; case 5: replay_menu(); // this is in PT_save_read_moves.c show_menu(); break; case 6: show_credits(); show_menu(); break; case 7: exit(0); break; default: clearscr(); show_menu(); break; } }