void GobangGame::onMove(InPacket &in, int from) { if (!isStarted || from != nextTurn) return; uint8 row, col; in >> row >> col; // Check if the move is illegal if (row >= BOARD_SIZE || col >= BOARD_SIZE || board[row][col]) return; nextTurn ^= 1; board[row][col] = (blackIndex == from ? CELL_BLACK : CELL_WHITE); // Notify the other player... OutPacket *out = group->createPacket(CMD_MOVE); *out << row << col; group->sendPacket(out, nextTurn, from); if (checkForWin(row, col)) { isStarted = false; blackIndex ^= 1; group->restart(); } }
int tictactoe::AIBehaviour(void) //tries to make AI seem somewhat intelligent { unsigned counter; bool moveMade = false; //won't mark the tictactoe board twice if the move has already been made if (playerTurn == 0 && gameOver == false) //only execs if its AI's turn { //sleep(800); int tryMove; //1st priority - > finish the game (last move) AICheckMove(O, &moveMade); //2nd priority - > interrupt player's finishing move AICheckMove(X, &moveMade); //3rd priority -> trying to make a play if (moveMade == false) { tryMove = rand()%9; while (ttt[tryMove] != blank) {tryMove = rand()%9;} ttt[tryMove] = O; } --numEmptySquares; //one square filled checkForWin(); playerTurn = 1; //now player's turn } return -1; }
void tictactoe::updateGame(unsigned position) //designed for player { if ((position >= 0) && (position <= 8) && (ttt[position] == blank)) //checks for mistakes { ttt[position] = X; --numEmptySquares; playerTurn = 0; //now AI's turn checkForWin(); } }
void Level::update(float dt) { if (_gameOver) { return; } _player->updatePosition(dt); checkForWin(); checkForAndResolveCollisions(_player); handleCollectCoins(_player); setViewpointCenter(_player->getPosition()); }
//Checks if game is over and handles the result void Game::OnLoop() { bool emptyCells = haveEmptyCells(); //If playing against computer and it is AI turn and there are still empty grid cells if (!roundOver && gameType == 1 && currentPlayer == 1 && emptyCells) { aiMove(); } checkForWin(); if (!roundOver && emptyCells) { return; } //No empty cells left, check for win or draw onGameOver(); };
void GameLevelLayer::update(float delta) { if (m_isGameOver) { return; } if (m_pPlayer) { m_pPlayer->update(delta); handleHazardCollisions(m_pPlayer); checkForWin(); checkForAndResolveCollisions(m_pPlayer); setViewpointCenter(m_pPlayer->getPosition()); } }
void gameData::changeTurns() { if(currentTurn == blackToken) { currentTurn = whiteToken; } else if(currentTurn == whiteToken) { currentTurn = blackToken; } selectedToken = noToken; if(((gameStatus == placing) && (blackPieces.piecesUnplaced <= 0) && (whitePieces.piecesUnplaced <= 0))) { gameStatus = moving; } checkForWin(); }
//Handles cell select by player void Game::OnLButtonDown(int mX, int mY) { //If round is currently finished or its computer turn, then dont allow to recieve input if (roundOver || (gameType == 1 && currentPlayer == 1)) { return; } //Finds cell based on mouse coordinates int ID = mX / 200; ID = ID + ((mY / 200) * 3); if (gridRows[ID] != GRID_TYPE_NONE) { return; } setCell(ID, currentPlayer); checkForWin(); switchTurn(); }
AIMove AIPlayer::findBestMove(Grid& grid) { AIMove bestMove; TreeHelper helper; std::vector<Point> availablePositions; std::vector<Point> adjacent; grid.getAllAvailablePositions(availablePositions); //Run the minmax for all of them. for (unsigned int i = 0; i < availablePositions.size(); ++i) { Minimax minimax; Point p = availablePositions[i]; grid.getAdjacentPositions(p, adjacent); int win = checkForWin(grid, p.x, p.y); //Create root node for the minimax. Node root; //The number of children for this node is equal to the number of adjacent nodes. root.addChild(adjacent.size()); root.setValue(win); root.setParent(nullptr); //For each adjacent node. std::vector<Node*>& nodes = root.getChild(); for (unsigned int n = 0; n < nodes.size() ; ++n) { //Set value to -1, assuming the other player will play in that position. nodes[n]->setValue(-1); //first level we assume is bad because the player will block our game. //Check for 2nd level adjacency. //Not for each first level adjacent node, get the adjacent nodes to that and build the second level. //So we can try to predict if its going to be good or not. Point pa2nd = adjacent[n]; std::vector<Point> adjacent2nd; grid.getAdjacentPositions(pa2nd, adjacent2nd); //Add the number of children equal to the number of adjacent nodes. nodes[n]->addChild(adjacent2nd.size()); std::vector<Node*>& secondLevel = nodes[n]->getChild(); for (unsigned int l2 = 0; l2 < secondLevel.size() ; ++l2) { //int winWin = checkForWin(grid, adjacent2nd[l2].x, adjacent2nd[l2].y); //give it the weight of victory secondLevel[l2]->setValue(1); } //Run the minimax. minimax.minmax(&root, true); int value = root.getValue(); if (bestMove.value < value) { bestMove.value = value; bestMove.position = p; } } adjacent.clear(); } return bestMove; }
void main() // john { FILE *fp; char* hangman_word; int wrongAnswers = 0; int guesses = 0; int win = 0; char used_letters[255]; int i; int random; // seed random number srand(time()); random = (rand() % NUMWORDS) +1; // wow fopen/fscanf/fread doesnt work in a function? fp = fopen("hangman.txt","r"); // read in lines until we get the random one we want for(i=0;i<random;i++) { // read line fscanf(fp,"%s\n",hangman_word); } //hangman_word = getHangmanWord(fp,random); fclose(fp); // setup array with blanks for(i=0;i<26;i++) { used_letters[i] = ' '; } // play the game until we get too many incorrect guesses or we win while(wrongAnswers <= MAX_WRONG && win == 0) { // clear screen clrscr(); // draw game drawHangman(wrongAnswers); drawGame(used_letters,hangman_word,guesses); // get user input used_letters[guesses] = promptHangmanGuess(used_letters,guesses); // win check win = checkForWin(used_letters,hangman_word); // did the get a wrong answer? if(!checkHangmanGuess(hangman_word,used_letters[guesses])) { wrongAnswers++; } // increment count guesses++; } // print game ending clrscr(); drawHangman(wrongAnswers); drawGame(used_letters,hangman_word,guesses); if(win == 1) { printf("You win! "); } else { printf("You lose! "); } printf("The word was %s",hangman_word); }
void machine_play(){ int saveTemp;//to store the return value from the selection function int repeat; //machine trying to win the game if ((board[0]=='t')&&(board[1]=='t')&&(board[2]==' ')) selection(3,player); else if ((board[3]=='t') && (board[4]=='t')&& (board[5]==' ')) selection(6,player); else if ((board[6]=='t') && (board[7]=='t')&& (board[8]==' ')) selection(9,player); else if ((board[0]=='t') && (board[4]=='t')&&(board[8]==' ')) selection(9,player); else if ((board[0]=='t')&&(board[3]=='t')&&(board[6]==' ')) selection(7,player); else if ((board[0]==' ')&&(board[3]=='t')&&(board[6]=='t')) selection(1,player); else if ((board[1]=='t')&&(board[4]=='t')&&(board[7]==' ')) selection(8,player); else if ((board[2]=='t')&&(board[5]=='t')&&(board[8]==' ')) selection(9,player); else if ((board[0]==' ')&&(board[4]=='t')&&(board[8]=='t')) selection(1,player); else if ((board[0]=='t')&&(board[3]==' ')&&(board[6]=='t')) selection(4,player); else if ((board[6]=='t')&&(board[4]==' ')&&(board[2])=='t') selection(5,player); else if ((board[2]=='t')&&(board[5]==' ')&&(board[8]=='t')) selection(6,player); else if ((board[2]=='t')&&(board[4]=='t')&&(board[6]==' ')) selection(7,player); else if ((board[2]==' ')&&(board[5]=='t')&&(board[8]=='t')) selection(3,player); else if ((board[6]=='t')&&(board[7]==' ')&&(board[8]=='t')) selection(8,player); else if ((board[6]==' ')&&(board[7]=='t')&&(board[8]=='t')) selection(7,player); //end of trying to win by the machine //start of defending by the machine else if ((board[0]=='X') && (board[1]=='X')&&(board[2]==' ')) selection(3,player); else if ((board[3]=='X') && (board[4]=='X')&& (board[5]==' ')) selection(6,player); else if ((board[6]=='X') && (board[7]=='X')&& (board[8]==' ')) selection(9,player); else if ((board[0]=='X') && (board[4]=='X')&&(board[8]==' ')) selection(9,player); else if ((board[0]=='X')&&(board[3]=='X')&&(board[6]==' ')) selection(7,player); else if ((board[1]=='X')&&(board[4]=='X')&&(board[7]==' ')) selection(8,player); else if ((board[2]=='X')&&(board[5]=='X')&&(board[8]==' ')) selection(9,player); else if ((board[0]==' ')&&(board[4]=='X')&&(board[8]=='X')) selection(1,player); else if ((board[0]=='X')&&(board[3]==' ')&&(board[6]=='X')) selection(4,player); else if ((board[6]=='X')&&(board[4]==' ')&&(board[2])=='X') selection(5,player); else if ((board[2]=='X')&&(board[5]==' ')&&(board[8]=='X')) selection(6,player); else if ((board[2]=='X')&&(board[4]=='X')&&(board[6]==' ')) selection(7,player); else if ((board[1]==' ')&&(board[4]=='X')&&(board[7]=='X')) selection(2,player); //end of defending by the machine //random play by the machine particularly when the game just begins else{ checkForWin(); repeat: saveTemp=selection((rand()%9+1),player); if (saveTemp==false) goto repeat; } }
int main(){ int player1Seed=3,player2Seed=3,machineSeed=3; int who_begins,c; int play,player2;//lebels for goto statements int i,previous; int vlue_frm_function; int choice_of_player; int select; int depart,destination; srand(time(NULL)); printf("\t\tSMARTSOFT FROM SMARTLAB\n"); printf("do you want to play with a player or with the machine\n"); printf("enter 1 to play with another player or 2 to play with the machine\n"); scanf("%d",&choice_of_player); for(i=0;i<9;i++) board[i]=' '; switch(choice_of_player){ case 1: while(board[i]=' '){ if (player==1){ display_board(); checkForWin(); player1: if (player1Seed!=0){ printf("player X it's is your turn\n"); printf("Make your box selection\n"); scanf("%d",&select); vlue_frm_function=selection(select,player);\ if (vlue_frm_function==false) goto player1; // display_board(); player1Seed--; } else { printf("player X it's is your turn\n"); printf("from___ to __ seperated by space\n"); scanf("%d%d",&depart,&destination); vlue_frm_function=fromTo(depart,destination,player); if (vlue_frm_function==false) goto player1; //display_board(); } display_board(); player=2; } // else if(selection(select,player)==1) // player=2; else if (player=2){ display_board(); checkForWin(); player2: if (player2Seed!=0){ printf("player t it's your turn\n"); printf("make your box selection\n"); scanf("%d",&select); vlue_frm_function= selection(select,player); if (vlue_frm_function==false) goto player2; player2Seed--; } else{ printf("player t it's your turn\n"); printf("from___To___seperated by spaces\n"); scanf("%d%d",&depart,&destination); vlue_frm_function=fromTo(depart,destination,player); if(vlue_frm_function==false) goto player2; } display_board(); player=1; // } } } break; case 2: who_begins=rand()%2; if(who_begins==1) goto play; while(board[i]=' '){ display_board(); checkForWin(); previous: printf("player X it's your turn\n"); printf("make your box selection\n"); scanf("%d",&select); vlue_frm_function= selection(select,player); if (vlue_frm_function==false) goto previous; player1Seed--; display_board(); play: player='m'; //the machines turn to play the game machine_play(); machineSeed--; display_board(); checkForWin(); player=1; } break; } supportsite(); return 0; }