private void GridBox_WriteNum(object sender, PaintEventArgs e) { GridBox curr = sender as GridBox; string holeNum = curr.HoleNum.ToString(); string ballNum = curr.BallNum.ToString(); StringFormat drawFormat = new StringFormat(); drawFormat.LineAlignment = StringAlignment.Center; drawFormat.Alignment = StringAlignment.Center; Rectangle r = new Rectangle(0, 0, curr.Width, curr.Height); Brush brushYellow = new SolidBrush(Color.Yellow); if (curr.HoleNum > 0) { using (Font myFont = new Font("Arial", 16.0f)) { e.Graphics.DrawString(holeNum, myFont, brushYellow, r, drawFormat); } } if (curr.BallNum > 0) { using (Font myFont = new Font("Arial", 16.0f)) { e.Graphics.DrawString(ballNum, myFont, brushYellow, r, drawFormat); } } }
public void GridBox_DrawWalls(object sender, PaintEventArgs e) { GridBox curr = sender as GridBox; float outerWallWidth = 10; float innerWallWidth = 7.5f; PointF topLeft = new PointF(0, 0); PointF topRight = new PointF(curr.Width, 0); PointF bottomLeft = new PointF(0, curr.Height); PointF bottomRight = new PointF(curr.Width, curr.Height); if (curr.HasLeftWall() || curr.Col == 0) { Pen pen = new Pen(Color.Red, curr.Col == 0 ? outerWallWidth : innerWallWidth); using (pen) { e.Graphics.DrawLine(pen, topLeft, bottomLeft); } } if (curr.HasRightWall() || curr.Col == size - 1) { Pen pen = new Pen(Color.Red, curr.Col == size - 1 ? outerWallWidth : innerWallWidth); using (pen) { e.Graphics.DrawLine(pen, topRight, bottomRight); } } if (curr.HasTopWall() || curr.Row == 0) { Pen pen = new Pen(Color.Red, curr.Row == 0 ? outerWallWidth : innerWallWidth); using (pen) { e.Graphics.DrawLine(pen, topLeft, topRight); } } if (curr.HasBottomWall() || curr.Row == size - 1) { Pen pen = new Pen(Color.Red, curr.Row == size - 1 ? outerWallWidth : innerWallWidth); using (pen) { e.Graphics.DrawLine(pen, bottomLeft, bottomRight); } } }
public void GridBox_DrawWalls(object sender, PaintEventArgs e) { GridBox curr = sender as GridBox; PointF topLeft = new PointF(0, 0); PointF topRight = new PointF(curr.Width, 0); PointF bottomLeft = new PointF(0, curr.Height); PointF bottomRight = new PointF(curr.Width, curr.Height); if (curr.LeftWall == 1) { Pen pen = new Pen(Color.Red, 10); using (pen) { e.Graphics.DrawLine(pen, topLeft, bottomLeft); } } if (curr.RightWall == 1) { Pen pen = new Pen(Color.Red, 10); using (pen) { e.Graphics.DrawLine(pen, topRight, bottomRight); } } if (curr.TopWall == 1) { Pen pen = new Pen(Color.Red, 10); using (pen) { e.Graphics.DrawLine(pen, topLeft, topRight); } } if (curr.BottomWall == 1) { Pen pen = new Pen(Color.Red, 10); using (pen) { e.Graphics.DrawLine(pen, bottomLeft, bottomRight); } } }
private void RenderBox(List <GridBox> arr) { for (int i = 0; i < arr.Count; i++) { GridBox box = arr[i]; int pw = (img.Width / 7); int ph = (img.Height / 7); Bitmap bm = new Bitmap(box.Width, box.Height); Rectangle r = new Rectangle(0, 0, box.Width, box.Height); if (box.Item == 0) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * emptyXY, ph * emptyXY, pw, ph, GraphicsUnit.Pixel); } } else if (box.Item == 1) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * ballXY, ph * ballXY, pw, ph, GraphicsUnit.Pixel); } } else if (box.Item == 2) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * holeXY, ph * holeXY, pw, ph, GraphicsUnit.Pixel); } } else if (box.Item == -1) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * errXY, ph * errXY, pw, ph, GraphicsUnit.Pixel); } } box.Image = bm; } ValidateGame(arr); }
private void initButton_Click(object sender, EventArgs e) { string gameDir = string.Empty; using (CustomOFD ofd = new CustomOFD()) { if (ofd.ShowDialog() == DialogResult.OK) { gameDir = ofd.GamePath; } else { return; } } string imgPath = gameDir + "/" + "puzzle.jpg"; string gamePath = gameDir + "/" + "puzzle.txt"; gameBox.Controls.Clear(); ToggleControls(true); img = Image.FromFile(imgPath); string path = gamePath; string[] lines = System.IO.File.ReadAllLines(path); // Counts of components string[] counts = lines[0].Split(' '); size = Convert.ToInt32(counts[0]); int balls = Convert.ToInt32(counts[1]); int holes = Convert.ToInt32(counts[1]); int walls = Convert.ToInt32(counts[2]); // Scaling ratio for gameboard if (img.Width >= resWidth) // shrink original image { if (img.Width >= img.Height) // shrink by width { ratio = (float)img.Width / (float)resWidth; } else // shrink by height { ratio = (float)img.Height / (float)resHeight; } } else // enlarge original image { if (img.Width >= img.Height) // enlarge by width { ratio = (float)resWidth / (float)img.Width; } else // enlarge by height { ratio = (float)resHeight / (float)img.Height; } } // Create gameboard (2D array of GridBox) float gridWidth = img.Width / ratio / (float)size; float gridHeight = img.Height / ratio / (float)size; float marginTop = 20; float marginLeft = 20; GameBoard = new GridBox[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { GameBoard[i, j] = new GridBox(); GameBoard[i, j].Row = i; GameBoard[i, j].Col = j; GameBoard[i, j].Item = 0; GameBoard[i, j].WallCount = 0; GameBoard[i, j].LeftWall = 0; GameBoard[i, j].RightWall = 0; GameBoard[i, j].TopWall = 0; GameBoard[i, j].BottomWall = 0; GameBoard[i, j].BallNum = 0; GameBoard[i, j].HoleNum = 0; GameBoard[i, j].Location = new System.Drawing.Point((int)((gridWidth * j) + marginLeft), (int)((gridHeight * i) + marginTop)); GameBoard[i, j].Name = "grid" + i.ToString() + j.ToString(); GameBoard[i, j].BackColor = System.Drawing.Color.Black; GameBoard[i, j].Size = new System.Drawing.Size((int)gridWidth, (int)gridHeight); GameBoard[i, j].SizeMode = PictureBoxSizeMode.Normal; gameBox.Controls.Add(GameBoard[i, j]); } } // Assign balls (Item = 1) for (int i = 1; i <= balls; i++) { string[] coords = lines[i].Split(' '); int row = Convert.ToInt32(coords[0]); int col = Convert.ToInt32(coords[1]); GameBoard[row, col].BallNum = i; GameBoard[row, col].Item = 1; } // Assign holes (Item = 2) for (int i = balls + 1; i <= (balls * 2); i++) { string[] coords = lines[i].Split(' '); int row = Convert.ToInt32(coords[0]); int col = Convert.ToInt32(coords[1]); GameBoard[row, col].HoleNum = i - balls; GameBoard[row, col].Item = 2; } // Assign walls for (int i = (balls * 2) + 1; i <= (balls * 2) + walls; i++) { string[] coords = lines[i].Split(' '); int row1 = Convert.ToInt32(coords[0]); int col1 = Convert.ToInt32(coords[1]); int row2 = Convert.ToInt32(coords[2]); int col2 = Convert.ToInt32(coords[3]); GridBox box1 = GameBoard[row1, col1]; GridBox box2 = GameBoard[row2, col2]; box1.WallCount++; box2.WallCount++; if (row1 == row2) // same row { if (col1 > col2) { box1.LeftWall = 1; box2.RightWall = 1; } else { box1.RightWall = 1; box2.LeftWall = 1; } } else // same column { if (row1 > row2) { box1.TopWall = 1; box2.BottomWall = 1; } else { box1.BottomWall = 1; box2.TopWall = 1; } } } // Render board for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { GridBox box = GameBoard[i, j]; box.Paint += new System.Windows.Forms.PaintEventHandler(this.GridBox_DrawWalls); box.Paint += new System.Windows.Forms.PaintEventHandler(this.GridBox_WriteNum); int pw = (img.Width / 7); int ph = (img.Height / 7); Bitmap bm = new Bitmap(box.Width, box.Height); Rectangle r = new Rectangle(0, 0, box.Width, box.Height); if (box.Item == 0) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * emptyXY, ph * emptyXY, pw, ph, GraphicsUnit.Pixel); } } else if (box.Item == 1) { Font f = new Font("Arial", 16.0f); Brush by = new SolidBrush(Color.Yellow); StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; string text = box.BallNum.ToString(); using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * ballXY, ph * ballXY, pw, ph, GraphicsUnit.Pixel); g.DrawString(text, f, by, r, sf); } } else if (box.Item == 2) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * holeXY, ph * holeXY, pw, ph, GraphicsUnit.Pixel); } } box.Image = bm; } } }
private void rightButton_Click(object sender, EventArgs e) { List <GridBox> Updates = new List <GridBox>(); for (int col = size - 2; col >= 0; col--) { for (int row = 0; row < size; row++) { GridBox box = GameBoard[row, col]; if (box.HasBall()) { Updates.Add(box); int currCol = col; GridBox currBox; while (true) { currBox = GameBoard[row, currCol]; if (currCol >= size - 1) { break; } if (!currBox.HasRightWall()) //no wall; move { GridBox nextBox = GameBoard[row, currCol + 1]; if (HasHoleNext(nextBox)) // has hole { if (nextBox.HoleNum == currBox.BallNum) // correct hole { currBox.Item = 0; currBox.BallNum = 0; nextBox.Item = 0; nextBox.HoleNum = 0; Updates.Add(nextBox); break; } else // incorrect hole { currBox.Item = -1; currBox.BallNum = 0; nextBox.Item = -1; nextBox.HoleNum = 0; Updates.Add(nextBox); break; } } else if (nextBox.Item == 0) //empty { nextBox.Item = 1; nextBox.BallNum = currBox.BallNum; currBox.Item = 0; currBox.BallNum = 0; currCol++; } else if (nextBox.Item == 1) // has ball { break; } } else // wall on bottom { break; } } Updates.Add(currBox); } } } RenderBox(Updates); }
private bool HasHoleNext(GridBox box) { return(box.HoleNum > 0); }
private void App_ResizeEnd(object sender, EventArgs e) { if (img != null && GameBoard != null) { gameBox.Controls.Clear(); // Scaling Data Console.WriteLine("Max Render Size: " + gameBox.Size.Width + ", " + gameBox.Size.Height); int resWidth = gameBox.Size.Width - (gameBox.Size.Width / 3); // Max width is 2/3 the gameBox width int resHeight = gameBox.Size.Height - (gameBox.Size.Height / 3); // Max height is 2/3 the gameBox height // Scaling ratio for gameboard if (img.Width >= resWidth) // shrink original image { if (img.Width >= img.Height) // shrink by width { ratio = img.Width / (float)resWidth; } else // shrink by height { ratio = img.Height / (float)resHeight; } } else // enlarge original image { if (img.Width >= img.Height) // enlarge by width { ratio = resWidth / (float)img.Width; } else // enlarge by height { ratio = resHeight / (float)img.Height; } } // Sync gameboard (2D array of GridBox) float gridWidth = img.Width / ratio / size; float gridHeight = img.Height / ratio / size; float marginTop = 20; float marginLeft = 20; GridBox emptyBox = new GridBox(); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { GameBoard[i, j] = GameBoard[i, j]; GameBoard[i, j].Location = new Point((int)((gridWidth * j) + marginLeft), (int)((gridHeight * i) + marginTop)); GameBoard[i, j].Size = new Size((int)gridWidth, (int)gridHeight); gameBox.Controls.Add(GameBoard[i, j]); } } // Render board for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { GridBox box = GameBoard[i, j]; box.Paint += new PaintEventHandler(GridBox_DrawWalls); box.Paint += new PaintEventHandler(GridBox_WriteNum); box.Paint += new PaintEventHandler(GridBox_DrawBorder); int pw = img.Width / 7; int ph = img.Height / 7; Bitmap bm = new Bitmap(box.Width, box.Height); Rectangle r = new Rectangle(0, 0, box.Width, box.Height); if (box.Item == 0) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * emptyXY, ph * emptyXY, pw, ph, GraphicsUnit.Pixel); } } else if (box.Item == 1) { Font f = new Font("Arial", 16.0f); Brush by = new SolidBrush(Color.Yellow); StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; string text = box.BallNum.ToString(); using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * ballXY, ph * ballXY, pw, ph, GraphicsUnit.Pixel); g.DrawString(text, f, by, r, sf); } } else if (box.Item == 2) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * holeXY, ph * holeXY, pw, ph, GraphicsUnit.Pixel); } } box.Image = bm; } } } }
private void InitButton_Click(object sender, EventArgs e) { bool nameValid = playerName.Text.Any(x => char.IsLetter(x)); if (!nameValid) { MessageBox.Show("Pleas enter a valid name to continue..."); return; } string originalText = initButton.Text; switch (originalText) { case "Start Game": case "Restart": case "Start New Game": gameBox.Controls.Clear(); gameBox.Visible = true; moves = 0; playerName.Enabled = false; ToggleControls(true); initButton.Text = "Pause Game"; timerObj.ResetTiming(); timerObj.StartTiming(); break; case "Pause Game": ToggleControls(false); gameBox.Visible = false; initButton.Text = "Resume Game"; timerObj.PauseTiming(); break; case "Resume Game": ToggleControls(true); gameBox.Visible = true; initButton.Text = "Pause Game"; timerObj.StartTiming(); break; default: initButton.Text = "UNKNOWN_STATE"; initButton.Enabled = false; playerName.Enabled = false; ToggleControls(false); gameBox.Visible = false; moves = 0; timerObj.PauseTiming(); break; } if (originalText == "Pause Game" || originalText == "Resume Game") { return; } string path = dataPath; string[] lines = File.ReadAllLines(path); // Counts of components string[] counts = lines[0].Split(' '); size = Convert.ToInt32(counts[0]); int balls = Convert.ToInt32(counts[1]); int holes = Convert.ToInt32(counts[1]); int walls = Convert.ToInt32(counts[2]); // Scaling Data Console.WriteLine("Max Render Size: " + gameBox.Size.Width + ", " + gameBox.Size.Height); int resWidth = gameBox.Size.Width - (gameBox.Size.Width / 3); // Max width is 2/3 the gameBox width int resHeight = gameBox.Size.Height - (gameBox.Size.Height / 3); // Max height is 2/3 the gameBox height // Scaling ratio for gameboard if (img.Width >= resWidth) // shrink original image { if (img.Width >= img.Height) // shrink by width { ratio = img.Width / (float)resWidth; } else // shrink by height { ratio = img.Height / (float)resHeight; } } else // enlarge original image { if (img.Width >= img.Height) // enlarge by width { ratio = resWidth / (float)img.Width; } else // enlarge by height { ratio = resHeight / (float)img.Height; } } // Create gameboard (2D array of GridBox) float gridWidth = img.Width / ratio / size; float gridHeight = img.Height / ratio / size; float marginTop = 20; float marginLeft = 20; GameBoard = new GridBox[size, size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { GameBoard[i, j] = new GridBox { Row = i, Col = j, Item = 0, WallCount = 0, LeftWall = 0, RightWall = 0, TopWall = 0, BottomWall = 0, BallNum = 0, HoleNum = 0, Location = new Point((int)((gridWidth * j) + marginLeft), (int)((gridHeight * i) + marginTop)), Name = "grid" + i.ToString() + j.ToString(), BackColor = Color.Black, Size = new Size((int)gridWidth, (int)gridHeight), SizeMode = PictureBoxSizeMode.Normal }; gameBox.Controls.Add(GameBoard[i, j]); } } // Assign balls (Item = 1) for (int i = 1; i <= balls; i++) { string[] coords = lines[i].Split(' '); int row = Convert.ToInt32(coords[0]); int col = Convert.ToInt32(coords[1]); GameBoard[row, col].BallNum = i; GameBoard[row, col].Item = 1; } // Assign holes (Item = 2) for (int i = balls + 1; i <= (balls * 2); i++) { string[] coords = lines[i].Split(' '); int row = Convert.ToInt32(coords[0]); int col = Convert.ToInt32(coords[1]); GameBoard[row, col].HoleNum = i - balls; GameBoard[row, col].Item = 2; } // Assign walls for (int i = (balls * 2) + 1; i <= (balls * 2) + walls; i++) { string[] coords = lines[i].Split(' '); int row1 = Convert.ToInt32(coords[0]); int col1 = Convert.ToInt32(coords[1]); int row2 = Convert.ToInt32(coords[2]); int col2 = Convert.ToInt32(coords[3]); GridBox box1 = GameBoard[row1, col1]; GridBox box2 = GameBoard[row2, col2]; box1.WallCount++; box2.WallCount++; if (row1 == row2) // same row { if (col1 > col2) { box1.LeftWall = 1; box2.RightWall = 1; } else { box1.RightWall = 1; box2.LeftWall = 1; } } else // same column { if (row1 > row2) { box1.TopWall = 1; box2.BottomWall = 1; } else { box1.BottomWall = 1; box2.TopWall = 1; } } } // Render board for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { GridBox box = GameBoard[i, j]; box.Paint += new PaintEventHandler(GridBox_DrawWalls); box.Paint += new PaintEventHandler(GridBox_WriteNum); box.Paint += new PaintEventHandler(GridBox_DrawBorder); int pw = img.Width / 7; int ph = img.Height / 7; Bitmap bm = new Bitmap(box.Width, box.Height); Rectangle r = new Rectangle(0, 0, box.Width, box.Height); if (box.Item == 0) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * emptyXY, ph * emptyXY, pw, ph, GraphicsUnit.Pixel); } } else if (box.Item == 1) { Font f = new Font("Arial", 16.0f); Brush by = new SolidBrush(Color.Yellow); StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }; string text = box.BallNum.ToString(); using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * ballXY, ph * ballXY, pw, ph, GraphicsUnit.Pixel); g.DrawString(text, f, by, r, sf); } } else if (box.Item == 2) { using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(img, r, pw * holeXY, ph * holeXY, pw, ph, GraphicsUnit.Pixel); } } box.Image = bm; } } }