Skip to content

Commit

Permalink
Artificial enemy added (#2)
Browse files Browse the repository at this point in the history
* "AI" intro

* minor player logic fix

* AE crucial logic fix
  • Loading branch information
JJ-Kira authored Jan 5, 2024
1 parent 6046a0e commit 6552f17
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 37 deletions.
185 changes: 185 additions & 0 deletions Battleships/ArtificialEnemy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cstring>
#include "Display.h"
#include "ArtificialEnemy.h"

using namespace std;

ArtificialEnemy::ArtificialEnemy(const std::string& name) : Player(name) {}

void ArtificialEnemy::PlayerStart()
{
cout << "----------" << endl;
cout << Name << " (AI):" << endl;
cout << "----------" << endl;
cout << endl;

cout << "Wait a minute for you enemy to place their vessels." << endl;

for (int i = 0; i < SHIPS_AMOUNT; i++)
{
Battleship& battleship = battleships[i];

cout << endl << "Thinking on vessel " << i + 1 << "..." << endl;

do {
battleship.EraseBattleshipCoordinates();

unsigned seed = time(0);
srand(seed);
battleship.positionLetter = 'A' + rand() % 10;

char letters[] = "ruld";
battleship.directionLetter = letters[rand() % 4];

int max = 10, min = 1;
int range = max - min + 1;
battleship.positionNumber = rand() % range + min;

int lengths[] = { 2, 3, 4, 6 };
battleship.length = lengths[rand() % 4];

battleship.WriteBattleshipCoordinates();

} while (battleship.IsColliding(ShipsBoard));

switch (battleship.length)
{
case 2:
twoTileShips++;
break;
case 3:
threeTileShips++;
break;
case 4:
fourTileShips++;
break;
case 6:
sixTileShips++;
break;
}

for (int j = 0; j <= i; j++)
{
battleships[j].AddShipCollision(ShipsBoard);
battleships[j].AddToShipBoard(ShipsBoard);
}
}
char answer = ' ';

while (answer != 'y' && answer != 'Y')
{
cout << "Enemy vessels ready." << endl;
cout << "Continue?" << endl;
cout << "Y/y: ";
cin >> answer;
}
}

bool ArtificialEnemy::PlayerTurn(Player& enemy)
{
ClearConsole();
cout << endl;
cout << "________________________________" << endl;
cout << Name << "'s turn (AI):" << endl;
cout << "--------------------------------" << endl;
cout << endl;

cout << "2 tile ships: " << twoTileShips << endl;
cout << "3 tile ships: " << threeTileShips << endl;
cout << "4 tile ships: " << fourTileShips << endl;
cout << "6 tile ships: " << sixTileShips << endl;
cout << endl;

enemy.GetSunkVesselsCount();
cout << endl;

cout << endl << "Fire in the hole!" << endl;

unsigned seed = time(0);
srand(seed);
char row = 'A' + rand() % 10;

int max = 10, min = 1;
int range = max - min + 1;
int column = rand() % range + min;

cout << row << column << endl;

Point hitPoint;

hitPoint.x = column - 1;
hitPoint.y = TransformBoardChar(row);

if (enemy.ShipsBoard[hitPoint.x][hitPoint.y] == SHIP)
{
cout << endl;
cout << "Result : HIT" << endl;

HitsBoard[hitPoint.x][hitPoint.y] = HIT;

enemy.ShipsBoard[hitPoint.x][hitPoint.y] = HIT;

int hitShipIdx = GetHitShipId(enemy.battleships, hitPoint);

for (int i = 0; i < enemy.battleships[hitShipIdx].length; i++)
{
Point& shipCoordinate = enemy.battleships[hitShipIdx].shipsCoordinates[i];
Point& hitCoordinate = enemy.battleships[hitShipIdx].hitsCoordinates[i];

if (shipCoordinate.x == hitPoint.x && shipCoordinate.y == hitPoint.y)
{
hitCoordinate.x = shipCoordinate.x;
hitCoordinate.y = shipCoordinate.y;

shipCoordinate.x = -1;
shipCoordinate.y = -1;
}
}

if (enemy.battleships[hitShipIdx].IsShipSunk())
{
switch (enemy.battleships[hitShipIdx].length)
{
case 2:
enemy.twoTileShips--;
break;
case 3:
enemy.threeTileShips--;
break;
case 4:
enemy.fourTileShips--;
break;
case 6:
enemy.fourTileShips--;
break;
}

enemy.battleships[hitShipIdx].AddHitCollision(HitsBoard);
enemy.battleships[hitShipIdx].AddToHitBoard(HitsBoard);
}
return true;
}
else
{
HitsBoard[hitPoint.x][hitPoint.y] = MISS;

cout << endl;
cout << "Result : MISS" << endl;

char answer = ' ';

while (answer != 'y' && answer != 'Y')
{
cout << endl;
cout << "Continue?" << endl;
cout << "Y/y: ";
cin >> answer;
}

return false;
}
}
12 changes: 12 additions & 0 deletions Battleships/ArtificialEnemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <cstring>
#include "Constants.h"
#include "Battleship.h"
#include "Player.h"

class ArtificialEnemy : public Player {
public:
ArtificialEnemy(const std::string& name);
void PlayerStart() override;
bool PlayerTurn(Player& enemy) override;
};
5 changes: 4 additions & 1 deletion Battleships/Battleship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,26 @@ bool Battleship::CheckConfig(int playerShipBoard[][BOARD_WIDTH], int sixTileShip
{
if ((positionLetter < 'A' || positionLetter > 'J') && (positionLetter < 'a' || positionLetter > 'j'))
{
cout << positionLetter << "!";
return false;
}

if (positionNumber > 10)
{
cout << positionNumber << "!";
return false;
}

if (directionLetter != 'r' && directionLetter != 'l' && directionLetter != 'u' && directionLetter != 'd' &&
directionLetter != 'R' && directionLetter != 'L' && directionLetter != 'U' && directionLetter != 'D')
{
cout << directionLetter << "!";
return false;
}

if (length < 2 || length > 6 || length == 5)
{
cout << length << "!";
return false;
}

Expand Down Expand Up @@ -168,7 +172,6 @@ void Battleship::WriteBattleshipCoordinates()
shipsCoordinates[i].y = shipsCoordinates[0].y;
}
}

}

