Skip to content

Commit 64a1b48

Browse files
committed
Final Project
1 parent 9993a89 commit 64a1b48

26 files changed

+1007
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ add_subdirectory(Utility)
3535
add_subdirectory(Shapes)
3636
add_subdirectory(DateTime)
3737
add_subdirectory(Shapes2)
38+
add_subdirectory(DoodleBug)
39+
add_subdirectory(LabExam)

DoodleBug/Ant.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "Ant.hpp"
2+
#include <iostream>
3+
4+
5+
Ant::Ant(Square* square) : Bug(square),MoveableBug(10000,3)
6+
{
7+
}
8+
9+
int Ant::PrioritizeMove(Square* square) const
10+
{
11+
if (square->GetOccupant()->GetId() == "NullBug")
12+
{
13+
return 1;
14+
}
15+
return -1;
16+
}
17+
18+
std::string Ant::GetId() const
19+
{
20+
return "Ant";
21+
}
22+
FoodBug* Ant::AsFoodBug()
23+
{
24+
return this;
25+
}
26+
const FoodBug* Ant::AsFoodBug() const
27+
{
28+
return this;
29+
}
30+
int Ant::AmountOfFood() const
31+
{
32+
return std::rand() % 2;
33+
}
34+
std::unique_ptr<MoveableBug> Ant::GetSpawn(Square* sqare) const
35+
{
36+
return std::make_unique<Ant>(sqare);
37+
}
38+
Ant::~Ant()
39+
{
40+
}

DoodleBug/Ant.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef ANT_HPP
2+
#define ANT_HPP
3+
#include "MoveableBug.hpp"
4+
#include "Square.hpp"
5+
#include "FoodBug.hpp"
6+
7+
8+
class Ant : public MoveableBug, public FoodBug
9+
{
10+
public:
11+
Ant(Square* square);
12+
const FoodBug* AsFoodBug() const;
13+
FoodBug* AsFoodBug();
14+
std::string GetId() const;
15+
int PrioritizeMove(Square* square) const;
16+
int AmountOfFood() const;
17+
std::unique_ptr<MoveableBug> GetSpawn(Square* square) const;
18+
~Ant();
19+
};
20+
21+
#endif

DoodleBug/Arena.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "Arena.hpp"
2+
#include <cstdlib>
3+
#include "DoodleBug.hpp"
4+
#include "Ant.hpp"
5+
#include <algorithm>
6+
#include <iostream>
7+
8+
9+
10+
Arena::Arena()
11+
{
12+
for (int x = 0; x < this->GetWidth(); x++)
13+
{
14+
for (int y = 0; y < this->GetHeight(); y++)
15+
{
16+
//Choose randomly between an ant, doodlebug, and nothing
17+
if (std::rand()< 5*(RAND_MAX/400))
18+
Squares[x][y] =
19+
Square(
20+
x,y,this,
21+
std::make_unique<DoodleBug>(
22+
&Squares[x][y]
23+
)
24+
);
25+
else if (std::rand()< 100*(RAND_MAX/400))
26+
Squares[x][y] =
27+
Square(
28+
x,y,this,
29+
std::make_unique<Ant>(
30+
&Squares[x][y]
31+
)
32+
);
33+
else
34+
Squares[x][y] = Square(x,y,this);
35+
}
36+
}
37+
}
38+
39+
const Square* Arena::GetSquare(int x, int y) const
40+
{
41+
return &Squares[x][y];
42+
}
43+
Square* Arena::GetSquare(int x, int y)
44+
{
45+
return &Squares[x][y];
46+
}
47+
int Arena::GetWidth() const
48+
{
49+
return ARENA_WIDTH;
50+
}
51+
int Arena::GetHeight() const
52+
{
53+
return ARENA_HEIGHT;
54+
}
55+
56+
57+
void Arena::Update()
58+
{
59+
std::vector<Bug*> UpdatedBugs; //Keep a list of the bugs we have updated so we don't double dip
60+
for (int x = 0; x < this->GetWidth(); x++)
61+
{
62+
for (int y = 0; y < this->GetHeight(); y++)
63+
{
64+
Bug* bug = this->GetSquare(x,y)->GetOccupant();
65+
66+
//If the bug has not been updated, update it
67+
if (std::find(UpdatedBugs.begin(),UpdatedBugs.end(),bug)
68+
== UpdatedBugs.end())
69+
{
70+
UpdatedBugs.push_back(bug);
71+
bug->Update();
72+
}
73+
}
74+
}
75+
}

DoodleBug/Arena.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef ARENA_HPP
2+
#define ARENA_HPP
3+
#include "Square.hpp"
4+
5+
#define ARENA_WIDTH 180
6+
#define ARENA_HEIGHT 40
7+
8+
class Arena
9+
{
10+
//The array of squares
11+
std::array<std::array<Square,ARENA_HEIGHT>,ARENA_WIDTH> Squares;
12+
public:
13+
Arena();
14+
15+
//Get squares at the locations given
16+
const Square* GetSquare(int x, int y) const;
17+
Square* GetSquare(int x, int y);
18+
19+
//Update all the squares
20+
void Update();
21+
22+
//Get the dimensions of the arena
23+
int GetWidth() const;
24+
int GetHeight() const;
25+
};
26+
27+
28+
#endif

DoodleBug/Bug.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "Bug.hpp"
2+
#include <assert.h>
3+
#include "Square.hpp"
4+
5+
6+
Bug::Bug() : currentSquare(0) {}
7+
Bug::Bug(Square* square)
8+
{
9+
this->SetSquare(square);
10+
}
11+
Square* Bug::GetSquare() const
12+
{
13+
return currentSquare;
14+
}
15+
16+
void Bug::SetSquare(Square* square)
17+
{
18+
this->currentSquare = square;
19+
assert(square != 0);
20+
}
21+
std::string Bug::ToString() const
22+
{
23+
std::string str = "";
24+
str += "Type: " + this->GetId() + "\n";
25+
str += "Location: " + std::to_string(this->GetSquare()->GetX())
26+
+ "," + std::to_string(this->GetSquare()->GetY()) + "\n";
27+
return str;
28+
}
29+
Bug::~Bug(){}

DoodleBug/Bug.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef BUG_HPP
2+
#define BUG_HPP
3+
#include <string>
4+
5+
6+
class Square;
7+
class FoodBug;
8+
9+
10+
//Class that represents bugs
11+
class Bug
12+
{
13+
//The square that the bug is on
14+
Square* currentSquare;
15+
public:
16+
//The square object passed in is the starting square for the bug
17+
Bug(Square* square);
18+
Bug();
19+
20+
//Called once per turn
21+
virtual void Update() = 0;
22+
23+
//Returns the current bug as a food bug, other wise null
24+
virtual FoodBug* AsFoodBug() = 0;
25+
virtual const FoodBug* AsFoodBug() const = 0;
26+
27+
//Gets the ID of the bug ("Ant", "DoodleBug", etc)
28+
virtual std::string GetId() const = 0;
29+
30+
//Gets a string representation of the bug
31+
virtual std::string ToString() const;
32+
33+
//Gets the current square
34+
Square* GetSquare() const;
35+
36+
//Sets the current square
37+
void SetSquare(Square* square);
38+
virtual ~Bug();
39+
};
40+
41+
#endif

DoodleBug/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_executable(DoodleBugMain DoodleBugMain.cpp)
2+
add_library(Square Square.cpp)
3+
add_library(Arena Arena.cpp)
4+
add_library(Bug Bug.cpp)
5+
add_library(MoveableBug MoveableBug.cpp)
6+
add_library(Ant Ant.cpp)
7+
add_library(DoodleBug DoodleBug.cpp)
8+
9+
create_test("DoodleBug Tests" "Test;Square;Arena;Ant;DoodleBug;MoveableBug;Bug;CppUTest;CppUTestExt")
10+
11+
target_link_libraries(DoodleBugMain Square Arena Ant DoodleBug MoveableBug Bug)

DoodleBug/DoodleBug.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "DoodleBug.hpp"
2+
3+
DoodleBug::DoodleBug(Square* square) : Bug(square),MoveableBug(3,8)
4+
{
5+
}
6+
7+
const FoodBug* DoodleBug::AsFoodBug() const
8+
{
9+
return 0;
10+
}
11+
FoodBug* DoodleBug::AsFoodBug()
12+
{
13+
return 0;
14+
}
15+
std::string DoodleBug::GetId() const
16+
{
17+
return "DoodleBug";
18+
}
19+
int DoodleBug::PrioritizeMove(Square* square) const
20+
{
21+
if (square->GetOccupant()->GetId() == "Ant")
22+
{
23+
return 1;
24+
}
25+
if (square->GetOccupant()->GetId() == "DoodleBug")
26+
{
27+
return -1;
28+
}
29+
return 0;
30+
}
31+
32+
std::unique_ptr<MoveableBug> DoodleBug::GetSpawn(Square* square) const
33+
{
34+
return std::make_unique<DoodleBug>(square);
35+
}
36+
37+
DoodleBug::~DoodleBug()
38+
{
39+
}

DoodleBug/DoodleBug.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef DOODLEBUG_HPP
2+
#define DOODLEBUG_HPP
3+
#include "MoveableBug.hpp"
4+
#include "Square.hpp"
5+
6+
7+
class DoodleBug : public MoveableBug
8+
{
9+
public:
10+
DoodleBug(Square* square);
11+
const FoodBug* AsFoodBug() const;
12+
FoodBug* AsFoodBug();
13+
std::string GetId() const;
14+
int PrioritizeMove(Square* square) const;
15+
std::unique_ptr<MoveableBug> GetSpawn(Square* square) const;
16+
~DoodleBug();
17+
};
18+
19+
#endif

0 commit comments

Comments
 (0)