6
Date handed out: 18 April, 2014, Friday Date submission due: 02 May 2014, Friday Programming Assignment 3: Battleships Purpose: The main purpose of this programming assignment is to revise the topics that we have covered so far including arrays, pointers, functions, repetitive statements, conditional statements and fundamentals of C programming. Description: You will write a program for playing a simplified version of the "Battleships" game. Your program should allow the game to be played between two players (in turns) or against the computer. Battleships is known as a guessing and/or pencil and paper game which dates from World War I. In this assignment, you will be implementing a simplified version of this game where you will keep the coordinates of our ships inside two-dimensional arrays. This will help you to practice with arrays, pointers and all of the three control constructs (sequence, selection and repetition). Similar to the previous assignment we recommend you not to try to compile your entire program in one "big bang." Compile it piece by piece. Test each piece that you have compiled to make sure it works correctly before you add the next piece. Please also do incremental submissions to LMS – Make sure you submit a version and then continue to upload a new version as you complete your assignment. Do not leave your submission to the very last minute. Battleships Description: The game will be played on two grids, one for each player. The grids are usually 10×10 square grids however in our implementation we are going to specify the size (number of cells) of the grids. The dimensions of the grid can be same or different (square or rectangle). The individual squares in the grid will be identified by two numbers. On each grid the players will arrange ships and record the shots of the opponent. Before play begins, each player will secretly arrange their ships on their grid. Each ship occupies a number of consecutive squares on the grid, arranged horizontally, vertically, or diagonally. The number of squares for each ship is determined by the type of the ship. The ships cannot overlap (i.e., only one ship can occupy any given square in the grid). The types and numbers of ships allowed are the same for each player. There are 4 types of ships in this simplified version of the game: Type of Ship Size (number of cells occupied) Number of distinct hits to sunk Aircraft Carrier 5 4 Battleship 3 2 Submarine 3 2 Patrol Boat 1 1 After the ships have been positioned, the game proceeds in a series of rounds. In each round, each player takes a turn to announce a target square in the opponent's grid which is to be shot at. The computer then announces whether or not the square is occupied by a ship. If all of a player's ships have been sunk, the game is over and their opponent wins. Implementation Your program will allow the users to choose whether they would like to play "Player vs Player" or "Single Player" mode. At the beginning, the computer will decide who is going to start the game randomly. The players will first choose the sizes of the grids they want to play with, and that will specify the number and types of ships as follows: MIDDLE EAST TECHNICAL UNIVERSITY, NORTHERN CYPRUS CAMPUS CNG140 C Programming – Programming Assignment 3

CNG140 Programming Assignment 3

Embed Size (px)

Citation preview

Page 1: CNG140 Programming Assignment 3

Date handed out: 18 April, 2014, Friday Date submission due: 02 May 2014, Friday

Programming Assignment 3: Battleships Purpose: The main purpose of this programming assignment is to revise the topics that we have covered so far including arrays, pointers, functions, repetitive statements, conditional statements and fundamentals of C programming. Description: You will write a program for playing a simplified version of the "Battleships" game. Your program should allow the game to be played between two players (in turns) or against the computer. Battleships is known as a guessing and/or pencil and paper game which dates from World War I. In this assignment, you will be implementing a simplified version of this game where you will keep the coordinates of our ships inside two-dimensional arrays. This will help you to practice with arrays, pointers and all of the three control constructs (sequence, selection and repetition). Similar to the previous assignment we recommend you not to try to compile your entire program in one "big bang." Compile it piece by piece. Test each piece that you have compiled to make sure it works correctly before you add the next piece. Please also do incremental submissions to LMS – Make sure you submit a version and then continue to upload a new version as you complete your assignment. Do not leave your submission to the very last minute. Battleships Description: The game will be played on two grids, one for each player. The grids are usually 10×10 square grids however in our implementation we are going to specify the size (number of cells) of the grids. The dimensions of the grid can be same or different (square or rectangle). The individual squares in the grid will be identified by two numbers. On each grid the players will arrange ships and record the shots of the opponent. Before play begins, each player will secretly arrange their ships on their grid. Each ship occupies a number of consecutive squares on the grid, arranged horizontally, vertically, or diagonally. The number of squares for each ship is determined by the type of the ship. The ships cannot overlap (i.e., only one ship can occupy any given square in the grid). The types and numbers of ships allowed are the same for each player. There are 4 types of ships in this simplified version of the game:

Type of Ship Size (number of cells occupied) Number of distinct hits to sunk Aircraft Carrier 5 4 Battleship 3 2 Submarine 3 2 Patrol Boat 1 1

