diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..6f1e3b2 --- /dev/null +++ b/README.txt @@ -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! diff --git a/battleship b/battleship new file mode 100755 index 0000000..4b64d7e Binary files /dev/null and b/battleship differ diff --git a/battleship.c b/battleship.c new file mode 100644 index 0000000..6b6434b --- /dev/null +++ b/battleship.c @@ -0,0 +1,104 @@ +#include +#include +#include + +/*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; i0 && ships >0){ + + //update board before next turn + for(i=0; i 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 to check if certain conditions are met (like hitting a ship) and 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.