Giter VIP home page Giter VIP logo

c_cpp's People

Watchers

Vitor Frango  avatar

c_cpp's Issues

Class to battleship

#include
#include

using namespace std;

const int BOARD_SIZE = 10;

// Enum for the different types of ships
enum ShipType {
Carrier,
Battleship,
Cruiser,
Destroyer
};

// Class for a ship
class Ship {
public:
// Constructor that takes the type of the ship
// and its position on the board as arguments
Ship(ShipType type, int x, int y) : type_(type), x_(x), y_(y) {}

// Getters for the type, x, and y properties
ShipType getType() const { return type_; }
int getX() const { return x_; }
int getY() const { return y_; }

private:
ShipType type_; // Type of the ship
int x_; // X coordinate of the ship
int y_; // Y coordinate of the ship
};

// Class for a battleship game board
class Board {
public:
// Constructor that initializes the board with the four ships
Board() {
ships_.push_back(Ship(Carrier, 0, 0));
ships_.push_back(Ship(Battleship, 2, 2));
ships_.push_back(Ship(Cruiser, 4, 4));
ships_.push_back(Ship(Destroyer, 6, 6));
}

// Function to draw the board with the ships on it
void draw() {
// Loop through all rows and columns of the board
for (int y = 0; y < BOARD_SIZE; y++) {
for (int x = 0; x < BOARD_SIZE; x++) {
// If there is a ship at the current position,
// draw the ship on the board
if (hasShipAt(x, y)) {
cout << "S ";
} else {
cout << ". ";
}
}
cout << endl;
}
}

private:
vector ships_; // Vector of ships on the board

// Function to check if there is a ship at a given position
bool hasShipAt(int x, int y) {
// Loop through all ships on the board
for (const auto& ship : ships_) {
// If a ship is found at the given position, return true
if (ship.getX() == x && ship.getY() == y) {
return true;
}
}

// If no ship is found at the given position, return false
return false;

}
};

// Main function to create a board and draw it
int main() {
Board board;
board.draw();
return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.