After the ships have been positioned, the game proceeds in a series of rounds. In each round, each player takes a turn to announce a target square in the opponent's grid which is to be shot at. The computer then announces whether or not the square is occupied by a ship. If all of a player's ships have been sunk, the game is over and their opponent wins. Implementation Your program will allow the users to choose whether they would like to play "Player vs Player" or "Single Player" mode. At the beginning, the computer will decide who is going to start the game randomly. The players will first choose the sizes of the grids they want to play with, and that will specify the number and types of ships as follows:

MIDDLE EAST TECHNICAL UNIVERSITY, NORTHERN CYPRUS CAMPUS CNG140 C Programming – Programming Assignment 3

Page 2: CNG140 Programming Assignment 3

Size of Grid Ships 16 cells <Size <= 25 cells 1 Aircraft, 2 Battleship, 1 Submarine, 2 Patrol Boats 25 cells <Size <= 64 cells 2 Aircraft, 2 Battleship, 2 Submarine, 2 Patrol Boats 64 cells < Size <=100 cells 5 Aircraft, 7 Battleship, 3 Submarine, 5 Patrol Boats The size of the grid cannot be less than 16 and greater than 100, and computer specifies the number and types of ships to play with using the table above . Deployment of Ships In "Player vs Player" mode deploy_player function will be called for each user. This function will ask users for the positions of each ship chosen at the beginning of the game. The position will be the centre of the ship. Then a submenu will ask for the directions. The ships may be placed vertically, horizontally or diagonally as in the following examples. Please note that the illustration below is to show the options we have to place the ships. They do not represent the whole grid.

Player 1: Where would you like to allocate your Submarine 1 (x,y)? 3, 3 Choose the directions (v for vertical, h for horizontal, u for diagonal upward, d for diagonal downward): v . . . In "single player" mode the computer will choose the coordinates and the directions randomly. Note that if the battleships overlap for the players, the program will issue a warning and ask for re-entering the position of the ships. In case there are overlaps for the computer, while the coordinates are chosen randomly (explained below), another set of random coordinates will be generated (they will be sorted without prompting any messages). Shooting Each player is going to shoot in turns. Before and after the shootings the last version of grids will be shown. The program should not allow the players to shoot at the same coordinates more than once.

Page 3: CNG140 Programming Assignment 3

Player1s TURN ----------------------

Player1 , where would you like to shoot (x,y)? 2, 3 It is a hit (sheep number 3)

Page 4: CNG140 Programming Assignment 3

Computers TURN ----------------------

Computer shoots: 2, 3 It misses (0)

Page 5: CNG140 Programming Assignment 3

In case a sheep is sunk (check number of hits to sunk), the whole body will become visible using the sheep number. Lets assume that in Player vs Player mode, Battleship with ship number 3, was hit 2 times.

If all of a player's ships are sunk, the game will be over. The program will then show the winning player and how s/he initially deployed his/her ships. Programming Requirements In order to implement this game you will need to write at least the following functions, but if you need more functions you can add them. deploy-player () – This function will implement the initial part of the game where the ships are deployed asking necessary questions. deploy_computer() – In case of "single player mode" the computer will randomly deploy the ships. For these two functions it is essential to have a mechanism to control potential overlap of ships. shoot_player() – Required questions will be asked to specify the coordinates of shooting, and necessary functions will be implemented following the rules and number of hits needed to sunk each type of ship. shoot_computer() – This function will introduce the shots of the computer. The computer will shoot randomly each time. In case the random coordinate chosen is already used, another random coordinate should be generated. In other words computer should not try to shoot the same coordinate more than once. Grading Schema: Your program will be graded as follows:

Grading Point Mark (10) Allowing the user to decide the mode of the game 1 Maintaining the state of the grids without violating the rules of the game as well as the rules of the assignment

3

Player functions for deployment 1 Player functions for shooting 1 Computer deployment function 1 Computer shooting function 1 Code quality (e.g., variable names, formulation of selection statements and loops, etc)

2

Page 6: CNG140 Programming Assignment 3

Rules: Please make sure that you follow the restrictions for the assignment as follows. • You are not allowed to use global variables. • Name your source file “Battleships.c” • Upload only source file. Do not compress it (zip, rar, …) • Late submission is not allowed. • Some students will be asked to demonstrate their code and if they cannot demonstrate or answer the

questions asked then they will get zero for that related part. If they do not show up for the demonstration then they will get zero for this assignment.