/// <summary> /// Update the position of the image /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <returns></returns> private PicBoxTile CalculatePosition(int row, int col) { PicBoxTile picBoxTile = new PicBoxTile(row, col); picBoxTile.Left = INIT_LEFT + WIDTH * col; picBoxTile.Top = INIT_TOP + HEIGHT * row; return(picBoxTile); }
/// <summary> /// Initialize the current hero's information to none /// </summary> private void IntializeCurrentHeroLocation() { currentHeroPostion = new PicBoxTile(heroRow, heroCol); currentHeroPostion.Type = TileType.None; currentHeroPostion.Image = null; pnlGameBoard.Controls.Add(currentHeroPostion); currentHeroPostion.BringToFront(); }
/// <summary> /// Set the current location to show hero image /// </summary> private void SetHero() { heroPicBoxTile = CalculatePosition(heroRow, heroCol); heroPicBoxTile.Type = TileType.Hero; heroPicBoxTile.Image = imgLst.Images[1]; pnlGameBoard.Controls.Add(heroPicBoxTile); board[heroRow, heroCol].Type = TileType.None; board[heroRow, heroCol].Image = null; heroPicBoxTile.BringToFront(); }
/// <summary> /// Set the current location to show destination image /// </summary> private void SetDestination() { picBoxDestination = CalculatePosition(heroRow, heroCol); picBoxDestination.Type = TileType.Destination; picBoxDestination.Image = imgLst.Images[4]; pnlGameBoard.Controls.Add(picBoxDestination); board[heroRow, heroCol].Type = TileType.Destination; board[heroRow, heroCol].Image = imgLst.Images[4]; picBoxDestination.BringToFront(); }
/// <summary> /// Set the current location to show box image on the destination /// </summary> /// <param name="picBoxPreviousTile"></param> private void SetBoxOnDestination(PicBoxTile picBoxPreviousTile) { picBoxBoxTile = picBoxPreviousTile; picBoxBoxTile = CalculatePosition(boxRow, boxCol); picBoxBoxTile.Type = TileType.Box; picBoxBoxTile.Image = imgLst.Images[5]; pnlGameBoard.Controls.Add(picBoxBoxTile); board[boxRow, boxCol].Type = TileType.Box; board[boxRow, boxCol].Image = imgLst.Images[3]; picBoxBoxTile.BringToFront(); }
/// <summary> /// Clear all global variable for new game /// </summary> private void ClearBoard() { pnlGameBoard.Controls.Clear(); txtNumbersOfMove.Text = ""; txtNumbersOfPush.Text = ""; board = null; heroPicBoxTile = null; picBoxDestination = null; picBoxBoxTile = null; heroRow = 0; heroCol = 0; boxRow = 0; boxCol = 0; totalPush = 0; totalMove = 0; destinationCount = 0; boxCount = 0; isDestination = false; allDestination = null; allBox = null; match = null; }
private void DrawBoard(List <int> lstNumFromFile) { int rowsCount = lstNumFromFile.ElementAt(0); int colsCount = lstNumFromFile.ElementAt(1); //set the tile type default value(none) TileType activeType = TileType.None; //remove the first two element of the int list //to calculate the position of TileType easily lstNumFromFile.RemoveAt(0); lstNumFromFile.RemoveAt(0); //to find the exact type position in the list //can't use the number of row or column //because all row and column's values are between 0 to 7 int positionOfType = 0; board = new PicBoxTile[rowsCount, colsCount]; for (int row = 0; row < rowsCount; row++) { for (int column = 0; column < colsCount; column++) { PicBoxTile picBoxTile = new PicBoxTile(row, column); activeType = (TileType)lstNumFromFile.ElementAt(3 * (positionOfType + 1) - 1); positionOfType++; switch (activeType) { case TileType.None: picBoxTile.Image = null; picBoxTile.Type = TileType.None; //board[row, column].Type = break; case TileType.Hero: picBoxTile.Image = imgLst.Images[1]; heroRow = row; heroCol = column; picBoxTile.Type = TileType.Hero; break; case TileType.Wall: picBoxTile.Image = imgLst.Images[2]; picBoxTile.Type = TileType.Wall; break; case TileType.Box: picBoxTile.Image = imgLst.Images[3]; picBoxTile.Type = TileType.Box; break; case TileType.Destination: picBoxTile.Image = imgLst.Images[4]; picBoxTile.Type = TileType.Destination; totalDestination++; break; default: break; } board[row, column] = picBoxTile; pnlGameBoard.Controls.Add(board[row, column]); } } }
private void btnDown_Click(object sender, EventArgs e) { PicBoxTile picBoxTile = GetTile(heroRow + 1, heroCol); switch (picBoxTile.Type) { case TileType.None: board[heroRow, heroCol].Image = null; board[heroRow, heroCol].Type = TileType.None; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); heroRow++; board[heroRow, heroCol].Image = imgLst.Images[1];; board[heroRow, heroCol].Type = TileType.Hero; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); txtNumbersOfMove.Text = (++totalMove).ToString(); break; case TileType.Hero: //never come to here because Hero is only one to be moved by arrow break; case TileType.Wall: //nothing to happen break; case TileType.Box: picBoxTile = GetTile(heroRow + 2, heroCol); if (picBoxTile.Type == TileType.None) { board[heroRow, heroCol].Image = null; board[heroRow, heroCol].Type = TileType.None; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); heroRow++; board[heroRow, heroCol].Image = imgLst.Images[1]; board[heroRow, heroCol].Type = TileType.Hero; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); //heroCol--; board[heroRow + 1, heroCol].Image = imgLst.Images[3]; board[heroRow + 1, heroCol].Type = TileType.Box; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); txtNumbersOfMove.Text = (++totalMove).ToString(); txtNumbersOfPush.Text = (++totalPush).ToString(); } else if (picBoxTile.Type == TileType.Wall) { //nothing to happen } else if (picBoxTile.Type == TileType.Destination) { //hero should be the box's position board[heroRow, heroCol].Image = null; board[heroRow, heroCol].Type = TileType.None; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); //box should be the position of Destination heroRow++; board[heroRow, heroCol].Image = imgLst.Images[1]; board[heroRow, heroCol].Type = TileType.Hero; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); //Destinateion disappears //heroCol--; board[heroRow + 1, heroCol].Image = imgLst.Images[3]; board[heroRow + 1, heroCol].Type = TileType.Box; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); txtNumbersOfMove.Text = (++totalMove).ToString(); txtNumbersOfPush.Text = (++totalPush).ToString(); totalDestination--; } break; case TileType.Destination: board[heroRow, heroCol].Image = null; board[heroRow, heroCol].Type = TileType.None; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); heroRow++; board[heroRow, heroCol].Image = imgLst.Images[1]; board[heroRow, heroCol].Type = TileType.Hero; pnlGameBoard.Controls.Add(board[heroRow, heroCol]); txtNumbersOfMove.Text = (++totalMove).ToString(); txtNumbersOfPush.Text = (++totalPush).ToString(); break; default: break; } ClearBoard(); }
/// <summary> /// Draw the board with all the elements of the list /// </summary> /// <param name="lstNumFromFile"></param> private void DrawBoard(List <int> lstNumFromFile) { int rowsCount = lstNumFromFile.ElementAt(0); int colsCount = lstNumFromFile.ElementAt(1); //set the tile type default value(none) TileType activeType = TileType.None; //remove the first two element of the int list //to calculate the position of TileType easily lstNumFromFile.RemoveAt(0); lstNumFromFile.RemoveAt(0); //to find the exact type position in the list //can't use the number of row or column //because all row and column's values are between 0 to 7 int positionOfType = 0; //save all elements to the 2 dimension array to check the property of the cell board = new PicBoxTile[rowsCount, colsCount]; //save all destination and box's location infor(row, col) //to compare all the element are the same(finishing condition) allDestination = CreateDictionary(); allBox = CreateDictionary(); for (int row = 0; row < rowsCount; row++) { for (int column = 0; column < colsCount; column++) { PicBoxTile picBoxTile = new PicBoxTile(row, column); //check the active type from the list to show the right image on the board activeType = (TileType)lstNumFromFile.ElementAt(3 * (positionOfType + 1) - 1); positionOfType++; switch (activeType) { case TileType.None: picBoxTile.Image = null; picBoxTile.Type = TileType.None; picBoxTile.Tag = ""; break; case TileType.Hero: picBoxTile.Image = imgLst.Images[1]; //hold the hero's position heroRow = row; heroCol = column; picBoxTile.Type = TileType.None; heroPicBoxTile = picBoxTile; break; case TileType.Wall: picBoxTile.Image = imgLst.Images[2]; picBoxTile.Type = TileType.Wall; picBoxTile.Tag = ""; break; case TileType.Box: picBoxTile.Image = imgLst.Images[3]; picBoxTile.Type = TileType.Box; picBoxTile.Tag = ""; match = new Match(row, column); //hold the current box's information on the dictionary allBox.Add(boxCount, match); boxCount++; break; case TileType.Destination: picBoxTile.Image = imgLst.Images[4]; picBoxTile.Type = TileType.Destination; picBoxTile.Tag = "Destination"; match = new Match(row, column); //hold the current destination's information on the dictionary allDestination.Add(destinationCount, match); destinationCount++; break; default: break; } board[row, column] = picBoxTile; pnlGameBoard.Controls.Add(board[row, column]); } } }
/// <summary> /// When click the down button, /// check the below one to move the hero /// and move/// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDown_Click(object sender, EventArgs e) { if (heroPicBoxTile != null) { PicBoxTile picBoxTile = GetTile(heroRow + 1, heroCol); switch (picBoxTile.Type) { case TileType.None: picBoxDestination = new PicBoxTile(heroRow, heroCol); IntializeCurrentHeroLocation(); if (CheckDestination(heroRow, heroCol)) { SetDestination(); } heroRow++; SetHero(); txtNumbersOfMove.Text = (++totalMove).ToString(); break; case TileType.Hero: //never come to here because Hero is only one to be moved by arrow break; case TileType.Wall: //nothing to happen break; case TileType.Box: PicBoxTile picBoxPreviousTile = GetTile(heroRow + 2, heroCol); if (picBoxPreviousTile.Type == TileType.None) { picBoxDestination = new PicBoxTile(heroRow, heroCol); IntializeCurrentHeroLocation(); if (CheckDestination(heroRow, heroCol)) { SetDestination(); } heroRow++; SetHero(); boxRow = heroRow + 1; boxCol = heroCol; SetBox(picBoxPreviousTile); txtNumbersOfMove.Text = (++totalMove).ToString(); txtNumbersOfPush.Text = (++totalPush).ToString(); } else if (picBoxPreviousTile.Type == TileType.Wall) { //nothing to happen } else if (picBoxPreviousTile.Type == TileType.Destination) { IntializeCurrentHeroLocation(); picBoxDestination = new PicBoxTile(heroRow, heroCol); if (CheckDestination(heroRow, heroCol)) { SetDestination(); } heroRow++; SetHero(); boxRow = heroRow + 1; boxCol = heroCol; SetBoxOnDestination(picBoxPreviousTile); txtNumbersOfMove.Text = (++totalMove).ToString(); txtNumbersOfPush.Text = (++totalPush).ToString(); } UpdateBoxLocationList(); CheckFinish(); break; case TileType.Destination: picBoxDestination = new PicBoxTile(heroRow, heroCol); IntializeCurrentHeroLocation(); if (CheckDestination(heroRow, heroCol)) { SetDestination(); } heroRow++; SetHero(); txtNumbersOfMove.Text = (++totalMove).ToString(); break; default: break; } } }
/// <summary> /// When click the Left button, /// check the before one to move the hero /// and move/// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLeft_Click(object sender, EventArgs e) { if (heroPicBoxTile != null) { PicBoxTile picBoxTile = GetTile(heroRow, heroCol - 1); switch (picBoxTile.Type) { case TileType.None: picBoxDestination = new PicBoxTile(heroRow, heroCol); IntializeCurrentHeroLocation(); if (CheckDestination(heroRow, heroCol)) { SetDestination(); } heroCol--; SetHero(); txtNumbersOfMove.Text = (++totalMove).ToString(); break; case TileType.Hero: //never come to here because Hero is only one to be moved by arrow break; case TileType.Wall: //nothing to happen break; case TileType.Box: PicBoxTile picBoxPreviousTile = GetTile(heroRow, heroCol - 2); if (picBoxPreviousTile.Type == TileType.None) { picBoxDestination = new PicBoxTile(heroRow, heroCol); IntializeCurrentHeroLocation(); //check whether or not the current position was destination //if it was destination, show the destination image on the board again if (CheckDestination(heroRow, heroCol)) { SetDestination(); } //show the image of hero on the board heroCol--; SetHero(); //sow the image of box on the board boxRow = heroRow; boxCol = heroCol - 1; SetBox(picBoxPreviousTile); txtNumbersOfMove.Text = (++totalMove).ToString(); txtNumbersOfPush.Text = (++totalPush).ToString(); } else if (picBoxPreviousTile.Type == TileType.Wall) { //nothing to happen } else if (picBoxPreviousTile.Type == TileType.Destination) { picBoxDestination = new PicBoxTile(heroRow, heroCol); IntializeCurrentHeroLocation(); if (CheckDestination(heroRow, heroCol)) { SetDestination(); } heroCol--; SetHero(); boxRow = heroRow; boxCol = heroCol - 1; SetBoxOnDestination(picBoxPreviousTile); txtNumbersOfMove.Text = (++totalMove).ToString(); txtNumbersOfPush.Text = (++totalPush).ToString(); } UpdateBoxLocationList(); CheckFinish(); break; case TileType.Destination: picBoxDestination = new PicBoxTile(heroRow, heroCol); IntializeCurrentHeroLocation(); if (CheckDestination(heroRow, heroCol)) { SetDestination(); } heroCol--; SetHero(); txtNumbersOfMove.Text = (++totalMove).ToString(); break; default: break; } } }