/* Move choosing algorithm for the AI. * * Parameters: * piece: The game piece for the AI. * * Returns an integer in range 0-8 representing a place in a vector. */ int Game::botMove(char piece) { int ret = 0; int favoured[9] = {4, 0, 2, 6, 8, 1, 3, 5, 7}; std::vector<char> tempBoard = board; char opponent = (piece == cross) ? naught : cross; for (int i = 0; i < 9; i++) { if (isLegal(i)) { tempBoard[i] = piece; if (checkWin(tempBoard) == ((piece == naught) ? naughtWin : crossWin)) return i; tempBoard[i] = clear; } } for (int i = 0; i < 9; i++) { if (isLegal(i)) { tempBoard[i] = opponent; if (checkWin(tempBoard) == ((opponent == naught) ? naughtWin : crossWin)) return i; tempBoard[i] = clear; } } for (int i = 0; i < 9; i++) { if (isLegal(favoured[i])) return favoured[i]; } return ret; }
int AI::verifWin() { if (!checkWin(TEAM_1)) return TEAM_1; if (!checkWin(TEAM_2)) return TEAM_2; return 0; }
/* move: move top element from tower (src) to tower (dest) * returns TRUE on a successful move */ int move(int src, int dest) { int disc, pDisc; disc = pop(src); pDisc = peek(dest); /* check here if it's okay to move it!! */ if (pDisc == 0 || disc < pDisc) { if (src != dest) fprintf(stderr, "Turn %d: Move disc %d from tower%d to tower%d.\n", ++turns, disc, src+1, dest+1); push(dest, disc); if (checkWin()) printf("You won!\n"); return TRUE; } else { fprintf(stderr, "Invalid move! (D%d T%d>T%d D%d)\n", disc, src+1, dest+1, pDisc); push(src, disc); return FALSE; } }
int playTurn(board_t * b, char ** linep, size_t *lineszp) { printf("Current board:\n"); printBoard(b); printf("x coordinate:\n"); int x = readInt(linep, lineszp); printf("y coordinate:\n"); int y = readInt(linep, lineszp); int result = click(b,x,y); determineKnownMines(b); if (result == CLICK_LOSE) { printf("Oh no! That square had a mine. You lose!\n"); revealMines(b); printBoard(b); return 1; } else if (result == CLICK_INVALID) { printf("That is not a valid square, please try again\n"); } else if (result == CLICK_KNOWN_MINE) { printf("You already know there is a mine there!\n"); } else if(checkWin(b)) { printBoard(b); printf("You win!\n"); return 1; } return 0; }
bool RulesChecker::_checkIfCaptureBreaksAlignement(std::pair<PAIR_INT, PAIR_INT> *captures, PAIR_INT last, GRID_REF grid, eTurn & turn, PLAYER_PAWNS_REF container){ // Get the captured pairs std::pair<int, int> a = (*captures).first; std::pair<int, int> b = (*captures).second; // Get captured's players's container // std::vector<std::pair<int, int>> & container = (turn == eTurn::TURN_PLAYER_1 ? _pawnsPlayer1 : _pawnsPlayer2); // remove the pawn for the check _removePawn(container, a, grid); _removePawn(container, b, grid); // check if it's a win, we want to return the opposite of the return value of checkWinm bool res = !checkWin(last.first, last.second, grid, turn); // rollback container.push_back(a); container.push_back(b); eBlock blockCurrentPlayer = (turn == eTurn::TURN_PLAYER_1 ? eBlock::PLAYER_1 : eBlock::PLAYER_2); grid[a.second][a.first] = blockCurrentPlayer; grid[b.second][b.first] = blockCurrentPlayer; return res; }
int main (){ printf("Start game\n"); initBoard(); /*ボードの初期化*/ showBoard(); /*ボードの表示*/ initStone(); /*石の初期化*/ initPlayer(); /*プレーヤーの初期化*/ cont = 1; /*継続判定の初期化*/ judge = 0; /*勝ち判定初期化*/ printf("\n"); while (cont == 1){ input(); /*マス目の選択受付*/ showBoard(); checkWin(); /*勝ち判定*/ if (judge == 1){ break; /*勝ち判定ありの場合、ループ脱出*/ } else { check(); /*ゲーム継続可能性(空きマスがあるか)の判定*/ changeStone(); changePlayer(); printf("\n"); } } printf("\n"); dispWinner(); /*judgeの値により勝者表示OR引分宣言*/ return 0; }
int main(int argc,char **argv) { setbuf(stdin,0); setbuf(stdout,0); setbuf(stderr,0); char me=0; // Am I X or O... don't know yet char them=0; // Is my opponent X or O if (argc<2) { fprintf(stderr,"Please specify whether %s should play X or O.\n", argv[0]); return 1; /* Issue a non-zero return code to indicate an error */ } if (argv[1][0]=='X') { me=1; them=2; } else if (argv[1][0]=='O') { me=2; them=1; } else { fprintf(stderr,"Please enter either X or O as the first argument to %s\n", argv[0]); return 1; } short int state=0; // Current state... starts off empty int turn=1; // Whose turn is it? X or O? while(state<STATE_TIE) { if (DBG) { fprintf(stderr,"It's %c's turn...\n",(turn==1)?'X':'O'); } if (me==turn) state = myTurn(state,me); else state=theirTurn(state,them); printf("%d\n",state); // Write state after each move to standard out state=checkWin(state); /* short int tempState = checkWin(state); */ /* if(tempState >= STATE_TIE){ */ /* fprintState(state); */ /* state=tempState; */ /* } */ if (turn==1) turn=2; // X just went... O next else turn=1; // O just went... X next } printf("%d\n",state); // Write final state to standard out if (state==STATE_TIE) fprintf(stderr,"Good game, but you didn't beat me.... it's a tie.\n"); else if (state==(STATE_TIE+me)) fprintf(stderr,"Got you this time!... I won.\n"); else fprintf(stderr,"You got me this time... you won.\n"); return 0; }
void Cell::open() { emit generate(m_x, m_y); if (isOpen()) { return; } else if (!isSuspect() & !isMined()) { if(minesAround() != 0) { m_open = true; } else { m_open = true; for (Cell *cell : m_neighbors) { if (!cell->isMined() & !cell->isSuspect() &cell->minesAround() == 0) { cell->open(); } else if (!cell->isMined() & !cell->isSuspect()) { cell->m_open = true; } } } emit checkWin(); } if(this->haveMine() & !isSuspect() & !isMined()) { emit lose(); } }
void MainWindow::play(int row1, int col1 , int row2 , int col2) { if(isNear(row1,col1,row2,col2)) { if(gobj.arr[row1][col1] != gobj.arr[row2][col2]) { gobj.swap(gobj.arr[row1][col1],gobj.arr[row2][col2]); if(gobj.arr[row1][col1]<7 && gobj.arr[row2][col2]<7) { if(!(gobj.checkLine(row1,col1)||gobj.checkLine(row2,col2))) gobj.swap(gobj.arr[row1][col1],gobj.arr[row2][col2]); } else if (gobj.arr[row1][col1] > 6 && gobj.arr[row2][col2]<7) { gobj.checkLine(row2,col2); gobj.typeBomb(gobj.arr[row1][col1],row1,col1,gobj.arr[row2][col2]); } else if (gobj.arr[row2][col2] > 6 && gobj.arr[row1][col1]<7) { gobj.checkLine(row1,col1); gobj.typeBomb(gobj.arr[row2][col2],row2,col2,gobj.arr[row1][col1]); } gobj.moveDown(); resetImage(); step++; ui->lcdNumber->display(gobj.score); ui->lcdNumber_2->display(step); checkWin(); } } }
bool IAclass::checkEndGame(std::map<poseCase, typeCase>& mapLevel) { if(checkWin(mapLevel)) { return true; } bool mapLevelIsFull = true; for(int i = 0; i < 7; ++i) { for(int j = 0; j < 6; ++j) { if(mapLevel[poseCase(i, j)] == VIDE) { mapLevelIsFull = false; break; } } if(mapLevelIsFull == false) { break; } } if(mapLevelIsFull) { return true; } return false; }
int LongLongNim::numberOfWins(int maxNum, int* move, int moveLen) { // check assert(moveLen<22 && moveLen>0); assert(move); assert(move[moveLen-1]<=22); // initilie cache mCache = new LoopArray(22); mMove = move; mMoveLen = moveLen; mMaxNum = maxNum; int win = 0; // for all element smaller than move for(int i=0; i<maxNum; i++) { LongLongNim::PLAYER winner = checkWin(i, A); printf("%d --> %c, ", i, (winner == A) ? 'A' : 'B'); if(winner == LongLongNim::B) win++; } printf("win=%d \n", win); // release folder delete mCache; mCache = NULL; mMove = NULL; mMoveLen = 0; mTotalBWinNum = 0; mMaxNum = 0; return win; }
//================================ //MOVE //================================ void Match::moveUp(Player *p) { // cout << " Moves Forward" << endl; if(p->getStatus() == p->BALL_WITH_OPP) { dodge(p); } else { int team = p->getTeam(); if(team == 1) { p->setPosX(p->getPosX() + 1); } else if(team == 2) { p->setPosX(p->getPosX() - 1); } checkBall(p); if(p->getNumber() == ball->getPlayer()) { updateBall(p); screen->updateMove(p, p->MOVE_UP); } } checkWin(p); }
std::pair<bool, std::pair<int, int> > checkThree(const std::vector<std::pair<int, int> > &free, const int &firstMove) { for (int i = 0; i < (int)free.size(); i++) { map[free[i].first][free[i].second] = firstMove; bool canwin = false; for (int j = 0; j < (int)free.size(); j++) { if (i == j) continue; if (checkWin(free[j].first, free[j].second, firstMove ^ 1)) { canwin = true; break; } } if (canwin) { map[free[i].first][free[i].second] = -1; continue; } bool notwin = false; for (int j = 0; j < (int)free.size(); j++) { if (i == j) continue; map[free[j].first][free[j].second] = firstMove ^ 1; bool flag = false; for (int k = 0; k < (int)free.size(); k++) { if (i == k || j == k) continue; if (checkWin(free[k].first, free[k].second, firstMove)) { flag = true; break; } } map[free[j].first][free[j].second] = -1; if (!flag) { notwin = true; break; } } map[free[i].first][free[i].second] = -1; if (!notwin) { return std::make_pair(true, free[i]); } } return std::make_pair(false, std::make_pair(-1, -1)); }
Sudoku::Sudoku() { size_ = 9; // 9 columns and 9 rows sumOfRowsCols_ = 45; // 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45 grid_ = vector<vector<int>> (size_, vector<int> (size_, 0)); // The grid is a vector of vectors validMoves_ = vector<vector<vector<bool>>> (size_, vector<vector<bool>> (size_, vector<bool> (size_, 1))); // Set all the moves to be valid (1) hasWon_ = checkWin(); // check if the board is a winner, false }
int evalBoard(int **board) { int sumRow; int sumCol; int sumHoriz1 = 0; int usedPositions = 0; int result; for(int row=0; row<NUM_ROWS; row++) { sumRow = 0; sumCol = 0; for (int col=0; col<NUM_COLS; col++) { sumRow += (int)board[row][col]; sumCol += (int)board[col][row]; if (row == col) { sumHoriz1 += (int)board[row][col]; } if (board[row][col] != 0) { usedPositions += 1; } result = checkWin(sumRow); if (result) { return result; } result = checkWin(sumCol); if (result) { return result; } } result = checkWin(sumHoriz1); if (result) { return result; } } int sumHoriz2 = (int)board[0][NUM_COLS-1] + (int)board[NUM_ROWS/2][NUM_COLS/2] + (int)board[NUM_ROWS-1][0]; switch(sumHoriz2) { case PLAYER_ONE_WIN: return PLAYER_ONE_VAL; case PLAYER_TWO_WIN: return PLAYER_TWO_VAL; } if (usedPositions == (NUM_ROWS * NUM_COLS)) { return DRAW; } return 0; }
std::pair<bool, std::pair<int, int> > checkOne(const std::vector<std::pair<int, int> > &free, const int &firstMove) { for (int i = 0; i < (int)free.size(); i++) { std::pair<int, int> pos = free[i]; if (checkWin(pos.first, pos.second, firstMove)) { return std::make_pair(true, pos); } } return std::make_pair(false, std::make_pair(-1, -1)); }
void CircuitSquare::Draw(HDC hdc, RECT rt){ if (checkWin(rt) == 1) throw 0; if (checkSqr() == 1) throw 1; if (checkBord() == 1) throw 3; HPEN hPen = CreatePen(PS_SOLID, border, RGB(BorderColor.R, BorderColor.G, BorderColor.B)); HPEN hOldPen = SelectPen(hdc, hPen); HBRUSH hBrush = CreateSolidBrush(RGB(255, 255, 255)); HBRUSH hOldBrush = SelectBrush(hdc, hBrush); Rectangle(hdc, LeftTop.x, LeftTop.y, RightBottom.x, RightBottom.y); }
int Board::getHeuristic() { if (checkWin()) { return checkWin(); } else if (isBoardFull() && !checkWin()) { return 0; } else { for (map<string,int>::iterator i=verificationStringsCountMap.begin();i!=verificationStringsCountMap.end();i++) i->second=0; rowWiseHeuristic(); colWiseHeuristic(); topDiagonalWiseHeuristic(); botDiagonalWiseHeuristic(); //printBoard(); if (verificationStringsCountMap[BLANKTHREEA]!=0) verificationStringsCountMap[BLANK2TWOA]-=verificationStringsCountMap[BLANKTHREEA]; if (verificationStringsCountMap[THREEABLANK]!=0) verificationStringsCountMap[TWOABLANK]-=verificationStringsCountMap[THREEABLANK]; if (verificationStringsCountMap[BLANKTHREEB]!=0) verificationStringsCountMap[BLANK2TWOB]-=verificationStringsCountMap[BLANKTHREEB]; if (verificationStringsCountMap[THREEBBLANK]!=0) verificationStringsCountMap[TWOBBLANK]-=verificationStringsCountMap[THREEBBLANK]; //for (map<string,int>::iterator i=verificationStringsCountMap.begin();i!=verificationStringsCountMap.end();i++) // cout<<i->first <<"=" <<i->second<<endl; return ((verificationStringsCountMap[BLANK2TWOA]+verificationStringsCountMap[TWOABLANK]-verificationStringsCountMap[BLANK2TWOB]-verificationStringsCountMap[TWOBBLANK]) +(verificationStringsCountMap[BLANKTHREEA]+verificationStringsCountMap[THREEABLANK]-verificationStringsCountMap[BLANKTHREEB]-verificationStringsCountMap[THREEBBLANK])*5); } }
LongLongNim::PLAYER LongLongNim::checkWin(int curNum, LongLongNim::PLAYER first) { if(curNum < mMove[0]) return swap_player(first); // curNum is equal to one of the number in move if(curNum <= mMove[mMoveLen-1]) { int i=0; for(; i<mMoveLen && mMove[i]!=curNum; i++); if(i<mMoveLen) { return first; } } // curNum (mMove[0], mMove[n-1]), but not equal to move[i], or // curNum is bigger than mMove[n-1] LongLongNim::PLAYER second = swap_player(first); LongLongNim::PLAYER winner = second; for(int i=0; i<mMoveLen && curNum<mMove[i]; i++) { // check mCache int num = curNum - mMove[i]; winner = checkCache(num); if(first == winner) { break; } assert(winner == second); // if the first can win only one of path, it will win finally. winner = checkWin(num, second); if(first == winner) { break; } } // add the result into cache RESULT res; res.num = curNum; res.winner = winner; mCache->add(res); return winner; }
bool Board::move(int pos, int& idx) { if (board[pos][5] != NONE) return false; for (idx = 0; idx < 6; idx++) if (board[pos][idx] == NONE) break; board[pos][idx] = current_color; checkWin(pos, idx, current_color); current_color = !current_color; return true; }
void hangedGame(char* word) { int i; int* found; int size = strlen(word); found = malloc(size * sizeof(int)); for ( i = 0; i < size; i++) { found[i] = 0; } char character = 0; int loop = 10; while (loop && !checkWin(word, found)) { printf("Il vous reste %d coups a jouer \n", loop); printf("Quel est le mot secret ? "); showWord(word, found); printf("\nProposez une lettre : "); character = lireCaractere(); checkCharacter(word, found, character); loop--; } if(checkWin(word, found)) { printf("\nBravo ! Vous avez gagné en %d coups !", loop); printf("\nLe mot secret était bien '%s'", word); } else { printf("Perdu ! Le mot secret était : %s", word); } free(found); }
void CombinedSquare::Draw(HDC hdc, RECT rt){ if (checkWin(rt) == 1) throw 0; if (checkSqr() == 1) throw 1; if (checkBord() == 1) throw 3; // if (checkIns(rt) == 1) throw 4; HPEN hPen = CreatePen(PS_SOLID, border, RGB(BorderColor.R, BorderColor.G, BorderColor.B)); HPEN hOldPen = SelectPen(hdc, hPen); HBRUSH hBrush = CreateSolidBrush(RGB(MainColor.R, MainColor.G, MainColor.B)); HBRUSH hOldBrush = SelectBrush(hdc, hBrush); Rectangle(hdc, LeftTop.x, LeftTop.y, RightBottom.x, RightBottom.y); fc.Load(); fc.Draw(hdc, rt); }
void BlackJack::newRound() { deck.shuffle(); takeBet(); dealCards(); showCards(); while(control) { menu(); } checkWin(); newDeal(); }
bool GameLogic::click(double x, double y) { if (mActive && Square::contains(x, y)) { for (auto itr : mBoards) { if(itr->click(x, y, mCurrentPlayer)) { int index = itr->getLatestTile(); setPlayableBoard(index); mCurrentPlayer = ++mCurrentPlayer % 2; checkWin(); return true; } } } return false; }
int main(int argc, char** argv) { startNewGame(); while (1) { int move; printf("Move? "); scanf("%d", &move); playerMove(move); printGameState(globalState); checkWin(globalState); computerMove(LOOK_AHEAD); printGameState(globalState); checkWin(globalState); } freeGameState(globalState); return 0; }
void multiPlayer() { do { system("CLS"); gameName(); draw(); setMove(getMove()); checkWin(); checkBoard(); }while(gameFinished!=true); printBoard(); system("PAUSE"); resetBoard(); }
void TicTacToe::on_gameField_buttonClicked(QAbstractButton *button) { switch(_currPlayer) { case 1: button->setIcon(QPixmap::fromImage(*_player1)); button->setWhatsThis("1"); break; case 2: button->setIcon(QPixmap::fromImage(*_player2)); button->setWhatsThis("2"); break; } button->setEnabled(false); bool won = checkWin(); if(won || full()) { if(won) { QListIterator<QAbstractButton *> i(gameField->buttons()); while (i.hasNext()) { QAbstractButton *qab = i.next(); qab->setEnabled(false); } switch(_currPlayer) { case 1: scorePlayer1->setText(QString::number(++_score1)); printInfo("Player 1 wins!"); break; case 2: scorePlayer2->setText(QString::number(++_score2)); printInfo("Player 2 wins!"); break; } } turnPlayer1->setText(""); turnPlayer2->setText(""); buttonNewGame->setEnabled(true); } else { switchPlayer(); } }
bool checkTwo(const std::vector<std::pair<int, int> > &free, const int &firstMove) { for (int i = 0; i < (int)free.size(); i++) { map[free[i].first][free[i].second] = firstMove; bool flag = false; for (int j = 0; j < (int)free.size(); j++) { if (i == j) continue; if (checkWin(free[j].first, free[j].second, firstMove ^ 1)) { flag = true; break; } } map[free[i].first][free[i].second] = -1; if (!flag) { return false; } } return true; }
Point Bot::getIndex() const { std::vector<Point> good_cells; if (checkWin(*desk_, bot_number_)) { throw Exception("Bot won and shouldn't make any " "moves."); } for (int i = 0; i < desk_->getLength(); i++) { for (int x = 0; x < desk_->getWidth(); x++) { Point pt(i, x); if (checkCoordinate(pt)) { good_cells.push_back(pt); } } } if (good_cells.size() != 0) { return theBestCell(good_cells); } return randomRationalCell(); }
/**@brief Update the game, given a location to place the disc */ void addToColumn(uint8_t col) { uint8_t currColor = p1_turn ? RED : BLUE, row; if(discs[col] < HEIGHT) { //insert the disc row = discs[col]; discs[col]++; game[row][col] = currColor; char temp_[24]; sprintf(temp_, "\nDisc added at %d, %d\n", col, row); SEGGER_RTT_WriteString(0, temp_); //Send data to Bluetooth connection char temp[14]; uint8_t send[14]; sprintf(temp, "Received %d %d", col, row); uint8_t i = 0; while(temp[i] != '\0') { //Accounts for 1 or 2 digit numbers send[i] = temp[i]; i++; } uint32_t err_code = ble_nus_string_send(&m_nus, send, i); if(err_code != NRF_SUCCESS) APP_ERROR_CHECK(err_code); //check if game is over if(checkWin(row, col)) { switch(p1_turn) { case true: SEGGER_RTT_WriteString(0, "Player 1 Wins!\n"); break; default: SEGGER_RTT_WriteString(0, "Player 2 Wins!\n"); break; } } p1_turn = !p1_turn; } else SEGGER_RTT_WriteString(0, "\nNo more moves may be made on this column\n"); }