Created
April 29, 2019 06:38
-
-
Save Matgsan/b0e631d1a3ba78b9778c721075855ffe to your computer and use it in GitHub Desktop.
ADSA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Matheus Santos | |
04/25/2019 | |
I hereby certify that this code is my work and my work alone and understand the syllabus regarding plagiarized code. | |
*/ | |
/* | |
* NOTE FOR PROFESSOR | |
* This code was developed in a MacBook so I couldn't add any sounds or use the direct input from the Keyboard. | |
* | |
* | |
* Labels: | |
* - M: Player | |
* - H: House | |
* - C: Cheese | |
* - S: Smart Rat | |
* - R: Dumb Rat | |
* | |
* I added Houses to the town. | |
* | |
* I added colors: | |
* - CYAN for Smart Rats; | |
* - BLUE for the Dumb Rats; | |
* - YELLOW for the Cheese Blocks; | |
* - GREEN for the 2 blocks range (Just to make easier to the player to see what can hit him and what he can hit); | |
* - RED for the player; | |
* - WHITE for the houses; | |
* | |
* | |
* | |
* Here is an example how I could implement the direct input, i hope you can give me some extra points for that. | |
#include <Windows.h> | |
#include <stdio.h> | |
if (GetAsyncKeyState(VK_UP) { | |
//TEST FOR UP KEY; | |
} else if (GetAsyncKeyState(VK_DOWN) { | |
//TEST FOR DOWN KEY; | |
} else if (GetAsyncKeyState(VK_RIGHT) { | |
//TEST FOR RIGHT KEY; | |
} else if (GetAsyncKeyState(VK_LEFT) { | |
//TEST FOR LEFT KEY; | |
}else if (GetAsyncKeyState(VK_RETURN){ | |
//TEST FOR THE ENTER KEY | |
} | |
* | |
* | |
*/ | |
#include <iostream> | |
#include <random> | |
#define ADD_COLOR(x) strcpy(&map_buffer[iMap],x); iMap += 7; | |
#define ADD_RESET() strcpy(&map_buffer[iMap],"\033[0m"); iMap += 4; isGreen=false; | |
const char SYMBOL_BLANK = '.'; | |
const char SYMBOL_ME = 'M'; | |
const char SYMBOL_HOUSE = 'H'; | |
const char SYMBOL_CHEESE = 'C'; | |
const char SYMBOL_SMART_RAT= 'S'; | |
const char SYMBOL_RAT = 'R'; | |
bool isRunning = true; | |
struct Point { | |
int x; | |
int y; | |
}; | |
struct Rat { | |
Point location; | |
bool isSmart; | |
bool isDead = false; | |
}; | |
struct Town { | |
int turn = 0; | |
int cheeseSize = 5; | |
int ratsSize = 5; | |
int houseSize = 5; | |
int height = 15; | |
int width = 15; | |
char** grid; | |
Rat* rats; | |
char _null; //nullterminator | |
}; | |
enum AttackOrDefense { NONE, HZ, TLS, EK, NOL }; | |
std::string attackOrDefenseName[5] = | |
{"NONE", "Huge ZipLocks", "Trashcan Lid Shields", "Exploding Kitties", "Noseplugs of Lavender"}; | |
struct Player { | |
Point location; | |
Point turnPos; | |
int hp = 5; | |
int hz = 5; | |
int nol = 5; | |
int tls = 1; | |
int ek = 6; | |
AttackOrDefense currentAttackOrDefense = NONE; | |
std::string name; | |
}; | |
void spawnCheese(); | |
void spawnHouses(); | |
void spawnRats(); | |
bool isBlockFilled(Town &, Player &, Point); | |
int gridDelta(Point, Point); | |
bool inRange(Point, Player &, int); | |
void init(Town &town, Player &player); | |
void drawMap(Town &town, Player &player); | |
bool movementMenu(Town &town, Player &player); | |
void moveRats(Town &town, Player &player); | |
void printInfo(const Town &town, const Player &player); | |
Point getClosestCheeseBlock(Town &town, Point); | |
void selectDefenseOrAttack(Player &player); | |
void doLogic(); | |
void deployDefenseOrAttack(); | |
void ratsAttacks(); | |
int main() { | |
Town town; | |
Player player; | |
init(town, player); | |
town.rats = new Rat[town.ratsSize]; | |
while (isRunning) { | |
//system("cls"); // ONLY WINDOWS | |
drawMap(town, player); | |
if(movementMenu(town, player)){ | |
moveRats(town, player); | |
drawMap(town, player); | |
selectDefenseOrAttack(player); | |
} | |
} | |
return 0; | |
} | |
void printInfo(const Town &town, const Player &player){ | |
std::cout << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl; | |
std::cout << "=====================================[INFO]=====================================" << std::endl; | |
std::cout << "Turn: " << town.turn << std::endl; | |
std::cout << "Your Position: " << player.location.x << "," << player.location.y << std::endl; | |
std::cout << "Hit Points: " << player.hp << std::endl; | |
std::cout << "Choosed Attack/Defense: " << attackOrDefenseName[player.currentAttackOrDefense] << std::endl; | |
std::cout << std::endl; | |
std::cout << "Backpack: "<< std::endl; | |
std::cout << " Huge Zip Locks: " << player.hz << std::endl; | |
std::cout << " Nose Plugs: " << player.nol << std::endl; | |
std::cout << " Exploding Kitties: " << player.ek << std::endl; | |
std::cout << " Trashcan Lid Shields: " << player.tls << std::endl; | |
} | |
Point getClosestCheeseBlock(Town &town, Point point) { | |
Point closestPoint; | |
Town _town = town; | |
for (int y = 0; y < town.height; y++) { | |
for (int x = 0; x < town.width + 1; x++) { | |
if (_town.grid[y][x] == SYMBOL_CHEESE) { | |
if (gridDelta(point, {x, y}) < gridDelta(point, closestPoint)) { | |
closestPoint = {x, y}; | |
} | |
} | |
} | |
} | |
return closestPoint; | |
} | |
bool isBlockFilled(Town& town, Player &player, Point point) { | |
if (player.location.x == point.x && player.location.y == point.y) { | |
std::cout << "A"; | |
return true; | |
} | |
for (int i = 0; i < town.ratsSize; i++) { | |
Rat rat = town.rats[i]; | |
if (rat.isDead) { | |
continue; | |
} | |
if (rat.location.x == point.x && rat.location.y == point.y) { | |
return true; | |
} | |
} | |
return town.grid[point.y][point.x] != SYMBOL_BLANK; | |
} | |
/* | |
void spawnHouses() { | |
int numberOfHouses = HOUSES; | |
Point point; | |
std::random_device random; | |
std::mt19937 generator(random()); | |
std::uniform_int_distribution<int> dist(1, numberOfHouses); | |
do { | |
int x = dist(generator); | |
int y = dist(generator); | |
point.x = x; | |
point.y = y; | |
if (!isBlockFilled(point)) { | |
grid.grid[point.y][point.x] = SYMBOL_HOUSE; | |
numberOfHouses--; | |
} | |
} while (numberOfHouses > 0); | |
} | |
void spawnCheese() { | |
int numberOfCheese = CHEESE_BLOCKS; | |
Point point; | |
std::random_device random; | |
std::mt19937 generator(random()); | |
std::uniform_int_distribution<int> dist(1, CHEESE_BLOCKS); | |
do { | |
int x = dist(generator); | |
int y = dist(generator); | |
point.x = x; | |
point.y = y; | |
if (!isBlockFilled(point)) { | |
grid.grid[point.y][point.x] = SYMBOL_CHEESE; | |
numberOfCheese--; | |
} | |
} while (numberOfCheese > 0); | |
} | |
void spawnRats() { | |
int numberOfRats = RATS_SIZE; | |
int index = 0; | |
Point point; | |
std::random_device random; | |
std::mt19937 generator(random()); | |
std::uniform_int_distribution<int> dist(1, RATS_SIZE); | |
do { | |
int x = dist(generator); | |
int y = dist(generator); | |
point.x = x; | |
point.y = y; | |
if (!isBlockFilled(point)) { | |
Rat rat; | |
rat.location.x = x; | |
rat.location.y = y; | |
if (numberOfRats <= abs(RATS_SIZE/2)) { | |
rat.isSmart = true; | |
} | |
rats[index] = rat; | |
numberOfRats--; | |
index++; | |
} | |
} while (numberOfRats > 0); | |
}*/ | |
void drawMap(Town &town, Player &player) { | |
Town _town = town; | |
_town.grid[player.location.y][player.location.x] = SYMBOL_ME; | |
for (int i = 0; i < town.ratsSize; i++) { | |
Rat rat = town.rats[i]; | |
if (rat.isDead) { | |
continue; | |
} | |
_town.grid[rat.location.y][rat.location.x] = rat.isSmart ? SYMBOL_SMART_RAT : SYMBOL_RAT; | |
} | |
printInfo(town, player); | |
std::cout << "================================================================================" << std::endl; | |
char map_buffer[1024]; | |
int iMap = 0; | |
bool isGreen = false; | |
for (int y = 0; y < _town.height; y++) { | |
for (int x = 0; x < _town.width + 1; x++) { | |
char symbol = _town.grid[y][x]; | |
bool isInRange = inRange({x,y}, player, 3); | |
if (!isInRange && isGreen) { | |
ADD_RESET(); | |
isGreen = false; | |
} | |
if (isInRange && symbol == SYMBOL_BLANK && !isGreen) { | |
ADD_COLOR("\033[0;32m"); | |
isGreen = true; | |
} | |
if (symbol == SYMBOL_CHEESE) { | |
ADD_COLOR("\033[0;33m"); | |
map_buffer[iMap++] = symbol; | |
ADD_RESET(); | |
continue; | |
} else if (symbol == SYMBOL_HOUSE) { | |
ADD_COLOR("\033[0;30m"); | |
map_buffer[iMap++] = symbol; | |
ADD_RESET(); | |
continue; | |
} else if (symbol == SYMBOL_RAT) { | |
ADD_COLOR("\033[0;34m"); | |
map_buffer[iMap++] = symbol; | |
ADD_RESET(); | |
continue; | |
} else if (symbol == SYMBOL_SMART_RAT) { | |
ADD_COLOR("\033[0;36m"); | |
map_buffer[iMap++] = symbol; | |
ADD_RESET(); | |
continue; | |
} else if (symbol == SYMBOL_ME) { | |
ADD_COLOR("\033[0;31m"); | |
map_buffer[iMap++] = symbol; | |
ADD_RESET(); | |
continue; | |
} | |
map_buffer[iMap++] = symbol; | |
} | |
} | |
map_buffer[iMap] = 0; | |
std::cout << map_buffer; | |
std::cout << "================================================================================" << std::endl; | |
} | |
void init(Town &town, Player &player) { | |
for (int i = 0; i < town.height; i++) { | |
for (int j = 0; j < town.width + 1; j++) { | |
town.grid[i][j] = j == town.width ? '\n' : SYMBOL_BLANK; | |
} | |
} | |
town._null = 0; | |
player.location = {0, 0}; | |
player.turnPos = player.location; | |
/* spawnHouses(); | |
spawnCheese(); | |
spawnRats(); | |
*/ | |
} | |
int gridDelta(Point p, Point p2) { | |
return abs(p.x - p2.x) + abs(p.y - p2.y); | |
} | |
bool inRange(Point p, Player &player, int size) { | |
return gridDelta(p, player.turnPos) <= size; | |
} | |
bool movementMenu(Town &town, Player &player) { | |
int xInput = 0; | |
int yInput = 0; | |
//main loop here.. | |
//wait for input | |
int _turn = town.turn; | |
const char | |
UP = 'W', | |
DOWN = 'S', | |
LEFT = 'A', | |
RIGHT = 'D'; | |
char movement[3]; | |
std::cout << std::endl << std::endl << "Movement Menu" << std::endl << std::endl | |
<< "UP - (W)" << std::endl | |
<< "DOWN - (S)" << std::endl | |
<< "LEFT - (A)" << std::endl | |
<< "RIGHT - (D)" << std::endl; | |
std::cout << std::endl << "What direction you want to go?" << std::endl; | |
std::cin.get(movement, 4, '\n'); | |
std::cin.get(); | |
for (int i = 0; i < 3; i++) { | |
switch (movement[i]) { | |
case UP: { | |
if (player.location.y > 0 && !isBlockFilled(town, player, {player.location.x, player.location.y - 1})) { | |
yInput--; | |
} | |
break; | |
} | |
case DOWN: { | |
if (player.location.y + 1 < town.height && !isBlockFilled(town, player, {player.location.x, player.location.y + 1})) { | |
yInput++; | |
} | |
break; | |
} | |
case LEFT: { | |
if (player.location.x > 0 && !isBlockFilled(town, player, {player.location.x - 1, player.location.y})) { | |
xInput--; | |
} | |
break; | |
} | |
case RIGHT: { | |
if (player.location.x + 1 < town.width && !isBlockFilled(town, player, {player.location.x + 1, player.location.y})) { | |
xInput++; | |
} | |
break; | |
} | |
} | |
} | |
if (inRange({player.location.x + xInput, player.location.y + yInput}, player, 3)) { | |
player.location.x += xInput; | |
player.location.y += yInput; | |
} | |
if (player.turnPos.x != player.location.x || player.turnPos.y != player.location.y) { | |
town.turn++; | |
player.turnPos = player.location; | |
} | |
return _turn != town.turn; | |
} | |
void moveRats(Town &town, Player &player) { | |
for (int i = 0; i < town.ratsSize; i++) { | |
Rat rat = town.rats[i]; | |
Point point = rat.isSmart ? player.location : getClosestCheeseBlock(town,rat.location); | |
if (gridDelta(rat.location, point) >= 2) { | |
int x = 0; | |
int y = 0; | |
if (point.x > rat.location.x && rat.location.x + 1 < town.width | |
&& !isBlockFilled(town, player,{rat.location.x + 1, rat.location.y})) { | |
x++; | |
} else if (rat.location.x > 0 && !isBlockFilled(town, player,{rat.location.x - 1, rat.location.y})) { | |
x--; | |
} | |
rat.location.x += x; | |
if (point.y > rat.location.y && rat.location.y + 1 < town.height | |
&& !isBlockFilled(town, player,{rat.location.x, rat.location.y + 1})) { | |
y++; | |
} else if (rat.location.y > 0 && !isBlockFilled(town, player,{rat.location.x, rat.location.y - 1})) { | |
y--; | |
} | |
rat.location.y += y; | |
town.rats[i] = rat; | |
} | |
} | |
} | |
void selectDefenseOrAttack(Player &player) { | |
int choice = 0; | |
while (choice == 0) { | |
std::cout << std::endl << std::endl << "Attack/Defense Menu" << std::endl << std::endl | |
<< "Noseplugs of Lavender - (1)" << std::endl | |
<< "Huge ZipLocks - (2)" << std::endl | |
<< "Trashcan Lid Shields - (3)" << std::endl | |
<< "Exploding Kitties - (4)" << std::endl | |
<< "None - (5)" << std::endl; | |
std::cout << std::endl << "What attack or defense you want to use?" << std::endl; | |
std::cin >> choice; | |
std::cin.clear(); | |
std::cin.ignore(100, '\n'); | |
if (isdigit(choice)) { | |
continue; | |
} | |
switch (choice) { | |
case 1: { | |
if (player.nol == 0) { | |
std::cout << "You don't have more Nose Plugs of Lavender" << std::endl; | |
choice = 0; | |
continue; | |
} | |
player.nol--; | |
player.currentAttackOrDefense = NOL; | |
break; | |
} | |
case 2: { | |
if (player.hz == 0) { | |
std::cout << "You don't have more Huge ZipLocks" << std::endl; | |
choice = 0; | |
continue; | |
} | |
player.hz--; | |
player.currentAttackOrDefense = HZ; | |
break; | |
} | |
case 3: { | |
if (player.tls == 0) { | |
std::cout << "You don't have more Trashcan Lid Shields" << std::endl; | |
choice = 0; | |
continue; | |
} | |
player.tls--; | |
player.currentAttackOrDefense = TLS; | |
break; | |
} | |
case 4: { | |
if (player.ek == 0) { | |
std::cout << "You don't have more Exploding Kitties" << std::endl; | |
choice = 0; | |
continue; | |
} | |
player.ek--; | |
player.currentAttackOrDefense = EK; | |
break; | |
} | |
case 5: { | |
player.currentAttackOrDefense = NONE; | |
break; | |
} | |
} | |
} | |
} | |
void doLogic(Town &town, Player &player) { | |
int iCheese = 0; | |
for (int y = 0; y < town.height; y++) { | |
for (int x = 0; x < town.width + 1; x++) { | |
char symbol = town.grid[y][x]; | |
if (symbol == SYMBOL_CHEESE) { | |
iCheese++; | |
if (gridDelta({x, y}, player.location) <= 2) { | |
switch (player.currentAttackOrDefense) { | |
case NOL: { | |
bool stun = rand() % 101 <= 35; | |
if (stun) { | |
isRunning = false; | |
std::cout << "You have been stunned" << std::endl << std::endl << std::endl; | |
std::cout << "GAME OVER"; | |
} | |
break; | |
} | |
case HZ: { | |
bool success = rand() % 101 <= 70; | |
if (success) { | |
town.grid[y][x] = SYMBOL_BLANK; | |
iCheese--; | |
std::cout << "You putted the Cheese into a ziplock" << std::endl; | |
}else{ | |
bool stun = rand() % 101 <= 65; | |
if (stun) { | |
isRunning = false; | |
std::cout << "You have been stunned" << std::endl << std::endl << std::endl; | |
std::cout << "GAME OVER"; | |
} | |
} | |
break; | |
} | |
default: { | |
bool stun = rand() % 101 <= 65; | |
if (stun) { | |
isRunning = false; | |
std::cout << "You have been stunned" << std::endl << std::endl << std::endl; | |
std::cout << "GAME OVER"; | |
} | |
break; | |
} | |
} | |
} | |
} else { | |
switch (player.currentAttackOrDefense) { | |
case EK: { | |
break; | |
} | |
case TLS: { | |
break; | |
} | |
default:{ | |
} | |
} | |
} | |
if (symbol == SYMBOL_RAT) { | |
if (gridDelta({x, y}, player.location) <= 3) { | |
switch (player.currentAttackOrDefense) { | |
case EK: { | |
bool kill = rand() % 101 <= 80; | |
if (kill) { | |
std::cout << "You killed a Rat" << std::endl; | |
town.grid[y][x] = SYMBOL_BLANK; | |
for (int i = 0; i < town.ratsSize; i++) { | |
Rat rat = town.rats[i]; | |
if (rat.location.x == x && rat.location.y == y) { | |
rat.isDead = true; | |
town.rats[i] = rat; | |
} | |
} | |
} | |
break; | |
} | |
default: { | |
bool bite = rand() % 101 <= 30; | |
if (bite) { | |
player.hp--; | |
std::cout << "You lost a hit point" << std::endl; | |
} | |
break; | |
} | |
} | |
} | |
}else if (symbol == SYMBOL_SMART_RAT) { | |
if (gridDelta({x, y}, player.location) <= 1) { | |
switch (player.currentAttackOrDefense) { | |
case EK: { | |
bool kill = rand() % 101 <= 80; | |
if (kill) { | |
std::cout << "You killed a Smart Rat" << std::endl; | |
town.grid[y][x] = SYMBOL_BLANK; | |
for (int i = 0; i < town.ratsSize; i++) { | |
Rat rat = town.rats[i]; | |
if (rat.location.x == x && rat.location.y == y) { | |
rat.isDead = true; | |
town.rats[i] = rat; | |
} | |
} | |
} | |
break; | |
} | |
case TLS: { | |
bool bite = rand() % 101 <= 15; | |
if (bite) { | |
player.hp--; | |
std::cout << "You lost a hit point" << std::endl; | |
} | |
break; | |
} | |
default: { | |
bool bite = rand() % 101 <= 85; | |
if (bite) { | |
player.hp--; | |
std::cout << "You lost a hit point" << std::endl; | |
} | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
if (iCheese == 0) { | |
isRunning = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment