using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Windows.Forms; namespace TrackMaker4 { //**This class represents a section of the board a track can go on public class Square : Button { private const int BLOCK_SIZE = 30;//Size of a square private const int PIECE_MAX = 2;//Highest number of possible pieces in a square private const int PIECE_MIN = 0;//Lowest number of possible pieces in a square private string piece_name = "";//Name of the piece %% not sure if necessary%% private int piece_number = 0;//%% Not sure If I need this yet private Piece[] pieces;//An array for the two pieces //Location values x and y private int x = 0; private int y = 0; private bool is_turnout = false;//Is it a turnout? private Piece current_piece;//Shallow Copy of Piece currently being displayed private bool is_active;//Is the piece active or is it blank? private bool toggled;//Is it toggled? (Is it on the second piece) //private Button button; public Square()//default { piece_number = 0;//%% Not sure If I need this //Location of the square this.x = 0; this.y = 0; this.is_turnout = false;//Default not a turnout this.pieces = new Piece [PIECE_MAX];//Array of size 2 current_piece = null;//Default no pieces this.Name = "Square_" + this.x.ToString() + ',' + this.y.ToString();//Name the square 0,0 this.is_active = false;//Not Active this.toggled = false;//Not toggled this.Image = null;//No Image } public Square(int xCoord, int yCoord)//Blank Square { piece_number = 0;//%% Not sure If I need this //Location = sent x,sent y this.x = xCoord; this.y = yCoord; this.is_turnout = false;//Not a Turnout this.is_active = false;//Not Active this.pieces = new Piece[PIECE_MAX];//Array of 2 current_piece = null;//No Current Piece this.Name = "Square_" + this.x.ToString() + ',' + this.y.ToString();//Name xCoord,yCoord this.toggled = false;//Toggled is false becasue the second piece is not the current piece } public Square(string name,int xCoord, int yCoord, bool turnout)//Explicit Value { this.piece_name = name;//Name gets set this.piece_number = 0;//%%This is for making the pieces array dynamic this.x = xCoord;//Vertical Piece up this.y = yCoord;//Bottom this.is_turnout = turnout;//Top this.pieces = new Piece[PIECE_MAX];//An array of pieces //Pieces get assigned elsewhere to avoid shallow copies this.current_piece=null;//Current Piece points at nothing. this.is_active = false;//The piece is defaulted inactive this.toggled = false;//The square hasnt been toggled } //Copy Constructor //Watch out for shallow Copies! //The equals operator makes a shallow copy for classes and structs //for ints, chars, squares, the equals operator makes a copy public Square(Square cpy) { this.piece_name = cpy.Name;//Copy the name this.piece_number = cpy.piece_number;//%%This is for making the pieces array dynamic this.x = cpy.x;//Vertical Piece up this.y = cpy.y;//Bottom this.is_turnout = cpy.is_turnout;//Copy over whether it is a turnout this.pieces = new Piece[PIECE_MAX];//Make a new array of pieces if (cpy.Is_Active)//If the copy is active { this.pieces[0] = new Piece(cpy.pieces[0]);//Copy the first piece over if (cpy.is_turnout == true)//If the copy is a turnout this.pieces[1] = new Piece(cpy.pieces[1]);//Copy the second piece else//Otherwise... this.pieces[1] = null;//Set the second piece to null this.current_piece = this.pieces[0];//Default the first piece as the current piece } else//If the piece isnt active this.current_piece = null;//The current piece points to noting this.is_active = cpy.is_active;//Copy over whether it is active or not this.toggled = false;//Since the currentpiece points to the first piece, it isn't toggled } //************************************Set Gets*******************************************// public bool Toggled//Was it toggled (is the second piece the current piece) { get { return toggled; } set { toggled = value; } } public bool Is_Active//Is the square active? { get { return is_active; } set { is_active = value;} } public Piece CurrentPiece//What is the piece currently displayed { get { return current_piece; } set { current_piece = value; } } //Where is the Square located? public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } public bool Is_Turnout//Is the square a turnout { get { return is_turnout; } set { is_turnout = value; } } public Piece [] Pieces { set { pieces = value; }//Makes a shallow copy get { return pieces; }//returns a location of the array } //Gets a piece at a specified index (0 or 1) of the pieces array public Piece Get_Piece(int index) { if(index >= PIECE_MAX) { MessageBox.Show("index out of range. defaulting to position 0"); return this.pieces[0]; } else return this.pieces[index]; } //************************************End Set Gets*******************************************// //Takes an integer equal to 0 or 1. It makes the current piece point at the indicated piece in the ... //... pieces array. public void Set_Current_Piece(int i) { if (i != 0 && i != 1) { MessageBox.Show("Error. Value for Set_Current_Piece must be either 0 or 1"); } else { if (i == 0) { this.toggled = false; this.CurrentPiece = pieces[0];//both are o now this.Image = CurrentPiece.Img; } else if (i == 1) { this.toggled = true; this.CurrentPiece = pieces[1]; this.Image = CurrentPiece.Img; } } } //Creates a turnout with the two Ids sent //Uses Set_Piece from Square.cs, and Set_Turnout_Piece from Piece.cs public void Set_Turnout(int p1, int p2) { //Creates a piece at each spot in the pieces array Set_Piece(0, p1); Set_Piece(1, p2); //Sets the images for the pieces pieces[0].Set_Turnout_Piece(true, p1, p2); pieces[1].Set_Turnout_Piece(false, p1, p2); } //** Checks to see if the index is in range and then sets.. // ...the spot of the pieces array at index to p public void Set_Piece(int index, int p) { if (index >= PIECE_MAX) { MessageBox.Show("index out of range. returning now"); return; } else if(index < PIECE_MIN) { MessageBox.Show("index out of range. returning now"); } else { this.pieces[index] = new Piece(p); } } } }