Commit 0d23f869 authored by Leopold Keller's avatar Leopold Keller
Browse files

commit

parent df76b5d7
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
\ No newline at end of file
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
\ No newline at end of file
......@@ -2,22 +2,29 @@
#include <iostream>
#include <iomanip>
#include <random>
#include "ship.h"
using namespace std;
using std::array;
GameBoard::GameBoard(){
for(int i=0; i<10;i++){
for(int j=0; j<10;j++)
m_enemyBoard[i][j]='.';
GameBoard::GameBoard()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
m_enemyBoard[i][j] = '.';
}
}
void GameBoard::printBoard()
{
for (int jt= 0; jt<= m_enemyBoard.size(); jt++){
for (int it = 0; it < 10; it++)
{
for (int kt = 0; kt < 10; kt++)
{
}
}
}
void GameBoard::printEnemyBoard()
{
for (int it = 0; it < 10; it++)
{
for (int kt = 0; kt < 10; kt++)
......@@ -33,184 +40,25 @@ for (int jt= 0; jt<= m_enemyBoard.size(); jt++){
}
}
cout << "\n\n";
}
void GameBoard::randomPlaceShips(){
random_device rnd_device;
mt19937 mersenne_engine{ rnd_device() };
uniform_int_distribution<int> dist{ 0, 9 };
uniform_int_distribution<int> dist2{ 0, 3 };
auto gen = [&dist, &mersenne_engine]() {
void GameBoard::randomPlaceShips()
{
random_device rnd_device;
mt19937 mersenne_engine{rnd_device()};
uniform_int_distribution<int> dist{0, 9};
uniform_int_distribution<int> dist2{0, 3};
auto gen = [&dist, &mersenne_engine]()
{
return dist(mersenne_engine);
};
auto gen2 = [&dist2, &mersenne_engine]() {
auto gen2 = [&dist2, &mersenne_engine]()
{
return dist2(mersenne_engine);
};
int finalx;
int finaly;
Direction finaldir;
//Dreadnought
while(true){
int x=gen();
int y=gen();
int direction=gen2();
Direction dir;
if(direction==0) dir=Direction::north; else if(direction==1)dir=Direction::east; else if(direction==2) dir=Direction::south; else if(direction==3) dir=Direction::west;
if(dir==Direction::north && y-4 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y-1]=='.' && m_enemyBoard[x][y-2]=='.' && m_enemyBoard[x][y-3]=='.' && m_enemyBoard[x][y-4]=='.'){ m_ships[0] = Ship(x,y,5,Direction::north); break; finalx=x;finaly=y; finaldir=dir;}
if(dir==Direction::west && x-4 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x-1][y] == '.' && m_enemyBoard[x-2][y] == '.' && m_enemyBoard[x-3][y] == '.' && m_enemyBoard[x-4][y] == '.'){ m_ships[0] = Ship(x,y,5,Direction::west); break; finalx=x;finaly=y; finaldir=dir;}
if(dir==Direction::east && x+4 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x+1][y] == '.' && m_enemyBoard[x+2][y] == '.' && m_enemyBoard[x+3][y] == '.' && m_enemyBoard[x+4][y] == '.'){ m_ships[0] = Ship(x,y,5,Direction::east); break; finalx=x;finaly=y; finaldir=dir;}
if(dir==Direction::south && y+4 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y+1] == '.' && m_enemyBoard[x][y+2] == '.' && m_enemyBoard[x][y+3] == '.' && m_enemyBoard[x][y+4] == '.'){ m_ships[0] = Ship(x,y,5,Direction::south); break; finalx=x;finaly=y; finaldir=dir;}
}
for(int i=0; i<5; i++){
m_enemyBoard[finalx][finaly]= '1';
if(finaldir==Direction::north) finaly--;
if(finaldir==Direction::south) finaly++;
if(finaldir==Direction::east) finalx++;
if(finaldir==Direction::west) finalx--;
}
//Cruisers
for(int i=2; i<=3;i++){
while(true){
int x=gen();
int y=gen();
int direction=gen2();
Direction dir;
if(direction==0) dir=Direction::north; else if(direction==1)dir=Direction::east; else if(direction==2) dir=Direction::south; else if(direction==3) dir=Direction::west;
if(dir==Direction::north && y-3 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y-1]=='.' && m_enemyBoard[x][y-2]=='.' && m_enemyBoard[x][y-3]=='.'){ m_ships[i] = Ship(x,y,4,Direction::north);
for(int j=0; j<4; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;
if(dir==Direction::west && x-3 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x-1][y] == '.' && m_enemyBoard[x-2][y] == '.' && m_enemyBoard[x-3][y] == '.'){ m_ships[i] = Ship(x,y,4,Direction::west);
for(int j=0; j<4; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
if(dir==Direction::east && x+3 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x+1][y] == '.' && m_enemyBoard[x+2][y] == '.' && m_enemyBoard[x+3][y] == '.'){ m_ships[i] = Ship(x,y,4,Direction::east);
for(int j=0; j<4; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
if(dir==Direction::south && y+3 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y+1] == '.' && m_enemyBoard[x][y+2] == '.' && m_enemyBoard[x][y+3] == '.' ){ m_ships[i] = Ship(x,y,4,Direction::south);
for(int j=0; j<4; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
}
}
}
//Destroyers
for(int i=4; i<=6;i++){
while(true){
int x=gen();
int y=gen();
int direction=gen2();
Direction dir;
if(direction==0) dir=Direction::north; else if(direction==1)dir=Direction::east; else if(direction==2) dir=Direction::south; else if(direction==3) dir=Direction::west;
if(dir==Direction::north && y-2 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y-1]=='.' && m_enemyBoard[x][y-2]=='.'){ m_ships[i] = Ship(x,y,3,Direction::north);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;
if(dir==Direction::west && x-2 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x-1][y] == '.' && m_enemyBoard[x-2][y] == '.'){ m_ships[i] = Ship(x,y,3,Direction::west);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
if(dir==Direction::east && x+2 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x+1][y] == '.' && m_enemyBoard[x+2][y] == '.'){ m_ships[i] = Ship(x,y,3,Direction::east);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
if(dir==Direction::south && y+2 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y+1] == '.' && m_enemyBoard[x][y+2] == '.'){ m_ships[i] = Ship(x,y,3,Direction::south);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
}
}
}
//Submarines
for(int i=7; i<=10;i++){
while(true){
int x=gen();
int y=gen();
int direction=gen2();
Direction dir;
if(direction==0) dir=Direction::north; else if(direction==1)dir=Direction::east; else if(direction==2) dir=Direction::south; else if(direction==3) dir=Direction::west;
if(dir==Direction::north && y-1 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y-1]=='.'){ m_ships[i] = Ship(x,y,2,Direction::north);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;
if(dir==Direction::west && x-1 >=0 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x-1][y] == '.'){ m_ships[i] = Ship(x,y,2,Direction::west);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
if(dir==Direction::east && x+1 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x+1][y] == '.'){ m_ships[i] = Ship(x,y,2,Direction::east);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
if(dir==Direction::south && y+1 <=9 && m_enemyBoard[x][y] == '.' && m_enemyBoard[x][y+1] == '.'){ m_ships[i] = Ship(x,y,2,Direction::south);
for(int j=0; j<3; j++){
m_enemyBoard[x][y]= (char)i;
if(dir==Direction::north) y--;
if(dir==Direction::south) y++;
if(dir==Direction::east) x++;
if(dir==Direction::west) x--;
}
break;}
}
}
}
}
\ No newline at end of file
}
#include "part.h"
bool Part::isDamaged() const{
return m_status;
}
void Part::setDamaged(){
m_status=1;
}
int Part::getRow() const{
return m_row;
}
int Part::getCol() const{
return m_col;
}
Part::Part(int row, int col):m_row(row),m_col(col),m_status(0){
}
\ No newline at end of file
#include "player.h"
Player::Player(const string &playerName) : m_playerName(playerName)
{
}
int Player::getGamesWon() const
{
return m_gamesWon;
}
int Player::getGamesLost() const
{
return m_gamesLost;
}
int Player::getGamesPlayed() const
{
return m_gamesPlayed;
}
void Player::addGameWon()
{
m_gamesWon++;
}
void Player::addGameLost()
{
m_gamesLost++;
}
string Player::getName()
{
return m_playerName;
}
\ No newline at end of file
......@@ -80,6 +80,7 @@ class Player
string getName();
private:
int m_gamesPlayed;
/**
* @brief Number of games won.
*/
......
#include "ship.h"
#include "part.h"
#include <stdexcept>
bool Ship::hasPartIn(int row, int col)
......@@ -41,4 +42,48 @@ bool Ship::isSunk()
return false;
}
return true;
}
\ No newline at end of file
}
Ship::Ship()
{
}
Ship::Ship(int row, int col, int lengthOfShip, Direction direction)
{
if (col > 9 || col < 0 || row > 9 || row < 0)
throw std::invalid_argument("Part exceeds Gameboard Range!");
if (direction == Direction::north && row - lengthOfShip >= 0)
{
for (int i = 0; i < lengthOfShip - 1; i++)
{
m_parts.push_back(Part(row, col));
row--;
}
}
if (direction == Direction::south && row + lengthOfShip <= 9)
{
for (int i = 0; i < lengthOfShip - 1; i++)
{
m_parts.push_back(Part(row, col));
row++;
}
}
if (direction == Direction::east && col + lengthOfShip <= 9)
{
for (int i = 0; i < lengthOfShip - 1; i++)
{
m_parts.push_back(Part(row, col));
col++;
}
}
if (direction == Direction::west && col - lengthOfShip >= 0)
{
for (int i = 0; i < lengthOfShip - 1; i++)
{
m_parts.push_back(Part(row, col));
col--;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment