Refractor code
This commit is contained in:
parent
24e11d14ff
commit
ca192db02f
|
@ -0,0 +1,138 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
using std::cin;
|
||||||
|
using std::cout;
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#define CLEAR system("cls")
|
||||||
|
#else
|
||||||
|
#define CLEAR system("clear")
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void printmatrix(int matrix[6][6])
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
for (int ii = 0; ii < 6; ii++)
|
||||||
|
cout << matrix[i][ii] << " ";
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void check(int matrix[6][6], int &j)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
for (int ii = 0; ii < 6; ii++)
|
||||||
|
if (matrix[i][ii] == 0) j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void getfirecoordinates(int &r, int &c)
|
||||||
|
{
|
||||||
|
do {
|
||||||
|
cout << "\n\nFire at ROWS: ";
|
||||||
|
cin >> r;
|
||||||
|
} while (r < 0 || r > 6);
|
||||||
|
|
||||||
|
do {
|
||||||
|
cout << "\nFire at COLUMNS: ";
|
||||||
|
cin >> c;
|
||||||
|
} while (c < 0 || c > 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int p1[6][6];
|
||||||
|
int p2[6][6];
|
||||||
|
|
||||||
|
int r, c; // Collects coordinates to fire
|
||||||
|
int j; // Counter used to check for victory
|
||||||
|
|
||||||
|
// Seeds the pseudo RNG with the current time
|
||||||
|
srand((unsigned int)time(NULL));
|
||||||
|
restart:
|
||||||
|
|
||||||
|
CLEAR;
|
||||||
|
cout << "\n▂▃▄▅▆▇█▓▒░ CXXBattleShip ░▒▓█▇▆▅▄▃▂";
|
||||||
|
cout << "\n\nWelcome!";
|
||||||
|
cout << "\n\n(1) Start [2P mode]\n\n(2) Credits\n\n(3) Exit...\n";
|
||||||
|
cin >> j;
|
||||||
|
|
||||||
|
switch (j) {
|
||||||
|
case 1:
|
||||||
|
CLEAR;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
CLEAR;
|
||||||
|
cout << "\n▂▃▄▅▆▇█▓▒░ CXXBattleShip ░▒▓█▇▆▅▄▃▂";
|
||||||
|
cout << "\n\nDeveloped by: Borededdy\nSome minor help: MGislv";
|
||||||
|
cout << "\n\nThank you for playing this!";
|
||||||
|
cout << "\nIf you enjoyed it, please star our repo ;)";
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate 2d arrays
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
for (int ii = 0; ii < 6; ii++) {
|
||||||
|
p1[ii][i] = rand() % 2;
|
||||||
|
p2[ii][i] = rand() % 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
j = 0;
|
||||||
|
while (1) {
|
||||||
|
cout << "#-----Player 1-----#\n\n\n";
|
||||||
|
printmatrix(p1);
|
||||||
|
|
||||||
|
getfirecoordinates(r, c);
|
||||||
|
|
||||||
|
if (p2[r][c] == 1) {
|
||||||
|
cout << "\n\nPLAYER 1 > Scored a hit!\n";
|
||||||
|
p2[r][c] = 0;
|
||||||
|
} else {
|
||||||
|
cout << "\n\nPLAYER 1 > Missed!\n";
|
||||||
|
}
|
||||||
|
sleep(4);
|
||||||
|
|
||||||
|
CLEAR;
|
||||||
|
check(p2, j);
|
||||||
|
|
||||||
|
if (j == 36) {
|
||||||
|
cout << "PLAYER 1 > Wins!\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
j = 0;
|
||||||
|
|
||||||
|
cout << "#-----Player 2-----#\n\n\n";
|
||||||
|
printmatrix(p2);
|
||||||
|
|
||||||
|
getfirecoordinates(r, c);
|
||||||
|
|
||||||
|
if (p1[r][c] == 1) {
|
||||||
|
cout << "\n\nPLAYER 2 > Scored a hit!\n";
|
||||||
|
p1[r][c] = 0;
|
||||||
|
} else {
|
||||||
|
cout << "\n\nPLAYER 2 > Missed!\n";
|
||||||
|
}
|
||||||
|
sleep(4);
|
||||||
|
|
||||||
|
CLEAR;
|
||||||
|
check(p1, j);
|
||||||
|
|
||||||
|
if (j == 36) {
|
||||||
|
cout << "PLAYER 2 > Wins!";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "\n\nWant to restart? [0 = yes]\n";
|
||||||
|
cin >> j;
|
||||||
|
|
||||||
|
if (j == 0)
|
||||||
|
goto restart;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
Binary file not shown.
|
@ -1,103 +0,0 @@
|
||||||
#include <iostream>
|
|
||||||
#include <ctime>
|
|
||||||
#include <unistd.h>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
//Defining clear shortcut
|
|
||||||
#define CLEAR system("clear")
|
|
||||||
|
|
||||||
//Player Vars
|
|
||||||
int p1[4][4];
|
|
||||||
int p2[4][4];
|
|
||||||
|
|
||||||
void action(){
|
|
||||||
int r, c; //Collects coordinates to fire
|
|
||||||
int l = 0; //Counter used to check win
|
|
||||||
int e = 0; //End Match
|
|
||||||
while(e < 1){
|
|
||||||
cout << "#-----Player 1-----#\n\n\n";
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
cout << p1[ii][i] << " ";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
cout << "\n\nFire at ROWS: ";
|
|
||||||
cin >> r;
|
|
||||||
cout << "\nFire at COLUMNS: ";
|
|
||||||
cin >> c;
|
|
||||||
if (p2[r][c] == 1){
|
|
||||||
cout << "\n\nPLAYER 1 > Hit!\n";
|
|
||||||
sleep(6);
|
|
||||||
p2[r][c] = 0;
|
|
||||||
}else{
|
|
||||||
cout << "\n\nPLAYER 1 > Miss!\n";
|
|
||||||
sleep(6);
|
|
||||||
}
|
|
||||||
cout << CLEAR;
|
|
||||||
for(int c = 0; c < 4; c++){
|
|
||||||
for(int cc = 0; cc < 4; cc++){
|
|
||||||
if(p2[cc][c] == 0){
|
|
||||||
l++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(l == 16){
|
|
||||||
cout << "\n\nPLAYER 1 > Wins!\n";
|
|
||||||
sleep(6);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
l = 0;
|
|
||||||
cout << "#-----Player 2-----#\n\n\n";
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
cout << p2[ii][i] << " ";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
cout << "\n\nFire at ROWS: ";
|
|
||||||
cin >> r;
|
|
||||||
cout << "\nFire at COLUMNS: ";
|
|
||||||
cin >> c;
|
|
||||||
if (p1[r][c] == 1){
|
|
||||||
cout << "\n\nPLAYER 2 > Hit!\n";
|
|
||||||
sleep(6);
|
|
||||||
p1[r][c] = 0;
|
|
||||||
}else{
|
|
||||||
cout << "\n\nPLAYER 2 > Miss!\n";
|
|
||||||
sleep(6);
|
|
||||||
}
|
|
||||||
cout << CLEAR;
|
|
||||||
for(int c = 0; c < 4; c++){
|
|
||||||
for(int cc = 0; cc < 4; cc++){
|
|
||||||
if(p1[cc][c] == 0){
|
|
||||||
l++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(l == 16){
|
|
||||||
cout << "\n\nPLAYER 1 > Wins!";
|
|
||||||
sleep(6);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
l = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void charging(){
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
p1[ii][i] = rand() % 2;
|
|
||||||
p2[ii][i] = rand() % 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
//Casting of TIME for srand
|
|
||||||
srand((unsigned int) time(NULL));
|
|
||||||
|
|
||||||
charging();
|
|
||||||
action();
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
|
|
36
README.md
36
README.md
|
@ -1,29 +1,25 @@
|
||||||
# 🚢 CXXBattleShip
|
# 🚢 CXXBattleShip
|
||||||
|
|
||||||
A simple but functional terminal BattleShip game, written in c++
|
A simple yet functional terminal BattleShip game, written in C++
|
||||||
To Open the game follow the instructions down here or in the wiki.
|
To compile the game follow the instructions.
|
||||||
|
|
||||||
#### - Linux
|
You need a C++ compiler, e.g. Clang, GCC, MSVC...
|
||||||
|
|
||||||
In the terminal type: <br>
|
#### - Unix
|
||||||
`g++ battleship.cpp` <br>
|
|
||||||
make sure that you have installed **Clang** or any **C++ Compilator**<br>
|
|
||||||
After that done type: <br>
|
|
||||||
`./a.out` <br>
|
|
||||||
To launch the executible file.
|
|
||||||
|
|
||||||
#### - MacOS
|
On MacOS you can install **Xcode** from the store or using the terminal:
|
||||||
|
`xcode-select --install`
|
||||||
In the bash terminal install **command line tools** <br>
|
|
||||||
`xcode-select --install` <br>
|
Else use [Homebrew](https://brew.sh/)
|
||||||
Then in the terminal type:<br>
|
|
||||||
`g++ battleship.cpp` <br>
|
Then compile:
|
||||||
After that done launch the .exec file <br>
|
`cpp CXXBattleShip.cpp`
|
||||||
To launch the executible file or just **open that in the folder of the game**
|
And execute the generated file.
|
||||||
|
|
||||||
#### - Windows
|
#### - Windows
|
||||||
|
|
||||||
Download the Windows release (this contains more instructions just for windows :/ )<br>
|
Use Mingw/[MSYS2](https://www.msys2.org/) or MSVC
|
||||||
Go in the game folder and launch the `.exe` file. <br>
|
|
||||||
<br>
|
### Binaries are available in the release section
|
||||||
|
|
||||||
Enjoy the game :)
|
Enjoy the game :)
|
||||||
|
|
|
@ -1,103 +0,0 @@
|
||||||
#include <iostream>
|
|
||||||
#include <ctime>
|
|
||||||
#include <unistd.h>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
//Defining clear shortcut
|
|
||||||
#define CLEAR system("clear")
|
|
||||||
|
|
||||||
//Player Vars
|
|
||||||
int p1[4][4];
|
|
||||||
int p2[4][4];
|
|
||||||
|
|
||||||
int action(){
|
|
||||||
int r, c; //Collects coordinates to fire
|
|
||||||
int l = 0; //Counter used to check win
|
|
||||||
int e = 0; //End Match
|
|
||||||
while(e < 1){
|
|
||||||
cout << "#-----Player 1-----#\n\n\n";
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
cout << p1[ii][i] << " ";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
cout << "\n\nFire at ROWS: ";
|
|
||||||
cin >> r;
|
|
||||||
cout << "\nFire at COLUMNS: ";
|
|
||||||
cin >> c;
|
|
||||||
if (p2[r][c] == 1){
|
|
||||||
cout << "\n\nPLAYER 1 > Hit!\n";
|
|
||||||
sleep(6);
|
|
||||||
p2[r][c] = 0;
|
|
||||||
}else{
|
|
||||||
cout << "\n\nPLAYER 1 > Miss!\n";
|
|
||||||
sleep(6);
|
|
||||||
}
|
|
||||||
cout << CLEAR;
|
|
||||||
for(int c = 0; c < 4; c++){
|
|
||||||
for(int cc = 0; cc < 4; cc++){
|
|
||||||
if(p2[cc][c] == 0){
|
|
||||||
l++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(l == 16){
|
|
||||||
cout << "\n\nPLAYER 1 > Wins!\n";
|
|
||||||
sleep(6);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
l = 0;
|
|
||||||
cout << "#-----Player 2-----#\n\n\n";
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
cout << p2[ii][i] << " ";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
cout << "\n\nFire at ROWS: ";
|
|
||||||
cin >> r;
|
|
||||||
cout << "\nFire at COLUMNS: ";
|
|
||||||
cin >> c;
|
|
||||||
if (p1[r][c] == 1){
|
|
||||||
cout << "\n\nPLAYER 2 > Hit!\n";
|
|
||||||
sleep(6);
|
|
||||||
p1[r][c] = 0;
|
|
||||||
}else{
|
|
||||||
cout << "\n\nPLAYER 2 > Miss!\n";
|
|
||||||
sleep(6);
|
|
||||||
}
|
|
||||||
cout << CLEAR;
|
|
||||||
for(int c = 0; c < 4; c++){
|
|
||||||
for(int cc = 0; cc < 4; cc++){
|
|
||||||
if(p1[cc][c] == 0){
|
|
||||||
l++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(l == 16){
|
|
||||||
cout << "\n\nPLAYER 1 > Wins!";
|
|
||||||
sleep(6);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
l = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void charging(){
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
p1[ii][i] = rand() % 2;
|
|
||||||
p2[ii][i] = rand() % 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
//Casting of TIME for srand
|
|
||||||
srand((unsigned int) time(NULL));
|
|
||||||
|
|
||||||
charging();
|
|
||||||
action();
|
|
||||||
}
|
|
|
@ -1,103 +0,0 @@
|
||||||
#include <iostream>
|
|
||||||
#include <ctime>
|
|
||||||
#include <unistd.h>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
//Defining clear shortcut
|
|
||||||
#define CLEAR system("CLS")
|
|
||||||
|
|
||||||
//Player Vars
|
|
||||||
int p1[4][4];
|
|
||||||
int p2[4][4];
|
|
||||||
|
|
||||||
int action(){
|
|
||||||
int r, c; //Collects coordinates to fire
|
|
||||||
int l = 0; //Counter used to check win
|
|
||||||
int e = 0; //End Match
|
|
||||||
while(e < 1){
|
|
||||||
cout << "#-----Player 1-----#\n\n\n";
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
cout << p1[ii][i] << " ";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
cout << "\n\nFire at ROWS: ";
|
|
||||||
cin >> r;
|
|
||||||
cout << "\nFire at COLUMNS: ";
|
|
||||||
cin >> c;
|
|
||||||
if (p2[r][c] == 1){
|
|
||||||
cout << "\n\nPLAYER 1 > Hit!\n";
|
|
||||||
sleep(6);
|
|
||||||
p2[r][c] = 0;
|
|
||||||
}else{
|
|
||||||
cout << "\n\nPLAYER 1 > Miss!\n";
|
|
||||||
sleep(6);
|
|
||||||
}
|
|
||||||
cout << CLEAR;
|
|
||||||
for(int c = 0; c < 4; c++){
|
|
||||||
for(int cc = 0; cc < 4; cc++){
|
|
||||||
if(p2[cc][c] == 0){
|
|
||||||
l++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(l == 16){
|
|
||||||
cout << "\n\nPLAYER 1 > Wins!\n";
|
|
||||||
sleep(6);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
l = 0;
|
|
||||||
cout << "#-----Player 2-----#\n\n\n";
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
cout << p2[ii][i] << " ";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
cout << "\n\nFire at ROWS: ";
|
|
||||||
cin >> r;
|
|
||||||
cout << "\nFire at COLUMNS: ";
|
|
||||||
cin >> c;
|
|
||||||
if (p1[r][c] == 1){
|
|
||||||
cout << "\n\nPLAYER 2 > Hit!\n";
|
|
||||||
sleep(6);
|
|
||||||
p1[r][c] = 0;
|
|
||||||
}else{
|
|
||||||
cout << "\n\nPLAYER 2 > Miss!\n";
|
|
||||||
sleep(6);
|
|
||||||
}
|
|
||||||
cout << CLEAR;
|
|
||||||
for(int c = 0; c < 4; c++){
|
|
||||||
for(int cc = 0; cc < 4; cc++){
|
|
||||||
if(p1[cc][c] == 0){
|
|
||||||
l++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(l == 16){
|
|
||||||
cout << "\n\nPLAYER 1 > Wins!";
|
|
||||||
sleep(6);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
l = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void charging(){
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
for(int ii = 0; ii < 4; ii++){
|
|
||||||
p1[ii][i] = rand() % 2;
|
|
||||||
p2[ii][i] = rand() % 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
//Casting of TIME for srand
|
|
||||||
srand((unsigned int) time(NULL));
|
|
||||||
|
|
||||||
charging();
|
|
||||||
action();
|
|
||||||
}
|
|
Binary file not shown.
Loading…
Reference in New Issue