What wrong with my loop .Need to calculate a running total - c#

I have to build some logic to write some sort of scoreboard. The idea is this:
There are many stages:
1st stage you have 2 numbers. 7 and 3=10
2nd stage you have another 2 numbers. 5 and 1 =6
After the loop has finished, the wanted result should be:
Stage 1 total score=15 (total 1st Stage + firstTry of secondStage)
Stage 2 total score=21 (total 1st Stage + (firstTry + SecondTry of SecondStage)
What's wrong with my loop? I dont seem to get the wanted resoult.
private void Calculate(Player player)
{
for (int i = 0; i < player.Game.Stages.Length; i++)
{
int firstThrow = player.Game.Stages[i].FirstTry;
int secondThrow = player.Game.Stages[i].SecondTry;
int sumFirstAndSecond = firstThrow + secondThrow;
//If firstTry + SecondTry==10 is spare
if ((sumFirstAndSecond == 10) && (firstThrow != 10) && i != player.Game.Stages.Length- 1)
{
int stageScore= player.Game.Stages[i].TotalScore +
player.Game.Stages[i + 1].FirstTry;
player.Game.Stages[i].TotalScore = stageScore;
}
}
}
public class Stage
{
public int FirstTry { get; set; }
public int SecondTry { get; set; }
public int TotalScore { get; set; }
}
public class Player
{
public Player(string name)
{
Name = name;
Game = new Game(name);
}
public Game Game { get; set; }
public string Name { get; set; }
}
public class Game
{
public Game(string playerName)
{
PlayerName = playerName;
Stages = new Stage[10];
for (int i = 0; i < Stages.Length; i++)
{
Stages[i] = new Stage();
}
}
public Stage[] Stages { get; internal set; }
public string PlayerName { get; set; }
}

Change this:
private void Calculate(Player player)
{
for (int i = 0; i < player.Game.Stages.Length; i++)
{
int firstThrow = player.Game.Stages[i].FirstTry;
int secondThrow = player.Game.Stages[i].SecondTry;
int sumFirstAndSecond = firstThrow + secondThrow;
if ((sumFirstAndSecond == 10) && (firstThrow != 10) && i != player.Game.Stages.Length- 1)
{
int stageScore= player.Game.Stages[i].TotalScore + player.Game.Stages[i + 1].FirstTry;
player.Game.Stages[i].TotalScore = sumFirstAndSecond + stageScore;
}
else if (i > 0) player.Game.Stages[i].TotalScore = player.Game.Stages[i - 1].TotalScore + sumFirstAndSecond;
}
}

Bowling?
Try this, and do not forget to add misses (as 0).
Should work for both running and final scoring.
// All balls, including misses (0)!
public readonly IList<int> _balls = new List<int>();
private int _currentBall;
public int CalculateTotalScore()
{
int score = 0;
_currentBall = 0;
for (var frame = 0; frame < 10; frame++)
{
if (_currentBall >= _balls.Count)
break;
if (_balls[_currentBall] == 10)
{
// Strrrike!
score += AggregateScoreFromCurrentBall(3);
_currentBall++;
}
else if (AggregateScoreFromCurrentBall(2) == 10)
{
// Spare
score += AggregateScoreFromCurrentBall(3);
_currentBall += 2;
}
else
{
score += AggregateScoreFromCurrentBall(2);
_currentBall += 2;
}
}
return score;
}
private int AggregateScoreFromCurrentBall(int numberOfBallsToSum)
{
var sum = 0;
for (var i = 0; i < numberOfBallsToSum; i++)
if (_currentBall + i < _balls.Count)
sum += _balls[_currentBall + i];
return sum;
}

Related

How to calculate the grade point average(GPA) of students

I want to write a method for a University class that takes a discipline as a parameter and returns the grade point average(GPA) of the students in that discipline, but I don't know how to do it, this is the signature of the method:
public double AverageOfDiscipline(discipline D)
{
double sum = 0;
for (int i = 0; i < students.Length; i++)
{
//???
}
return //????
}
and this is my project:
public enum discipline { Computer, Civil, Mechanical, Electrical}
public enum educationType { Undergraduate, Postgraduate}
class Student
{
public string nameAndLastName;
public double GPA;
public discipline discipline;
public educationType education;
public Student(string nameAndLastName, double GPA, discipline discipline, educationType education)
{
this.nameAndLastName = nameAndLastName;
this. GPA = GPA;
this.discipline = discipline;
this.education = education;
}
public string ToString()
{
return nameAndLastName + ": "+ education + ", "+ discipline+ "; "+"GPA: "+ GPA ;
}
}
class University
{
public string uniName;
public Student[] students;
public double AverageOfDiscipline(discipline D)
{
double sum = 0;
for (int i = 0; i < students.Length; i++)
{
//???
}
return //????
}
}
Thanks for your help.
For the university class you need to iterate through the students array,
check if they are pursuing the specified displine, sum then calculare average as below
public double AverageOfDiscipline(discipline D)
{
double sum = 0;
int totalStudentsForDispline = 0;
for (int i = 0; i < students.Length; i++)
{
if (students[i].discipline == D)
{
sum += students[i].GPA;
totalStudentsForDispline += 1;
}
}
return sum > 0 ? sum / totalStudentsForDispline : 0;
}

Reading string from a file to 2d array

i'm trying to convert a string from file to 2d array. Is it possible to make the array dynamic? And how can i return the array, so i can work with it? I'm really confused.
public interface IMazeLoader
{
Maze LoadMaze(string src);
}
class MazeLoader : IMazeLoader
{
public Maze LoadMaze(string filepath)
{
var width = 5;
var height = 5;
var map = new char[width, height];
var file = new StreamReader(filepath);
string line;
var lineCount = 0;
while ((line = file.ReadLine()) != null)
{
if (lineCount < height)
{
for (int i = 0; i < width && i < line.Length; i++)
{
map[i, lineCount] = line[i];
}
}
lineCount++;
}
file.Close();
return;
}
}
Here is my Maze class :
public class Maze
{
public static readonly char CHAR_WALL = '#';
public static readonly char CHAR_START = 'S';
public static readonly char CHAR_FINISH = 'F';
public int Width { get; internal set; }
public int Height { get; internal set; }
public int StartX { get; private set;}
public int StartY { get; private set; }
public int StartAngle { get; private set; }
private char[,] _plan;
public char[,] Plan { get { return _plan; } }
public Maze(char[,] plan)
{
}
Thanks for any reply, happy Eastern btw. :)
Something like this should allow you to load any rectangular dimension map:
public class MazeLoader : IMazeLoader
{
public Maze LoadMaze(string filepath)
{
char[][] loaded =
File
.ReadLines(filepath)
.Select(x => x.ToCharArray())
.ToArray();
int[] widths =
loaded
.Select(x => x.Length)
.Distinct()
.ToArray();
if (widths.Length != 1)
{
throw new InvalidDataException("Map Data Not Rectangular");
}
else
{
var width = widths[0];
var height = loaded.Length;
var map = new char[height, width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
map[i, j] = loaded[i][j];
return new Maze(map);
}
}
}
Now I tested this on this input:
aaaa
aaaa
bbbb
bbbb
bbbb
And got this map:
Please note that the file orientation and the map orientation are the same. I had to flip around how you were building our your map to get that to work.
Here's my alterations to Maze itself:
public class Maze
{
public int Width => this.Plan.GetUpperBound(1) - this.Plan.GetLowerBound(1) + 1;
public int Height => this.Plan.GetUpperBound(0) - this.Plan.GetLowerBound(0) + 1;
public char[,] Plan { get; private set; }
public Maze(char[,] plan)
{
this.Plan = plan;
}
}

Code not providing desired output

I am currently trying to figure out where I went wrong in this logic of code for the assignment. Any remarks or advice would be appreciated!
The output I keep getting no matter what input I put in keeps giving me:
average temperature 0,
highest temperature 5,
lowest temperature 0,
average excluding lowest 0,
number of cold days 10
First Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TemperatureAverager
{
class Temperatures
{
public double[] weeksTemperatures;
public double threshTemp;
public double average;
public double averageExcludingLowest;
public double highest;
public double lowest;
public int numOfThreshs;
public double[] WeeksTemperatures
{
get
{
return weeksTemperatures;
}
}
public double ThreshTemp
{
get
{
return threshTemp;
}
}
public double Average
{
set
{
average = value;
}
get
{
return average;
}
}
public double AverageExcludingLowest
{
set
{
averageExcludingLowest = value;
}
get
{
return averageExcludingLowest;
}
}
public double Highest
{
set
{
highest = value;
}
get
{
return highest;
}
}
public double Lowest
{
set
{
lowest = value;
}
get
{
return lowest;
}
}
public int NumberOfThreshs
{
set
{
numOfThreshs = value;
}
get
{
return numOfThreshs;
}
}
public Temperatures()
{
}
public Temperatures(double[] wTemperatures, double threshT)
{
weeksTemperatures = wTemperatures;
threshTemp = threshT;
}
public double DetermineAverage()
{
double average = 0;
for (int x = 0; x < 7; x++)
{
average = average + weeksTemperatures[x];
}
average = average / 7;
return average;
}
public double DetermineAverageExcludingLowest()
{
for (int x = 0; x < 7; x++)
{
averageExcludingLowest = averageExcludingLowest + weeksTemperatures[x];
if (weeksTemperatures[x] < lowest)
{
lowest = weeksTemperatures[x];
}
}
averageExcludingLowest = ((averageExcludingLowest - lowest) / 7); // calculate average excluding lowest temperature
return averageExcludingLowest;
}
public double DetermineLowest()
{
for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
{
if (weeksTemperatures[x] < lowest) //find the lowest temperature of the week
{
lowest = weeksTemperatures[x]; //and set it to lowest
}
}
return lowest;
}
public double DetermineHighest()
{
for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
{
if (weeksTemperatures[x] > highest) //find the highest temperature of the week
{
highest = weeksTemperatures[x]; //and set it to highest
}
}
return highest;
}
public double DetermineNumberOfThreshs()
{
for (int x = 0; x < 7; x++) //Traverse through the week's temperatures
{
if (weeksTemperatures[x] < threshTemp) //find the lowest temperature of the week
{
numOfThreshs++;
}
}
return numOfThreshs;
}
public override string ToString()
{
return "=====================\nWeekly Statistics\n" + "---------------------\n" + "Average Temperature: " + average + "\nHighest Temperature: "
+ highest + "\nLowest Temperature: " + lowest + "\nAvg. Excl. Lowest: " + averageExcludingLowest + "\n# of Cold Days: " + numOfThreshs + "\n====================="; //Formats and the invoice to be printed
}
}
}
Second Class
namespace TemperatureAverager
{
class TemperatureApp
{
static void Main(string[] args)
{
double[] week = new double[7];
string[] days = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; //array to track days of week
for (int x = 0; x < 7; x++)
{
Console.Write("What was the temperature on " + days[x] + "?: ");
string userTemperatureInput = Console.ReadLine();
week[x] = double.Parse(userTemperatureInput);
}
Console.Write("How cold is too cold?: ");
string userThreshInput = Console.ReadLine();
double thresh = double.Parse(userThreshInput);
Temperatures weekOne = new Temperatures(week, thresh);
Console.WriteLine(weekOne.ToString());
Console.ReadLine();
}
}
}
Class one
public class Temperatures
{
private double sum;
private int daysInWeek;
public double[] WeeksTemperatures { get; set; }
public double ThreshTemp { get; set; }
public double Average { get; set; }
public double AverageExcludingLowest { get; set; }
public double Highest { get; set; }
public double Lowest { get; set; }
public int NumOfThreshs { get; set; }
public Temperatures(double[] wTemperatures, double threshT)
{
this.WeeksTemperatures = wTemperatures;
this.ThreshTemp = threshT;
sum = 0.0;
daysInWeek = 7;
}
public void GetWeekStatistics()
{
GetSum();
DetermineLowest();
DetermineHighest();
DetermineAverage();
DetermineAverageExcludingLowest();
DetermineNumberOfThreshs();
}
private void GetSum()
{
for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
{
this.sum = this.sum + this.WeeksTemperatures[x];
}
}
public void DetermineLowest()
{
this.Lowest = this.WeeksTemperatures[0];
for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
{
if (this.WeeksTemperatures[x] < this.Lowest) //find the lowest temperature of the week
{
this.Lowest = this.WeeksTemperatures[x]; //and set it to lowest
}
}
}
public void DetermineHighest()
{
this.Highest = this.WeeksTemperatures[0];
for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
{
if (this.WeeksTemperatures[x] > this.Highest) //find the highest temperature of the week
{
this.Highest = this.WeeksTemperatures[x]; //and set it to highest
}
}
}
private void DetermineAverage()
{
this.Average = this.sum / daysInWeek;
}
public void DetermineAverageExcludingLowest()
{
this.AverageExcludingLowest = ((this.sum - this.Lowest) / daysInWeek); // calculate average excluding lowest temperature
}
public void DetermineNumberOfThreshs()
{
for (int x = 0; x < daysInWeek; x++) //Traverse through the week's temperatures
{
if (this.WeeksTemperatures[x] < this.ThreshTemp) //find the lowest temperature of the week
{
this.NumOfThreshs++;
}
}
}
public override string ToString()
{
return "=====================\nWeekly Statistics\n" + "---------------------\n" + "Average Temperature: " + this.Average + "\nHighest Temperature: "
+ this.Highest + "\nLowest Temperature: " + this.Lowest + "\nAvg. Excl. Lowest: " + this.AverageExcludingLowest + "\n# of Cold Days: " + this.NumOfThreshs + "\n====================="; //Formats and the invoice to be printed
}
}
Change in class two
Temperatures weekOne = new Temperatures(week, thresh);
weekOne.GetWeekStatistics();
You have 7 all over the code. Make it a constant like
public static int DaysCount = 7 on the Tempratures class and reference that whenever needed (see example below).
class Temperatures
{
public static int DaysCount = 7;
}
You can then use it like this:
static void Main(string[] args)
{
double[] week = new double[Temperatures.DaysCount];
}
I can't see where you run the Determine...() functions, så the values are never calculated.
In the Determine...() functions you should set the value to an appropriate initial value before you run the loop as in:
public double DetermineHighest()
{
highest = -273 // degrees
for (int x = 0; x < DaysCount; x++) //Traverse through the week's temperatures
{
if (weeksTemperatures[x] > highest) //find the highest temperature of the week
{
highest = weeksTemperatures[x]; //and set it to highest
}
}
return highest;
}

Linked List: Insert item btween current and current.next

Currently I am learning how to build my own linked list in C#. I have created a function called AddTrees that adds a tree to the end of the list. User create their trees through textboxes input:tree_name, tree_height, tree_price and tree_instock. I am requesting help with my InsertTree which is not inserting a tree between current and current.next_tree? But instead of instead of inserting it between, it adds it to the top of the list.
namespace Tree_farm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class TheTrees
{
private string tree_type = " ";
private int tree_height = 0;
public double tree_price = 0;
private int tree_instock = 0;
public TheTrees next_tree;
public TheTrees(string newtree, int newheight, int newinstock, double newprice)
{
tree_type = newtree;
tree_height = newheight;
tree_price = newprice;
tree_instock = newinstock;
next_tree = null;
}
public override string ToString()
{
return tree_type + " " + tree_height + " " + tree_price + " " + tree_instock;
}
}
public class ListForTrees
{
public TheTrees first_tree;
public TheTrees last_tree;
public int count = 0;
public ListForTrees(TheTrees new_tree)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
public ListForTrees()
{
}
public void InsertTree(TheTrees new_tree)
{
TheTrees current = first_tree;
if (count == 0)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
else if (count != 0)
{
if (new_tree.tree_price <= first_tree.tree_price)
{
new_tree.next_tree = first_tree;
first_tree = new_tree;
}
else if (new_tree.tree_price >= last_tree.tree_price)
{
last_tree.next_tree = new_tree;
last_tree = new_tree;
}
else
{
while (new_tree.tree_price > current.next_tree.tree_price)
{
current.next_tree = current;
}
new_tree.next_tree = current.next_tree;
current.next_tree = new_tree;
}
count++;
}
}
public void AddTree(TheTrees new_tree)
{
TheTrees current = first_tree;
if (count == 0)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
}
else if (count != 0)
{
if (new_tree.tree_price <= first_tree.tree_price)
{
new_tree.next_tree = first_tree;
first_tree = new_tree;
}
else if (new_tree.tree_price >= last_tree.tree_price)
{
last_tree.next_tree = new_tree;
last_tree = new_tree;
}
else
{
while (new_tree.tree_price > current.next_tree.tree_price)
{
current = current.next_tree;
}
new_tree.next_tree = current.next_tree;
current.next_tree = new_tree;
}
count++;
}
}
public void ClearTrees()
{
first_tree = null;
count = 0;
}
}
ListForTrees mainlist = new ListForTrees();
private void Form1_Load(object sender, EventArgs e)
{
}
private void BtnInsertTree_Click(object sender, EventArgs e)
{
//Insert Code
try
{
int height = Convert.ToInt32(TxtTreeHeight.Text);
int stock = Convert.ToInt32(TxtTreeStock.Text);
double price = Convert.ToDouble(TxtTreePrice.Text);
TheTrees treeinsert = new TheTrees(TxtTreeName.Text, height, stock, price);
mainlist.InsertTree(treeinsert);
}
catch
{
MessageBox.Show("Please check intput fields");
}
}
private void BtnAddTree_Click(object sender, EventArgs e)
{
try
{
int height = Convert.ToInt32(TxtTreeHeight.Text);
int stock = Convert.ToInt32(TxtTreeStock.Text);
double price = Convert.ToDouble(TxtTreePrice.Text);
TheTrees treeadd = new TheTrees(TxtTreeName.Text, height, stock, price);
mainlist.AddTree(treeadd);
}
catch
{
MessageBox.Show("Please check intput fields");
}
}
}
}
First, I think your expected result is wrong. It appears you want the linked list sorted in order of price (lowest to highest). If so, Cypress should actually go at the end of your list, not in the middle (80.00 > 50.00 > 2.00).
Secondly, I believe the issue rests in your while loop.
while (new_tree.tree_price > current.next_tree.tree_price)
{
current.next_tree = current;
}
I believe what you're trying to do is walk down your linked list. What you're actually doing is mucking up your linked list by changing current.next_tree to point to itself. Changing it to the following is a positive first step towards correcting your algorithm:
while (new_tree.tree_price > current.next_tree.tree_price)
{
current = current.next_tree;
}
EDIT: It should be noted that I do not get the same error that you get in your original post. When I insert items in the same order that you claim to, I get the list in correct sorted order. This is due to the fact that Cypress goes to the end, as expected. How did you get the result that you claim in the question?

