bool Board::hasWinner() const { for (int i = 0; i < mPlayersCnt; i++) { if (isWinner((playerId)i)) { return true; } } return false; }
void createSons() { int pid1, pid2; int player1 = 0; int player2 = 10; time_t t; pid1 = fork(); switch(pid1) { case -1: printf("No child created!\n"); exit(1); case 0: signal(SIGINT, my_handler); while(1) { pause(); srand((unsigned) time(&t)); printf("Player1 Dice!\n"); draws[0] = throwTheDice(); if(*gameStarted == 1) { play1(draws[0]); } sleep(4); kill(getppid(), SIGINT); } break; default: pid2 = fork(); switch(pid2) { case -1: printf("No child created!\n"); exit(1); case 0: signal(SIGINT, my_handler); while(1) { pause(); srand((unsigned) time(&t)); printf("Player2 Dice!\n"); draws[1] = throwTheDice(); if(*gameStarted == 1) { play2(draws[1]); } sleep(4); kill(getppid(), SIGINT); } break; default: signal(SIGINT,my_handler); printf("I'm the father!\n"); sleep(4); while(1) { kill(pid1, SIGINT); pause(); printf("Player1 dice: %d\n", draws[0]); printf("\n"); kill(pid2, SIGINT); pause(); printf("Player2 dice: %d\n", draws[1]); printf("\n"); if(draws[1] < draws[0]) { *gameStarted = 1; printf("Player1 win the dice and will start the game!\n"); printf("\n"); printArray(arr, arrSIZE); while(1) { kill(pid1, SIGINT); pause(); printf("Player1 dice: %d\n", draws[0]); isWinner(); printArray(arr, arrSIZE); kill(pid2, SIGINT); pause(); printf("Player1 dice: %d\n", draws[0]); isWinner(); printArray(arr, arrSIZE); } break; } else if(draws[1] > draws[0]) { *gameStarted = 1; printf("Player2 win the dice and will start the game!\n"); printf("\n"); printArray(arr, arrSIZE); while(1) { kill(pid2, SIGINT); pause(); printf("Player2 dice: %d\n", draws[1]); isWinner(); printArray(arr, arrSIZE); kill(pid1, SIGINT); pause(); printf("Player1 dice: %d\n", draws[0]); isWinner(); printArray(arr, arrSIZE); } break; } else printf("Dice's are equals, try again please!\n"); } } } }
int gameLoop(int game_state, board_conf_type board_conf, board_type board) { void *message; if (IsMessage()) { message = (void *) GetMessage(); ProcessMessage(message); } size_t board_bytes; char last_key; switch(game_state) { case INIT: board_bytes = board_conf.m * board_conf.n * sizeof(board_element); memset(board, (int)(unsigned char) TILE_EMPTY, board_bytes); board_point cursor_init = { .x = ceilf(((float) (board_conf.m - 1)) / 2.0), .y = ceilf(((float) (board_conf.n - 1)) / 2.0) }; board_conf.cursor = cursor_init; game_state = GAME; return gameLoop(game_state, board_conf, board); case INTRO: return gameLoop(game_state, board_conf, board); case MENU: fprintf(GAMETEXT_OUT_BUF, "Play again? [y/n]>>"); last_key = tolower(flushAndGetChar(stdin)); if (last_key == 'y') { game_state = INIT; } else if (last_key == 'n') { game_state = SHUTDOWN; } else { fprintf(GAMETEXT_OUT_BUF, "Invalid choice, please try again."); } return gameLoop(game_state, board_conf, board); case GAME: printBoard(board_conf, board); // fprintf(GAMETEXT_OUT_BUF, "Player %c's turn>>", PLAYER_SYMBOLS[board_conf.player_turn]); last_key = tolower(flushAndGetChar(KEY_BUFFER)); switch(last_key) { case KEY_UP: if (board_conf.cursor.y > 0) { board_conf.cursor.y -= 1; } break; case KEY_LEFT: if (board_conf.cursor.x > 0) { board_conf.cursor.x -= 1; } break; case KEY_DOWN: if (board_conf.cursor.y < board_conf.n - 1) { board_conf.cursor.y += 1; } break; case KEY_RIGHT: if (board_conf.cursor.x < board_conf.m - 1) { board_conf.cursor.x += 1; } break; case KEY_MARK: board[board_conf.cursor.y * board_conf.m + board_conf.cursor.x] = PLAYER_SYMBOLS[board_conf.player_turn]; if (isWinner(board_conf, board)) { board_conf.cursor.x = -1; board_conf.cursor.y = -1; printBoard(board_conf, board); fprintf(GAMETEXT_OUT_BUF, "Player %c wins!!!\n\n", PLAYER_SYMBOLS[board_conf.player_turn]); game_state = MENU; } // end of [if (isWinner//] board_conf.player_turn = (board_conf.player_turn + 1) % board_conf.num_players; break; default: fprintf(GAMETEXT_OUT_BUF, "Erroneous input: %c\n", last_key); } // end of [switch(last_key)] return gameLoop(game_state, board_conf, board); case SHUTDOWN: free(board); return 0; default: fprintf(stderr, "Invalid game state!"); return UNKNOWN_ERROR; } // End of [switch(game_state)] } // End of [gameLoop]
int main (void) { Stats players[2] = {{0, 0, 0, 0.0}, {0, 0, 0, 0.0}}; Cell playerOneGameBoard[ROWS][COLS]; Cell playerTwoGameBoard[ROWS][COLS]; Coordinate target; Coordinate targetTemp; Coordinate targetOrigin; Coordinate targetAI; WaterCraft ship[NUM_OF_SHIPS] = {{'c', 5, "Carrier"}, {'b', 4, "Battleship"}, {'r', 3, "Cruiser"}, {'s', 3, "Submarine"}, {'d', 2, "Destroyer"}}; Boolean huntMode = TRUE; Boolean targetMode = FALSE; Boolean flipper = TRUE; Boolean cardinals[4] = {TRUE, TRUE, TRUE, TRUE}; Boolean hasAShipSunked = FALSE; short sunkShip[2][NUM_OF_SHIPS] = {{5, 4, 3, 3, 2}, {5, 4, 3, 3, 2}}; short player = 0; short shot = 0; int option = 0; int north = 0, south = 0, east = 0, west = 0; int i = 0, counter = 1; char shipSymbol = '\0'; FILE *outStream = NULL; outStream = fopen (LOG_FILE_NAME, "w"); srand ((unsigned int) time (NULL)); welcomeScreen (); getchar(); system ("cls"); initializeGameBoard (playerOneGameBoard); initializeGameBoard (playerTwoGameBoard); printf ("> Please select from the following menu:\n"); printf ("> [1] Manually\n"); printf ("> [2] Randomly\n"); printf ("> Enter Option: "); scanf ("%d", &option); switch (option) { case 1: manuallyPlaceShipsOnGameBoard (playerOneGameBoard, ship); break; case 2: randomlyPlaceShipsOnGameBoard (playerOneGameBoard, ship); break; } randomlyPlaceShipsOnGameBoard (playerTwoGameBoard, ship); printf ("> Player 2 (Computer's) board has been generated.\n"); player = getRandomNumber (0, 1); printf ("> Player %d has been randomly selected to go first.\n", player + 1); getchar(); system ("cls"); while (TRUE) { fprintf (outStream, "Player %d's turn.\n", player + 1); switch (player) { case PLAYER_ONE: printf ("> Player 2's Board:\n"); printGameBoard (playerTwoGameBoard, FALSE); printf ("> PLAYER 1'S TURN\n"); do { target = getTarget (); shot = checkShot (playerTwoGameBoard, target); if (shot == -1) printf ("> Try inputting another target!\n"); } while (shot == -1); shipSymbol = playerTwoGameBoard[target.row][target.column].symbol; break; case PLAYER_TWO: printf ("> Player 1's Board:\n"); printGameBoard (playerOneGameBoard, TRUE); printf ("> COMPUTER'S TURN\n"); if (hasAShipSunked) { hasAShipSunked = FALSE; targetMode = FALSE; huntMode = TRUE; } if (targetMode) { target = targetAI; do { if (cardinals[NORTH]) { target.row = north; } else if (cardinals[SOUTH]) { target.row = south; } else if (cardinals[WEST]) { target.column = west; } else if (cardinals[EAST]) { target.column = east; } else if (!cardinals[NORTH] && !cardinals[SOUTH] && !cardinals[WEST] && !cardinals[EAST] && !hasAShipSunked) { target = targetOrigin; targetTemp = target; north = target.row - counter; targetTemp.row = north; if (checkShot (playerOneGameBoard, targetTemp) != -1 && north >= 0) { cardinals[NORTH] = TRUE; } targetTemp = target; south = target.row + counter; targetTemp.row = south; if (checkShot (playerOneGameBoard, targetTemp) != -1 && south <= 9) { cardinals[SOUTH] = TRUE; } targetTemp = target; west = target.column - counter; targetTemp.column = west; if (checkShot (playerOneGameBoard, targetTemp) != -1 && west >= 0) { cardinals[WEST] = TRUE; } targetTemp = target; east = target.column + counter; targetTemp.column = east; if (checkShot (playerOneGameBoard, targetTemp) != -1 && east <= 9) { cardinals[EAST] = TRUE; } counter++; } else { targetMode = FALSE; huntMode = TRUE; break; } shot = checkShot (playerOneGameBoard, target); } while (shot == -1 && targetMode == TRUE); if (shot == 1 && huntMode == FALSE) { for (i = 0; i < 4; i++) { if (flipper == FALSE) cardinals[i] = FALSE; if (cardinals[i] == flipper) flipper = FALSE; } } else { for (i = 0; i < 4; i++) { if (flipper == TRUE && cardinals[i] != FALSE) { cardinals[i] = FALSE; break; } } } flipper = TRUE; } if (huntMode) { counter = 1; flipper = TRUE; for (i = 0; i < 4; i++) cardinals[i] = TRUE; do { target.row = getRandomNumber (0, 9); target.column = getRandomNumber (0, 9); shot = checkShot (playerOneGameBoard, target); } while (shot == -1); if (shot == 1) targetOrigin = target; } if (shot == 1) { if (!cardinals[NORTH] && !cardinals[SOUTH] && !cardinals[WEST] && !cardinals[EAST] && !hasAShipSunked) { target = targetOrigin; } huntMode = FALSE; targetMode = TRUE; targetAI = target; if (cardinals[NORTH] == TRUE) { /* NORTH */ north = (targetAI.row - 1); checkBoundsOfCardinal (cardinals, north, NORTH); targetTemp = target; targetTemp.row = north; if (checkShot (playerOneGameBoard, targetTemp) == -1) cardinals[NORTH] = FALSE; } if (cardinals[SOUTH] == TRUE) { /* SOUTH */ south = targetAI.row + 1; checkBoundsOfCardinal (cardinals, south, SOUTH); targetTemp = target; targetTemp.row = south; if (checkShot (playerOneGameBoard, targetTemp) == -1) cardinals[SOUTH] = FALSE; } if (cardinals[WEST] == TRUE) { /* WEST */ west = targetAI.column - 1; checkBoundsOfCardinal (cardinals, west, WEST); targetTemp = target; targetTemp.column = west; if (checkShot (playerOneGameBoard, targetTemp) == -1) cardinals[WEST] = FALSE; } if (cardinals[EAST] == TRUE) { /* EAST */ east = targetAI.column + 1; checkBoundsOfCardinal (cardinals, east, EAST); targetTemp = target; targetTemp.column = east; if (checkShot (playerOneGameBoard, targetTemp) == -1) cardinals[EAST] = FALSE; } } shipSymbol = playerOneGameBoard[target.row][target.column].symbol; break; } if (shot == 1) { printf ("> %d, %d is a hit!\n", target.row, target.column); fprintf (outStream, "%d, %d is a hit!\n", target.row, target.column); players[player].numHits++; if (player == 1) hasAShipSunked = checkSunkShip (sunkShip, !player, shipSymbol, outStream); else checkSunkShip (sunkShip, !player, shipSymbol, outStream); } else { printf ("> %d, %d is a miss!\n", target.row, target.column); fprintf (outStream, "%d, %d is a miss!\n", target.row, target.column); players[player].numMisses++; } if (player == 0) updateGameBoard (playerTwoGameBoard, target); else updateGameBoard (playerOneGameBoard, target); if (isWinner (players, player)) { printf ("\n> Player %d wins!\n", player + 1); fprintf (outStream, "\n>>>>> Player %d wins! <<<<<\n", player + 1); break; } getchar(); player = !player; system ("cls"); } players[0].totalShots = players[0].numHits + players[0].numMisses; players[0].hitMissRatio = ((double) players[0].numHits/(double) players[0].numMisses) * 100; players[1].totalShots = players[1].numHits + players[1].numMisses; players[1].hitMissRatio = ((double) players[1].numHits/(double) players[1].numMisses) * 100; fprintf (outStream, "+===================================================\n"); fprintf (outStream, "| PLAYER STATS \n"); fprintf (outStream, "+---------------------------------------------------\n"); fprintf (outStream, "| PLAYER 1 : %d hits \n", players[0].numHits); fprintf (outStream, "| %d misses \n", players[0].numMisses); fprintf (outStream, "| %d total shots \n", players[0].totalShots); fprintf (outStream, "| %.2lf%% hit/miss ratio \n", players[0].hitMissRatio); fprintf (outStream, "| PLAYER 2 : %d hits \n", players[1].numHits); fprintf (outStream, "| %d misses \n", players[1].numMisses); fprintf (outStream, "| %d total shots \n", players[1].totalShots); fprintf (outStream, "| %.2lf%% hit/miss ratio \n", players[1].hitMissRatio); fprintf (outStream, "+==================================================="); fclose (outStream); return 0; }
bool Board::hasWinner() { return isWinner( GameController::instance()->firstPlayer() ) || isWinner( GameController::instance()->secondPlayer() ); }
void Game::UI() { /* system("Color XY") X= color fondo Y= color letras 0 = Negro 8 = Gris 1 = Azul 9 = Azul claro 2 = Verde A = Verde claro 3 = Aguamarina B = Aguamarina claro 4 = Rojo C = Rojo claro 5 = Púrpura D = Púrpura claro 6 = Amarillo E = Amarillo claro 7 = Blanco F = Blanco brillante */ system("Color F2"); setw("Bienvenido al juego del ahorcado\n"); cout << endl; setw("Ingrese el nombre del primer jugador:\n"); player1.setName(readString()); cout << endl; setw("Ingrese el nombre del segundo jugador:\n"); player2.setName(readString()); bool turn = true; //which player goes, true is first player do { system("cls"); if (turn) { system("Color F5"); setw(player1.getName() + " c\243al es la palabra que desea que " + player2.getName() + " adivine?\n"); } else { system("Color F3"); setw(player2.getName() + " c\243al es la palabra que desea que " + player1.getName() + " adivine?\n"); } word = readString(); input.clear(); for (int i = 0; i < word.size(); i++) word[i] = toupper(word[i]); input.resize(word.size(), '\376'); int attempts = 0; //max of 7 while (attempts < 7) //game { system("cls"); if (turn) { system("Color F3"); setw(player2.getName() + " tienes " + static_cast<char>((7 - attempts) + '0') + " intento/s.\n"); } else { system("Color F5"); setw(player1.getName() + " tienes " + static_cast<char>((7 - attempts) + '0') + " intento/s.\n"); } print(attempts); if (!letterInput()) ++attempts; if (isWinner()) { if (turn) { cout << endl; setw("Felicidades " + player2.getName() + " has ganado.\n"); player2.setPoints(player2.getPoints() + 1); save(player2); //Save points and name } else { cout << endl; setw("Felicidades " + player1.getName() + " has ganado.\n"); player1.setPoints(player1.getPoints() + 1); save(player1); //Save points and name } cin.get(); break; } if (attempts == 7) { system("cls"); system("Color FC"); setw("Has PERDIDO!\n"); print(attempts); cin.get(); } } turn = !turn; //change turns } while (option());//if decides to continue }