/*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; }
/* * Function: playOneGame * -------------------- * Starts the program by getting setting up the boggles object and * getting user input and initializing the board * * Preconditions: * * @param: Takes the dicitonary * @return: returns nothing */ void playOneGame(Lexicon& dictionary) { // Initialize GUI if(!BoggleGUI::isInitialized()){ BoggleGUI::initialize(4,4); } else { BoggleGUI::reset(); } BoggleGUI::setAnimationDelay(100); // Setup Board Boggle boggles = Boggle(dictionary,""); if (!getYesOrNo("Do you want to generate a random board? ")) { boggles = Boggle(dictionary,manualCase()); } // Setup the gui to show the words printGUI(boggles); userTurn(boggles); computerTurn(boggles); }
gboolean button_press_event_handler(GtkWidget *widget, GdkEventButton *event, gpointer data) { cairo_t *pointCr = gdk_cairo_create(widget->window), *lineCr = gdk_cairo_create(widget->window), *player1BoxCr = gdk_cairo_create(widget->window), *player2BoxCr = gdk_cairo_create(widget->window); cairo_set_source_rgba_from_string(pointCr, POINT_COLOR, 1); cairo_set_source_rgba_from_string(lineCr, LINE_COLOR, 1); cairo_set_source_rgba_from_string(player1BoxCr, PLAYER_1_BOX_COLOR, 1); cairo_set_source_rgba_from_string(player2BoxCr, PLAYER_2_BOX_COLOR, 1); cairo_set_line_width(lineCr, pointRadius * 1.4); gint x = (gint) event->x, y = (gint) event->y; if (isClickInsideFrame(x, y)) { timer = true; fadingLineAlpha = -1; gtk_widget_queue_draw(event_box); timer = false; fadingLineAlpha = 1; buttonPressCount %= 2; TurnResult result; if (!buttonPressCount) { result = playerTurn(lineCr, player1BoxCr, pointCr, head, x, y, lineLength, pointRadius, &BoxOfPlayer1, firstPlayerBox); if (result == lineDrawn) { gtk_statusbar_pop(GTK_STATUSBAR(data), status_context_id); if (opponent == HUMAN) buttonPressCount++; else { g_signal_handler_block(event_box, button_press_handler_id); computerPoints = computerTurn(head, lineCr, player2BoxCr, pointCr, lineLength, pointRadius, &BoxOfPlayer2, secondPlayerBox, &event_box, difficulty); g_signal_handler_unblock(event_box, button_press_handler_id); if (!computerPoints) { gtk_statusbar_push(GTK_STATUSBAR(data), status_context_id, "Game Over"); timer = false; result_message(player1Name, player2Name, BoxOfPlayer1, BoxOfPlayer2); } else { gtk_statusbar_push(GTK_STATUSBAR(data), status_context_id, player1Name); if ((*computerPoints)->x == (*(computerPoints + 1))->x) (*computerPoints)->verticalDown = (*(computerPoints + 1))->verticalUp = true; else (*computerPoints)->horizontalRight = (*(computerPoints + 1))->horizontalLeft = true; timer = true; g_timeout_add(fadeTimeInterval, (GSourceFunc) fade_out_handler, NULL); } } } if (result == frameIsFull) { gtk_statusbar_push(GTK_STATUSBAR(data), status_context_id, "Game Over"); result_message(player1Name, player2Name, BoxOfPlayer1, BoxOfPlayer2); } } else { result = playerTurn(lineCr, player2BoxCr, pointCr, head, x, y, lineLength, pointRadius, &BoxOfPlayer2, secondPlayerBox); if (result == lineDrawn) { buttonPressCount++; gtk_statusbar_push(GTK_STATUSBAR(data), status_context_id, player1Name); } if (result == frameIsFull) { gtk_statusbar_push(GTK_STATUSBAR(data), status_context_id, "Game Over"); result_message(player1Name, player2Name, BoxOfPlayer1, BoxOfPlayer2); } } } cairo_destroy(pointCr); cairo_destroy(lineCr); cairo_destroy(player1BoxCr); cairo_destroy(player2BoxCr); return TRUE; }
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; }