/*This function alternates between user and the computer and handles moves for both.*/ void playGame(int n, char board[21][21], bool humanPlayer) { int x = 0, y = 0; bool turn = !humanPlayer; //false is computer while (true) { if (turn) { humanTurn(n, board, humanPlayer); turn = !turn; if (checkWinner(n, board) != 'N') break; if (isBoardFull(n, board)) break; } if (!turn) { computerTurn(n, board, humanPlayer); turn = !turn; if (checkWinner(n, board) != 'N') break; if (isBoardFull(n, board)) break; } } return; }
int solutionCount(sudokuGrid *game,int solutionsCuttoff) { //this function destroys the board in the process of counting the number of solutions// int position, trialValue, count; //if the board is full up, we are done if (isBoardFull(game)) { count = 1; } else { count = 0; position = getEmptyCell(game); trialValue = MIN_VALUE; while ((count < solutionsCuttoff) && (trialValue <= MAX_VALUE)) { if (isLegalMove(game,position,trialValue)) { commitMove(game,position,trialValue); if (solutionCount(game,1)) { //game with trial value in this position is solvable count++; } //reset to a blank value to try another solution commitMove(game,position,BLANK_VALUE); } trialValue++; } } return count; }
int hasSolution(sudokuGrid *game) { int position, trialValue, solved; //if the board is full, we are done if (isBoardFull(game)) { solved = TRUE; } else { solved = FALSE; position = getEmptyCell(game); trialValue = MIN_VALUE; while (!solved && (trialValue <= MAX_VALUE)) { if (isLegalMove(game,position,trialValue)) { commitMove(game,position,trialValue); if (hasSolution(game)) { //game with trial value in this position is solvable solved = TRUE; } else { //reset to a blank value to try another solution commitMove(game,position,BLANK_VALUE); } } trialValue++; } } return solved; }
//main function int main() { /* variable declaration */ char done=' '; printf("WELCOME TO TIC-TAC-TOE GAME\n"); printf("PLAYER1 plays against another PLAYER2\n"); /*calling function for intializing the board with empty cells*/ initboard(); do { //print the board printboard(); player1_move();//call player1 move function to place the cell for player1 done=check();//check whether the player1 wins or not,if it wins,exit from the loop if(done!=' ') break; printboard(); /* check whether the board is full or not continue to the move for player2 and check whether the player 2 wins after their move and check whether the board is full or not*/ done=isBoardFull(); if(done!=' ') break; player2_move(); done=check(); if(done!=' ') break; done=isBoardFull(); if(done!=' ') break; }while(done==' '); /*check for return statement from the loop,to declare the win*/ if(done=='X') {printf("Congrats, Player1 Wins\n"); printboard();} else if(done=='0') {printf("Congrats, Player2 Wins\n"); printboard();} else printf("Match Draw\n"); return 0; }
/* * Begin game of tic-tac-toe */ void playGame(int **board, int board_size) { int winner = 0; int current_player = PLAYER1; position_t input; // if no winner, and board is not full yet, proceed playing while (winner == 0 && !isBoardFull(board, board_size)) { int move = 0; // scan the player input, if it is not valid, prompt player and scan again while (!move) { printBoard(board, board_size); input = takePlayerInput(board, current_player, board_size); // input is valid, place input on the board, and escape the scan loop if (checkPlayerInput(board, current_player, input, board_size) == 1) { move = 1; board[input.row][input.col] = current_player; } } // check if input is winning move, escape play loop if it is, or else switch player. if (checkHorizontal(board, current_player, input.row, board_size) == 1 || checkVertical(board, current_player, input.col, board_size) == 1 || checkDiagonal(board, current_player, input.row, input.col, board_size) == 1) { printf("~~~~Player %d wins!~~~~\n", current_player); winner = 1; } else { // switch player if (current_player == PLAYER1) current_player = PLAYER2; else current_player = PLAYER1; } } /* print the final status of the board */ printBoard(board, board_size); }
int generateRandomBoard(sudokuGrid *game) { int position, trialValue, solved; int trialValues[NUM_VALUES]; int i,j; //if the board is full, we are done if (isBoardFull(game)) { solved = TRUE; } else { //populate and then shuffle array of trial values j = 0; for (i = MIN_VALUE; i <= MAX_VALUE; i++) { trialValues[j] = i; j++; } shuffleArr(trialValues,NUM_VALUES); solved = FALSE; position = getEmptyCell(game); j = 0; while (!solved && (j < NUM_VALUES)) { trialValue = trialValues[j]; if (isLegalMove(game,position,trialValue)) { commitMove(game,position,trialValue); if (generateRandomBoard(game)) { //game with trial value in this position is solvable solved = TRUE; } else { //reset to a blank value to try another solution commitMove(game,position,BLANK_VALUE); } } j++; } } return solved; }
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); } }
pair chooseMoveABDepth (int* iboard, int player, int alpha, int beta) { ASSERT(player == XPL || player == OPL); pair myBest = {0,-1}; pair reply; int i,j, move; int moveCount=0, moveList[9]; //default score if(player == US){ myBest.score = alpha; } else { myBest.score = beta; } timesCalled++; pair temp; //temp.score = evaluateBoard(iboard); temp.move = -1; // if(level == 0){ // temp.score = evaluateBoard(iboard); // return temp; // } if(didHeWin(iboard,US)) { //printf("%c WON\n",US); //printboard(iboard); //temp.score = evaluateBoard(iboard); temp.score = evaluatePosition(iboard,US); return temp; } else if(didHeWin(iboard,THEM)) { //printf("%c WON\n",THEM); //printboard(iboard); //temp.score = evaluateBoard(iboard); temp.score = evaluatePosition(iboard,US); return temp; } else if(isBoardFull(iboard)) { //printf("FULL\n"); //printboard(iboard); temp.score = evaluatePosition(iboard,US); return temp; } // get legal moves in25 for(i = 0; i < 9; i++) { if( iboard[convert9To25[i]] == EMPTY) { moveList[moveCount++] = convert9To25[i]; } } // for(i = 0; i < 9; i++) { // if( iboard[convert9To25[cellScoreOrdered[i]]] == EMPTY) { // moveList[moveCount++] = convert9To25[cellScoreOrdered[i]]; // } // } myBest.move = moveList[0]; for(i=0; i < moveCount; i++) { move = moveList[i]; iboard[move] = player; reply = chooseMoveAB(iboard, otherPlayer(player),alpha,beta); iboard[move] = EMPTY; // maximum lower bound of possible solutions if(player == US && myBest.score < reply.score){ alpha = reply.score; myBest.score = evaluatePosition(iboard,US); myBest.move = move; } // minimum upper bound of possible solutions else if (player == THEM && myBest.score > reply.score){ beta = reply.score; myBest.score = reply.score; myBest.move = move; } if(alpha >= beta){ return myBest; } } return myBest; }
pair chooseMoveEvaluate (int* iboard, int player) { ASSERT(player == XPL || player == OPL); pair myBest = {0,-1}; pair reply; int i, move; int moveCount=0, moveList[9]; //default score if(player == US){ myBest.score = INT_MIN; } else { myBest.score = INT_MAX; } timesCalled++; pair temp; temp.score = 0; temp.move = -1; if(didHeWin(iboard,US)) { //printf("%c WON\n",US); //printf("=====returning{1}====================\n"); //printboard(iboard); //temp.score = 10+timesCalled; temp.score = evaluateBoard(iboard); return temp; } else if(didHeWin(iboard,THEM)) { //printf("%c WON\n",THEM); //printf("=====returning{-1}====================\n"); //printboard(iboard); //temp.score = -10-timesCalled; temp.score = evaluateBoard(iboard); return temp; } else if(isBoardFull(iboard)) { //printf("FULL\n"); //printf("=====returning{0}====================\n"); //temp.score = 0; return temp; } // get legal moves for(i = 0; i < 9; i++) { if( iboard[convert9To25[cellScoreOrdered[i]]] == EMPTY) { moveList[moveCount++] = convert9To25[cellScoreOrdered[i]]; } } // for(i = 0; i < 9; i++) { // if( iboard[convert9To25[i]] == EMPTY) { // moveList[moveCount++] = convert9To25[i]; // } // } // any legal move //myBest.move = pickValuedMove(moveList,moveCount); //printf("Move by %c;*********************************************\n",player); //for(i=0; i < moveCount; i++) { //printf("%d ", moveList[i]); //} //printf("\n"); for(i=0; i < moveCount; i++) { move = moveList[i]; iboard[move] = player; //printf("%c(to move(%d/%d) %d);best:%d ;reply:%d \n", // player,i+1,moveCount,move,myBest.score,reply.score); //printboard(iboard); reply = chooseMove(iboard, otherPlayer(player)); iboard[move] = EMPTY; //printf("RESET %c(to move(%d/%d) %d);best:%d ;reply:%d \n", // player,i+1,moveCount,move,myBest.score,reply.score); if( // maximizinf by selecting opponents less score (player == US && myBest.score < reply.score) || // minimizing by letting them select high score (player == THEM && myBest.score > reply.score)){ // printf("%c SCORE CHANGED (score:%d, best move:%d)!!\n", // player,reply.score,move); myBest.score = reply.score; myBest.move = move; } } return myBest; }