Coder Social home page Coder Social logo

battleship-game's Introduction

Battleship Game

This is a C++ implementation of the classic Battleship game. Two players place ships on a 10x10 grid and take turns firing at each other's ships.

Rules

Each player places 5 ships on their grid:

  • 1 x 4 cell ship
  • 2 x 3 cell ships
  • 3 x 2 cell ships
  • 4 x 1 cell ships

Players alternate turns firing at a grid coordinate

If a ship is hit, the player marks it on their tracking grid

The first player to sink all enemy ships wins

Code Overview

The core logic is in main():

  • a[][] - Player 1's ocean grid
  • pc[][] - Player 2's ocean grid
  • pcn[][] - Player 2's tracking grid

Ships are pre-populated for simplicity.

Game loop:

  1. Player 1 fires shot at pc[][]
  • Check for hit or miss and mark pcn[][]
  1. Check if Player 2 ships sunk. If yes, Player 1 wins
  2. Player 2 fires shot at a[][]
  • Check for hit or miss
  1. Check if Player 1 ships sunk. If yes, Player 2 wins
  2. Repeat turns until a player wins

win() function checks if all ships sunk by analyzing the ocean grid.

Input validation ensures shots are at unused spots.

Usage

  • Compile and run Battleship.cpp
  • Follow prompts to play

Enhancements

  • Allow players to position their own ships
  • Improve input validation
  • Add difficulty levels
  • Enhance UI and graphics

Conclusion

This demonstrates a basic C++ implementation of the game using 2D arrays, functions, and other language features.

Let me know if any part of the code needs further explanation!

Fundamentals-Computer-and-Programming

Fourth Project Computer and Programming Fundamentals

More info in pdf file Attached

https://en.wikipedia.org/wiki/Battleship_(game)

https://battleship-game.org/en/

https://telegram.me/nabardbot

قوانین بازی

  1. در حین بازی، هیچ کدام از بازیکنان محل کشتی های رقیبشان را نمی بینید.

1۰ است. × 2. ابعاد نقشه ی بازی 1۰

  1. هر بازیکن 1۰ کشتی از قرار زیر دارد: نام طول تعداد ناو 1 4 رزم ناو 2 3 ناوشکن 3 2 زیر دریایی 4 1

  2. هر کشتی می تواند به صورت اعمودی و یا افقی در صفحه قرار بگیرد.

  3. هر کشتی به اندازه ی یک خانه اطراف خود حریم دارد و کشتی دیگری نباید در آن فضا قرار بگیرد.

  4. هر بازیکن در نوبت خود به یکی از خانه های نقشه ی حریف شلیک می کند و مادامی که شلیکش یک شلیک موفق باشد

تصویر 1: نمایش حریم ناو پروژه ی چهارم مبانی برنامه سازی کشتی جنگی) ) Battleship بازی می تواند به شلیک کردن ادامه دهد.

  1. بازیکن این امکان را دارد که خودش کشتی هایش را در صفحه بچیند.

  2. پس از هر شلیک موفق، خانه هایی که ممکن نیست ادامه ی کشت ی ی اصابت شده در آن ها باشد در نقشه مشخص می شوند. این خانه ها بخشی از حریم کشتی هستند که در راستای شمالی-جنوبی و یا شرقی-غربی، که احتمال دارد ادامه ی کشتی در آن جا باشد، قرار ندارند. برای روشن شدن موضوع تصویر زیر را ببینید. در حقیقت این خانه ها به اعنوان خانه هایی که مورد اصابت قرار گرفته اند ولی خالی بودند مشخص می شوند.

  3. بازی تا جایی ادامه پیدا می کند که یکی از بازیکنان تمامی کشتی های بازیکن دیگر را غرق کرده باشد. در انتهای بازی و با ااعلم برنده ، بازنده از محل کشتی های برنده آگاه می شود. (هر دو نقشه به صورت کامل نشان داده می شوند.)

battleship-game's People

Contributors

varaste avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.