/* Main function */ int main() { int iNextPlayer = 0; //Variable used to switch between players. int result = 0; //Variable used for finding winners. int positionX, positionY; //Variable for the board. int nextPlayerOK = 0; //Variable used for checking square selection. int freeSquare = 0; printBoard2D(); //Prints the 2D board. while(result != 1){ nextPlayerOK = 0; iNextPlayer %= 2; //Next Player selection via modulus, 0 = X, 1 = O. if(iNextPlayer == 0){ //Sets the player. printf("\nPlayer X turn! Choose Your position."); cNextPlayer = 'X'; } else { printf("\nPlayer O turn! Choose Your position."); cNextPlayer = 'O'; } printf("\nColumn (X-position): "); scanf("%d", &positionX); //Player sets the x-position for its mark. printf("\nRow (Y-position): "); scanf("%d", &positionY); //Player sets the y.position for its mark. if(board2D[positionX-1][positionY-1] != 'X' && board2D [positionX-1][positionY-1] != 'O'){ //Check for open square. board2D[positionX-1][positionY-1]=cNextPlayer; //Sets mark. result = checkForWinner(); //Checks for winner. } else{ //Player selected an taken square. printf("\nPlace taken!"); nextPlayerOK = 1; //Sets an flag for wrong selection. } freeSquare = checkForDraw(); //Search for free square. if(result == 1){ //The game has a winner. printBoard2D(); printf("\nWinner is %c!\n", cNextPlayer); break; } else if((result == 0) && (freeSquare == 9)){ //The game is a draw. printBoard2D(); printf("\nDraw!\n"); break; } printBoard2D(); if(nextPlayerOK == 0) iNextPlayer++; //Sets the next player. } return 0; //Return 0 if no winner is found. }
void TicTacToe::boardUpdate(CCObject* obj) { CCLog("Board updated message rcvd, state: %d", ((Mark*)obj)->getState()); for (int16_t y = 0; y < 3; y++) { CCLog("[(0,%d):%d (1,%d):%d (2,%d):%d]", y, markList[0][y]->getState(), y, markList[1][y]->getState(), y, markList[2][y]->getState()); } checkForWinner(obj); }
void Engine::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f, 2.0f, -15.0f); glRotatef(xAngle, 1.0f, 0.0f, 0.0f); glRotatef(yAngle, 0.0f, 1.0f, 0.0f); glScalef(scale, scale, scale); drawSea(SEA_SIZE); glColor3f(1,0,0); this->renderText(-23, 2, -15.0f, QString("Player 1")); this->renderText(-24, 1.1f, -15.0f, getHitpoints(&player1)); glColor3f(0,0,1); this->renderText(19, 2, -15.0f, QString("Player 2")); this->renderText(20, 1.1f, -15.0f, getHitpoints(&player2)); glColor3f(1,1,0); this->renderText(-0.5d, 1.0d, 1.0d, pauseText); glColor3f(0,1,0); this->renderText(-2.5d, 1.0d, -1.0d, checkForWinner()); this->renderText(-4.5d, 1.0d, 1.0d, newGameText); glPushMatrix(); glTranslatef(player1.xPos, 0, player1.zPos); glRotatef(player1.yAngle, 0, 1, 0); player1.draw(1, 0, 0); glPopMatrix(); glPushMatrix(); glTranslatef(player2.xPos, 0, player2.zPos); glRotatef(player2.yAngle, 0, 1, 0); player2.draw(0, 0, 1); glPopMatrix(); Missile* m; foreach(m, missilesList) { glPushMatrix(); glTranslatef(m->x, 0.4, m->z); glRotatef(m->angle, 0, 1, 0); m->draw(); glPopMatrix(); }
int main(int argc, char *argv[]) { // miscellaneous /*int disciplines[NUM_REGIONS]; int dice[NUM_REGIONS];*/ Game g; // store the winner of each game int winner; // store game states within the game int keepPlaying; int turnFinished; int diceRollAmount; // random char *actions[] = ACTION_NAMES; int diceRoll; int passedTurns = 0; // seed rand! srand(time(NULL)); // while the game is wanting to be played, create new game, etc. keepPlaying = TRUE; while (keepPlaying == TRUE) { // create the game //randomDisciplines(disciplines); //randomDice(dice); // you can change this to randomiseDisciplines() and randomiseDice() later int disciplines[NUM_REGIONS] = {CYAN,PURP,YELL,PURP,YELL,RED ,GREE,GREE, RED ,GREE,CYAN,YELL,CYAN,BLUE,YELL,PURP,GREE,CYAN,RED }; int dice[NUM_REGIONS] = {9,10,8,12,6,5,3,7,3,11,4,6,4,9,9,2,8,10,5}; // rig board like the real game rigBoard(disciplines, dice); g = newGame(disciplines, dice); printf("Game created! Now playing...\n"); // start the game with noone as the winner winner = NO_ONE; while (winner == NO_ONE) { printLineBreak(); // start new turn by setting turnFinished to false then // rolling the dice diceRollAmount = 0; diceRoll = 0; while (diceRollAmount < DICE_AMOUNT) { diceRoll += rollDice(); diceRollAmount++; } throwDice(g, diceRoll); // new turn means new line break! printf("[Turn %d] The turn now belongs to University %c!\n", getTurnNumber(g), getWhoseTurn(g) + UNI_CHAR_NAME); // printf("The dice has casted a %d!\n", diceRoll); printf("\n"); // keep going through the player's turn until // he/she decided to pass and finish the turn turnFinished = FALSE; while (turnFinished == FALSE && passedTurns < MAX_PASS) { // processes requests and all subrequests for a move and // checks if they are legal. only gives a move within the // scope of the defined actionCodes that is legal int turnPerson = getWhoseTurn(g); /* printf("Stats for %c:\n", turnPerson + UNI_CHAR_NAME); printf("KPIs: %d\n", getKPIpoints(g, turnPerson)); printf("ARCs: %d\n", getARCs(g, turnPerson)); printf("Campuses: %d\n", getCampuses(g, turnPerson)); printf("GO8s: %d\n", getGO8s(g, turnPerson)); printf("Publications: %d\n", getPublications(g, turnPerson)); printf("Patents: %d\n", getIPs(g, turnPerson)); int discipleIndex = 0; char *discipleNames[] = DISCIPLE_NAMES; while (discipleIndex < NUM_DISCIPLINES) { printf("Type %s: %d\n", discipleNames[discipleIndex], getStudents(g, turnPerson, discipleIndex)); discipleIndex++; } */ printf("\n"); action a = decideAction(g); // if not passing, make the move; otherwise end the turn if (a.actionCode == PASS) { turnFinished = TRUE; printf("You have passed onto the next person.\n"); } else { // write what the player did, for a logs sake. /* printf("The action '%s' has being completed.\n", actions[a.actionCode]); if (a.actionCode == BUILD_CAMPUS || a.actionCode == OBTAIN_ARC || a.actionCode == BUILD_GO8) { printf(" -> Destination: %s\n", a.destination); } else if (a.actionCode == RETRAIN_STUDENTS) { printf(" -> DisciplineTo: %d\n", a.disciplineTo); printf(" -> DisciplineFrom: %d\n", a.disciplineFrom); } */ assert(isLegalAction(g, a)); // break this and the code dies. trololol! if (a.actionCode == START_SPINOFF) { if (rand() % 3 <= 1) { a.actionCode = OBTAIN_PUBLICATION; } else { a.actionCode = OBTAIN_IP_PATENT; } } makeAction(g, a); if (a.actionCode == PASS) { passedTurns++; } else { passedTurns = 0; } if (passedTurns >= MAX_PASS || getKPIpoints(g, turnPerson) >= WINNING_KPI) { turnFinished = TRUE; } } // if there is not a winner or pass, add a seperating line // to seperate actions being clumped together if (turnFinished == FALSE) { printf("\n"); } } // check if there is a winner winner = checkForWinner(g); } if (passedTurns >= MAX_PASS) { printf("AI passes too much.\n"); return EXIT_FAILURE; } printLineBreak(); printf("GAME OVER!\n"); printf("Vice Chanceller %c Won in %d Turns!!\n", winner + UNI_CHAR_NAME, getTurnNumber(g)); printf("\n"); int counter = UNI_A; while (counter < NUM_UNIS + UNI_A) { printf("Uni %c scored %d KPIs\n", counter + UNI_CHAR_NAME, getKPIpoints(g, counter)); counter++; } printLineBreak(); disposeGame(g); // ask to play again printf("Ctrl+C will exit the game.\nOtherwise, the game will " "recommence by hitting enter."); int a = scanf("%*c"); a++; } return EXIT_SUCCESS; }
int main(){ //imposta il seme del random al tempo attuale. Necessario per la funzione rand() srand(time(0)); /* *\ ** Ciclo principale, chiede la modalita' di gioco e inizializza il ciclo di gioco ** ** Decide casualmente il giocatore iniziale. ** \* */ do{ emptyArray(gameBoard); printf("Seleziona modalita:\n 0 -- umano vs pc\n 1 -- umano vs umano\n 2 -- pc vs pc\n"); scanf("%d", &mod); if(mod == -1){ break; } if(rand() % 2 + 1 == 1){ turn = 1; printf("Giocatore 2 inizia per primo.\n"); } else{ turn = 0; printf("Giocatore 1 inizia per primo.\n"); } /* *\ ** Ciclo di gioco. Inizia cercando un vincitore. ** ** In base alla modalita' selezionata e al turno, richiede un input al giocatore umano ** ** o calcola le mosse dell'IA. ** ** Stampa il campo da gioco e infine controlla se il e' pieno ** \* */ do{ if(checkForWinner(gameBoard) == 1 || checkForWinner(gameBoard) == 2){ break; } if(turn == 0 && pc == 2 || turn == 1 && pc == 1){ printf("-------------------\n"); (mod == 0 || mod == 2) ? printf("Turno del computer\n") : printf("Turno del giocatore 1\n"); printf("-------------------\n"); if(mod == 1) printInstructions(); (mod == 0 || mod == 2) ? (moveResult = elaborateMove(gameBoard, pc)) : (moveResult = makeMove(gameBoard, pc)); } else if(turn == 0 && player == 2 || turn == 1 && player == 1){ printf("-------------------\n"); (mod == 2) ? printf("Turno del computer\n") : printf("Turno del giocatore 2\n"); printf("-------------------\n"); printInstructions(); (mod == 2) ? (moveResult = elaborateMove(gameBoard, player)) : (moveResult = makeMove(gameBoard, player)); } if(moveResult != -1){ if(turn == 0){ turn = 1; } else{ turn = 0; } } else{ printf("Partita terminata.\n"); turn = -1; } createTable(gameBoard); if(gameBoardFull(gameBoard) && turn != -1){ checkForWinner(gameBoard); turn = -1; } }while(turn != -1); turn = 0; }while(mod != -1); getchar(); }