using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Media; namespace TrackMaker4 { //This form runs a track made in form1 (the edit mode) public partial class Run : Form { const int MAX_TRAINS = 4; //Piece Id Numbers const int VERT = 0;//Vertical const int HORIZ = 1;//Horizontal const int TLP = 2;//Top Left Piece const int TRP = 3;//Top Right Piece const int BLP = 4;//Bottom Left Piece const int BRP = 5;//Bottom Right Piece //Sides of a Piece const int LEFT = 0; const int UP = 1; const int RIGHT = 2; const int DOWN = 3; public Point TrainControlPoint;//A spot on the screen where the train controls will be located public String[] Colors;//Holds potential train colors public String[] Available_Colors;//Holds available colors //Makes lables that show x and y values GroupBox gbxIndexLabelsR; GroupBox gbxIndexLabelsC; GroupBox gbxRunGrid;//A groupbox where the track goes Square [] turnouts;//Record the Turnouts //Meant to hold the dimentions of the track int xvals; int yvals; Square[,] taken_spots;//2-D Array to store track squares List Trains = new List();//A list that holds the train Queue Current_Color = new Queue();//This Queue helps visualize trains moving //It holds the colors of the track where trains are, so they can be replaced when the trains move List Tcontrollers = new List();//A list of groupboxes to be used as controllers for trains private int Tcnt = 0;//A counter indicating the number of trains string Dir;//Directory Image[] Piece_Images;//An array holding the images for pieces public Run() { InitializeComponent(); Dir = Environment.CurrentDirectory;//Holds current directory //An array of images for a piece Piece_Images = new Image[6] { Image.FromFile(Dir + "\\verticalP.jpg"), Image.FromFile(Dir+" \\horizontalP.jpg"), Image.FromFile(Dir + "\\TLP.jpg"), Image.FromFile(Dir + "\\TRP.jpg"), Image.FromFile(Dir + "\\BLP.jpg"), Image.FromFile(Dir + "\\BRP.jpg") }; //An array of names of colors that trains can be Colors = new String[5] { "Red", "Blue", "Orange", "Green", "Purple" }; Available_Colors = new String[5];//This is used to make sure trains cant be the same color(string array) //At first, add all colors in. Remove them as you use them later for (int i = 0; i < Colors.Count(); i++) { Available_Colors[i] = Colors[i]; } } private void Run_Load(object sender, EventArgs e) { Setboard(this);//Setboard is a function in Run.cs that creates a track to run a train on //Color the spot where the trains are foreach (Square s in gbxRunGrid.Controls)//foreach box in the grid { for (int i = 0; i < Trains.Count; i++)// look at all the trains { Train CurT = Trains[i];//Current Train if (s.X == taken_spots[CurT.Tx, CurT.Ty].X && s.Y == taken_spots[CurT.Tx, CurT.Ty].Y)//If a train is at that spot, { Current_Color.Enqueue(s.BackColor);//Store track color s.BackColor = CurT.Clr;//Set track color to train color to visualize the train } if (s.X == taken_spots[CurT.Tx, CurT.Ty].X && s.Y == taken_spots[CurT.Tx, CurT.Ty].Y)//%%DOn't Think I need this. Redundant %% { s.BackColor = CurT.Clr; } } s.Enabled = false;//I dont want these to be clickable until the track's in/outs have been defined } foreach(GroupBox g in Tcontrollers)//Disables controlls { g.Enabled = false; } } //********************************GET/SETS*****************************// public int Xvals//Width of grid (in Squares) { set { xvals = value; } get { return xvals; } } public int Yvals//Height of grid (in Squares) { set { yvals = value; } get { return yvals; } } public Square [,] TakenSpots//an array to store the track in { set {taken_spots = value;}//end set Square get { return taken_spots; } } public Square[] Turnouts//Record the Turnouts { set { turnouts = value;} get { return turnouts; } } //********************************END GET/SETS***********************// //** NEVER IMPLEMENTED //private void bttnLights_Click(object sender, EventArgs e) //{ // Button clicked = (Button)sender; // if (clicked.Text == "Off") // { // clicked.Text = "On"; // clicked.BackColor = Color.FromName("black"); // clicked.ForeColor = Color.FromName("yellow"); // } // else // { // clicked.Text = "Off"; // clicked.BackColor = Color.FromName("black"); // clicked.ForeColor = Color.FromName("white"); // } //} //Sets Squares on the board according to Array from Form1 (sets up the track) private void Setboard(object sender) { int boarder = (30 / 2); //Make a GroupBox for the track and other Squares to go in (Board) gbxRunGrid = new GroupBox(); //Set values for the (Board) gbxRunGrid.Width = Xvals * 30 + boarder; gbxRunGrid.Height = Yvals * 30 + boarder; gbxRunGrid.Location = new Point(boarder, boarder*2); gbxRunGrid.Text = "Track"; //Make labels next to it to index rows and columns gbxIndexLabelsR = new GroupBox(); gbxIndexLabelsC = new GroupBox(); //Set dimentions for Rows Indes Label gbxIndexLabelsR.Width = 30+(boarder*2); gbxIndexLabelsR.Height = gbxRunGrid.Height; //Go to the right hand side of the (Board) int IndX = gbxRunGrid.Location.X + gbxRunGrid.Width+boarder; int IndY = boarder*2;//Start at the same height as the board gbxIndexLabelsR.Location = new Point(IndX, IndY); Controls.Add(gbxRunGrid);//Add the (Board) to the form Controls.Add(gbxIndexLabelsR);//Add the Rows Indes Label to the form IndX = boarder;//This size is relative to the gbxIndexLabelsR IndY = 25;//This is as well for (int i = 0; i < xvals; i++) { //*********************Put values in Vertical Bar***************// gbxIndexLabelsR.Controls.Add(new Label { Location = new Point(IndX, IndY), Width = 30,//30 and 25 just fit the best. Height = 25, Text = i.ToString(), }); IndY+=27;//This also was just guess and check. It fits the best //*********************End Put values in Vertical Bar***************// //*********************Populate Board**************************// for (int j = 0; j < yvals; j++) { Square tempS = taken_spots[i,j];//Look at each Square in the array if (tempS.Is_Active==false)//Make a default Square if not active { gbxRunGrid.Controls.Add(new Square//Add a default plain square. Holds no pieces { Image = tempS.Image, Name = tempS.Name, Text = i.ToString() + "," + j.ToString(), Location = new Point(i * (30 - 3) + boarder, j * (30 - 3) + boarder),//No science to the -3 just took gaps out Size = new Size(30, 30), Is_Turnout = false, Is_Active = false, X = i, Y=j, }); }//endif else //Taken from the array. Not Default { gbxRunGrid.Controls.Add(new Square//Add a Square to the form from the array { Is_Turnout = tempS.Is_Turnout, //Picture Image = tempS.Image, Location = new Point(i * (30 - 3) + boarder, j * (30 - 3) + boarder),//No science to the -3 just took gaps out Size = new Size(30, 30), CurrentPiece = tempS.CurrentPiece, Is_Active = tempS.Is_Active,//If it gets in here it is active Pieces = tempS.Pieces, X = tempS.X, Y = tempS.Y, }); }//end else }//End for j }//End for i //*********************End Populate Board**************************// //**************************Put Values in Horizontal Bar***************// //Dimensions gbxIndexLabelsC.Height = 30 + boarder; gbxIndexLabelsC.Width = gbxRunGrid.Width; IndX = gbxRunGrid.Width + gbxRunGrid.Location.X; //Location IndY = gbxRunGrid.Height + gbxRunGrid.Location.Y; IndX = gbxRunGrid.Location.X; gbxIndexLabelsC.Location = new Point(IndX, IndY); Controls.Add(gbxIndexLabelsC); //Populate with labels for indexed Columns IndX = 25; IndY = boarder; for (int i=0;i< Yvals;i++) { gbxIndexLabelsC.Controls.Add(new Label { Location = new Point(IndX, IndY), Width = 25,//Again, 25 and 25 just fit best. Guess and check Height = 25, Text = i.ToString(), }); IndX += 27;//27 just fit best. guess and check } //**************************Put Values in Horizontal Bar***************// //Apply addiotional attributes to Squares on the board foreach (Square S in gbxRunGrid.Controls) { if (S.Is_Active==true)//If the Square is active { if (S.Is_Turnout == true)//If the square is a turnout { S.Click += Grid_Button_Click;//Add the event handler S.Set_Current_Piece(0);//Set the first piece as the current piece S.Image = Piece_Images[S.CurrentPiece.Id];//Set the correct image S.BackColor = Color.Black;//Black to indicate turnout S.Set_Current_Piece(0);//%%Redundant%% //*******Turnout Lookup********** //TurnoutLookup.Add } } S.FlatAppearance.BorderSize = 0;//Changes the look of the buttons } //Figure out where to put the train controls //x,y,width,height //**Get initial height, width, and x/y locations //x = columns index label's width + rows index label's width + boarder * 3 //(boarder * 3 is to account for seperation between rows and columns labels, and train controls) int x = gbxIndexLabelsC.Width+gbxIndexLabelsR.Width+(boarder*3); int y = gbxTrain1.Location.Y; int width = gbxTrain1.Width; int height = gbxTrain1.Height; //**For first train control gbxTrain1.SetBounds(x, y, width, height);//Train controller 1 x += gbxTrain1.Width + 30;//Move over to the right //**For second train control gbxTrain2.SetBounds(x, y, width, height);//Train controller 2 x = gbxIndexLabelsC.Width + gbxIndexLabelsR.Width + (boarder * 3);//move back over to the left y += gbxTrain1.Height + 30;//move it down //**For third train control gbxTrain3.SetBounds(x, y, width, height);//Train Controller 3 x += gbxTrain1.Width + 30;//move it over //**For fourth train control gbxTrain4.SetBounds(x, y, width, height);//Train Controller 4 //Adjust some location things y += gbxTrain4.Height + 30; bttnStopTrains.SetBounds(x, y, bttnStopTrains.Width, bttnStopTrains.Height); x = bttnStopTrains.Location.X; x -= bttnStopTrains.Width; x -= 30; bttnGoTrains.SetBounds(x, y, bttnGoTrains.Width, bttnGoTrains.Height); //**Add the gbxTrain's to a list of controllers Tcontrollers.Add(gbxTrain1); Tcontrollers.Add(gbxTrain2); Tcontrollers.Add(gbxTrain3); Tcontrollers.Add(gbxTrain4); //**Changes the bounds of this form to see all controls this.Width = gbxTrain2.Location.X + gbxTrain2.Width + boarder; int h = bttnGoTrains.Location.Y + bttnGoTrains.Height; //Changed depending on the furthest right and down things on the form if (gbxIndexLabelsC.Location.Y + gbxIndexLabelsC.Height > h) this.Height = gbxIndexLabelsC.Location.Y+gbxIndexLabelsC.Height+(3* boarder); else this.Height = h+(3* boarder); //Hide the Train Controllers until their trains are created. foreach (GroupBox g in Tcontrollers) { g.Hide(); } }//End Setboard //**Adds a train to the form private void addTrainToolStripMenuItem_Click(object sender, EventArgs e) { if(Tcnt>=MAX_TRAINS)//Makes sure you don't go over the limit for number of trains { MessageBox.Show("You can only have " + MAX_TRAINS.ToString() + " Trains."); return; } NewTrain NT = new NewTrain();//Bring up form for creation of new train //Needs Xvals and Yvals and the TakenSpots Array NT.Xvals = xvals; NT.Yvals = yvals; //Needs to know how many trains they're are currently for the Id NT.Id = Tcnt; NT.TakenSpots = TakenSpots;//Shallow copy of the board NT.TColors = Available_Colors;//We only want colors that arent taken. Makes unique trains // NT.TColors = Colors; NT.ShowDialog(); if(NT.DialogResult == DialogResult.OK) { Train newt = new Train(NT.NewT);//Make a new train newt.Id = Tcnt;//Set Id of the new train bool good_spot=true;//Used to see if the train was put in an acceptable spot for(int i=0; i Qtrains = new Queue(); //stuff all the trains in a queue foreach(Train t in Trains) Qtrains.Enqueue(t); while(!(Qtrains.Count==0)) { Train CurT = Qtrains.Dequeue(); if (CurT.Speed != 0 && CurT.Stopped == false) { CurT.SpeedCntr++; if (CurT.Speed <= CurT.SpeedCntr) { //** These foreach loops arent great for efficiency by they show where the train is foreach (Square s in gbxRunGrid.Controls)//Put Text and Color back { if (s.X == taken_spots[CurT.Tx, CurT.Ty].X && s.Y == taken_spots[CurT.Tx, CurT.Ty].Y) { s.BackColor = Current_Color.Dequeue(); s.Text = ""; } } MoveTrain(CurT, this);//Moves the train based on direction. Check the function bellow // tbxTrainTest.Text = "ID:" + CurT.Id.ToString() + " X:" + CurT.Tx.ToString() + " Y:" + CurT.Ty.ToString();//Testing%% foreach (Square s in gbxRunGrid.Controls) { if (s.X == taken_spots[CurT.Tx, CurT.Ty].X && s.Y == taken_spots[CurT.Tx, CurT.Ty].Y) { Current_Color.Enqueue(s.BackColor);//Color the track the color of the train s.BackColor = CurT.Clr; s.Text = CurT.Id.ToString();//Change the text of the square } } CurT.SpeedCntr = 0;//Reset the speed counter } } } //%%This is still problamatic if trians are going at eachother at different speeds%% Check_Collision();//Check to see if the trains have collided } //**Moves train in a direction based on ins/outs of piece it's on private void MoveTrain(Train A, object sender) { int next=10; Square prev = TakenSpots[A.Tx, A.Ty];//Looks at the square the train is on if(A.Clockwise == true)//If clockwiase, move based on the outs next = this.TakenSpots[A.Tx, A.Ty].CurrentPiece.Out; else if (A.Clockwise == false)//if counterClockwise, move based on ins { next=this.TakenSpots[A.Tx, A.Ty].CurrentPiece.In; } if(next == LEFT) { A.Tx --; } else if(next ==UP) { A.Ty --; } else if (next ==RIGHT) { A.Tx ++; } else if(next ==DOWN) { A.Ty ++; } //Check to see if the train was derailed Square cur = TakenSpots[A.Tx, A.Ty]; if (Derailed(cur, prev,A)) { SoundPlayer simpleSound = new SoundPlayer(Dir+"\\Explosion.WAV"); simpleSound.Play(); bttnStop1.PerformClick();//Stop all the trains, and disable things accordingly A.Stopped = true; bttnGoTrains.Enabled = false;//Disable "Ready" Button bttnStopTrains.Enabled = false;//Disable Stop Trains Button foreach(GroupBox g in Tcontrollers)//Disable all train controllers { Tcontrollers[A.Id].Enabled = false; } Tcontrollers[A.Id].Tag = -1;//Disassociate the controllers with the trains. Just to be thurough MessageBox.Show("Train " + A.Id.ToString() + " crashed at " + A.Tx + "," + A.Ty + "."+ " Derailed!"); derail_to_file(A.Tx, A.Ty, A.Id.ToString(), cur.CurrentPiece.In, prev.CurrentPiece.Out); } } //**This determines the ins and outs of the track pieces right before running void In_and_Outs() { //%%NONE of this ERROR CHECKS FOR GOING OFF THE TRACK YET! //%%ALSO DOESN'T ACCOUNT FOR NON-CYCLICAL TRACKS //Keep track of visited spots int[,] Visited = new int[xvals, yvals]; //Fill spots with 0's for (int i = 0; i < Xvals; i++) for (int j = 0; j < Yvals; j++) Visited[i, j] = 0; Queue all_trains = new Queue();//Enqueue all the trains for(int i=0; i io_touts = new List();//A list of Turouts //When the function comes up on a turnout, it saves the turnout, //It also saves where the "train" was going when it "got there" List WasUp = new List(); List WasDown = new List(); List WasLeft = new List(); List WasRight = new List(); //** Lets map out the ins/outs for the track while ((all_trains.Count == 0)==false)//** while queue isn't empty... { Train Temp = all_trains.Dequeue();//Look at the next train //Grab the location of the train int x = Temp.Tx; int y = Temp.Ty; int orignx=x; int origny=y; //bool goRight = true;//Init values to avoid warning // bool goUp = true; //Four ints. Stands for Up, Down, Left, and Right. //If the int is a 0, it is disregarded. //If the int is a 1, the train went that direction recently... //(the last direction the train went before changing direction) //If the int is a 2, that is the direction it just went. int U = 0; int D = 0; int L = 0; int R = 0; //** Set where train "came from" (how it will get into that piece later) //(Trains are created with input from the user telling where the train is going to and coming from) if (Temp.Coming_From == LEFT) { R = 2; }//Came from left. Going Right else if (Temp.Coming_From == UP) { D = 2; } else if (Temp.Coming_From == RIGHT) { L = 2; } else if (Temp.Coming_From == DOWN) { U = 2; } if (Visited[x, y] != 0)//If the spot was visited at this point... { //...then the track need not be defined from that train continue; } else//Otherwise, define the track from the current train { do //While There are more turnouts D1 // { if (turnout_index >= 0)//If there are any turnouts to go back and define previous passes { Square current_turnout = io_touts[0];//Look at the first turnout in the list current_turnout.Set_Current_Piece(0);//Current piece gets first piece x = current_turnout.X; y = current_turnout.Y; } orignx = x; origny = y; do//D2 { //while not back at start, or we havent visited everything on that cycle if (taken_spots[x, y].Is_Turnout == true) { //If the spot has never been visited if ((Visited[x, y]) == 0) { Visited[x, y] = 1;//check visited once turnout_index++;//move index up io_touts.Add(taken_spots[x, y]);//Add turnout to //Set Piece2 bool advance = false; bool set = true; //Set Piece1 taken_spots[x, y].Set_Current_Piece(0); advance = true; SetPiece(ref x, ref y, ref U, ref D,ref L, ref R, advance,set); //Add current state of directions to lists WasUp.Add(U); WasDown.Add(D); WasLeft.Add(L); WasRight.Add(R); } //If the spot was visited once else if (Visited[x, y] == 1) { Visited[x, y] = 2; //Set Piece2 int ind = 0; if (io_touts.Contains(taken_spots[x, y])) { for(int i=0; i 0);//While all turnouts have been covered }//end of else visited //Make sure all spots are visited%% }//end while no more trains to map from } //**Gets the location of the piece, the directional ints, and whether to advance to the next piece or set the piece //(Not sure if advance or set_io are necessary with the new implementation)%% void SetPiece(ref int x,ref int y, ref int u, ref int d,ref int l, ref int r,bool advance,bool set_io) { //*** This function sets the in and out of a piece based on a couple of things ***// //*** 1) The type of piece. 2)The two directions most recently changed to ***/// //Get The Corners First if (taken_spots[x, y].CurrentPiece.Id == TRP)//Top Right Piece { //set in/out if (r == 2)//Went RIGHT into the piece { if (set_io) { taken_spots[x, y].CurrentPiece.In = LEFT; taken_spots[x, y].CurrentPiece.Out = DOWN; } if (advance) { u = 0;//Now We're Going Down d = 2; l = 0; r = 1; y++; } } else if (u == 2)//Went UP into the piece { if (set_io) { taken_spots[x, y].CurrentPiece.In = DOWN; taken_spots[x, y].CurrentPiece.Out = LEFT; } if (advance)//Only wanna move if this is the piece we're focusing on { //Now We're Going Left u = 1; d = 0; l = 2; r = 0; x--; } } } else if (taken_spots[x, y].CurrentPiece.Id == TLP)//Top Left Piece {//set in/out if (u == 2)//Went UP into the piece { if (set_io) { taken_spots[x, y].CurrentPiece.In = DOWN; taken_spots[x, y].CurrentPiece.Out = RIGHT; } if (advance)//Only wanna move if this is the piece we're focusing on { u = 1;//Now We're Going Right d = 0; l = 0; r = 2; x++; } } else if (l == 2)//went left into the piece { if (set_io) { taken_spots[x, y].CurrentPiece.In = RIGHT; taken_spots[x, y].CurrentPiece.Out = DOWN; } if (advance)//Only wanna move if this is the piece we're focusing on { u = 0;//Now We're Going Down d = 2; l = 1; r = 0; y++; } } } else if (taken_spots[x, y].CurrentPiece.Id == BRP)//Bottom Right Piece { //set in/out if (d == 2)//Went DOWN into the piece { if (set_io) { taken_spots[x, y].CurrentPiece.In = UP; taken_spots[x, y].CurrentPiece.Out = LEFT; } if (advance)//Only wanna move if this is the piece we're focusing on { u = 0;//Now We're Going left d = 1; l = 2; r = 0; x--; } } else if (r == 2)//Went RIGHT into the piece { if (set_io) { taken_spots[x, y].CurrentPiece.In = LEFT; taken_spots[x, y].CurrentPiece.Out = UP; } if (advance)//Only wanna move if this is the piece we're focusing on { u = 2;//Now We're Going Up d = 0; l = 0; r = 1; y--; } } } else if (taken_spots[x, y].CurrentPiece.Id == BLP)//Bottom Left Piece (shaped like an L) { //set in/ if (l == 2)//Came in in going LEFT { if (set_io) { taken_spots[x, y].CurrentPiece.In = RIGHT; taken_spots[x, y].CurrentPiece.Out = UP; } if (advance)//Only wanna move if this is the piece we're focusing on { u = 2;//Now We're Going Up d = 0; l = 1; r = 0; y--; } } else if (d == 2)//Came in going DOWN { if (set_io) { taken_spots[x, y].CurrentPiece.In = UP; taken_spots[x, y].CurrentPiece.Out = RIGHT; } if (advance)//Only wanna move if this is the piece we're focusing on { u = 0;//Now We're Going Right d = 1; l = 0; r = 2; //goRight = true; x++; } } } //Now for the Straight lines else if (taken_spots[x, y].CurrentPiece.Id == VERT)//Vertical Piece { if (u != 0)//Going Up { if (set_io) { taken_spots[x, y].CurrentPiece.In = DOWN; taken_spots[x, y].CurrentPiece.Out = UP; } if (advance)//Only wanna move if this is the piece we're focusing on { u = 2; y--; } } else if(d!=0)//Going Down { if (set_io) { taken_spots[x, y].CurrentPiece.In = UP; taken_spots[x, y].CurrentPiece.Out = DOWN; } if (advance)//Only wanna move if this is the piece we're focusing on { d = 2; y++; } } //Whatever direction the train was going before (right/left) it's 1 now, because up or down is 2 if (r != 0) r = 1; if (l != 0) l = 1; } else if (taken_spots[x, y].CurrentPiece.Id == HORIZ)//Horizontal { if (r!=0)//Going Right { if (set_io) { taken_spots[x, y].CurrentPiece.In = LEFT; taken_spots[x, y].CurrentPiece.Out = RIGHT; } if (advance)//Only wanna move if this is the piece we're focusing on { r = 2; x++; } } else if (l != 0)//Going LEFT { if (set_io) { taken_spots[x, y].CurrentPiece.In = RIGHT; taken_spots[x, y].CurrentPiece.Out = LEFT; } if (advance)//Only wanna move if this is the piece we're focusing on { l = 2; x--; } } //Whatever direction the train was going before right/left, it's a 1 now, because right or left is a 2 if (u != 0) u = 1; if (d != 0) d= 1; } } //Handler for turnout clicks on the track private void Grid_Button_Click(object sender, EventArgs e) { Square sClicked = (Square)sender; //Get location of sqauare int x = sClicked.X; int y = sClicked.Y; if (sClicked.Toggled == false)//If it was on the first piece { sClicked.Set_Current_Piece(1);//switch current piece to the second piece taken_spots[x, y].Set_Current_Piece(1);//Set correct image } else if (sClicked.Toggled==true) { sClicked.Set_Current_Piece(0);//set current to first piece on board taken_spots[x, y].Set_Current_Piece(0);//set the current piece in array to first %%Not sure if needed } } //**Event handler for the numeric_up_down in train controllers private void nudSpeed_ValueChanged(object sender, EventArgs e) { NumericUpDown nud = (NumericUpDown)sender; //which groupbox am I in? // GroupBox par = (GroupBox)nud.Parent;//Look at the groupbox I'm in foreach(Train t in Trains)//find the associated train { if (t.Id == Convert.ToInt32(nud.Tag))//if we found it { if (Convert.ToInt32(nud.Value) == 0) t.Speed = 0; else t.Speed = 10 - Convert.ToInt32(nud.Value);//change the speed } } } //**Check to see if two trains hit eachother. //This function just looks at all the trains and sees if any are on the same Square //If so, the sound plays, the correct controlls are disabled, and the user is notified of the crash private void Check_Collision() { Train CurT; Train CheckT; //List Collisions = new List(); //look at trains 1 by 1 for (int i = 0; i < Trains.Count; i++) { CurT = Trains[i]; //see if that train hit any of the other trains for (int j = 0; j < Trains.Count; j++) { CheckT = Trains[j]; if (CheckT != CurT)//account for comparint train against itsself { //if we have a hit, record it %% //if we have a hit stop and tell user if ((CurT.Tx == CheckT.Tx) && (CurT.Ty == CheckT.Ty)) { SoundPlayer simpleSound = new SoundPlayer(Dir + "\\Crash.WAV"); simpleSound.Play(); bttnStopTrains.PerformClick(); MessageBox.Show("Collision of Tester and Tester1"); bttnGo1.Enabled = false; Tcontrollers[CurT.Id].Enabled = false; Tcontrollers[CurT.Id].Tag = -1; bttnGoTrains.Enabled = false; bttnStopTrains.Enabled = false; CurT.Speed = 0; CheckT.Speed = 0; }//end check crash }//end if chckt != curt }//end for j->Trains.Count }//end for i->Trains.Count }//end Check_Collision ////**Appends info inside to a file called DirectionDump.txt //private void InProgressDump(int something) //{ // // From https://msdn.microsoft.com/en-us/library/system.io.file.appendtext(v=vs.110).aspx // if (!File.Exists(Dir.ToString() + "\\DirectionDump.txt")) // { // // Create a file to write to. // using (StreamWriter sw = File.CreateText(Dir.ToString() + "\\DirectionDump.txt")) // { // sw.WriteLine("Directions"); // } // } // // This text is always added, making the file longer over time // // if it is not deleted. // using (StreamWriter sw = File.AppendText(Dir.ToString() + "\\DirectionDump.txt")) // { // sw.WriteLine(something.ToString()); // } //} //see if next move is ok //Checks out of previous square with the in of the current square. If the two dont line up, the train is derailed //IF derailed, true is returned, if not, false is returned bool Derailed(Square current, Square previous,Train T) { int cid = current.CurrentPiece.Id; int pid = previous.CurrentPiece.Id; if (T.Clockwise) { if (current.Is_Active == false || previous.Is_Active == false) return true; if (previous.CurrentPiece.Out == RIGHT && current.CurrentPiece.In == LEFT) return false; else if (previous.CurrentPiece.Out == DOWN && current.CurrentPiece.In == UP) return false; else if (previous.CurrentPiece.Out == LEFT && current.CurrentPiece.In == RIGHT) return false; else if (previous.CurrentPiece.Out == UP && current.CurrentPiece.In == DOWN) return false; else return true; } else if (T.Clockwise == false) { if (current.Is_Active == false || previous.Is_Active == false) return true; if (previous.CurrentPiece.In == RIGHT && current.CurrentPiece.Out == LEFT) return false; else if (previous.CurrentPiece.In == DOWN && current.CurrentPiece.Out == UP) return false; else if (previous.CurrentPiece.In == LEFT && current.CurrentPiece.Out == RIGHT) return false; else if (previous.CurrentPiece.In == UP && current.CurrentPiece.Out == DOWN) return false; else return true; } else return false; }//end of connected //private void Dump_Info() //{ // //From https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx // using (System.IO.StreamWriter thefile = // new System.IO.StreamWriter(Dir.ToString()+"\\InfoDump.txt")) // { // //TRAIN INFO // thefile.WriteLine("TRAIN INFO"); // thefile.WriteLine(Tester.Id.ToString());//Train Id // thefile.WriteLine("X: " + Tester.Tx + " Y: " + Tester.Ty);//Train Location // //TRACK INFO // thefile.WriteLine("TRACK INFO"); // foreach(Square s in gbxRunGrid.Controls) // { // if(s.Is_Active) // { // //Track X,Y // thefile.WriteLine("X: " + s.X + " Y: " + s.Y); // //Toggled? // int i = 12;//arbitrary. Just not 1 or 0 // if (s.Toggled) // i = 1; // else // i = 0; // thefile.WriteLine("Current Piece:"+ i); // //Is_Turnout & Pieces // //Piece 1 // thefile.WriteLine("Piece1"); // s.Set_Current_Piece(0); // thefile.WriteLine("Id= " + s.CurrentPiece.Id);//Id // thefile.WriteLine("In: " + s.CurrentPiece.In + " Out: " + s.CurrentPiece.Out);//In/Out // if (s.Is_Turnout) // { // //Piece2 // thefile.WriteLine("Piece2"); // s.Set_Current_Piece(1); // thefile.WriteLine("Id= " + s.CurrentPiece.Id);//Id // thefile.WriteLine("In: " + s.CurrentPiece.In + " Out: " + s.CurrentPiece.Out);//In/Out // s.Set_Current_Piece(0); // thefile.WriteLine("Is_Turnout = true"); // } // else // thefile.WriteLine("Is_Turnout = false"); // thefile.WriteLine("\n"); // } // } // } //}//end of dump info private void gbxTrain_Enter(object sender, EventArgs e) { } //private void hideControlsToolStripMenuItem_Click(object sender, EventArgs e) //{ // gbxTrain1.Hide(); //} private void pictureBox2_Click(object sender, EventArgs e) { } //**Event handler for radiobutton clockwise changing. Changes direction of associated train private void rbttnClockwise_CheckedChanged(object sender, EventArgs e) { RadioButton ChangedRbttn = (RadioButton)sender; GroupBox par = (GroupBox)ChangedRbttn.Parent; int ind = Convert.ToInt32(par.Tag); if (ChangedRbttn.Checked) Trains[ind].Clockwise = true; else Trains[ind].Clockwise = false; } //**Wipes out list of trains, makes colors available, and sets traincount to 0, so id will be too //Also, hides all controllers for trains, and returns track to original colors. private void removeAllTrainsToolStripMenuItem_Click(object sender, EventArgs e) { if (Tcnt == 0) { MessageBox.Show("No trains to remove."); return; } else { //set colors of trains back foreach(Square s in gbxRunGrid.Controls) { s.BackColor = Color.LightGray; if(s.Is_Active) s.Text = " ";//Take Train Text off } while(Current_Color.Count!=0) { Current_Color.Dequeue(); } Trains = new List(); Tcnt = 0; bttnGoTrains.Enabled = true; //hide and reset controllers foreach (GroupBox g in Tcontrollers) { g.Hide(); foreach(Control c in g.Controls) { NumericUpDown nudCheck = new NumericUpDown(); c.Enabled = true; if(c.GetType()==nudCheck.GetType()) { c.Text = "0"; } } } //put colors back in for (int i = 0; i < Colors.Count(); i++) { Available_Colors[i] = Colors[i]; } }//end else } ////**TESTING. Sows info about trains who are derailed private void derail_to_file(int x, int y, string train, int cpin,int ppout)//currnet piece in, and previous piece out { if (!File.Exists(Dir.ToString() + "\\InOut.txt")) { // Create a file to write to. using (StreamWriter sw = File.CreateText(Dir.ToString() + "\\DerailFile.txt")) { sw.WriteLine("DerailFile"); } } using (StreamWriter sw = File.AppendText(Dir.ToString() + "\\DerailFile.txt")) { sw.WriteLine("X:" + x.ToString()); sw.WriteLine("Y:" + y.ToString()); sw.WriteLine("Train ID: " + train); sw.WriteLine("Previous Piece Out: " + ppout.ToString()); sw.WriteLine("Current Piece In:" + cpin.ToString()); sw.WriteLine("***********************************"); } } ////**TESTING. Shows all in/outs. It's old. Not sure if it works //private void showAllInoutsToolStripMenuItem_Click(object sender, EventArgs e) //{ // if (!File.Exists(Dir.ToString() + "\\InOut.txt")) // { // // Create a file to write to. // using (StreamWriter sw = File.CreateText(Dir.ToString() + "\\InOut.txt")) // { // sw.WriteLine("InOuts"); // } // } // // This text is always added, making the file longer over time // // if it is not deleted. // foreach(Square s in gbxRunGrid.Controls) // using (StreamWriter sw = File.AppendText(Dir.ToString() + "\\InOut.txt")) // { // if (s.Is_Active) // { // sw.WriteLine("Square"); // sw.WriteLine("X:" + s.X); // sw.WriteLine("Y:" + s.Y); // sw.WriteLine(s.CurrentPiece.In.ToString()); // sw.WriteLine(s.CurrentPiece.Out.ToString()); // if(s.Is_Turnout) // { // sw.WriteLine("Pice2"); // sw.WriteLine("X:" + s.X); // sw.WriteLine("Y:" + s.Y); // s.Set_Current_Piece(1); // sw.WriteLine(s.CurrentPiece.In.ToString()); // sw.WriteLine(s.CurrentPiece.Out.ToString()); // s.Set_Current_Piece(0); // } // } // } //} }//End public partial class Run : Form }