Track_Data.cs

For easier viewing download the code on the my code page or copy and paste into your IDE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Capstone_Networking_Project_Test_0
{
    class Track_Data
    {
        public const int NUM_SECTIONS = 19;
        public const int NUM_TURNOUTS = 9;
        private int[] DEFAULT_TURNS = new int[NUM_TURNOUTS] { 0, 0, 0, 1, 0, 0, 0, 0, 1 };

        private int[] sections = new int[NUM_SECTIONS];
        private int[] turnouts; 
        private Train[] trains = new Train[INetwork_Utils.MAX_CLIENTS];
        private int numTrains = -1;
        struct Train
        {
            public int ID { get; set; }
            public int Speed { get; set; }
            public string Username { get; set; }
        }

        public Track_Data()
        {
            ResetTrack();
        }

        public void AdjustTurnout(int index, int direction)
        {
            turnouts[index] = direction;
        }

        public void EnterSection(int section, int direction) //might want to change to bool to test if something went wrong
        {
            sections[section] = direction;
        }

        public void ExitSection(int section)
        {
            sections[section] = INetwork_Utils.OPEN;
        }

        public int MakeTrain(int _i, int _speed, string _username)
        {
            numTrains++;
            Console.WriteLine("Number of trains is: " + numTrains);
            trains[numTrains].ID = _i;
            trains[numTrains].Speed = _speed;
            trains[numTrains].Username = _username;
            return numTrains;
        }

        public void ChangeSpeed(int i, int s)
        {
            trains[i].Speed = s;
        }

        public string GetTurns()
        {
            string str = "";
            for (int i = 0; i < turnouts.Length; i++)
            {
                str += turnouts[i] + INetwork_Utils.DELIM;
            }
            return str;
        }

        public string SpeedAt(int i)
        {
            return trains[i].Speed.ToString();
        }

        public string GetSections()
        {
            string str="";
            for (int i = 0; i < sections.Length; i++)
            { str += sections[i] + INetwork_Utils.DELIM; }
            return str;
        }

        public int RemoveTrain(int id)
        {
            int removedAt = -1;
            for (int i = 0; i < INetwork_Utils.MAX_CLIENTS - 1; i++)
            {
                if (trains[i].ID == id)
                {
                    if (removedAt == -1)
                        removedAt = i;
                    Train temp = trains[i];
                    trains[i] = trains[i + 1];
                    trains[i + 1] = temp;
                }
            }
            numTrains--;
            return removedAt;
        }

        public void ResetTrack()
        {
            turnouts = DEFAULT_TURNS;
            for (int i = 0; i < sections.Length; i++) //initialize the sections to all be open
                sections[i] = INetwork_Utils.OPEN;
        }

        public string GetNameAt(int i)
        { return trains[i].Username; }
    }
}
css.php