C# Class Method to loop through a List created in the same Class

To start off I am new to C# and I am in need of some help. I a class that contains a list. I can set the items in the list from the application but not from the class(where it needs to be done). I am also needing to move an event to class as well. This works in the app as well. Any help with this is greatly appreciated. Below is the code from my class:
namespace CarRace
{
class Cars
{
public string Name { get; set; }
public int StartPOS { get; set; }
public int Speed { get; set; }
public int CurPOS { get; set; }
public double Location { get; set; }
public Cars(string Name, int StartPOS, int Speed, int CurPOS, long Location)
{
this.Name = Name;
this.StartPOS = StartPOS;
this.Speed = Speed;
this.CurPOS = 0;
this.Location = 0;
}
public static int CompareCurrentLocation(Cars c1, Cars c2)
{
return c2.Location.CompareTo(c1.Location);
}
}
}
I am needing to add this to the class:
if (File.Exists("NewRace.xml"))
{
XDocument doc = XDocument.Load("NewRace.xml");
var nCars = doc.Descendants("Car").Select(x => new Cars("", 0, 0, 0, 0)
{
Name = x.Attribute("Name").Value,
StartPOS = int.Parse(x.Attribute("StartPOS").Value),
Speed = int.Parse(x.Attribute("Speed").Value),
CurPOS = 0
});
}
And this:
int p = 1;
int prevPOS = 1;
DateTime? PrevTime;
double dist = 0;
double PrevLoc = 0;
if (i == 0)
{
return;
}
foreach (var car in RaceList)
{
dist = (((car.Speed * 1609.344) * 1) / 1609.344) / 3600;
car.Location = car.Location + dist;
}
Comparison<Cars> compLoc = Cars.CompareCurrentLocation;
RaceList.Sort(compLoc);
PrevTime = DateTime.Now;
foreach (var car in RaceList)
{
if (car.Location != PrevLoc)
{
car.CurPOS = p;
}
else
{
car.CurPOS = prevPOS;
}
prevPOS = car.CurPOS;
PrevLoc = car.Location;
p++;
}
Thanks
Thanks for all of your replies. I tried the car.speed/3600 but only get
back 0 for that so I did the long way. I did get everything working
the way I needed it to.
That's because you are doing an integer division (car.Speed is of type int) so the fractional part of the result is discarded. Since the car's speed is less than 3600 this would hence always result in zero. You can avoid this by casting car.Speed to a double first - this should produce the result you want:
dist = (double)car.Speed / 3600;
Thanks for all of your replies. I tried the car.speed/3600 but only get back 0 for that so I did the long way. I did get everything working the way I needed it to. I am a rookie at this and any tips are greatly appreciated. Below is the code I have updated to.
namespace CarRace
{
class Cars
{
public string Name { get; set; }
public int StartPOS { get; set; }
public int CurPOS { get; set; }
public int Speed { get; set; }
public double Location { get; set; }
public string Make { get; set; }
private static List<Cars> CarList;
private static string ProgPart;
public Cars(string Name, int StartPOS, int CurPOS, int Speed, double Location, string Make)
{
this.Name = Name;
this.StartPOS = StartPOS;
this.CurPOS = CurPOS;
this.Speed = Speed;
this.Location = Location;
this.Make = Make;
}
public static List<Cars> FillList()
{
return CarList;
}
public static void Main()
{
try
{
bool Prog = false;
//Get Part to display
while (!Prog)
{
Console.WriteLine("Select the part you would like to test.(1 or 2)");
ProgPart = Console.ReadLine();
if (ProgPart == "1" || ProgPart == "2")
{
Prog = true;
}
}
Console.WriteLine("----------------------------------------");
//Read XML File and set the CarList
if (File.Exists("NewRace.xml"))
{
XDocument doc = XDocument.Load("NewRace.xml");
var nCars = doc.Descendants("Car").Select(x => new Cars("", 0, 0, 0, 0, "")
{
Name = x.Attribute("Name").Value,
StartPOS = int.Parse(x.Element("StartPOS").Value),
CurPOS = 0,
Speed = int.Parse(x.Element("Speed").Value),
Location = double.Parse(x.Element("Location").Value),
Make = x.Attribute("Make").Value
});
CarList = new List<Cars>();
foreach (var car1 in nCars)
{
if (ProgPart == "1")
{
CarList.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, car1.Speed, car1.Location, car1.Make));
}
else
{
CarList.Add(new Cars(car1.Name, car1.StartPOS, car1.CurPOS, 0, car1.Location, car1.Make));
}
}
}
else
{
Console.WriteLine("File Not Found!");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static int CompareCurrentLocation(Cars c1, Cars c2)
{
return c2.Location.CompareTo(c1.Location);
}
public static Boolean UpdateLeaderBoard(long i)
{
try
{
int p = 1;
int prevPOS = 1;
DateTime? PrevTime;
double dist = 0;
double prevLocation = 0;
if (i == 0)
{
return false;
}
foreach (var car in CarList)
{
if (ProgPart == "2")
{
switch (car.Make)
{
case "300ZX":
//Slow continuous gain of speed by percentage after 60 with top speed of 320.
if (i <= 15)
{
car.Speed = car.Speed + (60 / 10);
}
else if (car.Speed < 320)
{
double percent = (double)(car.Speed * .1);
if ((Convert.ToInt32(car.Speed + percent)) > 320)
{
car.Speed = 320;
}
else
{
car.Speed = Convert.ToInt32(car.Speed + (percent));
}
}
break;
case "Camero":
//0-60 4 seconds 60-220 20 seconds Top 220
if (i <= 4)
{
car.Speed = car.Speed + (60 / 4);
}
else if (i <= 20)
{
car.Speed = car.Speed + 10;
}
break;
case "Mustang":
//0-top(210) 25 seconds
if (car.Speed <= 200)
{
car.Speed = car.Speed + (200 / 20);
}
break;
case "VW Bug":
//Constant Speed
car.Speed = 165;
break;
default:
Console.WriteLine("Make not found");
break;
}
}
//Set Cars Current Location
dist = (((car.Speed * 1609.344) * 1) / 1609.344) / 3600;
car.Location = car.Location + dist;
}
//Sort list by location
Comparison<Cars> compLoc = Cars.CompareCurrentLocation;
CarList.Sort(compLoc);
PrevTime = DateTime.Now;
//Set the Current Position
foreach (var car in CarList)
{
if (car.Location != prevLocation)
{
car.CurPOS = p;
}
else
{
car.CurPOS = prevPOS;
}
prevPOS = car.CurPOS;
prevLocation = car.Location;
p++;
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}

Categories

Resources