Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

Programming Assignment: Basic Game of Battleship

I. Learner Objectives:

At the conclusion of this programming assignment, participants should be able to:

Apply and implement pointers 2-dimenional arrays

Define and apply structs in C

II. Prerequisites:

Before starting this programming assignment, participants should be able to:

Apply and implement pointers in C

Pass output parameters to functions

Analyze a basic set of requirements and apply top-down design principles for a problem

Apply repetition structures within an algorithm

Construct while (), for (), or do-while () loops in C

Compose C programs consisting of sequential, conditional, and iterative statements

Eliminate redundancy within a program by applying loops and functions

Create structure charts for a given problem

Open and close files

Read, write to, and update files

Manipulate file handles

Apply standard library functions: fopen (), fclose (), fscanf (), and fprintf ()

Compose decision statements ("if" conditional statements)

Create and utilize compound conditions

Summarize topics from Hanly&Koffman Chapter 8 including:

What is an array?

Distinguishing between single dimensional and 2-dimentional arrays

What is an index?

III. Overview & Requirements:

Write a program that simulates the game of Battleship. The game will be completely text-based (see Sample Execution). Battleship is a two player Navy game. The objective of the game is to sink all ships in your enemy's fleet. The Player to sink his/her enemy's fleet first wins. Both players' fleets consist of 5 ships that are hidden from the enemy. Each ship may be differentiated by its "size" (besides the Cruiser and Submarine) or number of cells it expands on the game board. The Carrier has 5 cells, Battleship has 4 cells, Cruiser has 3 cells, Submarine has 3 cells, and the Destroyer has 2 cells.

The program should be built such that the user is Player1 and the computer is Player2. Two boards exist within the game. Hint: each board should be implemented as a 2-dimensional array. Each 2-dimensional array should be 10 X 10. One represents Player1's board and one represents Player2's board. At the beginning of the game each Players' game board should be initialized to all '-' indicating that no ships have been placed on either board. Before the game starts, Player1 should have the option to either manually place each of the 5 ships in his/her fleet or to have them randomly placed on the board. If Player1 decides to place the ships manually, then he/she should be prompted to place the Carrier first, Battleship second, Cruiser third, Submarine fourth, and the Destroyer last. Note that ships cannot be placed diagonally on the board, they can only be placed vertically or horizontally. You program must check to see if the user tries to place a ship outside the boundaries of the board or on top of a ship that has already been placed. Each cell on the board that contains part of the ship must be indicated by 'c' for Carrier, 'b' for Battleship, 'r' for Cruiser, 's' for Submarine, or 'd' for Destroyer. For example, if the Carrier was placed then the board should contain 5 'c' s for each cell on the board that has a piece of the Carrier, etc. Once Player1's ships have been placed, Player2's ships must be randomly placed. Note that the placement of Player2's ships must be unknown. Thus, Player2's board will only display '-' in each cell after the placement of each ship. The program should randomly select Player1 or Player2 to go first.

Once it has been decided on which player goes first, the game starts. Whenever it's Player1's turn, a prompt should be displayed asking for a position to target (specifying where to "shoot") on the enemy's (Player2's) board (2-dimensional array). The position should be specified in terms of a row and a column on the board. The row and column should always be displayed along with the board. If the position specified happens to hit a ship, then a '*' should replace the '-' on Player2's board. If the positioned specified misses any one of the ships in the fleet, then a 'm' should replace the '-' on Player2's board. Note that from turn-to-turn each player should NOT be allowed to enter the same position. Also, between turns clear the screen (system("cls")). In one turn, a player can only take one shot at the enemy's (other player's) fleet. When Player2 takes a shot at Player1's board, each hit should be specified with a '*' and each miss with a 'm' on Player1's board. The game is over win Player1 or Player2 has sunk all of the ships in the fleet of the enemy.

For each move made by Player1 and Player2, the results should be echoed to a file called "battleship.log". In this file, you should log the targeted position by each player on each move and whether it was a hit on one of the ships in the fleet. Also, if one of the ships happens to sink, then note this in the log file. For more information about the rules of Battleship visit: Rules of Battleship.

At the end of the game, Player1's and Player2's statistics should be written to "battleship.log". The stats include total number of hits, total number of misses, total number of shots, hits to misses ratio (as a percentage), and won or lost the game. Note that the statistics should be placed into a structure called Stats. You need two variables of type Stats, one for Player1 and one for Player2. Once the game has ended you should write the contents of each struct variable to the "battleship.log" file.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M91082295
  • Price:- $70

Priced at Now at $70, Verified Solution

Have any Question?


Related Questions in C/C++

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As