int executeCutCPU(SDL_Renderer *ren, Board *B){ if (determineWinner(B)) return 1; // Nothing to do int i, j; minmaxImplementation(B, &i, &j, minMaxDepth, 1); // Estimate position of desired I and J starting with Cut // The rest is the same as in Basic Execute Cut B->edges[i][j] = noArc; B->edges[j][i] = noArc; eraseArc(ren, B->Vertices[findIndex(B, i)].x, B->Vertices[findIndex(B, i)].y, B->Vertices[findIndex(B, j)].x, B->Vertices[findIndex(B, j)].y); if (B->Vertices[findIndex(B, i)].locked > 0){ endNode(ren, B->Vertices[findIndex(B, i)].x, B->Vertices[findIndex(B, i)].y); } else{ drawNode(ren, B->Vertices[findIndex(B, i)].x, B->Vertices[findIndex(B, i)].y); } if (B->Vertices[findIndex(B, j)].locked > 0){ endNode(ren, B->Vertices[findIndex(B, j)].x, B->Vertices[findIndex(B, j)].y); } else{ drawNode(ren, B->Vertices[findIndex(B, j)].x, B->Vertices[findIndex(B, j)].y); } //the next two lines rotate the turn display...you should not need to chagne this stringColor(ren, 5, 5, "Cut's Turn:", white); stringColor(ren, 5, 5, "Short's Turn:", black); return 1; }
int minmaxImplementation(Board * B, int * bestI, int * bestJ, int maxDepth, int isCutMove) { // The main core of both CPU players, short and cut // The idea is: we simulate the game for some depth and on every step we pick the best move for current player, // which is the worst for another player. Previous player should take the best move depending on current player moves // and so on. // If we have won or reached maximum depth, let's just estimate the length of shortest path for Short if (determineWinner(B) || maxDepth <= 0) return heuristicsFunction(B); // Estimate maximum or minimum heuristics value on current step int extremeHeuristics; if (isCutMove) extremeHeuristics = -1; else extremeHeuristics = 0x7FFFFFFF; int i, j, res_I, res_J;; for (i = 0; i < B->num; i++) for (j = i + 1; j < B->num; j++) if (B->edges[i][j] == stdArc || B->edges[j][i] == stdArc) { // Temporarily set this arc to locked / noArc if (isCutMove) { B->edges[i][j] = noArc; B->edges[j][i] = noArc; } else { B->edges[i][j] = lockedArc; B->edges[j][i] = lockedArc; } // Check it again from the position of another player. int curHeuristics = minmaxImplementation(B, NULL, NULL, maxDepth - 1, !isCutMove); // Revert the arc back B->edges[i][j] = stdArc; B->edges[j][i] = stdArc; // If our heuristics is better (more or less than current dependending on who are we), set it up if ((curHeuristics <= extremeHeuristics && !isCutMove) || (curHeuristics >= extremeHeuristics && isCutMove)) { extremeHeuristics = curHeuristics; res_I = i; res_J = j; } } // Return best step if needed (needed only for top recursion level) if (bestI != NULL && bestJ != NULL) { *bestI = res_I; *bestJ = res_J; } return extremeHeuristics; }
bool Battle::checkDeadPlayer() { if (!player1->isAlive()) { cout << "You died!" << endl; determineWinner(player2); return true; } else if (!player2->isAlive()) { cout << player2->getName() << "level " << player2->getLevel() << " died!" << endl; determineWinner(player1); return true; } return false; }
int main (void) { /* initialize suit array */ const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"}; /* initialize face array */ const char *face[NUM_FACES] = {"Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}; /* initalize deck array */ int deck[SUITS][NUM_FACES] = {0}; /* initialize winning card arrays */ int winningCards[13] = {0}, winningCardsComp[13] = {0}; // Initialize suit and face arrays to store current hand info int numSuits[SUITS] = {0}; int numFaces[NUM_FACES] = {0}; int numSuitsComp[SUITS] = {0}; int numFacesComp[NUM_FACES] = {0}; int keepCards[HAND_SIZE] = {0}; int winner = -1, wins = 0, losses = 0, choice = 0; // initialize 2 hands Card hand1[HAND_SIZE] = {{0, 0, 0}}; Card hand2[HAND_SIZE] = {{0, 0, 0}}; srand ((unsigned) time (NULL)); /* seed random-number generator */ while(choice != 3){ displayMenu(); choice = getMenuChoice(); if(choice == 1){ system("cls"); winner = -1; // Shuffle deck resetDeck(deck); shuffle (deck); resetKeepCards(keepCards); // Deal player hand deal (deck, hand1, 1, keepCards); // Deal computer hand deal(deck, hand2, 0, keepCards); resetCardArrays(numSuits, numFaces); resetCardArrays(numSuitsComp, numFacesComp); setCardArrays(numSuitsComp, numFacesComp, hand2); // Print player and computer hands printf("Your hand:\n"); printf("-------------------------\n"); printHand(hand1, face, suit); /* **For testing** printf("\nComputer's hand:\n"); printf("-------------------------\n"); printHand(hand2, face, suit);*/ printSecretHand(); // Get cards player would like to keep getKeepCards(keepCards); // Deal replacement cards to player deal(deck, hand1, 0, keepCards); // Determine which cards computer will keep resetKeepCards(keepCards); determineCompKeepCards(keepCards, numSuitsComp, numFacesComp, hand2); // Deal replacement cards to computer deal(deck, hand2, 0, keepCards); // Print new hands system("cls"); printf("Your hand:\n"); printf("-------------------------\n"); printHand(hand1, face, suit); printf("\nComputer's hand:\n"); printf("-------------------------\n"); printHand(hand2, face, suit); // Determine winner resetCardArrays(numSuits, numFaces); resetCardArrays(numSuitsComp, numFacesComp); setCardArrays(numSuitsComp, numFacesComp, hand2); setCardArrays(numSuits, numFaces, hand1); setDetermineWinnerArray(numSuits, numFaces, winningCards); setDetermineWinnerArray(numSuitsComp, numFacesComp, winningCardsComp); winner = determineWinner(winningCards, winningCardsComp); // Display winner if(winner == 1){ printf("\nYou win!\n"); wins++; } else if(winner == 0){ printf("\nComputer wins\n"); losses++; } system("pause"); } if(choice == 2){ displayWinsLosses(wins, losses); } } return 0; }
int main(int argc, char **argv) { SDL_Window *win = NULL; SDL_Renderer *ren = NULL; SDL_Event e; Board B; B.num = numNodes; int quit = GAME_UNDERWAY; int turn = rand() % 2; // TODO!!! // Add a simple menu here that shows up in the black console window to choose PvP or Computer vs. Player int isCutCPU, isShortCPU; //THE MENU NEED IMPROVEMENT. iT TESTS FOR INPUT BUT IT EXITS IF WRONG. //A LOOP IS NEEDED TO GET INPUTS UNTIL SOMETHING VALID // Main menu printf("Make a choice:\n"); printf("1) Player versus player\n"); printf("2) Player versus CPU (Player as Short)\n"); printf("3) Player versus CPU (Player as Cut)\n"); printf("4) CPU versus CPU\n"); printf("Enter your choice: "); //I found this one while looking for minimax algortihm. //I tought it was interesting so I gave it a try int choice; if (scanf_s("%i", &choice) != 1 || choice < 1 || choice > 4) { printf("Unable to read your input.\n"); return 1; } choice--; isCutCPU = choice & 0x1; isShortCPU = (choice & 0x02) >> 1; // Probably don't need to modify anything else below initializeGraphicsWindow(&win, &ren); SDL_SetRenderDrawColor(ren, 255, 255, 255, 255); // creates a white background for the board SDL_RenderClear(ren); //clears the game board to ensure it is plain white SDL_RenderPresent(ren); //renders the gameboard to the screen if (turn){ stringColor(ren, 5, 5, "Short's Turn:", white); } //displays initial turn else{ stringColor(ren, 5, 5, "Cut's Turn:", black); } createAndDrawBoard(ren,&B);//generates random planar graph and draws it //This is the main loop. //It calls executeTurn once per mouse click until the user quits or someone wins while (quit==GAME_UNDERWAY){ while (SDL_PollEvent(&e) != 0) //Loops through event queue { //User requests quit if (e.type == SDL_QUIT) //allows user to x out of program { quit = USER_QUIT; } if (e.type == SDL_MOUSEBUTTONDOWN) //handles mouse button events { turn = executeTurn(turn, ren, e.button, &B); quit = determineWinner(&B); //returns 0,2,or 3 as defined above inline with the declaration of quit } } SDL_RenderPresent(ren); //presents changes to the screen that result from the execution of one turn if (isCutCPU && turn == 0) { executeCutCPU(ren, &B); turn = 1; if (quit = determineWinner(&B)) break; SDL_RenderPresent(ren); SDL_Delay(1000); } else if (isShortCPU && turn == 1) { executeShortCPU(ren, &B); turn = 0; if (quit = determineWinner(&B)) break; SDL_RenderPresent(ren); SDL_Delay(1000); } } // Display who won displayWinBanner(ren, quit); while ((quit == CUT_WINS) || (quit == SHORT_WINS)) //if there was a winner hold the screen until the game window is closed. { SDL_RenderPresent(ren); //present changes to the screen // wait until the user closes the window while (SDL_PollEvent(&e) != 0) { //User requests quit if (e.type == SDL_QUIT) { quit = USER_QUIT; } } } freeGraphicsWindow(&win, &ren);//Terminate SDL and end program deleteBoard(&B); //deallocates dynamic memory return 0; }
/** * * @param argc * @param argv * @return */ int main(int argc, char** argv) { //Command line input int players; int cards; cards = atoi(argv[1]); players = atoi(argv[2]); cards = 5; //Force cards to 5 // players = 5; //Validate if (players < MINPLAYERS || players > MAXPLAYERS) { printf("Players too high or too low!!!\n"); return 0; } if (cards < MINCARDS || cards > MAXCARDS) { printf("Cards too high or too low!!!\n"); return 0; } //Create struct card deck[NUMCARDS]; //Create the deck createDeck(deck); //Fill the deck //Display printf("Unshuffled deck(suit, val):\n"); //printDeck(deck); printDeckColumned(deck); printf("\n"); //Shuffle shuffleDeck(deck); //Display printf("Shuffled deck(suit, val):\n"); //printDeck(deck); printDeckColumned(deck); printf("\n"); //Deal printf("Dealing.\n\n"); struct hand * hands = dealDeck(players, cards, deck); //Show Hands printf("Player's hands(unsorted):\n"); printHandsNamed(players, cards, hands); printf("\n"); //Sort Hands sortHands(players, cards, hands); //Show Hands printf("Player's hands(sorted):\n"); printHandsNamed(players, cards, hands); printf("\n"); // //Fixed cards for testing (suit, value) // struct card card1 = makeCard(4,3); // struct card card2 = makeCard(4,4); // struct card card3 = makeCard(4,5); // struct card card4 = makeCard(4,6); // struct card card5 = makeCard(4,7); // // //Fixed hand for testing // struct hand fixedHand; // fixedHand.cards[0] = card1; // fixedHand.cards[1] = card2; // fixedHand.cards[2] = card3; // fixedHand.cards[3] = card4; // fixedHand.cards[4] = card5; // // printHandsNamed(1,5,&fixedHand); // rankHand(&fixedHand); // printHandsNamed(1,5,&fixedHand); // printf("%i",fixedHand.rank); //Rank hands rankHands(players, hands); //Show hands with ranks printf("Players hands' ranks:\n"); printHandsRanked(players, hands); //Determine winner determineWinner(players, hands); return (0); }