/*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 main(int argc, char* argv[]){ displayMode = CONSOLE; if(argc>1){ if (str_equals(argv[1], "gui")){ displayMode = GUI; } } int initializationError = initialize(); if (initializationError == 1){ // allocation error occured allocationFailed(); } else if (initializationError == 2){ // SDL failed to initialize exit(0); } display(); while (1){ if (isEndGame()){ if (displayMode == CONSOLE){ break; } else{ gameEnded = 1; } } if (turn != player1 && gameMode == SINGLE_PLAYER_MODE && !gameEnded){ if (computerTurn()){ allocationFailed(); } } else{ short error = humanTurn(turn); if (error != 0 && error != 2){ //error occured or "start" command entered return error; } } } if (displayMode == CONSOLE){ printEndGameResults(); } return 0; }
int main (int argc, char *argv[]) { int round, humanWins=0, computerWins=0; int humanToss; const int numberOfRounds = 7; int yes, ch, numberOfRolls; int humanSum, robotSum, j; /* Start game loop. */ for ( round = 1; round<=numberOfRounds; round++ ) { /* Resets */ humanSum = robotSum = numberOfRolls = ch = 0; yes = 1; printf("\nRound %d\n\n", round ); printf("Player's Turn: (hit enter)"); gets( input ); /* pause for dramatic effect */ while ( yes ) { humanToss = humanTurn(); humanSum += humanToss; numberOfRolls++; printf("\nPlayer total: %d\n", humanSum); printf("\nDo you wish to throw again? [Y or N] :"); scanf ("%s", &ch ); ch = toupper(ch); if ( ch != 89 ) { yes = 0; } } printf("\nRobot's Turn:"); sleep (1500); for (j = 0; j < numberOfRolls; j++) { printf ("\n"); robotSum += computerTurn(); sleep (1500); /* More pause drama. Adjust as personally necessary. */ } /* Determine Winner of the Round */ if ( humanSum > robotSum ) { humanWins++; printf("\nPlayer wins the round. human: %3d. robot: %3d\n", humanWins, computerWins ); } else if ( robotSum > humanSum ) { computerWins++; printf("\nRobot wins the round. human:%3d. robot: %3d\n", humanWins, computerWins ); } else if ( robotSum == humanSum) { computerWins++; printf("\nTie goes to the robot. human:%3d. robot: %3d\n", humanWins, computerWins ); } /* Round over. */ } /* Determine Winner to the Game */ if ( humanWins > computerWins ) { printf("\n\nWINNER!! You win the game!\n"); } else if ( computerWins < humanWins ) { printf("\n\nThe robot wins the game!\n"); } else { printf("\n\nTie Game!\n"); } system ("pause"); return 0; }