int main(int argc, char *argv[]) { if (argc > 1) { if (strcmp(argv[1], "0") == 0) { test_speed(); } else if (strcmp(argv[1], "1") == 0) { test(); } else if (strcmp(argv[1], "2") == 0) { test_perft_startpos(4); test_perft(); } else if (strcmp(argv[1], "3") == 0) { test_bratko_kopec(); } else if (strcmp(argv[1], "4") == 0) { test_mate_in_2(); test_eval_depth_1(); test_eval_depth_2(); } } else { // Start here // Waiting for GUI char *ret; char input[1001]; while (1) { ret = fgets(input, 1000, stdin); if (ret && strstr(input, "uci")) { break; } } Chess chess; Util::open_debug_file(); chess.sort_alfarray = true; chess.start_game(); chess.processCommands(input); } }
Board* Board::ReplacePlayers(Player* player1, Player* player2) { Board* board = new Board(); Chess* piece; Player* player; for (int i = 0; i < pp9k::BoardSize; i++) { for (int j = 0; j < pp9k::BoardSize; j++) { piece = this->Chesses[i + j * pp9k::BoardSize]; if (piece == NULL) { board->Chesses[i + j * pp9k::BoardSize] = NULL; } else { player = piece->GetPlayer()->GetSide() == White ? player1 : player2; board->Chesses[i + j * pp9k::BoardSize] = piece->ReplacePlayer(player); } } } board->Confirmed = this->Confirmed; board->Turn = this->Turn; return board; }
void SceneGame::startClient(Ref* sender) { MenuItem* item = (MenuItem*)sender; item->setEnabled(false); Ref* obj = item->getUserObject(); item = (MenuItem*)obj; item->setEnabled(false); _bRedSide = false; if (NetBattle::Connect("127.0.0.1")) { cocos2d::log("client Start Game"); for (int i = 0; i < 32; i++) { Chess* s = _c->at(i); s->_row = 9 - s->_row; s->_col = 8 - s->_col; s->setPosition(s->fromPlate()); } NetBattle::RecvStart(); schedule(schedule_selector(SceneGame::CheckRecv)); } else { cocos2d::log("Connect failure...."); } }
void SceneGame::restartInit() { // init Chess for (int i = 0; i < 32; ++i) { _c->at(i)->initChess(i, true); } if (!_bRedSide) { for (int i = 0; i < 32; i++) { Chess* s = _c->at(i); s->_row = 9 - s->_row; s->_col = 8 - s->_col; s->setPosition(s->fromPlate()); } NetBattle::RecvStart(); schedule(schedule_selector(SceneGame::CheckRecv)); } _selectSprite->setOpacity(0); _selectid = -1; _bRedTurn = true; _steps->clear(); }
void SceneGame::doRegret() { if (_steps->size() == 0) return; Step* step = *_steps->rbegin(); _steps->pop_back(); Chess* s = _c->at(step->moveid); s->_row = step->rowFrom; s->_col = step->colFrom; s->setPosition(s->fromPlate()); Chess* kill; if (step->killid != -1) { kill = _c->at(step->killid); kill->_dead = false; kill->setVisible(true); } _bRedTurn = !_bRedTurn; delete step; _selectid = -1; _selectSprite->setVisible(false); }
//每当落子后,判断胜负 bool ChessBoard::judgeVictory(Chess& lastchess) { int row = lastchess.getRow(); int column = lastchess.getColumn(); int type = lastchess.getChessType(); //垂直方向 if(fiveInDirection(row, column, type, VERTICAL)) { return VICTORY; } //水平方向 if(fiveInDirection(row, column, type, HORIZON)) { return VICTORY; } //右斜方向 if(fiveInDirection(row, column, type, RIGHT)) { return VICTORY; } //左斜方向 if(fiveInDirection(row, column, type, LEFT)) { return VICTORY; } return CONTINUE; }
bool Board::IsCheck(Color side) { Moves* moves = new Moves(); int j = 0; for (int i = 0; i < BoardSize * BoardSize; i++) { if (this->Chesses[i] != NULL && this->Chesses[i]->GetPlayer()->GetSide() == side) this->Chesses[i]->GetAvailableMoves(this, moves, true); for (; j < moves->GetLength(); j++) { Move* move = moves->GetMove(j); Chess* captured = move->GetChessCaptured(); if (captured != NULL && captured->GetChessType() == King) { delete moves; return true; } } } delete moves; return false; }
int main(int argc, char *argv[]) { QApplication a(argc, argv); Chess w; w.show(); return a.exec(); }
void reset() { int remStep=step; Chess chess; for(int i = 1; i <= remStep; i++) { chess.DelMove(); } }
void Game_private::usergo(int x, int y) { if (mp_chess->getData(x, y) != 0) return; mp_chess->setOneData(x, y, 1); mp_area->setCanUserclick(false); int judge_ans = mp_chess->judge(); if (judge_ans == 1) { gameWin(); return; } if (judge_ans == 2) { gameOver(); return; } Glib::Threads::Thread::create( sigc::mem_fun(*this, &Game_private::runAI) ); }
//指定一个棋子 void ChessBoard::setOneChess(Chess chess) { int row = chess.getRow(); int column = chess.getColumn(); this->chessboardmat[row][column] = chess.getChessType(); steps.push_back(chess); }
void test_speed() { const char input[] = "position fen r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1"; puts(input); Chess chess; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 2; chess.make_move(); }
void test_eval_depth_2() { const char input[] = "position fen 4k2n/8/6p1/6P1/6p1/6P1/8/4K2N w - - 0 1 "; puts(input); Chess chess; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 5; chess.default_seldepth = 0; chess.make_move(); }
void Game_private::runAI() { printf("Thread AI run\n"); mp_AI->RunAI(mp_chess); if (mp_AI->get_ans_tx()) { printf("电脑投降了\n"); gameWin(); return; } mp_chess->setOneData(mp_AI->get_ans_x(), mp_AI->get_ans_y(), 2); int judge_ans = mp_chess->judge(); if (judge_ans == 1) { gameWin(); return; } if (judge_ans == 2) { gameOver(); return; } mp_area->queue_draw(); mp_area->setCanUserclick(true); }
int main() { //Game g; //g.playOneGame(0); Chess c; c.foo(); //c.playOneGame(0); system("pause"); return 0; }
//------------------------------------------------------------------------------ bool RookPiece::areSquaresLegal(int src_row, int src_col, int dest_row, int dest_col, Piece* game_area[8][8], Chess& game_control) { if(src_row == dest_row) { // be sure that all squares are empty int col_offset = (dest_col - src_col > 0) ? 1 : -1; for(int check_col = src_col + col_offset; check_col != dest_col; check_col += col_offset) { // check if the coordinates are valid if((check_col < 0 || check_col > 7)) { return false; } if(game_area[src_row][check_col] != NULL) { game_control.setErrorCode(ERROR_PIECE_ON_WAY); return false; } } return true; } else if(dest_col == src_col) { int row_offset = (dest_row - src_row > 0) ? 1 : -1; for(int check_row = src_row + row_offset; check_row != dest_row; check_row += row_offset) { // check if the coordinates are valid if((check_row < 0 || check_row > 7)) { return false; } if(game_area[check_row][src_col] != NULL) { game_control.setErrorCode(ERROR_PIECE_ON_WAY); return false; } } return true; } return false; }
void test_mate_in_2() { const char input[] = "position fen 2bqkbn1/2pppp2/np2N3/r3P1p1/p2N2B1/5Q2/PPPPKPP1/RNB2r2 w " "KQkq - 0 1"; puts(input); Chess chess; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 5; chess.default_seldepth = 0; chess.make_move(); }
void test_bratko_kopec_12() { const char input[] = "position fen r3r1k1/ppqb1ppp/8/4p1NQ/8/2P5/PP3PPP/R3R1K1 b - " "- 0 1 bm Bf5; id BK.12;"; puts(input); Chess chess; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 2; chess.make_move(); assert(strcmp(Util::move2str(chess.best_move), "d7f5 ") == 0); }
void test_move_h2h4() { Chess chess; chess.start_game(); const char input[] = "position startpos moves h2h4"; chess.uci->position_received(input); // chess.max_time = 300000 / 40; chess.max_time = 0; chess.gui_depth = 4; chess.make_move(); assert(strcmp(Util::move2str(chess.best_move), "h7h5 ") == 0); assert(chess.nodes == 34298); assert(7000 < chess.table->eval->hash->hash_nodes); }
void test_bratko_kopec_10() { const char input[] = "position fen " "3rr1k1/pp3pp1/1qn2np1/8/3p4/PP1R1P2/2P1NQPP/R1B3K1 b - - 0 " "1 bm Ne5; id BK.10;"; puts(input); Chess chess; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 5; chess.make_move(); assert(strcmp(Util::move2str(chess.best_move), "c6e5 ") == 0); }
void test_bratko_kopec_2() { const char input[] = "position fen 3r1k2/4npp1/1ppr3p/p6P/P2PPPP1/1NR5/5K2/2R5 w - " "- 0 1 bm d5; id BK.02;"; puts(input); Chess chess; chess.sort_alfarray = true; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 5; chess.make_move(); assert(strcmp(Util::move2str(chess.best_move), "d4d5 ") == 0); }
void test_perft_pos(const char *input, const int depth, const uint64_t *expected_nodes) { for (int i = 1; i <= depth; i++) { Chess chess; chess.start_game(); chess.table->setboard(input); uint64_t start_time = Util::get_ms(); uint64_t nodes = chess.perft(i); uint64_t stop_time = Util::get_ms(); uint64_t duration = stop_time - start_time; printf("depth: %d nodes: %lu time: %lu ms knps: %lu\n", i, nodes, duration, duration == 0 ? 0 : nodes / duration); assert(nodes == expected_nodes[i - 1]); } }
bool ChessSaver::chess_check(Chess& chess, Chess::PieceType player_type_) { int black_num = 0; int white_num = 0; Chess::PieceType type; int i, j; for (i = 0; i < Chess::SIZE; i++) { for (j = 0; j < Chess::SIZE; j++) { type = chess.get_point(i, j); if (type == Chess::BLACK) black_num++; else if (type == Chess::WHITE) white_num++; } } if (player_type_ == Chess::BLACK) //мФ╪рж╢╨звсохоб { if (black_num == white_num) return true; return false; } else if (player_type_ == Chess::WHITE) { if (black_num - 1 == white_num) return true; return false; } else return false; }
void test_bratko_kopec_1b() { const char input[] = "position fen 1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - " "- 0 1 bm Qd1+; id BK.01;"; puts(input); Chess chess; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 1; chess.make_move(); assert(strcmp(Util::move2str(chess.best_move), "d6d1 ") == 0); assert(chess.mate_score == 21995); assert(chess.root_moves[0].value == 21995); assert(chess.depth == 1); }
void test_calculate_evarray_new(Chess &chess) { for (int i = 0; i < chess.nof_legal_root_moves; i++) { chess.root_moves[i].move = 100 - i; chess.root_moves[i].value = i; } /* for (int i = 0; i < chess.nof_legal_root_moves; i++) { printf("%2d. %2d %2d\n", i + 1, chess.root_moves[i].move, chess.root_moves[i].value); } */ chess.calculate_evarray_new(); /* for (int i = 0; i < chess.nof_legal_root_moves; i++) { printf("%2d. %2d %2d\n", i + 1, chess.root_moves[i].move, chess.root_moves[i].value); } */ for (int i = 0; i < chess.nof_legal_root_moves - 1; i++) { assert(chess.root_moves[i].value >= chess.root_moves[i + 1].value); } }
void ChessSelectionWidget::okPushButtonClicked() { if(isOK()) { ChessType type = AIType; QString stype(tr("ai")); if(serverRadioButton->isChecked()) { type = ServerType; stype = tr("server"); } else if(clientRadioButton->isChecked()) { type = ClientType; stype = tr("client"); } else if(replayRadioButton->isChecked()) { type = ReplayType; stype = tr("replay"); } ChessColor color = RedColor; QString scolor("red"); if(ClientType == type) { color = BlackColor; scolor = tr("black"); } Chess_Info(tr("your selection: type=%1 color=%2 ip=%3 port=%4") .arg(stype) .arg(scolor) .arg(ipLineEdit->text()) .arg(portSpinBox->value())); ChessInformation *ci = ChessInformation::instance(); ci->init(type, color, ipLineEdit->text(), portSpinBox->value()); Chess *chess = Chess::instance(); chess->setAttribute(Qt::WA_DeleteOnClose); //窗口关闭时自动删除 this->hide(); chess->show(); } else { Chess_Warning(tr("error input, please check!")); QMessageBox::warning(this, tr("ERROR INPUT"), tr("ERROR INPUT, PLEASE CHECK!")); } }
void SceneGame::ComputerMove(float dt) { Step* step = AI::getStepByAlphaBetaSearch(this, 2); Chess* moveChess = _c->at(step->moveid); moveChess->_row = step->rowTo; moveChess->_col = step->colTo; _bRedTurn = !_bRedTurn; moveChess->setPosition(moveChess->fromPlate()); if (step->killid != -1) { Chess* killChess = _c->at(step->killid); killChess->_dead = true; killChess->setVisible(false); } _steps->push_back(step); }
void test_calculate_evarray(Chess &chess) { for (int i = 0; i < chess.nof_legal_root_moves; i++) { chess.root_moves[i].move = 100 - i; chess.root_moves[i].value = i; } chess.calculate_evarray(); for (int i = 0; i < chess.nof_legal_root_moves - 1; i++) { assert(chess.root_moves[i].value >= chess.root_moves[i + 1].value); } }
void test_bratko_kopec_1() { // 2018.01.23. linode, c++: info depth 5 seldepth 5 time 16964 nodes 1865184 nps 109949 // 2018.01.23. core i5 c++: info depth 5 seldepth 5 time 8952 nodes 1865184 nps 208353 const char input[] = "position fen 1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - " "- 0 1 bm Qd1+; id BK.01;"; puts(input); Chess chess; chess.sort_alfarray = true; chess.start_game(); chess.table->setboard(input); chess.max_time = 0; chess.gui_depth = 5; chess.default_seldepth = 0; chess.break_if_mate_found = false; // remove chess.make_move(); assert(strcmp(Util::move2str(chess.best_move), "d6d1 ") == 0); assert(chess.mate_score == 21995); assert(chess.root_moves[0].value == 21995); }
//悔棋 bool ChessBoard::backStep() { if(steps.size() < 2) { return false; } for(int i = 0; i < 2; i++) { vector<Chess>::iterator it = steps.end() - 1; Chess chess = *it; int row = chess.getRow(); int column = chess.getColumn(); this->chessboardmat[row][column] = EMPTY_CHESS; steps.pop_back(); } return true; }