battle ship C program source code, Exercises of Computer Programming

battle ship C program source code

Typology: Exercises

2016/2017

Uploaded on 03/30/2017

abbas_jibrin
abbas_jibrin 🇬🇧

4.5

(4)

1 document

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm And Data Structure
Q. Find a simple computer game program source code written in
Clanguage from the Internet, compile and run it. Make your program
ready for presentation in the computer lab.
#include "battleship.h"
void welcomeScreen (void) {
printf ("XXXXX XXXX XXXXXX XXXXXX XX
XXXXXX XXXXX XX XX XX XXXX\n");
printf ("XX XX XX XX XX XX XX XX XX XX
XX XX XX XX\n");
printf ("XXXXX XX XX XX XX XX XXXX XXXX
XXXXXX XX XXXX\n");
printf ("XX XX XXXXXX XX XX XX XX XX XX
XX XX XX\n");
printf ("XXXXX XX XX XX XX XXXXXX XXXXXX
XXXXX XX XX XX XX\n");
printf ("\n\n");
printf ("RULES OF THE GAME:\n");
printf ("1. This is a two player game.\n");
printf ("2. Player 1 is you and Player 2 is the computer.\n");
printf ("3. Player 1 will be prompted if user wants to manually
input coordinates\n");
printf (" for the game board or have the computer randomly
generate a game board\n");
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download battle ship C program source code and more Exercises Computer Programming in PDF only on Docsity!

Algorithm And Data Structure Q. Find a simple computer game program source code written in Clanguage from the Internet, compile and run it. Make your program ready for presentation in the computer lab.

#include "battleship.h" void welcomeScreen (void) { printf ("XXXXX XXXX XXXXXX XXXXXX XX XXXXXX XXXXX XX XX XX XXXX\n"); printf ("XX XX XX XX XX XX XX XX XX XX XX XX XX XX\n"); printf ("XXXXX XX XX XX XX XX XXXX XXXX XXXXXX XX XXXX\n"); printf ("XX XX XXXXXX XX XX XX XX XX XX XX XX XX\n"); printf ("XXXXX XX XX XX XX XXXXXX XXXXXX XXXXX XX XX XX XX\n"); printf ("\n\n"); printf ("RULES OF THE GAME:\n"); printf ("1. This is a two player game.\n"); printf ("2. Player 1 is you and Player 2 is the computer.\n"); printf ("3. Player 1 will be prompted if user wants to manually input coordinates\n"); printf (" for the game board or have the computer randomly generate a game board\n");

printf ("4. There are five types of ships to be placed by longest length to the\n"); printf (" shortest; [c] Carrier has 5 cells, [b] Battleship has 4 cells, [r] Cruiser\n"); printf (" has 3 cells, [s] Submarine has 3 cells, [d] Destroyer has 2 cells\n"); printf ("5. The computer randomly selects which player goes first \n"); printf ("6. The game begins as each player tries to guess the location of the ships\n"); printf (" of the opposing player's game board; [*] hit and [m] miss \n"); printf ("7. First player to guess the location of all ships wins\n\n"); }

void initializeGameBoard (Cell gameBoard[][COLS]) { int i = 0, j = 0;

for (i = 0; i < ROWS; i++) for (j = 0; j < COLS; j++) { gameBoard[i][j].symbol = WATER; gameBoard[i][j].position.row = i; gameBoard[i][j].position.column = j; } }

void putShipOnGameBoard (Cell gameBoard[][COLS], WaterCraft ship, Coordinate position, int direction) { int i = ship.length - 1;

for (i = 0; i < ship.length; i++) { if (direction == HORIZONTAL) gameBoard [position.row][position.column + i].symbol = ship.symbol; else /* VERTICAL */ gameBoard [position.row + i][position.column].symbol = ship.symbol; } }

: manuallyPlaceShipsOnGameBoard (Cell [][], WaterCraft []);

void manuallyPlaceShipsOnGameBoard (Cell gameBoard[][COLS], WaterCraft ship[]) { char stringPosition[11] = ""; int i = 0, j = 0;

Coordinate position[5]; Boolean isValid = FALSE; fflush (stdin); for (i = 0; i < NUM_OF_SHIPS; i++) {

while (TRUE) { system ("cls"); printGameBoard (gameBoard, TRUE); printf ("> Please enter the %d cells to place the %s across (no spaces):\n", ship[i].length, ship[i].name); printf ("> "); scanf ("%s", stringPosition); /* convertStringtoPosition returns false if unsuccessful */ if (convertStringtoPosition (position, stringPosition, ship[i].length)) { isValid = TRUE; for (j = 0; j < ship[i].length; j++) {

if (gameBoard[position[j].row][position [j].column].symbol == WATER) { gameBoard[position[j].row][position [j].column].symbol = ship[i].symbol; } else { isValid = FALSE; printf ("> Invalid input!\n"); if (j != 0) while (j >= 0) { gameBoard [position[j].row][position[j].column].symbol = WATER; j--; }

if (isValidLocation (gameBoard, position, direction, ship[i].length)) break; } putShipOnGameBoard (gameBoard, ship[i], position, direction); } }

void updateGameBoard (Cell gameBoard[][COLS], Coordinate target) { switch (gameBoard[target.row][target.column].symbol) { /* miss / case WATER: gameBoard[target.row][target.column].symbol = MISS; break; / hit */ case CARRIER: case BATTLESHIP: case CRUISER: case SUBMARINE: case DESTROYER: gameBoard[target.row][target.column].symbol = HIT; break; case HIT: case MISS: default:

break; } } /**

  • Function name : checkBoundsOfCardinal () void checkBoundsOfCardinal (Boolean cardinals[], int bound, int direction) { switch (direction) { case NORTH: if (bound < 0) cardinals[0] = FALSE; else cardinals[0] = TRUE; break;

case SOUTH: if (bound > 9) cardinals[1] = FALSE; else cardinals[1] = TRUE; break;

case WEST: if (bound < 0)

case CARRIER: if (--sunkShip[player][0] == 0) { printf ("> Player %d's Carrier sunked!\n", player

  • 1);

/* Write to battleship.log / fprintf (stream, "Player %d's Carrier sunked!\n", player + 1); sunked = TRUE; } break; case BATTLESHIP: if (--sunkShip[player][1] == 0) { printf ("> Player %d's Battleship sunked!\n", player + 1); / Write to battleship.log */ fprintf (stream, "Player %d's Battleship sunked! \n", player + 1);

sunked = TRUE; } break; case CRUISER: if (--sunkShip[player][2] == 0) { printf ("> Player %d's Cruiser sunked!\n", player

  • 1);

/* Write to battleship.log / fprintf (stream, "Player %d's Cruiser sunked!\n", player + 1) sunked = TRUE; } break; case SUBMARINE: if (--sunkShip[player][3] == 0) { printf ("> Player %d's Submarine sunked!\n", player + 1); / Write to battleship.log */ fprintf (stream, "Player %d's Submarine sunked! \n", player + 1); sunked = TRUE; } break; case DESTROYER: if (--sunkShip[player][4] == 0) { printf ("> Player %d's Destroyer sunked!\n", player + 1);

/* Write to battleship.log */ fprintf (stream, "Player %d's Destroyer sunked! \n", player + 1);

sunked = TRUE;

return isValid; }

/** Boolean flag = TRUE; char temp = '\0'; int i = 0, j = 0, k = 1;

/* checks if length of input is good / if (strlen (stringPosition)/2 == length) { / loops through the length of the ship / for (i = 0; i < length && flag; i++) { / checks if each cell is a digit */ if (isdigit (stringPosition[j]) && isdigit (stringPosition [k])) { position[i].row = stringPosition[j] - '0'; position[i].column = stringPosition[k] - '0'; j += 2; k += 2; } else { flag = FALSE; } } } else {

flag = FALSE; }

return flag; }

*/ Boolean isWinner (Stats players[], int player) { Boolean isWin = FALSE; if (players[player].numHits == 17) isWin = TRUE; return isWin; }

Coordinate generatePosition (int direction, int length) { Coordinate position;

if (direction == HORIZONTAL) { position.row = getRandomNumber (0, ROWS); position.column = getRandomNumber (0, COLS - length); } else { /* VERTICAL */ position.row = getRandomNumber (0, ROWS - length); position.column = getRandomNumber (0, COLS); } return position;

case CRUISER: case SUBMARINE: case DESTROYER: hit = 1; break;

case HIT: case MISS: default: hit = -1; break; } return hit; } int getRandomNumber (int lowest, int highest) { if (lowest == 0) return rand () % ++highest;

if (lowest > 0) return rand () % ++highest + lowest; }