CPerftSuite::CPerftSuite(void) { std::ifstream params; params.open("perftsuite.epd", std::ifstream::in); if (params.is_open()) { std::string line; int square = chess::A8; while (! params.eof()) { getline(params, line); if (line[0] == '#') continue; boost::char_separator<char> sep(";"); boost::char_separator<char> sep2(" "); boost::tokenizer<boost::char_separator<char>> toks(line, sep); // 1st tok is fen auto tok = toks.begin(); const std::string fen = *tok++; CBoard b; b.set_fen_position(fen); std::cout << b.fen() << " "; int depth = 1; while (tok != toks.end()) { boost::tokenizer<boost::char_separator<char>> toks2(*tok, sep2); auto tok2 = toks2.begin(); tok2++; uint64_t i = std::stoull(*tok2); if (i > 100000000) break; // takes too long CPerft p(b); auto res = p.DoPerft(depth); if (res.nodes == i) { std::cout << "."; } else { std::cout << "WRONG (depth " << depth << ": " << res.nodes << " != expected " << i << ")" << std::endl; ASSERT(res.nodes == i); break; } depth++; tok++; } } std::cout << "completed"; } }
void CDisease::loadFrom(QTextStream &is) { QString colorStr = is.readLine(); color = (DiseaseType)DiseaseType_SL.indexOf(colorStr); QString cureStatusStr = is.readLine(); status = (CureStatus)CureStatus_SL.indexOf(cureStatusStr); int cubesSize = is.readLine().toInt(); CBoardItemsFactory factory; CBoard* board = CBoardItemsFactory().getBoard(); for (int i = 0; i < cubesSize; ++i) { //CDiseaseCube *newCube = dynamic_cast<CDiseaseCube*>(factory.create(is)); //newCube->delayedLoad(); //int id = newCube->location->getId(); //delete newCube; //cubesInGame += newCube; int id; is >> id; CCity* destination = dynamic_cast<CCity*>(board->findItemById(id)); if (destination == nullptr) { QMessageBox::critical(nullptr, "Load error", "Error - cannot restore "+DiseaseType_SL[color]+" to an unknown city #"+QString::number(id)); } else { addSingleCube(destination); } } is.readLine(); }
bool CApp::bCreateWorld() { m_pCamera = 0; m_hBoard = 0; // Create and setup camera ------------------------------------------------ m_pCamera = new CMyCamera( pGetGraphics() ); if( !m_pCamera->bCreateRenderCamera( iGetWindowWidth(), iGetWindowHeight() ) ) return false; m_pCamera->CalculateProjection( M3D_PI * 0.5f, 2.0f, 0.001f ); m_pCamera->SetPosition( vector3( -0.01f, 0.025f, 0 ) ); m_pCamera->SetLookAt( vector3( 0, 0, 0.05f ), vector3( 0, 1, 0 ) ); m_pCamera->CalculateView(); // Register board-entity and create an instance --------------------------- pGetScene()->RegisterEntityType( "board", CBoard::pCreate ); m_hBoard = pGetScene()->hCreateEntity( "board" ); if( !m_hBoard ) return false; CBoard *pBoard = (CBoard *)pGetScene()->pGetEntity( m_hBoard ); if( !pBoard->bInitialize() ) return false; return true; }
void CGame::Play() { CBoard *Board = new CBoard; Board->DrawBoard(); delete Board; CSnake *Snake = new CSnake; //sets difficulty switch(dif_lvl) { case easy: CSnake::delay = 160; CSnake::multiplier = 1; break; case medium: CSnake::delay = 140; CSnake::multiplier = 1; break; case hard: CSnake::delay = 120; CSnake::multiplier = 0.9; break; case extreme: CSnake::delay = 100; CSnake::multiplier = 0.8; break; } while(Snake->Move() != 0) { if(GetAsyncKeyState(VK_UP) & 0x8000) { Snake->ChangeDirection(up); } else if(GetAsyncKeyState(VK_DOWN) & 0x8000) { Snake->ChangeDirection(down); } else if(GetAsyncKeyState(VK_LEFT) & 0x8000) { Snake->ChangeDirection(static_cast<Direction>(2)); } else if(GetAsyncKeyState(VK_RIGHT) & 0x8000) { Snake->ChangeDirection(static_cast<Direction>(3)); } else if(GetAsyncKeyState(VK_ESCAPE) & 0x8000) break; } int score = Snake->GetScore(); delete Snake; GameOver(score); }
// ---------------------------------------------------------------------------- // main_exec // ---------------------------------------------------------------------------- int main_exec(int fdfb, int fdrc, int fdlcd, char *cfgfile) { // srand(time(NULL)); if(FBInitialize(720, 576, 8, fdfb) < 0) return -1; if(RcInitialize(fdrc) < 0) return -1; CBoard Board; Board.LoadSettings(SETTINGSFILE); if(Board.LoadGame(GAMEFILE) <= 0) Board.NewGame(); Board.InitBoard(); doexit = 0; while(!doexit) { actcode = 0xEE; RcGetActCode(); Board.MoveCursor(); while(realcode != 0xEE) RcGetActCode(); } Board.SaveSettings(SETTINGSFILE); Board.SaveGame(GAMEFILE); shutdown(); return 0; }
bool CBitbases::probe(eval_t& eval, const CBoard& board) { if (loaded()) { int32 player = (board.getSide() == eBlack ? _BLACK : _WHITE); int32 piece[MAX_PIECES] = { _EMPTY, _EMPTY, _EMPTY, _EMPTY, _EMPTY, _EMPTY, _EMPTY, _EMPTY, _EMPTY }; int32 square[MAX_PIECES] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int32 count = 0; bb_t pieces = board.getOccupBits(); while (pieces) { EField from = popFirstBit(pieces); EPiece type = board.getPiece(from); square[count] = from; switch (type) { case eW_P: piece[count] = _WPAWN; break; case eW_N: piece[count] = _WKNIGHT; break; case eW_B: piece[count] = _WBISHOP; break; case eW_R: piece[count] = _WROOK; break; case eW_Q: piece[count] = _WQUEEN; break; case eW_K: piece[count] = _WKING; break; case eB_P: piece[count] = _BPAWN; break; case eB_N: piece[count] = _BKNIGHT; break; case eB_B: piece[count] = _BBISHOP; break; case eB_R: piece[count] = _BROOK; break; case eB_Q: piece[count] = _BQUEEN; break; case eB_K: piece[count] = _BKING; break; case eNP: break; } ++count; } eval_t eval_eg = m_lib.probe_egbb(player, piece, square); if (eval_eg != NOTFOUND) { eval = eval_eg; ++m_hits; return(true); } } return(false); }
void CDisease::addSingleCube(CCity * location) { if (status == TREATED) return; if (location->cubesOf(color) < 3) { CDiseaseCube* newCube = dynamic_cast<CDiseaseCube*>(CBoardItemsFactory().create("CDiseaseCube", location->getZoom(), location->pos())); //newCube->whereStr = location->getName(); newCube->color = color; newCube->moveTo(location); newCube->updateOptions(); cubesInGame += newCube; newCube->stackUnder(location); CBoard* board = newCube->container; for (int index = DiseaseType_SL.size() - 1; index > (int)color; --index) for (CDiseaseCube* cube : board->disease((DiseaseType)index)->cubesIn(location)) if (cube->nth == 1) newCube->stackUnder(cube); } else QMessageBox::critical(nullptr, "Add too many cubes", "Error - city " + location->getName() + " cannot contain more than 3 cubes"); }
bool CTablebases::probe(eval_t& eval, const CBoard& board) { if (loaded()) { int rgiCounters[10]; for (int32 i = 0; i < 10; ++i) rgiCounters[i] = 0; const int rgsq_cnt = C_PIECES * 5 + 1; square rgsqWhite[rgsq_cnt]; square rgsqBlack[rgsq_cnt]; for (int32 i = 0; i < rgsq_cnt; ++i) rgsqWhite[i] = rgsqBlack[i] = XX; VInitSqCtr(rgiCounters, rgsqWhite, 0, board.getPieceBits(eW_P)); VInitSqCtr(rgiCounters, rgsqWhite, 1, board.getPieceBits(eW_N)); VInitSqCtr(rgiCounters, rgsqWhite, 2, board.getPieceBits(eW_B)); VInitSqCtr(rgiCounters, rgsqWhite, 3, board.getPieceBits(eW_R)); VInitSqCtr(rgiCounters, rgsqWhite, 4, board.getPieceBits(eW_Q)); VInitSqCtr(rgiCounters + 5, rgsqBlack, 0, board.getPieceBits(eB_P)); VInitSqCtr(rgiCounters + 5, rgsqBlack, 1, board.getPieceBits(eB_N)); VInitSqCtr(rgiCounters + 5, rgsqBlack, 2, board.getPieceBits(eB_B)); VInitSqCtr(rgiCounters + 5, rgsqBlack, 3, board.getPieceBits(eB_R)); VInitSqCtr(rgiCounters + 5, rgsqBlack, 4, board.getPieceBits(eB_Q)); int iTb = IDescFindFromCounters(rgiCounters); if (iTb) { bb_t wk = board.getPieceBits(eW_K); while (wk) rgsqWhite[C_PIECES * 5] = popFirstBit(wk); bb_t bk = board.getPieceBits(eB_K); while (bk) rgsqBlack[C_PIECES * 5] = popFirstBit(bk); color side; int fInvert; square* psqW; square* psqB; if (iTb > 0) { side = eWhite == board.getSide() ? x_colorWhite : x_colorBlack; fInvert = 0; psqW = rgsqWhite; psqB = rgsqBlack; } else { side = eWhite == board.getSide() ? x_colorBlack : x_colorWhite; fInvert = 1; psqW = rgsqBlack; psqB = rgsqWhite; iTb = -iTb; } if (FRegisteredFun(iTb, side)) { square sqEnP = eNF != board.getEp() ? board.getEp() : XX; INDEX ind = PfnIndCalcFun(iTb, side) (psqW, psqB, sqEnP, fInvert); int tbValue = L_TbtProbeTable(iTb, side, ind); if (tb_broken != tbValue) { if (tbValue > 0) eval = MATE_VALUE + 2 * (-tb_mate_in_1 + tbValue - 1); else if (tbValue < 0) eval = -MATE_VALUE + 2 * (tb_mate_in_1 + tbValue); else eval = DRAW_VALUE; ++m_hits; return (true); } } } } return(false); }
int AI::calculate(CBoard &board) { return board.sampleHeuristicValue(); }
DECISION_INFO AI::decision(GAME_INFO gameInfo) { DECISION_INFO result={0}; CBoard myBoard(gameInfo.board); //myboard CBoard targetBoard(gameInfo.targetBoard);//enemy's board //myBoard.printBoard(); int currentBlock=gameInfo.nextBlock[0]; int maxTurn=CBoard::maxTurn[currentBlock]; CBoard tmp; int maxScore=-999999999; for(int x=-2;x<10;x++) //for possible X { for(int turn=0;turn<maxTurn;turn++) //for possible rotation { tmp=myBoard; int posY=tmp.dropBlock(x,0,currentBlock,turn); if(posY==0) continue; int filledLine=tmp.countFilledLine(); int score=calculate(tmp); score+=filledLine; //printf("[[%d::( %d, %d ) ]] ",score,x,turn); if(score>maxScore) { maxScore=score; result.posX=x; result.turn=turn; result.useHold=0; } } } printf("\n"); printf("put block on... posX:%d turn: %d\n",result.posX,result.turn); int randomMessage=rand()%4; switch(randomMessage) { case 0: makeMessage(result,L"hello~ \r\nhi~"); break; case 1: makeMessage(result,L"100ÄÞº¸!\r\n1000ÄÞº¸!"); break; case 2: //clear message makeMessage(result,L" "); break; case 3: makeMessage(result,NULL); //Reuse previous message(if all data is NULL, server uses previous message) break; } /* myBoard.printBoard(); System.out.println(); myBoard.dropBlock(result.posX,0,gameInfo.nextBlock[0],result.turn); myBoard.deleteFilledLine(); myBoard.printBoard(); */ /* result.posX=rand()%10; result.turn=rand()%4; result.useHold=0; memset(result.message,0,sizeof(result.message)); //set all data to NULL */ return result; }
int main(int args, char** argc) { try { Window::Init("FPS"); } catch (const std::runtime_error &g) { std::cout << g.what() << std::endl; Window::Quit(); return -1; } Timer timer; int frame = 0; //setting a position to draw image in SDL_Texture *img = Window::LoadImage("../img/squares.png"); //set up board CBoard Gameboard; SDL_Rect pos = {Window::mBox.w / 2 - (5*TILE_SIZE), Window::mBox.h - (17*TILE_SIZE),TILE_SIZE*16,TILE_SIZE*10}; //Gameboard.Init(10,16,pos); Gameboard.LoadForMenus(); bool quit = false; SDL_Event e; CTetromino piece; CTetromino *tetrapiece; tetrapiece = &piece; int fpsCount = 0; //main loop timer.Start(); while(!quit) { //read input, handle it while(SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDLK_UP: if (Gameboard.GameState == MENUS) { Gameboard.SelectorPos -= SELECTOR_JUMP; } break; case SDLK_DOWN: if (Gameboard.GameState == GAME) { if (piece.can_move_down() && Gameboard.is_clear_down(tetrapiece)) { //grays out old piece position Gameboard.GrayOutOldPosition(tetrapiece); //moves coordinates piece.MoveDown(); //updates piece moves += 10; } else { Gameboard.landed = true; } } else if (Gameboard.GameState == MENUS) { Gameboard.SelectorPos += SELECTOR_JUMP; } break; case SDLK_LEFT: if (Gameboard.GameState == GAME) { if (piece.can_move_left() && Gameboard.is_clear_left(tetrapiece)) { Gameboard.GrayOutOldPosition(tetrapiece); piece.MoveLeft(); moves -=1; } } break; case SDLK_RIGHT: if (Gameboard.GameState == GAME) { if (piece.can_move_right() && Gameboard.is_clear_right(tetrapiece)) { Gameboard.GrayOutOldPosition(tetrapiece); piece.MoveRight(); moves += 1; } } break; case SDLK_r: if (Gameboard.GameState == GAME) { Gameboard.GrayOutOldPosition(tetrapiece); Gameboard.RotateTetromino(tetrapiece, moves, CLOCKWISE); } break; case SDLK_c: if (Gameboard.GameState == GAME) { Gameboard.GrayOutOldPosition(tetrapiece); Gameboard.RotateTetromino(tetrapiece, moves, COUNTERCLOCKWISE); } break; case SDLK_RETURN: if (Gameboard.GameState == MENUS) { if (Gameboard.SelectorPos == 0) { Gameboard.GameState = GAME; Gameboard.Init(10,16,pos); } else if (Gameboard.SelectorPos == 3*SELECTOR_JUMP) { quit = true; } else if (Gameboard.SelectorPos == 2*SELECTOR_JUMP) { Gameboard.GameState = HISCORES; } else if(Gameboard.SelectorPos == 1*SELECTOR_JUMP) { Gameboard.GameState = OPTIONS; } } break; case SDLK_ESCAPE: Gameboard.LoadForMenus(); Gameboard.GameState = MENUS; break; } break; case SDL_KEYUP: switch (e.key.keysym.sym) { case SDLK_RIGHT: break; case SDLK_LEFT: break; case SDLK_UP: break; case SDLK_DOWN: break; } break; } } //render the scene //std::cout << moving_tile << std::endl; Window::Clear(); switch (Gameboard.GameState) { case MENUS: { Gameboard.DrawMenus(Window::mRenderer, Window::font); break; } case GAME: { if (Gameboard.landed) { Gameboard.landed = true; Gameboard.MarkLanded(); Gameboard.EatLine(); Gameboard.CreateTetromino(tetrapiece); Gameboard.GetNewPieceOnBoard(tetrapiece); moves = 0; } Gameboard.Update(tetrapiece); Gameboard.DrawBoard(Window::mRenderer); Gameboard.DrawScore(Window::mRenderer, Window::font); break; } case GAMEOVER: { //TODO: this should run only once //Gameboard.landed = true; Gameboard.DrawGameOverScreen(Window::mRenderer, Window::font); Gameboard.DrawHiscores(Window::mRenderer, Window::font); break; } case HISCORES: { Gameboard.DrawHiscores(Window::mRenderer, Window::font); //std::cout << "drawn"; break; } }; Window::Present(); frame += 1; // increment rendered frames if (frame > 62) SDL_Delay(17); if (timer.Ticks() > 1000) { if (Gameboard.GameState == GAME) { if (piece.can_move_down() && Gameboard.is_clear_down(tetrapiece)) { Gameboard.GrayOutOldPosition(tetrapiece); piece.MoveDown(); moves += 10; } else { Gameboard.landed = true; } } fpsCount = frame; timer.Restart(); Window::ChangeWindowTitle("FPS: " + std::to_string(fpsCount)); frame = 0; } } //SDL CLEANUP and QUIT SDL_DestroyTexture(img); Window::Quit(); return 0; }