void Battleship::EraseBattleshipCoordinates()
Expand Down
2 changes: 2 additions & 0 deletions Battleships/Battleships.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="ArtificialEnemy.cpp" />
<ClCompile Include="Battleship.cpp" />
<ClCompile Include="BattleshipsManager.cpp" />
<ClCompile Include="Display.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Player.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ArtificialEnemy.h" />
<ClInclude Include="Battleship.h" />
<ClInclude Include="BattleshipsManager.h" />
<ClInclude Include="Constants.h" />
Expand Down
6 changes: 6 additions & 0 deletions Battleships/Battleships.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ArtificialEnemy.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Battleship.h">
Expand All @@ -47,6 +50,9 @@
<ClInclude Include="BattleshipsManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ArtificialEnemy.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="1.txt">
Expand Down
24 changes: 14 additions & 10 deletions Battleships/BattleshipsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@

using namespace std;

BattleshipsManager::BattleshipsManager(string name1, string name2) {
player1.Name = name1;
player2.Name = name2;
BattleshipsManager::BattleshipsManager(string name1, string name2, int numberOfPlayers) {
players = new Player * [2];
players[0] = new Player(name1);
mode = numberOfPlayers;

ClearConsole();
player1.PlayerStart();
players[0]->PlayerStart();

ClearConsole();
player2.PlayerStart();
if (mode == 2)
players[1] = new Player(name2);
else if (mode == 1)
players[1] = new ArtificialEnemy(name2);

players[1]->PlayerStart();
}

void BattleshipsManager::LetTheGameBegin() {
Player currentPlayer = player1;

while (true)
{
if (NextTurn(player1, player2)) return;
if (NextTurn(player2, player1)) return;
if (NextTurn(*players[0], *players[1])) return;
if (NextTurn(*players[1], *players[0])) return;
}
}

bool BattleshipsManager::NextTurn(Player activePlayer, Player activeEnemy) {
bool BattleshipsManager::NextTurn(Player &activePlayer, Player &activeEnemy) {
while (activePlayer.PlayerTurn(activeEnemy))
{
if (activeEnemy.GetVesselCount())
Expand Down
8 changes: 5 additions & 3 deletions Battleships/BattleshipsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
#include <fstream>
#include "Player.h"
#include "Display.h"
#include "ArtificialEnemy.h"

using namespace std;

class BattleshipsManager {
private:
Player player1, player2;
Player** players;
int mode;
public:
BattleshipsManager(string name1, string name2);
BattleshipsManager(string name1, string name2, int numberOfPlayers);
void LetTheGameBegin();
bool NextTurn(Player activePlayer, Player activeEnemy);
bool NextTurn(Player &activePlayer, Player &activeEnemy);
};

Loading

0 comments on commit 6552f17

Please sign in to comment.