piece_info_t * make_valid_moves(board_t * board, role_t role, uintptr_t * retsize){ uintptr_t i; piece_info_t * ret; uint16_t _retsize; ret = NULL; _retsize = 0; piece_info_t ** pieces; switch (role){ case ROLE_NONE: free(ret); return false; break; case ROLE_BLACK: pieces = board->pieces_black; break; case ROLE_WHITE: pieces = board->pieces_white; break; } for (i = 0;i < BLOKUSDUO_PIECENUM;i++){ if (pieces[i] != NULL){ // alreay used piece continue; } piece_info_t move_cand; move_cand.role = role; move_cand.type = i; uintptr_t x,y,rot,rev; for(x = 0;x < BLOKUSDUO_BOARDSIZE;x++){ for(y = 0;y < BLOKUSDUO_BOARDSIZE;y++){ for(rot = 0;rot < 4; rot++){ for(rev = 0;rev < 2;rev++){ move_cand.x = x; move_cand.y = y; move_cand.reversed = rev; move_cand.rot = rot; if (validate_move(board, &move_cand)){ ret = realloc(ret, sizeof(piece_info_t) * (1 + _retsize)); memcpy(&ret[_retsize] , &move_cand, sizeof(piece_info_t)); _retsize++; } } } } } } *retsize = _retsize; return ret; }
/* * return true if successfully a move is done * */ bool do_move(board_t * board, piece_info_t * move){ if (!validate_move(board, move)){ return false; } piece_info_t ** pieces; uint8_t * piecenum; switch(move->role){ case ROLE_BLACK: pieces = board->pieces_black; piecenum = &board->blacknum; break; case ROLE_WHITE: pieces = board->pieces_white; piecenum = &board->whitenum; break; case ROLE_NONE: return false; break; } pieces[move->type] = malloc(sizeof(piece_info_t)); memcpy(pieces[move->type], move, sizeof(piece_info_t)); uintptr_t i; uint32_t mask, color_mask; int8_t x,y; mask = 1 << move->x; color_mask = move->role << move->x; board->occupied[move->y] |= mask; board->cellcolors[move->y] |= color_mask; piece_cell_t * cells; uintptr_t sz; cells = piece_info_points(move, &sz); for (i = 1;i < sz;i++){ x = cells[i].x; y = cells[i].y; mask = 1 << x; color_mask = move->role << x; board->occupied[y] |= mask; board->cellcolors[y] |= color_mask; } free(cells); (*piecenum)++; return true; }
static int validate_field_value (struct cb_field *f) { if (f->values) { validate_move (CB_VALUE (f->values), CB_TREE (f), 1); } if (f->children) { for (f = f->children; f; f = f->sister) { validate_field_value (f); } } return 0; }
void setupPossibleMoves( struct moveset *mv, unsigned char color ) { usint i; (*mv).movecount = 0; for( i = 0; i < AREA; i++ ) { if( IsNotPiece(i) && validate_move(i, color) != 0 ) { *( (*mv).moves + (*mv).movecount ) = i; (*mv).movecount++; } } }
bool OpeningBook::get_move(vector<move>& available_moves, vector<history_item>& made_moves, move& m) { if (!book_open) { return false; } int random = rand(); move valid_move; move last_move; string move_string; move move_move; string last_move_string; /* * Check if we are dealing with the first move of the game or not */ if (made_moves.size() == 0) { move_string = book[random % book.size()][0]; move_move = string_to_move(move_string); if (validate_move(move_move, available_moves, &valid_move)) { m = valid_move; set_first_and_last(move_string, 0); return true; } else { // Something went wrong, the game might not have history :( book_open = false; return false; } } // Check if we aren't too far in the game already if (made_moves.size() >= max_line_size) { book_open = false; return false; } //Check if the last move is in the book. last_move = made_moves.back().m; last_move_string = move_to_string_very_basic(last_move); if (!set_first_and_last(last_move_string, made_moves.size() - 1)) { book_open = false; return false; } unsigned int index_to_be_returned = first + random % (last - first + 1); if (book[index_to_be_returned].size() <= made_moves.size()) { book_open = false; return false; } move_string = book[index_to_be_returned][made_moves.size()]; move new_move = string_to_move(move_string); if (validate_move(new_move, available_moves, &valid_move)) { m = last_move = valid_move; set_first_and_last(move_string, made_moves.size()); return true; } // If you reached at this point.. there are no more moves... book_open = false; return false; }