summaryrefslogtreecommitdiffstats
path: root/C++/Polymorphism
diff options
context:
space:
mode:
Diffstat (limited to 'C++/Polymorphism')
-rw-r--r--C++/Polymorphism/INSTRUCTIONS47
-rw-r--r--C++/Polymorphism/Polymorphism.cpp155
-rw-r--r--C++/Polymorphism/PolymorphismTooCool.cpp205
-rw-r--r--C++/Polymorphism/input.txt1
4 files changed, 408 insertions, 0 deletions
diff --git a/C++/Polymorphism/INSTRUCTIONS b/C++/Polymorphism/INSTRUCTIONS
new file mode 100644
index 0000000..87fad1e
--- /dev/null
+++ b/C++/Polymorphism/INSTRUCTIONS
@@ -0,0 +1,47 @@
+**NOTE** You are free to write a non-trivial program of your choice that demonstrates polymorphism (runtime or compile time or both). Or, if you cannot think of your own program idea, you may complete the below program. It is payroll again, so I wanted to give you an option to do something different so you don't become bored.
+If you choose to use this program, then below are criteria:
+Write a program that does the following:
+
+ Create a class called Employee and it will store variables for first name, last name, hours worked, wage, gross pay, net pay, tax, overtime wage, overtime pay, overtime hours, and deductions
+ Get data via the keyboard or from an input file for first name, last name, wage, hours worked and deductions. The remaining varaibles are derived values
+ To demonstrate polymorphism, you will have two functions with the same name: payroll. The payroll functions will do all calculations. Except, one function will be for a manager (salaried employee) and one function will be for hourly employees
+ For further polymorphism practice, you will have two functions with the same name: output. These output functions will output all payroll information. One function will output manager information and one will output all hourly employee information
+
+Assignment Notes for the payroll program:
+
+ Make variables either arrays or vectors - your choice
+ Don't forget to check for overtime!
+ Only output overtime for hourly employees if it exists
+ Managers do not have overtime since they are salaried. The get the same paycheck each pay period, so do not look for overtime for them.
+ If you want, you can output all information to an output file instead of the screen. Depending on how you write this program, this may make your program easier.
+
+Assignment Notes for custom program:
+
+ Make variables either arrays or vectors - your choice
+ Your program must have at least two classes
+ Each class must have at least two non-trivial functions
+ You must demonstrate run time or compile time (or both!) polymorphism in your program
+ If you are not sure what to write and do not want another payroll program, try modifying your project 9 so that it uses polymorphism! Then, most of the program is written already
+
+General Notes:
+Be sure to use comments in your program: Name, Program Description, Date and anywhere else in the program you deem necessary.
+If you are stuck, I will help you!
+Grading Rubric:
+
+ If you do not include comments at the top of the program (name, program description, date), you will lose 15 points
+ If your program is not object-oriented, you will receive a 0/100 (OOP is requred for this assignment)
+ If your program does not use functions, you will lose up to 75 points (depending on the number of functions missing)
+ If your program does not use polymorphism, you will lose up to 100 points since this is the point of the assignment
+ If your program does not compile (run), then I will give a grade of 0/100. But will give you the change to repair for points back (some points are better than none)
+ If your program is late (within 48 hours of the due date), you will lose 25 points
+ If your program is late beyond the 48 hour due date, I will typically still accept it, but you will lose far more points. Depends on when you turn it in
+ If you use global variables in your program, I will deduct 5 points for each used
+ If your program is not formatted nicely (code all over the place, ugly), you will lose up to 25 points depending on the extent
+ If your program stops working when I run it, you will lose points. The exact amount depends on the severity of the error
+ If your program still has your friend's name on it, I will send you a message asking you to try harder while giving you a 0/100
+ If you submit a file type I cannot open, such as .sln, you will receive a 0/100. You will be able to resubmit for credit, but you will lose up to 90 points (depending on how late it is)
+ If your program looks like a a professional programmer wrote it, I will write to you to ask if you want a job. Well, maybe not. But, I will ask about the code
+ This is just a list of typical issues I run into when grading to give you some idea of where your points go. Points can be taken off for other reasons.
+
+
+
diff --git a/C++/Polymorphism/Polymorphism.cpp b/C++/Polymorphism/Polymorphism.cpp
new file mode 100644
index 0000000..d09c2d6
--- /dev/null
+++ b/C++/Polymorphism/Polymorphism.cpp
@@ -0,0 +1,155 @@
+// Name: msglm
+// Date: Nov-14th-2022
+// Program Name: parse CSV
+// Description: parses a character-seperated value sequence. Not very robust.
+
+//This program is a bit less barebones and cool than I wanted, that's because, while working on the original project, I noticed my GPU fan had gone out
+//and that I have been risking overheating my computer. I promptly ceased using my computer and repaired the device. This was about 50% of the way through
+//completing the program. By the time I had the thing fixed, I was working on borrowed time and opted to scrap the original project for something more simple.
+//
+//The project in question was a small, tile-based territorial control game where AI would battle it out for control over the whole board. I had a good portion of it done, but
+//still a lot more to get done. Once I got my GPU back in, I saw how much was necessary, and ditched the whole project because of time constraints.
+//
+//Lesson learned: never do cool things when under a deadline.
+
+#include <iostream>
+#include <string>
+#include <fstream>
+using namespace std;
+
+class parseCSV {
+ public:
+ string input;
+ char delim;
+ string processed;
+
+ void setdelim(char x) {
+ delim = x;
+ }
+
+ void setdelim(string x) {
+ delim = x[0];
+ }
+
+ void takeInput() {
+ cout << "Please enter the data that you would like to be parsed\n";
+ cin >> input;
+ }
+
+ void takeInput(string toParse) {
+ input = toParse;
+ }
+
+ void takeInput(ifstream & file) {
+
+ file.open("input.txt");
+ if (!file) {
+ cout << "ERROR! input.txt does not exist!";
+ exit(1);
+ }
+ getline(file, input);
+ file.close();
+ }
+
+ void process() {
+ for(int charPos = 0; charPos < input.length(); charPos++) {
+ if (input.at(charPos) == delim) {
+ processed.push_back('\n');
+ } else {
+ processed.push_back(input[charPos]);
+ }
+ }
+
+ }
+
+ void output() {
+ cout << processed;
+ }
+
+ void output(string location) {
+ ofstream output;
+ output.open(location);
+ output << processed;
+ output.close();
+ }
+
+};
+
+
+int main() {
+
+ parseCSV parse;
+ int choice;
+
+ //Taking in the data to be parsed
+ cout << "Would you like too...\n";
+ cout << "1. Read input from a file\n";
+ cout << "2. Input data manually\n";
+ cout << "Enter Selection: ";
+ cin >> choice;
+
+ switch (choice) {
+ case 1:
+ {
+ ifstream file;
+ parse.takeInput(file);
+ break;
+ }
+ case 2:
+ {
+ parse.takeInput();
+ break;
+ }
+ default:
+ cout << "You did not give a valid input. Exiting.";
+ exit(1);
+ break;
+ }
+
+ //Setting the deliminator
+ char temp;
+ cout << "Please selecte a deliminator for your file: ";
+ cin >> temp;
+ parse.setdelim(temp);
+
+ //process the input
+ parse.process();
+
+ //Outputting
+ cout << "Would you like too...\n";
+ cout << "1. Have the output put in a file\n";
+ cout << "2. Have the output put on the screen\n";
+ cout << "Enter Selection: ";
+ cin >> choice;
+
+ switch (choice) {
+ case 1:
+ {
+ string temploc;
+ cout << "Where would you like to write the file?\n";
+ cin >> temploc;
+ parse.output(temploc);
+ break;
+ }
+ case 2:
+ {
+ parse.output();
+ break;
+ }
+ default:
+ {
+ cout << "You did not give a valid input. Exiting.";
+ exit(1);
+ break;
+ }
+ }
+
+
+}
+/*
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
diff --git a/C++/Polymorphism/PolymorphismTooCool.cpp b/C++/Polymorphism/PolymorphismTooCool.cpp
new file mode 100644
index 0000000..472817b
--- /dev/null
+++ b/C++/Polymorphism/PolymorphismTooCool.cpp
@@ -0,0 +1,205 @@
+// Name: msglm
+// Date: Nov-13-2022
+// Program Name: Brutal Geo-politics
+// Description: A civlization game with econoimcs and power where the goal is to take every tile on the board. This version is played by the AI
+
+
+#include <iostream>
+#include <string>
+#include <array>
+#include <random>
+#include <vector>
+
+using namespace std;
+
+//classes
+class civilization {
+ public:
+ //icon will represent the civilization on the map
+ char icon;
+ //aggressiveness will represent how likely a civilization is to
+ //aquire territory by stealing it rather than buying it.
+ //higher = more likely to steal; lower = more likely to buy.
+
+ ///AXED DUE TO TIME CONSTRAINTS
+ //This will control the AI for the civilizations
+ //float aggressiveness;
+
+ //resources that each civlization has
+ int manpower;
+ int money;
+
+ //The percentage of the economicOutput a civilization will take per turn from each of its territories.
+ float taxRate;
+
+ void quasiConstructor(default_random_engine & randNumGen, char chosenIcon) {
+ icon = chosenIcon;
+
+ uniform_real_distribution<float> percent(0.0,1.0);
+ aggressiveness = percent(randNumGen);
+ taxRate = percent(randNumGen);
+ }
+
+};
+
+//structs
+class tile {
+ public:
+ //The current owner of a civilization
+ civilization owner;
+
+ //Dictates how much manpower is in a tile
+ int manpower;
+ float populationGrowth;
+ //Compliance of a tile is how docile a tile is towards its master civilization
+ //Compliance will lower if the master civilization is out of money or out of manpower
+ //Compliance will also lower if taxes are too high (see economicOutputExpectation)
+ //If compliance reaches zero, a revolt will happen and the current tile will become its own civilization
+ int compliance;
+
+
+ //The raw value of the tile; this amount of money will need to be exchanged to take a tile by purchasing
+ int value;
+
+ //The value, per turn, the tile generates.
+ //If nobody owns the tile, the value of the tile will simply go up
+ //If someone does own this tile, a percentage of that tile's economicOutput will be collected
+ //by the master civilization in accordance with their tax rate.
+ float economicOutput;
+
+ //The percentage of economicOutput a tile expects to keep
+ //If a tax rate goes over this amount, a tile will lower in its compliance.
+ float economicOutputExpectation;
+
+ //If a tile is a core tile, it will never revolt.
+ //This status is reserved for the starting point of a civilization (the capital) or tiles that have been
+ //annhilated after failing to revolt.
+ bool coreTile;
+
+ //If this tile is a wilderness tile, it has never been contacted by a civilization.
+ //This means no one owns it and civilizations cannot be spawned out of them (except during game start).
+ bool wilderness;
+
+ //From my research, there is no good way to pass
+ //parameters to a constructor function that belongs to an object
+ //that's inside of an array.
+ //Because of this, the following function must be called after the object it initalized
+ //to be able to use a universal random number generator that doesn't involve global variables.
+ //I would use global variables, but I also want points, so instead we get this:
+ void quasiConstructor( default_random_engine & randNumGen ) { //Pass by reference because, in this instance for maximum performance
+ wilderness = true;
+
+ uniform_int_distribution<int> manpowerRange(1,100000);
+ manpower = manpowerRange(randNumGen);
+
+ uniform_real_distribution<float> populationGrowthRange(0.0,0.9);
+ populationGrowth = populationGrowthRange(randNumGen);
+
+ uniform_int_distribution<int> economicOutputRange(1,15000);
+ economicOutput = economicOutputRange(randNumGen);
+
+ value = economicOutput;
+
+ uniform_real_distribution<float> economicOutputExpectationRange(0.0,0.9);
+ economicOutputExpectation = economicOutputExpectationRange(randNumGen);
+ }
+
+ void claim(civilization claimant) {
+ if (wilderness) {
+ owner = claimant;
+ wilderness = false;
+ }
+
+ }
+ void popGrowth() { manpower = manpower + (populationGrowth*manpower); }
+ //Revolt will succeed if the manpower of a tile is greater than 10% of its master's manpower
+ //OR
+ //if the economic value of a tile is greater than 10% of its master's money
+ //if neither of these conditions are the case and the revolt fails, the tile's population will be evicerated and the tile
+ //will becomine a coreTile, unable to revolt ever again.
+ void revolt() {}
+
+};
+
+//functions
+
+
+//TODO: figure out a way to pass the board without hard-coding 64 as the array size
+//and without using a vector (because vectors are bloat).
+//"declaration of ‘board’ as multidimensional array must have bounds for all dimensions except the first"
+//there is literally zero reason for this to be the case and gives further reasoning to deprecate C/C++.
+void printBoard(tile board[][64], unsigned int boardsize) {
+ for(int xpos=0; xpos < boardsize; xpos++) {
+ cout << endl;
+ for (int ypos = 0; ypos < boardsize; ypos++) {
+ if (board[xpos][ypos].wilderness) {
+ cout << ".";
+ } else {
+ cout << board[xpos][ypos].owner.icon;
+ }
+ }
+ }
+}
+
+
+int main() {
+
+ //Create the board and all its glory
+ const unsigned int SIZE = 64;
+ const unsigned int CIVAMOUNT = 4;
+ bool gameGoing;
+ char winner;
+ random_device rd;
+ default_random_engine randNumGen(rd());
+ tile playingBoard[SIZE][SIZE];
+ for(int xpos = 0; xpos < SIZE; xpos++) {
+ for (int ypos = 0; ypos < SIZE; ypos++) {
+ playingBoard[xpos][ypos].quasiConstructor(randNumGen);
+ }
+ }
+
+ //Spawning some civilizations
+
+ //this gets to be a vector due to the stack-based situation civilizations will exist in
+ //civlizations can be born or die at a whim, they need to be able to be added to a stack as well.
+ vector<civilization> civlist;
+ uniform_int_distribution<int> randomSpot(0,SIZE);
+
+ //TODO make this create CIVAMOUNT of civilizations, each with a random char representing it
+ civlist.push_back(civilization());
+ civlist.back().quasiConstructor(randNumGen, 'R');
+ playingBoard[randomSpot(randNumGen)][randomSpot(randNumGen)].claim(civlist.back());
+
+ civlist.push_back(civilization());
+ civlist.back().quasiConstructor(randNumGen, 'U');
+ playingBoard[randomSpot(randNumGen)][randomSpot(randNumGen)].claim(civlist.back());
+
+ civlist.push_back(civilization());
+ civlist.back().quasiConstructor(randNumGen, 'J');
+ playingBoard[randomSpot(randNumGen)][randomSpot(randNumGen)].claim(civlist.back());
+
+ //Turn system
+
+ while (gameGoing) {
+ for(civilization civ : civilist) {
+
+ }
+
+ }
+ }
+
+
+ //Check for win condition (3 turns where every tile is owned by one civ)
+
+ //output each turn to a file
+ printBoard(playingBoard, SIZE);
+
+}
+
+/*
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
diff --git a/C++/Polymorphism/input.txt b/C++/Polymorphism/input.txt
new file mode 100644
index 0000000..3f8c45d
--- /dev/null
+++ b/C++/Polymorphism/input.txt
@@ -0,0 +1 @@
+test,test,test