Question

i need to to create a battleship game in c++ using 2d array and functions the...

i need to to create a battleship game in c++ using 2d array and functions

the pograms has to be 10*10 and have 2 enemy ship and 2 friendly ships each taking 3 spaces randomly placed

first the program would display the user the full ocean and the issuer would need to input cordinates and much check that the corinates are correct if not ask again . when a the two enemies ships are drestroyed the game ends or if one friendly ship is destroy the game ends

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <cstdlib>
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <string>
using std::string;
using std::cout;
using std::cin;
const int empty=0;
const int occupied=1;
const int hit=2;
const int miss=3;
int Ocean[10][10];
void Initialize_Ocean()
{
for (int r = 0; r < 10; r++)
{
for (int c = 0; c < 10; c++)
{
if (rand()%17 < 1)
{
Ocean[r][c]=occupied;
}
else
{  
Ocean[r][c]=empty;
}
}
}
}
int getNumberOfShips()
{
int number = 0;
for (int r = 0; r < 10; r++)
{
for (int c = 0; c < 10; c++)
{
if ((Ocean[r][c]==occupied) && !(Ocean[r][c]==hit))
{
number++;
}
}
}
return number;
}
string Draw_Ocean()
{
string s = "Number of ships left: ";
char buffer[30];
s += itoa(getNumberOfShips(), buffer, 10);
s += "\n ";
for (int i = 0; i < 10; i++)
{
s += itoa((i % 10), buffer,10);
}
s += "\n +";
for (int i = 0; i < 10; i++)
{
s += "-";
}
s += "+\n";
for (int r = 0; r < 10; r++)
{
s += itoa((r % 10), buffer,10);
s += "|";
for (int c = 0; c < 10; c++)
{
if (Ocean[r][c] == empty)
{
s += " ";
}
else if (Ocean[r][c] == occupied)
{
s += " ";
}
else if (Ocean[r][c] == hit)
{
s += "x";
}
else if (Ocean[r][c] == miss)
{
s += "#";
}
}
s += "|\n";
}
s += " +";
for (int i = 0; i < 10; i++)
{
s += "-";
}
s += "+\n";
return s;
}
bool fire_missile(int r, int c)
{
if (r >= 0 && r < 10 && c >= 0 && c < 10)
{
if (Ocean[r][c]==occupied)
{
Ocean[r][c]=hit;
return true;
}
else
{
Ocean[r][c]=miss;
return false;
}
}
return false;
}

void PlayGame()
{
int row=0, column=0;
string msg;
bool update = false;
system("cls");
cout << "Welcome to battleship!\nThe playing field is a 10x10 grid.\nEnter -1 to exit the game.\n";
cout << Draw_Ocean();
while (column != -1)
{
if (update)
{
system("cls");
cout <<Draw_Ocean();
update = false;
}
cout << ("=============\n" + msg + "\n");
if (getNumberOfShips() == 0)
{
cout << ("Congratulations! All ships are hit! You win 1 player battleship!");
break;
}
cout << "Choose a column:";
cin >> column;
if (column == -1)
{
break;
}
cout << "Choose a row: ";
cin >> row;
if (row == -1)
{
break;
}
if (fire_missile(row, column))
{
msg = "Battleship Sunk!";
}
else
{
msg = "Miss!";
}
//msg = "Missile Launched. Ships left: " + getNumberOfShips() + ".";
update = true;
}
}
void main()
{
srand((unsigned)time(0));
Initialize_Ocean();
PlayGame();
cout << "Bye!";
}

Add a comment
Know the answer?
Add Answer to:
i need to to create a battleship game in c++ using 2d array and functions the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Imagine we are using a two-dimensional array as the basis for creating the game battleship. In...

    Imagine we are using a two-dimensional array as the basis for creating the game battleship. In the game of battleship a '~' character entry in the array represents ocean, a '#' character represents a place ion the ocean where part of a ship is present, and an 'H' character represents a place in the ocean where part of a ship is present and has been hit by a torpedo. Thus, a ship with all 'H' characters means the ship has...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

  • In C++ program use the new style od C++ not the old one. Simple Battleship You...

    In C++ program use the new style od C++ not the old one. Simple Battleship You will make a game similar to the classic board game Battleship. You will set up a 5 x 5, 2 dimensional array. In that array, you will use a random number generator to select 5 elements that will act as the placeholders for your "battleships". Your user will get 10 guesses to "seek and destroy" the battleships. After their 10 guesses, you will tell...

  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...

    C++ Project - Create a memory game in c++ using structs and pointers. For this exercise, you will create a simple version of the Memory Game. You will again be working with multiple functions and arrays. You will be using pointers for your arrays and you must use a struct to store the move and pass it to functions as needed. Program Design You may want to create two different 4x4 arrays. One to store the symbols the player is...

  • i need this in C# please can any one help me out and write the full...

    i need this in C# please can any one help me out and write the full code start from using system till end i am confused and C# is getting me hard.I saw codes from old posts in Chegg but i need the ful complete code please thanks. Module 4 Programming Assignment – OO Design and implementation (50 points) Our Battleship game needs to store a set of ships. Create a new class called Ships. Ships should have the following...

  • C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The...

    C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • python code( using functions please)! Rules of the game: - This game requires a dice. The...

    python code( using functions please)! Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT