int main() { initscr(); keypad(stdscr, TRUE); noecho(); start_color(); init_pair(1, COLOR_GREEN, COLOR_BLACK); init_pair(2, COLOR_MAGENTA, COLOR_BLACK); finit(9,9,0,2); clear(); printw( " Etherial Void \n\n" "Is it that all exists only in the\n" "etherial landscape of the mind?\n\n" "The past is uncertian,\n\n" "The present is now the past,\n\n" "The future might only be thought of.\n\n" "Is then not our mind's eye\n" "the root of reality?\n\n" "But should that eye be blind,\n" "where then would rest the soul?" "\n\n" "(press any key to continue)\n"); getch(); clear(); printw( " I look,\n" "but I see nothing."); getch(); clear(); getch(); printw( " Perhaps,\n" "there is nothing to see."); getch(); helpmsg="Use the arrow keys or WASD to move the charecter."; clear(); while(loop) { switch(z) { case 0: nroom=1; eroom=2; sroom=3; wroom=4; uroom=5; light=2; wall= "Some sort of wall mesage should be displayed here,\n" "It should inform the player of the solidity of this wall,\n" "and it should warn that attempts to surpass this fact are admonishable."; msg(9,6, " I <do> see something:\n" "There seem to be dimly glowing etchings on the ground which signify,\n" "'There is naught to the north.'"); msg(12,9, " There is <something> here:\n" "In the ground there are shallow grooves filled with a\n" "faintly luminescent material. I cannot comprehend them."); if(sw[0]&&sw[1]&&sw[2]&&sw[3]) { map[0][9][9]='^'; smsg(9,9, "This is a new stairwell."); } break; case 1: sroom=0; lvr(9,9,0); break; case 2: wroom=0; wall= "There are some large deep grooves on the wall"; lvr(9,9,1); break; case 3: nroom=0; lvr(9,9,2); break; case 4: eroom=0; lvr(9,9,3); break; case 5: eroom=6; droom=1; break; case 6: wroom=5; break; default: endwin(); printf("Error: 42\n"); loop=0; exit(42); } unmove(); uncover(0); showmap(); input(); } endwin(); return 0; }
bool ChessBoard::makeOwnKingInCheck(string src, string dest, string player) { Piece* ptempPiece = NULL; if (existPiece(dest)) ptempPiece = m_board[dest]; move(src, dest); if (kingIsInCheck(player)) { unmove(src, dest, ptempPiece); return true; } unmove(src, dest, ptempPiece); return false; }
/* Quiescence Search */ move_t quiescence(board_t *b, int32_t alpha, int32_t beta) { uint8_t i; move_t m, best; move_list_t *list; /* Initialize the best possible move as blank */ SET_BLANK_MOVE(best); m.eval = heuristic(b, onmove); if(m.eval >= beta) { best.eval = m.eval; return best; } else if(m.eval > alpha) { alpha = m.eval; } /* Get the next possible Good captures only */ list = gen_move_list(b, TRUE); /*TODO: Re-order the move list (SEE - Static Exchange Eval) */ /*reorder_move_list(b, list); */ /* For each possible next move... */ for(i = 0; i < list->size; i++) { /* Let's see the board after that move... */ move(b, list->move[i]); /* Quiescence Search recursion */ m = quiescence(b, -beta, -alpha); m.eval = -m.eval; /* Restores the previous board (before the possible move) */ unmove(b); /* Beta cutoff */ if(m.eval >= beta) { best = list->move[i]; best.eval = m.eval; break; /* Alpha cutoff */ } else if(m.eval > alpha) { best = list->move[i]; alpha = best.eval = m.eval; /* Best possible move until now */ } else if(i == 0 || m.eval > best.eval) { best = list->move[i]; best.eval = m.eval; } } /* Clear temporary information and return */ clear_move_list(list); return best; }
void backtrack(char *arr, int len, char *curptr, int curlen) { int c[2]; char temp; int combinations=0; if(accept(curlen, len)) { print(arr, len); } else { combinations = generate_combinations(arr, c); for (int i=0;i<combinations;i++) { move(arr,c[i],&temp,curlen); backtrack(arr,len,curptr+1,curlen+1); unmove(arr,c[i],temp,curlen); } } }
bool trn_game_try_to_move(TrnGame* game,void (*move)(TrnPiece * const), void (*unmove)(TrnPiece * const)) { if (game->status != TRN_GAME_ON) return false; bool managedToMove = true; trn_grid_remove_piece(game->grid, game->current_piece); move(game->current_piece); if (! trn_grid_can_set_cells_with_piece(game->grid, game->current_piece)) { managedToMove = false; unmove(game->current_piece); } trn_grid_fill_piece(game->grid, game->current_piece); return managedToMove; }
static void MBMatchServerLog(unsigned int LogLevel, const char* Msg, bool Newline) { static std::mutex LogMutex; std::lock_guard<std::mutex> Lock(LogMutex); char Str[1024 * 16]; ArrayView<char> Remaining = Str; auto Consumed = strftime(Str, sizeof(Str), "%FT%T%z", localtime(&unmove(time(0)))); if (!Consumed) { assert(false); return; } Remaining.remove_prefix(Consumed); auto Append = [&](const char* a) { auto Zero = strcpy_safe(Remaining, a); Remaining.remove_prefix(Zero - Remaining.data()); }; Append(" | "); Append(Msg); Append(Newline ? "\n" : ""); fputs(Str, stdout); if (LogLevel & MMatchServer::LOG_FILE) MLogFile(Str); #ifdef _DEBUG if (LogLevel & MMatchServer::LOG_DEBUG) { OutputDebugString(Msg); if (Newline) OutputDebugString("\n"); } #endif }
// Nega-max演算法(recursive) // board: 盤面資訊(0~59), 表示法如下(X:邊界, 0:棋盤格子): // XXXXXX // X0000X // X0000X // X0000X // X0000X // X0000X // X0000X // X0000X // X0000X // XXXXXX // // piece_num: 記錄各兵種之存活棋子數 // dark_num: 記錄可翻棋子數 // red_num, black_num: 紅黑存活棋子數 // turn: 輪走方 // depth: 目前深度 // max_depth: 最大深度 int search(int board[BOARD_SIZE], int piece_num[PIECE_TYPE], int dark_num, int red_num, int black_num, int turn, int depth, int max_depth, clock_t t) { int s[MAX_MOVE], d[MAX_MOVE], cap_s[MAX_MOVE], cap_d[MAX_MOVE]; int i, m, n; int curr_score, score, value; int src, dest, cap; int factor; // factor: 將分數轉換為相對於自己的分數 factor = (turn == RED) ? 1 : -1; // 終止條件 if(red_num == 0) return -(MAX_SCORE-depth-1) * factor; if(black_num == 0) return (MAX_SCORE-depth-1) * factor; if(depth == max_depth) return factor * eval(piece_num); src = 0; dest = 0; score = -MAX_SCORE; if(clock() > t) return score; // 產生走法 move_gen(board, turn, s, d, &n, cap_s, cap_d, &m); // if(depth == 1) // printf("%d %d\n", n, m); // 對吃子步進行搜尋 for(i = 0; i < m; i++){ cap = move(board, piece_num, &red_num, &black_num, cap_s[i], cap_d[i]); value = -search(board, piece_num, dark_num, red_num, black_num, !turn, depth + 1, max_depth, t); if(value >= score){ score = value; src = cap_s[i]; dest = cap_d[i]; } unmove(board, piece_num, &red_num, &black_num, cap_s[i], cap_d[i], cap); // if(depth == 1) // printf("%d %d %d\n", value, cap_s[i], cap_d[i]); } // 對走子步進行搜尋 for(i = 0; i < n; i++){ cap = move(board, piece_num, &red_num, &black_num, s[i], d[i]); value = -search(board, piece_num, dark_num, red_num, black_num, !turn, depth + 1, max_depth, t); if(value >= score){ score = value; src = s[i]; dest = d[i]; } unmove(board, piece_num, &red_num, &black_num, s[i], d[i], cap); // if(depth == 1) // printf("%d %d %d\n", value, s[i], d[i]); } // 計算當前盤面之審局分數 curr_score = eval(piece_num) * factor; // 判斷是否執行null move if(dark_num > 0 && score <= curr_score){ value = -search(board, piece_num, dark_num, red_num, black_num, !turn, depth + 1, max_depth, t); if(value >= score){ score = value; src = 0; dest = 0; } // if(depth == 1) // printf("%d 0 0\n", value); } if(depth == 1){ best_src = src; best_dest = dest; } return score; }
/* Alpha Beta Pruning - Minimax Search Algorithm */ move_t alpha_beta(board_t *b, int32_t alpha, int32_t beta, uint32_t ply) { uint8_t i, type; move_list_t *list; move_t m, best; /* Query ECO tree */ if(atoi(config->name) >= 50) if(query_eco(&m)) return m; /* Query transposition table */ type = query_transposition(b->hash, alpha, beta, ply, &m); switch(type) { case TYPE_ALPHA: /* Chooses the best alpha between the old and the one from the table */ alpha = MAX(alpha, m.eval); /* If we have a Beta cutoff, returns */ if(alpha >= beta) return m; break; case TYPE_BETA: /* Chooses the best beta between the old and the one from the table */ beta = MIN(beta, m.eval); /* If we have a Beta cutoff, returns */ if(alpha >= beta) return m; break; case TYPE_EXACT: /* If the table had an exact evaluation, returns it */ return m; case TYPE_INVALID: /* If the transposition is invalid, just ignore and keep going*/ break; } /* If it's a leaf node, evaluate it properly */ if(ply == 0) { m.eval = heuristic(b, onmove); return m; } /* Default type of the value to be inserted in the Transposition table */ type = TYPE_ALPHA; /* Initialize the best possible move as blank */ SET_BLANK_MOVE(best); /* Get the possible next moves */ list = gen_move_list(b, FALSE); /* For each possible next move... */ for(i = 0; i < list->size; i++) { /* Let's see the board after that move... */ move(b, list->move[i]); /* Did we reach any end game condition? */ switch(end(b)) { case CHECK_MATE: m.eval = (onmove == b->onmove) ? -MAX_HEU : MAX_HEU; break; case STALE_MATE: case REPETITION: case FIFTY_MOVES: case TWO_KINGS: m.eval = -MAX_HEU; break; case NO_MATE: default: /* If not, keep searching down in the search tree */ m = alpha_beta(b, -beta, -alpha, ply - 1); m.eval = -m.eval; break; } /* Restores the previous board (before the possible move) */ unmove(b); /* Beta cutoff */ if(m.eval >= beta) { best = list->move[i]; best.eval = m.eval; type = TYPE_BETA; break; /* Alpha cutoff */ } else if(m.eval > alpha) { best = list->move[i]; alpha = best.eval = m.eval; type = TYPE_EXACT; /* Best possible move until now */ } else if(i == 0 || m.eval > best.eval) { best = list->move[i]; best.eval = m.eval; } /* If our time's up, return immediately */ if(get_timeout()) { clear_move_list(list); return best; /*break;*/ } } /* Update the Transposition table */ add_transposition(b->hash, type, ply, best); /* Clear temporary information and return */ clear_move_list(list); return best; }