Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Aly Milich Final Project README

This game of Battleship was designed and coded by Aly Milich for the Columbia High School Summer Program 2016. The player is meant to create the size of their board and attempt to sink the 4 battleships randomly placed by the opponent (the computer). After entering the coordinates of the player's guess, the spot on the board will either change to a 'X', indicating a miss, or a 'S', indicating a hit. The player, which starts with 5 lives, will lose 1 life for each miss and gain 1 point for each hit. The game ends either when the player loses all of its lives or all of the ships are hit. For a more challenging game, the player can make the board larger and for an easier game, the player should choose a smaller board size.

The game does not include ships of varying sizes as was suggested after submitting the proposal because I wanted to make sure the basic parts of the game worked before adding on extra aspects. I thought it would be an interesting twist to allow the user to select the board size even though the ships are only one size. Another difference is that the user does not place his or her own ships and instead is only attacking the computer. I was having some issues implementing this aspect and wanted to finish in time so I had to leave it out. It is a little different than a classic game of battleship but still a lot of fun to play!
Binary file added battleship
Binary file not shown.
104 changes: 104 additions & 0 deletions battleship.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

/*battleship game
@author Aly Milich
*/

int main(){

int lives=5, xchoice, ychoice, score, boardsize, ships = 4, compxarr[4], compyarr[4];

printf("Welcome to Battleship.\n");
printf("Please input the size of your board (i.e. enter 4 for a 4x4 board)\n");

scanf("%d", &boardsize);

//create board
char board[boardsize][boardsize];

int i, j;

//print board, googled how to do this and found a solution on Stack Overflow
for(i =0; i<boardsize; i++){
for(j=0; j<boardsize; j++){
board[i][j] = '.';
printf("%c", board[i][j]);
}
printf("\n");
}

printf("You will have 5 chances to find the 4 battleships on your opponent's board. You will lose a life for each incorrect guess and gain a point for each correct guess.\n");

srand(time(NULL));

//generate 4 random ships and store the coordinates in an array
for(i =0; i<4; i++){
int compx = rand()%boardsize +1;
compxarr[i] = compx;
int compy = rand()%boardsize +1;
compyarr[i] = compy;
}

while(lives>0 && ships >0){

//update board before next turn
for(i=0; i<boardsize; i++){
for(j=0; j<boardsize; j++){
printf("%c", board[i][j]);
}
printf("\n");
}

printf("Take a guess!\n");
printf("Input the row you would like to guess.\n");
scanf("%d", &xchoice);

printf("Input the column you would like to guess.\n");
scanf("%d", &ychoice);

//check if the input is valid, if not ask user to change guess
if(xchoice > boardsize || ychoice > boardsize){
printf("Those coordinates are invalid. Please choose values less than or equal to %d\n", boardsize);
printf("Input the row you would like to guess.\n");
scanf("%d", &xchoice);

printf("Input the column you would like to guess.\n");
scanf("%d", &ychoice);
}

//check if user hit a ship (if guess is in compx or compy arrays. I tried to do this with a loop and it ended up looping the entire thing and causing lives to decrease by 4 with each guess so i just wrote out the possibilities
if(xchoice-1 == compxarr[0]|| xchoice-1 == compxarr[1]||xchoice-1 == compxarr[2]||xchoice-1 == compxarr[3] && ychoice-1 == compyarr[0] || ychoice-1 == compyarr[1] || ychoice-1 == compyarr[2] || ychoice-1 == compyarr[3]){
printf("You hit a ship!\n");
ships--;
score++;
printf("There are %d ships left to find.\n", ships);
printf("Score: %d\n", score);
board[xchoice-1][ychoice-1] = 'S';
}
else{
printf("You missed.\n");
lives--;
printf("You have %d lives left.\n", lives);
board[xchoice-1][ychoice-1] = 'X';
}

//user wins if they sink all of the ships
if(ships == 0){
for(i=0; i<boardsize; i++){
for(j=0; j<boardsize; j++){
printf("%c", board[i][j]);
}
printf("\n");
}
printf("You won!\n");
}
}

//user loses if they miss 5 times
if(lives ==0)
printf("You didn't find all of the battleships. You lose.\n");

return 0;
} //end main
6 changes: 6 additions & 0 deletions proposal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Aly Milich

For my final project I would like to make a battleship game. The rules will be very similar to the classic board game except the user will be able to customize their level and how large/small they would like the board to be. The easier the level, the more ships will be placed on the board (for a higher probability of hitting the ships). Depending on how much time it takes to code the basic parts of the game, I may have the board write to an external file, but I most likely will just print the board and the computer's moves.
This project will use a lot of arrays and conditional statements. The board will be a 2D array and the conditional statements will represent different scenarios, for example if the user hits a ship. It will also use functions so, instead of rewriting what happens when a ship is hit, I can just call a function to change the score/lives. The entire game will be in a loop while the user has ships left.
I would like to make this project because Battleship is a popular game and I think that it combines many of the topics we have covered. I have made many games in the past in Java but this would be my first of this type and in C.
I plan on using the standard input/output library as well as <assert.h> to check if certain conditions are met (like hitting a ship) and <time.h> to choose random spots to place the ship. These are the only ones I can think of now but I may have to add more as I go.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the classic game of Battleship, the ships are of varying sizes. Do you plan on doing this or do you intend to just make them a fixed size? And does your player select where the ships will be placed or will they be random?

The project sounds good. A suggestion I have is to place your functions in separate files (the definitions go in .c files and the prototypes go in the corresponding .h file) to help keep things organized. Also, it may be interesting to implement a very simple AI for the computer (assuming your ships are larger than 1 in size), where the computer aims in a smaller radius around where it hit until it sinks a ship as opposed to continuing to randomly pick locations.