void test_reasoned_move(){ makeMove(g_board,0); assert_int_equal(3, getReasonedMove(g_board)); makeMove(g_board,1); assert_int_equal(6, getReasonedMove(g_board)); }
int makeMove(int gameBoard[BOARD_SIZE][BOARD_SIZE], int player){ int row, column, pos; printf("Scegli una casella (0-8): "); scanf("%d", &pos); if(pos < 3){ row = 0; column = pos; } else if(pos >= 3 && pos < 6){ row = 1; column = pos % 3; } else if(pos >= 6 && pos < 9){ row = 2; column = pos % 3; } else if(pos >= 9 || pos < -1){ printf("Il numero inserito non corrisponde ad una casella.\n"); makeMove(gameBoard, player); } else if(pos == -1){ return -1; } if(gameBoard[row][column] == 0){ gameBoard[row][column] = player; } else{ printf("La casella selezionata e' gia' piena. Inserire un\'altra casella.\n"); makeMove(gameBoard, player); } }
//helper function to simulate winning moves void winning_move(int* moves, int expected_winner){ int i = 0; while((winnerIs(g_board) == 0) && validMovesLeft(g_board)){ if(getCurrentPlayer(g_board) == PLAYER_ONE){ makeMove(g_board, moves[i]); ++i; } else { makeMove(g_board, getReasonedMove(g_board)); } } assert_int_equal(expected_winner, winnerIs(g_board)); /* * assert_true(winnerIs(g_board) == 0); makeMove(g_board, 0); //player one moves makeMove(g_board, 1); //player two moves assert_true(winnerIs(g_board) == 0); makeMove(g_board, 0); //player one moves makeMove(g_board, 2); //player two moves assert_true(winnerIs(g_board) == 0); makeMove(g_board, 0); //player one moves makeMove(g_board, 3); //player two moves assert_true(winnerIs(g_board) == 0); makeMove(g_board, 0); //player one moves makeMove(g_board, 3); //player two moves assert_int_equal(expected_winner, winnerIs(g_board)); */ }
void GameTable::makeMove(int x, int y) { _map[x][y] = 0; int dirs[4] = {0,1,2,3}; std::random_shuffle(dirs, dirs + 4); for (int i = 0; i < 4; ++i) { /* *0-Up *1-Left *2-Down *3-Right */ switch (dirs[i]) { case 0: if (x >= 2 && _map[x - 2][y] == 1) { _map[x - 1][y] = 0; makeMove(x - 2, y); _map[x - 1][y] = 0; } break; case 1: if (y >= 2 && _map[x][y - 2] == 1) { _map[x][y - 1] = 0; makeMove(x, y - 2); _map[x][y - 1] = 0; } break; case 2: if (x < _width - 2 && _map[x + 2][y] == 1) { _map[x + 1][y] = 0; makeMove(x + 2, y); _map[x + 1][y] = 0; } break; case 3: if (y < _height - 2 && _map[x][y + 2] == 1) { _map[x][y + 1] = 0; makeMove(x, y + 2); _map[x][y + 1] = 0; } break; } } _map[x][y] = 0; }
GameResult Board::playGame(Patterns* patterns, std::default_random_engine& engine) { GameResult result; while (!isGameOver(&result)) { makeMove(patterns->getMove(*this, engine)); } return result; }
std::vector<Piece*> ChessBoard::findThreatheningPieces( Piece* p ) { std::vector<Piece*> pieces; pieces.push_back( NULL ); std::vector<Piece*> toCheck; if( p->color == WHITE ) { toCheck = blackPieces; } else { toCheck = whitePieces; } for( std::vector<Piece*>::iterator it = toCheck.begin(); it != toCheck.end(); ++it ) { if( (*it)->inPlay ) { Move m((*it)->x, (*it)->y, p->x, p->y); bool b = makeMove((*it)->x, (*it)->y, p->x, p->y); if( b ){ goBackAMove(); pieces.push_back( getPiece( (*it)->x, (*it)->y ) ); } } } return pieces; }
bool GameEngine::makeComputerMove() { if (getSetupMode()) { return false; } if (!isAnyMovePossible(m_curPlayer)) { return false; } // m_curPlayer = m_computerColor; if (m_curPlayer == Defs::White) { m_engine->setStrength(m_whiteSkill); } else { m_engine->setStrength(m_blackSkill); } m_thinkingInProgress = true; KReversiPos move = m_engine->computeMove( *this, true ); m_thinkingInProgress = false; if( !move.isValid() ) return false; if( (m_curPlayer == Defs::White && m_isWhiteHuman) || (m_curPlayer == Defs::Black && m_isBlackHuman)) { qDebug() << "Strange! makeComputerMove() just got not computer move!"; return true; } makeMove(move); m_undoStack.push( m_changedChips ); emit computerMoved (move.row*8+move.col); return true; }
int localGame() { system("cls"); char winner = 0; TILE board[121]; memset(&board, 0, sizeof board); initBoard(board); char currentPlayer = 1; TILE t = {0,0,0,0}, g; do { jumpTo(0,0); printf("%s, make a move!\n", currentPlayer==1?"Attacker":"Defender"); printBoard(board); int r; t = selectPiece(board, currentPlayer, t); if (tileEmpty(t)) return 0; printIntense(t); g = selectNewPosition(board, t, &r); if (tileEmpty(g)) return 0; if (r) continue; makeMove(t, g, board); winner = hasWinner(board, board[getIndex(g)]); currentPlayer = currentPlayer % 2 + 1; t = g; } while (!winner); // reblit board to capture last move jumpTo(0,1); printBoard(board); return winner; }
int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&width,&height); scanf("%s",way); scanf("%d",&wallNum); for(int i=0;i<wallNum;i++) { scanf("%d%d%d%d",&wall[i].y1,&wall[i].x1,&wall[i].y2,&wall[i].x2); } if(strcmp(way,"RRRRRUURUURULLDLLDRDDLULLDLUUUURRRRULURULLURRRRRDDDRDDRUURRRDRRDDDDDRUUUUURUULDLUULURRUUR")==0) { printf("INCORRECT\n"); continue; } makeGraph(); makeMove(); flood(0,0,startFlood); flood(endX,endY,endFlood); if(singleCheck() || unnessaryCheck()) { printf("INCORRECT\n"); } else { printf("CORRECT\n"); } } return 0; }
bool isLegalMove(Board_t self, int move) { makeMove(self, move); bool isLegal = wasLegalMove(self); undoMove(self); return isLegal; }
void genNumWin(int n[X_BOARD]) { int i, j, k; int xsquare[Y_BOARD][X_BOARD]; int xxsquare[Y_BOARD][X_BOARD]; for(i=0; i<X_BOARD; i++) { n[i]=0; if(y_Base(i, square) != -1) /*has base exsit?*/ { duplicate(xsquare, square); makeMove(COMPUTER, i, xsquare); for(j=0; j<X_BOARD; j++) { for(k=0; k<Y_BOARD; k++) { if(xsquare[k][j] == EMPTY || xsquare[k][j] == BASE) { duplicate(xxsquare, xsquare); xxsquare[k][j] = COMPUTER; if(isWon(COMPUTER, xxsquare)) n[i]++; } } } } } }
//Handle mouse input void mouse(int mouseButton, int buttonState, int x, int y) { GLfloat mouseX,mouseY; screenToWorld(x,y,mouseX,mouseY); Point mousePt(mouseX,mouseY,0); if(sb->getResetButton().pointInButton(mousePt) && buttonState == GLUT_DOWN) //did user click on reset button { sb->getResetButton().setButtonIsPressed(true); return; } if(sb->getResetButton().pointInButton(mousePt) && sb->getResetButton().isButtonPressed() && buttonState == GLUT_UP) //did user click on reset button { initGame(); initStartupScript(); return; } sb->getResetButton().setButtonIsPressed(false); if (gameStatus!= IN_PLAY) return; if( mouseButton == GLUT_LEFT_BUTTON && buttonState == GLUT_DOWN) { gamePiece tmp = player1Turn?player1GamePiece:player2GamePiece; Move m= getSlotFromPoint(mousePt); makeMove(m ,tmp); } glutPostRedisplay(); }
int ChessBoard::test1_max(int depth, int& mv) { if (depth==0) { mv = 0; return m_vlBlack - m_vlRed; } Moves mvs; //int selfSide = m_sdPlayer; int best = -999999; generateMoves(mvs); for(int i=0; i<mvs.count(); i++) { int oldVal = m_vlBlack - m_vlRed; int pcCapture; if (makeMove(mvs[i], pcCapture)) { int temp_mv; int val = test1_min(depth-1, temp_mv); if (val>best) { best = val; mv = mvs[i]; } undoMakeMove(mvs[i], pcCapture); } int newVal = m_vlBlack - m_vlRed; Q_ASSERT(oldVal==newVal); } return best; }
int main(int argc, char** argv) { char board[26][26]; int n; int numberOfMoves = 0; int *moves = &numberOfMoves; char computerColour, currentColour; printf("Enter the board dimension: "); scanf("%d", &n); makeBoard(board, n); printf("Computer plays (B/W) : "); scanf(" %c", &computerColour); currentColour = 'B'; char nextColour = 'W'; char tempColour; bool gameOver = 0; char rowMove, colMove; printBoard(board, n); while(!gameOver){ if(movesAvailable(board, n, currentColour)){ if(currentColour == computerColour){ decideMove(board, n, computerColour, moves); numberOfMoves++; } else{ printf("Enter move for colour %c (RowCol): ", currentColour); scanf(" %c%c", &rowMove, &colMove); if(checkLegalInPosition(board, n, rowMove - 'a', colMove - 'a', currentColour)){ makeMove(board, n, rowMove - 'a', colMove - 'a', currentColour); numberOfMoves++; } else{ printf("Invalid move.\n"); printf("%c player wins.\n", findWinner(board, n)); return 0; } } printBoard(board, n); } else{ printf("%c player has no valid move.\n", currentColour); } tempColour = currentColour; currentColour = nextColour; nextColour = tempColour; if(!movesAvailable(board, n, 'B') && !movesAvailable(board, n, 'W')){ printf("%c player wins.\n", findWinner(board, n)); gameOver = true; } } return (EXIT_SUCCESS); }
void test_valid_moves(){ int i = 0; for(i=0; i < rows; ++i){ assert_true(validMove(g_board,4)==1); makeMove(g_board, 4); } assert_true(validMove(g_board,4)==0); }
void MainWindow::onMakeMove(Grid *newMove) { #ifdef DEBUG_VIEW printLine2("mainWindow->onMakeMove() with new grid at address: ", newMove); #endif emit makeMove(newMove); }
/* Given a board and side on move, makes a move uniformly at random (game state is updated) */ int makeRandomMove(int board[2][NUM_PITS+1], int *side) { int move; move = pickRandomMove(board, *side); makeMove(board, side, move); return move; }
void MainWindow::playerClick(BoardIndex row, BoardIndex column) { if(gbw->gameBoard()->getColour(row,column) == T_EMPTY) { if(currentTurn == T_RED && !redai) { makeMove(row, column); } } }
/* validMove should return true IFF the chosen column is not full */ void test_validMove() { board_type *board = createBoard(X, Y); int i; CU_ASSERT_TRUE(validMove(board, 0)); for(i = 0; i < Y - 1; i++) makeMove(board, 0); CU_ASSERT_TRUE(validMove(board, 0)); makeMove(board, 0); CU_ASSERT_FALSE(validMove(board, 0)); free(board); }
inline Move selectedMakeMove(const PieceType pt, const Square from, const Square to, const Position& pos) { static_assert(PM == Promote || PM == NonPromote, ""); assert(!((pt == Gold || pt == King || MT == Drop) && PM == Promote)); Move move = ((MT == NonCapture || MT == NonCaptureMinusPro) ? makeMove(pt, from, to) : makeCaptureMove(pt, from, to, pos)); if (PM == Promote) { move |= promoteFlag(); } return move; }
void ChessPlayer::makeBookMove(const Chess::Move& move) { m_timeControl.startTimer(); makeMove(move); m_timeControl.update(false); m_eval.setBookEval(true); emit moveMade(move); }
minimaxRes minimaxPru(gameState* state, int depth, minimaxRes alpha, minimaxRes beta, int isMax){ char colorOfPlayer = state->turn; char colorOfEnemy = BLACK; gameState* currState = (gameState*)malloc(sizeof(gameState)); minimaxRes toRet; int index = 0, turn = 1; piece** currBoard; if (colorOfPlayer == BLACK){ colorOfEnemy = WHITE; turn = -1; } if (depth == 0){ // End of rec toRet.index = 0; toRet.value = isMax * turn * scoreOfBoard(state); if (toRet.value == 4000) toRet.value *= -1; free(currState); return toRet; } itemMove* moves = getMoves(state); itemMove* head = moves; if (&(head->move) == NULL){//Is a leaf toRet.index = 0; toRet.value = isMax * turn * scoreOfBoardWith(state, moves); if (toRet.value == 4000) toRet.value *= -1; destroyMoveList(moves); free(currState); return toRet; } while (head != NULL){ if (!isMateWith(state,moves)){ currBoard = makeMove(state->board, (head)); copyDtoS(currState->board, currBoard); destroyBoard(currBoard); currState->turn = colorOfEnemy; if (beta.value <= alpha.value){ break; } toRet = minimaxPru(currState, depth - 1, alpha, beta, -1 * isMax); if (isMax==1) replaceMMRs(&alpha, &toRet, isMax, index); else replaceMMRs(&beta, &toRet, isMax, index); head = head->next; index++; } } destroyMoveList(moves); free(currState); if (isMax==1) return alpha; else return beta; }
/* * Extension: Checks and adds to hash table using a bit ID based hash function */ void generateUniqueBoardBitHash(BoardNode currentBoard, int rowMove, int colMove, int currRow, int currCol) { BoardNode generatedBoard; generatedBoard = makeMove(copyParentToChild(currentBoard,createBoard(currentBoard)),rowMove,colMove,currRow,currCol,DELETE); bitEncoder(generatedBoard); if(generateBitHashKey(generatedBoard)) { addToQueue(generatedBoard); checkTarget(generatedBoard); freeBoardArray(generatedBoard); } }
void XboardEngine::startThinking() { setForceMode(false); sendTimeLeft(); if (m_nextMove.isNull()) write("go"); else makeMove(m_nextMove); }
// Get move as input then place it on board void Player::getMove(Board& board) { bool done = false; while (!done) { getCoords(); done = makeMove(board); } }
static float solver(struct Board* board, struct Node* node){ int i; //If the node's children haven't been generated, generate them if(node->childrenCount == NEEDS_CHILDREN){ struct Move moves[MAX_MOVES]; node->childrenCount = getMoves(board, moves, 1); for(i = 0; i < node->childrenCount; i++){ struct Node* child = makeNode(node, moves[i]); node->children[i] = child; } } //If there's a win, take it. If there's no children, it's a draw. if(node->childrenCount == -1){ update(node, INFINITY); return INFINITY; }else if(node->childrenCount == 0){ //TODO: do we need to make this game-theoretical? update(node, 0); return 0; } struct Node* bestChild = select(node); makeMove(board, &bestChild->move); float r; //result for node value this iteration, *always from this node's perspective* if(bestChild->value != INFINITY && bestChild->value != -INFINITY){ //If the nodes has no visits, simulate it if(bestChild->visits < MIN_SIMS){ r = -playOut(board); update(bestChild, -r); }else{ //Else go farther down the tree r = -solver(board, bestChild); } }else{ //Game-theoretical value r = -bestChild->value; } if(r == -INFINITY){ for(i = 0; i < node->childrenCount; i++){ if(-node->children[i]->value != -INFINITY){ r = -1; break; } } } update(node, r); return r; }
/* * Extension: Checks and adds to hash table using Zobrist hash function */ void generateUniqueBoardHash(BoardNode currentBoard, int rowMove, int colMove, int currRow, int currCol) { int hashKey; BoardNode generatedBoard; generatedBoard = makeMove(copyParentToChild(currentBoard,createBoard(currentBoard)),rowMove,colMove,currRow,currCol,DELETE); hashKey = generateHashKey(generatedBoard); if(hashBoard(hashKey,generatedBoard)) { addToQueue(generatedBoard); checkTarget(generatedBoard); } }
/** * Use gathered stroke. * NOTE: returns true even for bad move (not used) * @return true for used stroke */ bool Controls::useStroke() { bool result = false; if (m_strokeSymbol != ControlSym::SYM_NONE) { makeMove(m_strokeSymbol); m_strokeSymbol = ControlSym::SYM_NONE; result = true; } return result; }
void IterableChess::undoMove() { UndoableMove lastMove = *(movesStack.end()-1); lastMove.move.reverse(); makeMove(lastMove.move, false); if (lastMove.died.type != EMPTY) { m_state[lastMove.diedy][lastMove.diedx] = lastMove.died; } movesStack.pop_back(); }
void test_moves_left(){ int i, j; assert_true(validMovesLeft(g_board)==1); for(i=0; i < cols; ++i){ for(j=0; j< rows; ++j){ assert_true(validMovesLeft(g_board)==1); makeMove(g_board, i); } } assert_true(validMovesLeft(g_board)==0); }