void login() //登陆界面 { send_info send ; //system("clear"); printf("\033[40;36m\n\n\n\t\t欢迎你,登陆界面\n"); printf("\n\t*************************************************\n"); printf("\n\n\t\t 用户名: "); is_check(send.name); printf("\n\t\t 密 码: "); is_check(send.passwd); send.type = CLIENT_LOAD; write(sock_fd ,&send ,sizeof(send));//向服务端写数据 }
void regiser_client()//注册新用户 { pthread_mutex_lock(&mutex); send_info send; //system("clear"); printf("\033[40;34m\n\t\t\t****************************\n"); printf("\t\t 用户名: "); is_check(send.name); printf("\n\t\t 密 码: "); is_check(send.passwd); send.type = CLIENT_REGISTER;//用户注册 write(sock_fd ,&send ,sizeof(send));//向服务器发送数据 pthread_mutex_unlock(&mutex); }
/* * Fill the provided list with all pseudolegal moves in the given position. * Pseudolegal moves are moves which would be legal if we didn't have to worry * about leaving our king in check. */ int generate_pseudo_moves(const position_t* pos, move_t* moves) { if (is_check(pos)) return generate_evasions(pos, moves); move_t* moves_head = moves; moves += generate_pseudo_tactical_moves(pos, moves); moves += generate_pseudo_quiet_moves(pos, moves); return moves-moves_head; }
/* * Generate all pseudolegal moves that should be considered during quiescence * search. Currently this is just captures and promotions to queen. */ int generate_quiescence_moves(const position_t* pos, move_t* moves, bool generate_checks) { if (is_check(pos)) return generate_evasions(pos, moves); move_t* moves_head = moves; moves += generate_pseudo_tactical_moves(pos, moves); if (generate_checks) moves += generate_pseudo_checks(pos, moves); return moves-moves_head; }
/* * Fill the provided list with all legal moves in the given position. */ int generate_legal_moves(position_t* pos, move_t* moves) { if (is_check(pos)) return generate_evasions(pos, moves); int num_pseudo = generate_pseudo_moves(pos, moves); move_t* moves_tail = moves+num_pseudo; move_t* moves_curr = moves; while (moves_curr < moves_tail) { check_pseudo_move_legality(pos, *moves_curr); if (!is_pseudo_move_legal(pos, *moves_curr)) { *moves_curr = *(--moves_tail); *moves_tail = 0; } else { ++moves_curr; } } return moves_tail-moves; }
void clear_illegal_moves(char board[BOARD_SIZE][BOARD_SIZE], COLOR player){ Move *prev_move = moves_head, *curr_move = moves_head; char tmp_board[BOARD_SIZE][BOARD_SIZE]; while (curr_move != NULL){ duplicate_board(board, tmp_board); exc_move(tmp_board, curr_move, player); if (is_check(tmp_board, player) != 0){ if (curr_move == moves_head){ moves_head = curr_move->next; free(curr_move); curr_move = moves_head, prev_move = moves_head; } else{ prev_move->next = curr_move->next; free(curr_move); curr_move = prev_move->next; } } else { prev_move = curr_move; curr_move = curr_move->next; } } }
int main (void) { init_leds(); init(); send_start(); is_check(); send_slave_write(); is_check(); send_data(); is_check(); while (1) { send_repeat_start(); is_check(); send_slave_read(); is_check(); receive_data(); is_check(); set_leds(read_data()); //delay_ms(100); } }
// minimax recursive func, using alpha-beta pruning int alpha_beta_minimax(char board[BOARD_SIZE][BOARD_SIZE], COLOR player, int depth, int alpha, int beta){ Move* move_list = get_all_moves(board, player); Move* curr_move = move_list; if (depth == 0 && curr_move == NULL){ best_move = NULL; return -500; } if (curr_move == NULL && player != curr_player){ if (!is_check(board, !curr_player)) return -450; else return 500; } if (curr_move == NULL && player == curr_player){ if (!is_check(board, curr_player)) return 450; else return -500; } if (depth == minimax_depth || curr_move == NULL){ clear_old_moves(move_list); return calc_score(board, curr_player); } if (depth == 0){ best_move = curr_move; if (curr_move->next == NULL){ best_move->score = 500; return 500; } } char init_board[BOARD_SIZE][BOARD_SIZE]; duplicate_board(board, init_board); // MAX if (depth % 2 == 0){ while (curr_move != NULL){ exc_move(board, curr_move, player); curr_move->score = alpha_beta_minimax(board, (player == 0), depth + 1, alpha, beta); if (curr_move->score > alpha){ alpha = curr_move->score; if (depth == 0) best_move = curr_move; } if (alpha >= beta){ if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; duplicate_board(init_board, board); return alpha; } curr_move = curr_move->next; duplicate_board(init_board, board); } if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; return alpha; } // MIN else{ while (curr_move != NULL){ exc_move(board, curr_move, player); curr_move->score = alpha_beta_minimax(board, (player == 0), depth + 1, alpha, beta); if (curr_move->score < beta){ beta = curr_move->score; if (depth == 0) best_move = curr_move; } if (alpha >= beta){ if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; duplicate_board(init_board, board); return beta; } curr_move = curr_move->next; duplicate_board(init_board, board); } if (depth != 0) clear_old_moves(move_list); else moves_head = move_list; return beta; } }
void notate_move(Board *board, Move *move, char *result) { Move moves[MAX_MOVES]; int count = gen_legal_moves(board, moves); int piece = board->squares[move->src]; int capture = board->squares[move->dst]; char rank1 = '1' + move->src / 8; char file1 = 'a' + move->src % 8; char rank2 = '1' + move->dst / 8; char file2 = 'a' + move->dst % 8; int show_rank1 = 0; int show_file1 = 0; if (PIECE(piece) == PAWN) { if (file1 != file2) { capture = 1; } if (capture) { show_file1 = 1; } } // ambiguity int ambiguous = 0; int unique_rank = 1; int unique_file = 1; for (int i = 0; i < count; i++) { Move *other = moves + i; if (move->dst != other->dst) { continue; // different target } if (move->src == other->src) { continue; // same move } if (piece != board->squares[other->src]) { continue; // different piece } ambiguous = 1; if (move->src % 8 == other->src % 8) { unique_file = 0; } if (move->src / 8 == other->src / 8) { unique_rank = 0; } } if (ambiguous) { if (unique_rank && unique_file) { show_file1 = 1; } else if (unique_rank) { show_rank1 = 1; } else if (unique_file) { show_file1 = 1; } else { show_rank1 = 1; show_file1 = 1; } } // castle int castle = 0; if (PIECE(piece) == KING) { castle = 1; if (move->src == 4 && move->dst == 6) { strcpy(result, "O-O"); result += 3; } else if (move->src == 4 && move->dst == 2) { strcpy(result, "O-O-O"); result += 5; } else if (move->src == 60 && move->dst == 62) { strcpy(result, "O-O"); result += 3; } else if (move->src == 60 && move->dst == 58) { strcpy(result, "O-O-O"); result += 5; } else { castle = 0; } } if (!castle) { // piece switch (PIECE(piece)) { case KNIGHT: *result++ = 'N'; break; case BISHOP: *result++ = 'B'; break; case ROOK: *result++ = 'R'; break; case QUEEN: *result++ = 'Q'; break; case KING: *result++ = 'K'; break; } // source if (show_file1) { *result++ = file1; } if (show_rank1) { *result++ = rank1; } // capture if (capture) { *result++ = 'x'; } // target *result++ = file2; *result++ = rank2; // promotion if (move->promotion) { *result++ = '='; switch (move->promotion) { case KNIGHT: *result++ = 'N'; break; case BISHOP: *result++ = 'B'; break; case ROOK: *result++ = 'R'; break; case QUEEN: *result++ = 'Q'; break; } } } // check Undo undo; do_move(board, move, &undo); if (is_check(board)) { if (has_legal_moves(board)) { *result++ = '+'; } else { *result++ = '#'; } } undo_move(board, move, &undo); // null terminator *result++ = 0; }
int check_status() { is_check(11,1); return 10; }
int main(int argc, char **argv) { setbuf(stdout, NULL); signal(SIGINT, INThandler); unsigned char ply = 6; int opt; while ((opt = getopt(argc, argv, "p:")) != -1) { switch (opt) { case 'p': ply = atoi(optarg); break; default: fprintf(stderr, "Usage: %s [-p ply]\n", argv[0]); exit(EXIT_FAILURE); } } struct position pos; init_position(&pos); assert(consistency(&pos)); struct timespec realt_old, realt_new; tt = new_trans_tables(); #ifdef _USE_HISTORY hist = init_history(); #endif #ifdef _DEBUG testing(); #endif #ifndef _XBOARD print_position(&pos); #endif while (1) { if (is_check(&pos)) { fprintf(stderr, "check.\n"); } user_input(&pos); #ifndef _XBOARD print_position(&pos); #endif assert(consistency(&pos)); if (is_check(&pos)) { fprintf(stderr, "check.\n"); } current_utc_time(&realt_old); computer_move(&pos, ply); assert(consistency(&pos)); current_utc_time(&realt_new); #ifndef _XBOARD fprintf(stderr, "One second: %12llu ns\n", (long long unsigned)BILLION); fprintf(stderr, "Real time: %12llu ns\n", (long long unsigned)minus_time(&realt_new, &realt_old)); print_position(&pos); #endif } return (0); }