Skip to content

A basic multiplayer game of Battleship written in C language without the use of any external libraries.

Notifications You must be signed in to change notification settings

andr3cruz/battleship-in-c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

battleship-in-c

Introduction

A basic multiplayer game of Battleship written in C language without the use of any external libraries.

Made as a project for Programming Lab (CC 2013)

Command Line

The game can be compiled and ran via terminal using a simple Makefile script

If you wish to use a more time efficient matrix structure for the board use the command:

make MATRIX
./battleship

If you wish to use a more memory efficient PR Quadtree structure for the board use the command:

make QUAD
./battleship

Game

In this first implementation the game is played with 2 players sharing the same terminal.

When you run the game you will be welcomed by the start screen

Welcome to Battleship in C!

and asked input on the size of the board and how many of each type of ship to play with.

There are currently 6 types of ships available: Carrier, Battleship, Cruiser, Submarine, Destroyer and T_ship.

~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~
~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ O O O ~
O O O O O   O O O O ~   ~ O O O ~   ~ O O O ~   ~ O O ~ ~   ~ ~ O ~ ~
~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ O ~ ~
~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~   ~ ~ ~ ~ ~

Then you will be asked if you want to want to deploy your ships MANUALLY or RANDOMLY

If you choose to deploy ships MANUALLY you will be prompted with the following screen:

PLAYER 1, place your ships:

     1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20
 1 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |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~ | ~ | ~ | ~ | ~ | ~ | ~ |
14 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
15 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
16 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
17 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
18 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
19 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
20 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |

~ ~ ~ ~ ~
~ ~ ~ ~ ~
O O O O O
~ ~ ~ ~ ~
~ ~ ~ ~ ~

1) Rotate ship left
2) Rotate ship right
3) Place ship

here you can rotate your ships however you like and place them on the board.

If you choose to deploy ships RANDOMLY all the ships will be placed by the program.

Once you deploy all of your ships or choose to have it done for you, you'll be presented with the turn taking screen

WAITING FOR PLAYER 1
Press <ENTER> to continue!

the purpose of this screen is for the each player to not see each other's board and cheat when switching, due to it being played in the same terminal.

Once you press ENTER you will be presented with your boards and asked for input on the coordinates of where you want to attack

     1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20
 1 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
 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 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
19 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | O | O | O | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |
20 | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ | ~ |

Player 1's turn!
Type the numerical coordinate X of where you want to ATTACK

each character defines a state on the board:

  • ~ is WATER
  • X is a HIT
  • M is a MISS
  • O is a SHIP

Game finishes once a player sinks all of the other player's ships!

About

A basic multiplayer game of Battleship written in C language without the use of any external libraries.

Resources

Stars

Watchers

Forks

Packages

No packages published