//by clicking on each tile take the number of row, column, and type of the tile // if it is a box. Then, keep related image in the currentTool private void Tile_Click(object sender, EventArgs e) { Grides mytile = sender as Grides; currentTile = mytile; currentRow = currentTile.row; currentCol = currentTile.column; currentType = tile[currentRow, currentCol].type; switch (currentType) { case 6: currentTool = Grides.ObstacleType.blueBox; break; case 7: currentTool = Grides.ObstacleType.redBox; break; case 8: currentTool = Grides.ObstacleType.greenBox; break; case 9: currentTool = Grides.ObstacleType.yellowBox; break; default: MessageBox.Show("You should select just a box"); break; } }
/// <summary> /// The click event handler to generate buttons /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonGenerate_Click(object sender, EventArgs e) { try { int row = Convert.ToInt16(textBoxRow.Text); int column = Convert.ToInt16(textBoxColumn.Text); tile = new Grides[row, column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { tile[i, j] = new Grides(); tile[i, j].Location = new Point(j * WIDTH + LEFT, i * HEIGHT + TOP); tile[i, j].Width = WIDTH; tile[i, j].Height = HEIGHT; tile[i, j].Visible = true; tile[i, j].BorderStyle = BorderStyle.Fixed3D; tile[i, j].SizeMode = PictureBoxSizeMode.StretchImage; tile[i, j].BringToFront(); this.Controls.Add(tile[i, j]); tile[i, j].Click += Tile_Click; } } } catch (Exception error) { MessageBox.Show(error.Message); } }
/// <summary> /// especify images(enum) and the number of them to each grid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Tile_Click(object sender, EventArgs e) { Grides myImage = sender as Grides; switch (currentTool) { case Grides.ObstacleType.None: myImage.Image = TKhaleghiAssignment2.Properties.Resources.None; myImage.Tag = 0; break; case Grides.ObstacleType.Wall: myImage.Image = TKhaleghiAssignment2.Properties.Resources.Wall; myImage.Tag = 1; break; case Grides.ObstacleType.blueDoor: myImage.Image = TKhaleghiAssignment2.Properties.Resources.BlueDoor; myImage.Tag = 2; break; case Grides.ObstacleType.redDoor: myImage.Image = TKhaleghiAssignment2.Properties.Resources.RedDoor; myImage.Tag = 3; break; case Grides.ObstacleType.greenDoor: myImage.Image = TKhaleghiAssignment2.Properties.Resources.GreenDoor; myImage.Tag = 4; break; case Grides.ObstacleType.yellowDoor: myImage.Image = TKhaleghiAssignment2.Properties.Resources.YellowDoor; myImage.Tag = 5; break; case Grides.ObstacleType.blueBox: myImage.Image = TKhaleghiAssignment2.Properties.Resources.BlueBox; myImage.Tag = 6; break; case Grides.ObstacleType.redBox: myImage.Image = TKhaleghiAssignment2.Properties.Resources.RedBox; myImage.Tag = 7; break; case Grides.ObstacleType.greenBox: myImage.Image = TKhaleghiAssignment2.Properties.Resources.GreenBox; myImage.Tag = 8; break; case Grides.ObstacleType.yellowBox: myImage.Image = TKhaleghiAssignment2.Properties.Resources.YellowBox; myImage.Tag = 9; break; } }
// if next tile is a door withe same color of the box, //it removes the tile from the panel, and it calls the BoxNumbers method public void Remove(Grides currentTile) { this.panel1.Controls.Remove(currentTile); tile[currentRow, currentCol].type = 0; boxNumber = BoxNumbers(numOfRow, numOfColumn); if (boxNumber == 0) { MessageBox.Show("Congratulation!"); this.panel1.Controls.Clear(); textBoxNumMov.Text = ""; } }
// load the game by reading the text file and initialize each line //(row, column, and the image type) to the array in the nested loop public void load(string fileName) { this.panel1.Controls.Clear(); try { StreamReader reader = new StreamReader(fileName); numOfRow = int.Parse(reader.ReadLine()); numOfColumn = int.Parse(reader.ReadLine()); tile = new Grides[numOfRow, numOfColumn]; int x = INIT_LEFT; int y = INIT_TOP; for (int i = 0; i < numOfRow; i++) { for (int j = 0; j < numOfColumn; j++) { tile[i, j] = new Grides(); tile[i, j].row = int.Parse(reader.ReadLine()); tile[i, j].column = int.Parse(reader.ReadLine()); tile[i, j].type = int.Parse(reader.ReadLine()); switch (tile[i, j].type) { case 0: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.None; break; case 1: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.Wall; break; case 2: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.BlueDoor; break; case 3: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.RedDoor; break; case 4: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.GreenDoor; break; case 5: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.YellowDoor; break; case 6: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.BlueBox; break; case 7: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.RedBox; break; case 8: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.GreenBox; break; case 9: tile[i, j].Image = TKhaleghiAssignment2.Properties.Resources.YellowBox; break; } tile[i, j].Left = x; tile[i, j].Top = y; tile[i, j].Width = WIDTH; tile[i, j].Height = HEIGHT; tile[i, j].Visible = true; tile[i, j].SizeMode = PictureBoxSizeMode.StretchImage; this.panel1.Controls.Add(tile[i, j]); tile[i, j].Click += Tile_Click; x += WIDTH; } x = INIT_LEFT; y += HEIGHT; } reader.Close(); } catch (Exception error) { MessageBox.Show(error.Message); } }