Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <windows.h>
- using namespace std;
- #define ROW 10
- #define COL 10
- // The size of each type of ship
- #define S_CARRIER 5
- #define S_BATTLESHIP 4
- #define S_DESTROYER 3
- #define S_SUBMARINE 3
- #define S_PATROLBOAT 2
- enum hit {
- BLANK = ' ',
- MISS = '*',
- HIT = 'x',
- CARRIER = 'C',
- BATTLESHIP = 'B',
- DESTROYER = 'D',
- SUBMARINE = 'S',
- PATROLBOAT = 'P'
- };
- // Various prototypes for the functions
- void welcome_display();
- bool play();
- void player_populate(hit players_board_def[ROW][COL]);
- void boat_check(hit players_board_def[ROW][COL], int boat_type);
- void board_display(hit board[ROW][COL]);
- void board_display(hit board[ROW][COL], hit ref[ROW][COL]);
- bool is_sunk(hit boat, hit board[ROW][COL], hit ref[ROW][COL]);
- bool is_game_over(hit board[ROW][COL], hit ref[ROW][COL]);
- int hit2space(int space);
- void player_attack(const hit computer_const_board_ref[ROW][COL], hit player_board_off[ROW][COL]);
- int main()
- {
- bool valid=true;
- // Clears the screen then displays a graphic for the startup of the program
- //welcome_display();
- system("cls");
- do{
- valid = play();
- }while (valid);
- printf("\n");
- system("pause");
- }
- bool play(){
- // bool valid;
- // For the players own board
- hit player_board_def[ROW][COL];
- hit computer_board_def[ROW][COL] = {
- { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
- { BLANK, DESTROYER, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
- { BLANK, DESTROYER, CARRIER, CARRIER, CARRIER, CARRIER, CARRIER, BLANK, BLANK, BLANK},
- { BLANK, DESTROYER, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
- { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK},
- { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, BLANK},
- { BLANK, SUBMARINE, SUBMARINE, SUBMARINE, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, PATROLBOAT},
- { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, PATROLBOAT},
- { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BATTLESHIP, BLANK, BLANK, BLANK},
- { BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK}
- };
- // Invisible reference board keeping track of boat locations... ((Should be const))
- hit player_const_board_ref[ROW][COL];
- hit computer_const_board_ref[ROW][COL];
- // For the board the players will be attacking
- hit player_board_off[ROW][COL];
- hit computer_board_off[ROW][COL];
- for(int i = 0; i < ROW; i++)
- for(int j = 0; j < COL; j++)
- player_board_def[i][j] = /*computer_board_def[i][j] =*/ player_const_board_ref[i][j] = computer_const_board_ref[i][j] = player_board_off[i][j] = computer_board_off[i][j] = BLANK;
- // player_populate(player_board_def);
- // board_display(player_board_def);
- // placementLogic(computer_board_def);
- // board_display(computer_board_def);
- // Assume all boards are set at this point so copy said boards
- for(int i = 0; i < ROW; i++)
- for(int j = 0; j < COL; j++){
- player_const_board_ref[i][j] = player_board_def[i][j];
- computer_const_board_ref[i][j] = computer_board_def[i][j];
- }
- /*
- do{
- player_attack
- is_game_over
- ai_move
- is_game_over
- }while (valid);
- */
- for (int y=0; y<15; y++){
- printf("\n Computer player's board...\n");
- board_display(computer_board_def);
- player_attack(computer_const_board_ref, player_board_off);
- printf(" Human player's board...\n");
- board_display(player_board_off);
- }
- // This is just to ask if you want to make a new game or not. Based on that it will start a new game or quit
- // This can be put in a separate function if needed later...
- bool new_game;
- char choice;
- int x=0;
- do{
- printf("\n\n Do you want to start a new game? (y/n): ");
- scanf("%c", &choice);
- fflush(stdin);
- switch (toupper(choice)){
- case 'Y':
- x=1;
- new_game=true;
- break;
- case 'N':
- x=2;
- new_game=false;
- break;
- default:
- printf(" Incorrect option entered, please choose again\n\n");
- x=3;
- }
- }while (x==3);
- return (new_game);
- }
- void player_attack(const hit computer_const_board_ref[ROW][COL], hit player_board_off[ROW][COL]){
- char coord[3]={0};
- char tchar;
- bool valid;
- do{
- do{
- valid = true;
- printf("\n Enter the coordinate you want to attack: ");
- fgets(coord, 3, stdin);
- fflush(stdin);
- strupr(coord);
- if(!isalpha(coord[0])){
- tchar = coord[0];
- coord[0] = coord[1];
- coord[1] = tchar;
- }
- if ((coord[0] < 'A' || coord[0] > 'J') || (coord[1] <'0' || coord[1] >'9')){
- printf("\n Incorrect coordinate entered. Please choose again.\n");
- valid = false;
- }else
- valid = true;
- }while (!valid);
- coord[0] -= 'A';
- coord[1] -= '0';
- if (player_board_off[coord[1]][coord[0]] != BLANK){
- printf("\n That coordinate has already been attacked. Please choose again.\n");
- valid = false;
- }
- }while (!valid);
- printf(" You are attacking: %d%d\n", coord[0], coord[1]);
- if (computer_const_board_ref[coord[1]][coord[0]]!=BLANK){
- player_board_off[coord[1]][coord[0]]=HIT;
- }else if (computer_const_board_ref[coord[1]][coord[0]]==BLANK){
- player_board_off[coord[1]][coord[0]]=MISS;
- }
- }
- /*********************************************************************************************************
- Purpose: This is to have the player choose which boat they want to place and then proceed to
- another function that based on the boat choosen will ask for coordinates and valid them. This continues
- until all boats have been placed.
- Input: hit board[ROW][COL]
- Returns: Nothing
- *********************************************************************************************************/
- void player_populate(hit player_board_def[ROW][COL]){
- int boattypes[5] = { CARRIER, BATTLESHIP, DESTROYER, SUBMARINE, PATROLBOAT };
- bool boatplaced[5] = { false } ;
- int boats = 0;
- int choice;
- bool valid;
- printf("\n");
- do{
- do{
- valid = true;
- board_display(player_board_def);
- printf("\n 1) Aircraft Carrier\n 2) Battleship\n 3) Destroyer\n 4) Submarine\n 5) Patrol Boat\n\n");
- printf(" Choose which ship you want to place: ");
- scanf("%d", &choice);
- fflush(stdin);
- if (choice > 0 && choice < 6){
- valid = true;
- }else{
- printf(" Incorrect option entered. Please choose again.\n\n");
- valid = false;
- }
- }while (!valid);
- // Comment needed here...
- choice--;
- if(!boatplaced[choice]){
- boat_check(player_board_def, boattypes[choice]);
- boats++;
- boatplaced[choice] = true;
- }else{
- printf("\n You have already placed that ship. Please choose a different ship.\n\n");
- }
- }while (boats < 5);
- }
- /*********************************************************************************************************
- Purpose: This is to have the player choose which boat they want to place and then proceed to
- another function that based on the boat choosen will ask for coordinates and valid them. This continues
- until all boats have been placed.
- Input: hit player_board_def[ROW][COL]
- Returns: Nothing
- *********************************************************************************************************/
- void boat_check(hit player_board_def[ROW][COL], int boat_type)
- {
- char coord1[3]={0};
- char coord2[3]={0};
- char tchar;
- bool valid;
- do{
- valid = true;
- // Input & validation for the start coordinates from the user
- do{
- printf("\n Enter first coordinate: ");
- fgets(coord1, 3, stdin);
- fflush(stdin);
- strupr(coord1);
- if(!isalpha(coord1[0])){
- tchar = coord1[0];
- coord1[0] = coord1[1];
- coord1[1] = tchar;
- }
- }while ((coord1[0] < 'A' || coord1[0] > 'J') && (coord1[0] <'0' || coord1[0] >'9'));
- // Input & validation for the end coordinates from the user
- do{
- printf(" Enter second coordinate: ");
- fgets(coord2, 3, stdin);
- fflush(stdin);
- strupr(coord2);
- if(!isalpha(coord2[0])){
- tchar = coord2[0];
- coord2[0] = coord2[1];
- coord2[1] = tchar;
- }
- }while ((coord2[1] < 'A' || coord2[1] > 'J') && (coord2[1] <'0' || coord2[1] >'9'));
- // This changes the chars in the arrays to an actual number so that we can use them to interface
- // with the location in the array it corrisponds to.
- coord1[0] -= 'A';
- coord1[1] -= '0';
- coord2[0] -= 'A';
- coord2[1] -= '0';
- // This checks the inputted numbers to see if they are valid by one of the element pairs being equal
- // to each other. Such as if you have A2 & D2 as the coordinates the second element in both arrays
- // are the same or if you have A2 & A5, the 'A's are the same (This verifies that the start & end points
- // are truly in a horizontal or vertical line)
- if ((coord1[0]==coord2[0] && coord1[1]!=coord2[1]) || (coord1[0]!=coord2[0] && coord1[1]==coord2[1]))
- {
- for(int i = 0; i < 2; i++)
- {
- if(coord1[i] > coord2[i]) // Comment needed here...
- {
- for(int j = 0; j < 2; j++)
- {
- tchar = coord1[j];
- coord1[j] = coord2[j];
- coord2[j] = tchar;
- }
- }
- }
- // Comment needed here...
- if((coord2[0] - coord1[0]) + (coord2[1] - coord1[1]) + 1 != hit2space(boat_type))
- valid = false;
- for(int i = coord1[0]; i <= coord2[0]; i++) // Checking horizontal
- if(player_board_def[coord1[1]][i] != BLANK)
- valid = false;
- for(int i = coord1[1]; i <= coord2[1]; i++) // Checking vertical
- if(player_board_def[i][coord1[0]] != BLANK)
- valid = false;
- }
- // If none of the previous checks have not been met then the coordinates cannot be
- // correct therefore we must ask again
- else
- {
- valid = false;
- }
- if (!valid)
- printf("\n Incorrect coordinates entered, please choose again\n");
- } while (!valid);
- for(int i = coord1[0]; i <= coord2[0]; i++)
- player_board_def[coord1[1]][i] = (hit) boat_type; // Or respective boat type
- for(int i = coord1[1]; i <= coord2[1]; i++)
- player_board_def[i][coord1[0]] = (hit) boat_type; // Or respective boat type
- }
- /*********************************************************************************************************
- Purpose: This function simply prints out any array it is given in the board format that the battleship
- game is supposed to be displayed in
- Input: hit board[ROW][COL]
- Returns: Nothing
- *********************************************************************************************************/
- void board_display(hit board[ROW][COL]){
- char letters_array[11]={' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
- // This is for displaying the first line of dashes
- for (int z=0; z < 4*COL+4; z++){
- printf("-");
- }
- printf("\n");
- // This is for displaying A-J
- for (int x=0; x < 11; x++){
- printf(" %c |", letters_array[x]);
- }
- printf("\n");
- // These for loops are for making the board dynamically based on the #define values and displays
- // what is in the array in the board format accordingly
- for (int x=0, y=0; x < ROW; x++, y++){
- for (int z=0; z < 4*COL+4; z++){
- printf("-");
- }
- printf("\n");
- // This is for displaying 0-9
- printf(" %d |", y);
- // This is for displaying the contents of the element of the array we are currently at
- for (int y=0; y < COL; y++){
- printf(" %c |", board[x][y]);
- }
- printf("\n");
- }
- // This is for displaying the last line of dashes
- for (int z=0; z < 4*COL+4; z++){
- printf("-");
- }
- printf("\n");
- }
- /*********************************************************************************************************
- Purpose: This is the overloaded version of the function that prints out any array it is given in the board
- format that the battleship game is supposed to be displayed in. This functions takes two boards
- Input: hit board[ROW][COL] & hit ref[ROW][COL]
- Returns: Nothing
- *********************************************************************************************************/
- /*void board_display(hit board[ROW][COL], hit ref[ROW][COL]){
- char letters_array[11]={' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
- // This is for displaying the first line of dashes
- for (int z=0; z < 4*COL+4; z++){
- printf("-");
- }
- printf("\n");
- // This is for displaying A-J
- for (int x=0; x < 11; x++){
- printf(" %c |", letters_array[x]);
- }
- printf("\n");
- // These for loops are for making the board dynamically based on the #define values and displays
- // what is in the array in the board format accordingly
- for (int x=0, y=0; x < ROW; x++, y++){
- for (int z=0; z < 4*COL+4; z++){
- printf("-");
- }
- printf("\n");
- // This is for displaying 0-9
- printf(" %d |", y);
- // This is for displaying the contents of the element of the array we are currently at
- for (int y=0; y < COL; y++){
- printf(" %c |", is_sunk(ref[x][y], board, ref) ? tolower(board[x][y]) : board[x][y]);
- }
- printf("\n");
- }
- // This is for displaying the last line of dashes
- for (int z=0; z < 4*COL+4; z++){
- printf("-");
- }
- printf("\n");
- }
- // Comment needed here...
- bool is_game_over(hit board[ROW][COL], hit ref[ROW][COL])
- {
- int boats[BOATCOUNT] = { CARRIER, BATTLESHIP, DESTROYER, SUBMARINE, PATROLBOAT };
- int sinks = BOATCOUNT;
- for(int i = 0; i < BOATCOUNT; i++)
- if(is_sunk(boats[i], board, ref))
- sinks--;
- return !sinks;
- }
- // Comment needed here...
- bool is_sunk(hit boat, hit board[ROW][COL], hit ref[ROW][COL])
- {
- int size = hit2space(boat);
- if(!size)
- return false;
- for(int i = 0; i < ROW; i++)
- for(int j = 0; j < COL; j++)
- if(ref[i][j] == BOAT && board[i][j] == HIT)
- size--;
- return !size;
- }
- */
- // Comment needed here...
- int hit2space(int space)
- {
- switch(space)
- {
- case CARRIER: return S_CARRIER;
- case BATTLESHIP: return S_BATTLESHIP;
- case DESTROYER: return S_DESTROYER;
- case SUBMARINE: return S_SUBMARINE;
- case PATROLBOAT: return S_PATROLBOAT;
- default: return 0;
- }
- }
- /*********************************************************************************************************
- Purpose: This just prints out the first screen to display with a cool ascii graphic for the player to see
- Input: Nothing
- Returns: Nothing
- *********************************************************************************************************/
- void welcome_display(){
- // This just gives time for the user so they can fullscreen the command prompt to be able to see
- // the game without scrolling up & down
- printf("\n Loading; Please wait");
- for (int x=0; x<3; x++){
- printf(". ");
- Sleep(1150);
- }
- system("cls");
- // This is fairly obvious that this is the title and graphic that is displayed when you start the program
- printf("\n");
- printf("\t\t\t\tWelcome to\n");
- printf("\t\t\t\ ================\n");
- printf(" ____ __ __ ___ __ \n");
- printf(" /\\ _`\\ /\\ \\__/\\ \\__/\\_ \\ /\\ \\ __ \n");
- printf(" \\ \\ \\_\\ \\ __ \\ \\ ,_\\ \\ ,_\\//\\ \\ __ ____\\ \\ \\___ /\\_\\ _____ \n");
- printf(" \\ \\ _ <' /'__`\\ \\ \\ \\/\\ \\ \\/ \\ \\ \\ /'__`\\ /',__\\\\ \\ _ `\\/\\ \\/\\ '__`\\ \n");
- printf(" \\ \\ \\_\\ \\/\\ \\_\\.\\_\\ \\ \\_\\ \\ \\_ \\_\\ \\_/\\ __//\\__, `\\\\ \\ \\ \\ \\ \\ \\ \\ \\_\\ \\\n");
- printf(" \\ \\____/\\ \\__/.\\_\\\\ \\__\\\\ \\__\\/\\____\\ \\____\\/\\____/ \\ \\_\\ \\_\\ \\_\\ \\ ,__/\n");
- printf(" \\/___/ \\/__/\\/_/ \\/__/ \\/__/\\/____/\\/____/\\/___/ \\/_/\\/_/\\/_/\\ \\ \\/ \n");
- printf(" \\ \\_\\ \n");
- printf(" Developers are: \\/_/ \n");
- printf(" # _ ");
- printf(" Larry Ing _ # # (.)\n");
- printf(" Luke Kontes =====|_|#_#___|__ ");
- printf(" Erich Healy _ |____________| _ ");
- printf(" Mark de Guzman =====|.| | . . . . | |.|==== ");
- printf(" _ .---------------------------. _ ");
- printf(" =====|.|| . . . . . . . . ||.|====\n");
- printf(" \\---------------------' `--------------/\n");
- printf(" \\ . . . . . . . . . . . . USS-42 /\n");
- printf(" \\ . . . . . . . . . . . /\n");
- printf(" \\__________________________________________________________/\n");
- printf(" wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
- printf(" wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n");
- printf(" wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww \n");
- printf("\n");
- printf("\n");
- // Possibly use a sleep instead here...
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement