Tyson Toller
Game Programmer - C#
Link to Github - https://github.com/HoneyTbone?tab=repositories
Discrete Mathematics Programming Projects
using System;
/* Predator/Prey program that takes user input and displays the amount of bunnies and wolves alive after each month based on set rules */
class Program {
public static void Main (string[] args) {
// simple system of rules defined by the program
Console.WriteLine("The bunny simulation");
Console.WriteLine("___Rules___");
Console.WriteLine("Baby Bunnies turn into adult bunnies after 1 month");
Console.WriteLine("Adult Bunnies die after 3 months");
Console.WriteLine("For every 2 adult bunnies there 2 new baby bunnies");
Console.WriteLine("For every 2 wolves there 1 new wolf ");
Console.WriteLine("Wolves will eat 1 bunny each month");
Console.WriteLine("Each Wolf will die after 3 months or if they dont eat");
Console.WriteLine("Each Wolf only has a 50% chance of catching a bunny");
Console.WriteLine("Note: Wolves eat before both bunnies and wolfs reproduce");
Console.WriteLine("________________________________________");
// Grabs user input for number at month 0
Console.WriteLine("How many Adult Bunnies are there at month 0?");
int adults = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("How many Baby Bunnies are there at month 0?");
int babies = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("How many months should we run the simulation");
int months = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Should we have wolves in this simulation? Type Yes/No");
string wolfQuestion = Console.ReadLine();
int wolves = 0;
// adds wolves to chart
if(wolfQuestion == "yes" || wolfQuestion == "Yes")
{
Console.WriteLine("How many Wolves are there at month 0?");
wolves = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("If we run the simulation for " + months + " months with " + adults + " adults " + babies + " babies and " + wolves + " wolves we get ---");
Console.WriteLine("________________________________________");
Console.WriteLine("Month Adults Babies Wolves");
}
else if (wolfQuestion == "no" || wolfQuestion == "No")
{
Console.WriteLine("If we run the simulation for " + months + " months with " + adults + " adults " + babies + " babies we get---");
Console.WriteLine("________________________________________");
Console.WriteLine("Month Adults Babies");
}
Console.WriteLine("___________________________");
Random rnd = new Random();
// creation of bunny, wolf generations
int currentMonth = 0;
int wolf1 = 0, wolf2 = 0, wolf3 = 0;
int totalWolves = wolves;
int bunny1 = adults, bunny2 = 0, bunny3 = 0 , baby =babies;
int totalBunny = adults;
Simulation();
void Simulation()
{
int i = 0;
while (i < months)
{
// displays chart of information
Console.WriteLine(currentMonth + " " + totalBunny + " " + baby + " " + totalWolves);
int x = 0;
while(x < totalWolves)
{
// 50 percent chance for wolf to catch bunny
int rand = rnd.Next(1,2);
if (rand == 1)
{
Eat();
}
else
{
Die();
}
x++;
}
// Increasing wolf generations, and reproduction
wolf3 = wolf2;
wolf2 = wolf1;
if(totalBunny == 0 && baby == 0)
{
wolf1 = 0;
}
else
{
wolf1 = totalWolves / 2;
}
totalWolves = wolf1 + wolf2 + wolf3;
// The bunnies increase in age and reproduce
bunny3 = bunny2;
bunny2 = bunny1;
bunny1 = baby;
baby = totalBunny;
totalBunny = bunny1 + bunny2 + bunny3;
// increase month and restart for next month
currentMonth++;
i++;
// checks if there are no more wolves and bunnies then displays how many months it took for extinction
if(totalBunny == 0 && totalWolves == 0 && baby == 0)
{
Console.WriteLine("After " + currentMonth + " months all wolves and bunnies have died off");
break;
}
}
}
void Eat()
{
if(totalBunny > 0 || baby > 0)
{
// selects which bunny will die
// if there are no bunnies of that generation, always prios the older generations.
int randBunny = rnd.Next(1,4);
switch(randBunny)
{
case 1:
if(baby > 0)
{
baby = baby - 1;
}
else if(bunny3 > 0)
{
bunny3 = bunny3 - 1;
}
else if (bunny2 > 0)
{
bunny2 = bunny2 - 1;
}
else if (bunny1 > 0)
{
bunny1 = bunny1 - 1;
}
else
{
Die();
}
break;
case 2:
if(bunny1 > 0)
{
bunny1 = bunny1 - 1;
}
else if (bunny3 > 0)
{
bunny3 = bunny3 - 1;
}
else if (bunny2 > 0)
{
bunny2 = bunny2 - 1;
}
else if (baby > 0)
{
baby = baby - 1;
}
else
{
Die();
}
break;
case 3:
if(bunny2 > 0)
{
bunny2 = bunny2 - 1;
}
else if (bunny3 > 0)
{
bunny3 = bunny3 - 1;
}
else if (bunny1 > 0)
{
bunny1 = bunny1 - 1;
}
else if (baby > 0)
{
baby = baby - 1;
}
else
{
Die();
}
break;
case 4:
if(bunny3 > 0)
{
bunny3 = bunny3 - 1;
}
else if (bunny2 > 0)
{
bunny2 = bunny2 - 1;
}
else if (bunny1 > 0)
{
bunny1 = bunny1 - 1;
}
else if (baby > 0)
{
baby = baby - 1;
}
else
{
Die();
}
break;
}
}
else
{
// If there are no more bunnies alive the wolf then instead dies
Die();
}
}
void Die()
{
// selects which wolf will die, if there are none in that generation, will prio older generations.
int randWolf = rnd.Next(1,3);
switch(randWolf)
{
case 1:
if(wolf1 > 0)
{
wolf1 = wolf1 - 1;
}
else if (wolf3 > 0)
{
wolf3 = wolf3 - 1;
}
else if (wolf2 > 0)
{
wolf2 = wolf2 - 1;
}
break;
case 2:
if(wolf2 > 0)
{
wolf2 = wolf2 - 1;
}
else if (wolf3 > 0)
{
wolf3 = wolf3 - 1;
}
else if (wolf1 > 0)
{
wolf1 = wolf1 - 1;
}
break;
case 3:
if(wolf3 > 0)
{
wolf3 = wolf3 - 1;
}
else if (wolf2 > 0)
{
wolf2 = wolf2 - 1;
}
else if (wolf1 > 0)
{
wolf1 = wolf1 - 1;
}
break;
}
}
}
}
using System;
/* Takes the input of a positive whole number between one and one billion and convert it to the base
of the users choice between 2 and 16 */
class Program {
public static void Main (string[] args)
{
double baseTen = 0;
double baseTenOutput = UserInput(baseTen);
while (baseTenOutput < 0)
{
baseTenOutput = UserInput(baseTen);
}
while(baseTenOutput > 100000000000000)
{
baseTenOutput = UserInput(baseTen);
}
Console.WriteLine("_____________________________");
Console.WriteLine("What base would you like to convert " + baseTenOutput);
Console.WriteLine("Input a number between 2 and 16");
int convertBase = Convert.ToInt32(Console.ReadLine());
double result = baseTenOutput;
string convertOutput = "";
output();
Console.WriteLine("Reading Remainders from bottom to top.");
Console.WriteLine("_____________________________");
if(convertBase == 2)
{
while (convertOutput.Length % 4 != 0)
{
convertOutput = convertOutput + "0";
}
}
convertOutput = ReverseString(convertOutput);
Console.WriteLine(baseTenOutput + " is " + convertOutput + " in base " + convertBase);
///////////////////////////////////////////////////////////////////////////////////
void output()
{
while (Math.Floor(result) != 0)
{
double r = -1;
r = result % convertBase;
Console.WriteLine("|" + result + " / " + convertBase + "|");
result = Math.Floor(result / convertBase);
r = Math.Floor(r);
Console.WriteLine("|Quotient is " + result + "|");
string h = Convert.ToString(r);
//checks if the remainder is a two digit number, if it is, sets the number to its corresponding letter
switch(r)
{
case 10:
h = "A";
break;
case 11:
h = "B";
break;
case 12:
h = "C";
break;
case 13:
h = "D";
break;
case 14:
h = "E";
break;
case 15:
h = "F";
break;
default:
//left blank on purpose
break;
}
Console.WriteLine("|Remainder is " + r + "|");
convertOutput = convertOutput + h;
}
}
///////////////////////////////////////////////////////////////////////////////////
// Reverses the output string so its in correct order
static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
///////////////////////////////////////////////////////////////////////////////////
// The user selects a valid number
static double UserInput(double baseTenOutput)
{
Console.WriteLine ("Input a number between 0 and 1,000,000,000,000(Trillion)");
baseTenOutput = Convert.ToDouble(Console.ReadLine());
if(baseTenOutput < 0)
{
Console.WriteLine("Input a positive number please.");
}
else if (baseTenOutput > 1000000000000)
{
Console.WriteLine("Please input a smaller number");
}
return baseTenOutput;
}
}
/* Computer Science Project for Math
Chapter 1 Question 2 in Cpmputer Science Projects
Tyson Toller
October 1st 2021 */
/* Takes an input of two strings and finds the
AND, OR and XOR of the collective characters (includes spaces) */
using System;
public class Program {
public static void Main(string[] args) {
// inputed strings from user
string stringOne;
string stringTwo;
string or = "";
string and = "";
string xor = "";
// stores temp values
string resultOr = "";
string resultAnd = "";
string resultXor = "";
Console.WriteLine("Compiles Two Integers To find bitwise 'OR' 'AND' and 'XOR'");
////////////////////////////////////////////////////////////////////////////////////
// Input of String One
Console.WriteLine("Input a string with 'n' characters to represent string one.");
stringOne = Console.ReadLine();
// Input of String Two
Console.WriteLine("Input a string with 'n' characters to represent string two.");
stringTwo = Console.ReadLine();
// Stores the values of each character from stringOne and stringTwo
char y;
char z;
// Prints the Strings to the console
Console.WriteLine("String One is " + stringOne);
Console.WriteLine("String Two is " + stringTwo);
////////////////////////////////////////////////////////////////////////////////////
// Finds 'OR' for the Two Strings
for (int i = 0; i < stringOne.Length; i ++) // adds the chars from stringOne
{
y = stringOne[i];
resultOr = resultOr + y;
}
for (int i = 0; i < stringTwo.Length; i ++) // adds the chars from stringTwo
{
z = stringTwo[i];
resultOr = resultOr + z;
}
for (int i = 0; i < resultOr.Length; i ++) // Removes duplicate chars
{
if(!or.Contains(resultOr[i]))
{
or += resultOr[i];
}
}
Console.WriteLine("'OR' for the two strings is " + or);
////////////////////////////////////////////////////////////////////////////////////
// Finds 'AND' for the Two Strings
for (int i = 0; i < stringOne.Length; i ++) // Checks all of stringOne
{
y = stringOne[i];
for (int c = 0; c < stringTwo.Length; c ++) // Checks all of stringTwo
{
z = stringTwo[c];
if (y==z)
{
// If char z = char y, we add it to temp 'AND'
resultAnd = resultAnd + z;
}
}
}
for (int i = 0; i < resultAnd.Length; i ++) // Removes duplicate chars
{
if(!and.Contains(resultAnd[i]))
{
and += resultAnd[i];
}
}
Console.WriteLine("'AND' for the two strings is " + and);
////////////////////////////////////////////////////////////////////////////////////
// Prints the XOR for the Two Strings
if (String.Equals(stringOne, stringTwo))
{
Console.WriteLine("'XOR' for the two strings is 'NO RESULT'! The two strings are the same.");
}
else
{
for (int i = 0; i < stringOne.Length; i ++) // Checks stringTwo to stringOne
{
y = stringOne[i];
if(stringTwo.Contains(y))
{
/////////// Left blank on Purpose
}
else
{
resultXor = resultXor + y;
}
}
for (int i = 0; i < stringTwo.Length; i ++) // Checks stringOne to stringTwo
{
z = stringTwo[i];
if(stringOne.Contains(z))
{
/////////// Left blank on Purpose
}
else
{
resultXor = resultXor + z;
}
}
for (int i = 0; i < resultXor.Length; i ++) // Removes duplicate chars
{
if(!xor.Contains(resultXor[i]))
{
xor+= resultXor[i];
}
}
}
Console.WriteLine("'XOR' for the two strings is " + xor);
/////////////////////////////////////////////////////////////////////////////
Console.WriteLine("Duplicates of characters are removed.");
}
}