Skip to content
Snippets Groups Projects
Commit 95f523a2 authored by Floris's avatar Floris
Browse files

board visual update

- Fixed board not updating/drawing on start
- Fixed pixel offsets and nonsquare tiles with FitBoard method
parent 3c963424
No related branches found
No related tags found
No related merge requests found
...@@ -27,33 +27,34 @@ namespace PuzzlePlayer_Namespace ...@@ -27,33 +27,34 @@ namespace PuzzlePlayer_Namespace
Clear(); Clear();
} }
public override void Draw (Graphics gr, Point p, Size s) public override void Draw (Graphics gr, Rectangle r) //draws board in rectangle R. warning: will stretch image unless FitBoard() is used for rectangle size
{ {
gr.FillRectangle(Brushes.Beige, p.X, p.Y, s.Width, s.Height); Size tilesize = new Size(r.Width / boardState.GetLength(0), r.Height / boardState.GetLength(1));
gr.FillRectangle(Brushes.Gainsboro, r.X, r.Y, tilesize.Width*boardState.GetLength(0), tilesize.Height*boardState.GetLength(1));
for (int i = 0; i < boardState.GetLength(0); i++) for (int i = 0; i < boardState.GetLength(0); i++)
{ {
for(int j = 0; j < boardState.GetLength(1); j++) for(int j = 0; j < boardState.GetLength(1); j++)
{ {
gr.DrawRectangle(Pens.Black, gr.DrawRectangle(Pens.Black,
p.X+i*s.Width/boardState.GetLength(0), r.X+i* tilesize.Width,
p.Y+j*s.Height/boardState.GetLength(1), r.Y+j* tilesize.Height,
s.Width / boardState.GetLength(0), tilesize.Width,
s.Height / boardState.GetLength(1)); tilesize.Height);
if (boardState[i,j] == 0) if (boardState[i,j] == 0)
{ {
gr.FillEllipse(Brushes.White, gr.FillEllipse(Brushes.White,
(int)(p.X + ((double)i + 0.125) * s.Width / boardState.GetLength(0)), (int)(r.X + ((double)i + 0.125) * tilesize.Width),
(int)(p.Y + ((double)j + 0.125) * s.Height / boardState.GetLength(1)), (int)(r.Y + ((double)j + 0.125) * tilesize.Height),
s.Width / boardState.GetLength(0) * 3 / 4, tilesize.Width * 3 / 4,
s.Height / boardState.GetLength(1) * 3 / 4); tilesize.Height * 3 / 4);
} }
if (boardState[i, j] == 1) if (boardState[i, j] == 1)
{ {
gr.FillEllipse(Brushes.Black, gr.FillEllipse(Brushes.Black,
(int)(p.X + ((double)i + 0.125) * s.Width / boardState.GetLength(0)), (int)(r.X + ((double)i + 0.125) * tilesize.Width),
(int)(p.Y + ((double)j + 0.125) * s.Height / boardState.GetLength(1)), (int)(r.Y + ((double)j + 0.125) * tilesize.Height),
s.Width / boardState.GetLength(0) * 3 / 4, tilesize.Width * 3 / 4,
s.Height / boardState.GetLength(1) * 3 / 4); tilesize.Height * 3 / 4);
} }
} }
} }
......
...@@ -43,7 +43,7 @@ namespace PuzzlePlayer_Namespace ...@@ -43,7 +43,7 @@ namespace PuzzlePlayer_Namespace
return false; return false;
} }
} }
public abstract void Draw(Graphics gr, Point p, Size s); public abstract void Draw(Graphics gr, Rectangle r);
// a methode for solving the whole board. It uses the private SolveStep methode untill the whole board is solved // a methode for solving the whole board. It uses the private SolveStep methode untill the whole board is solved
public SOLUTIONS Solve() public SOLUTIONS Solve()
{ {
......
...@@ -14,9 +14,9 @@ namespace PuzzlePlayer_Namespace ...@@ -14,9 +14,9 @@ namespace PuzzlePlayer_Namespace
private Button solvebutton; private Button solvebutton;
private Button hintbutton; private Button hintbutton;
private Button generatebutton; private Button generatebutton;
private Label boardlabel;
private TextBox informationbox; private TextBox informationbox;
public Graphics graphics; private Graphics graphics;
private Rectangle boardspace;
private Board board; private Board board;
public Board Board //Updating the Board member will immediately call board.Draw method so that the board is updated visually public Board Board //Updating the Board member will immediately call board.Draw method so that the board is updated visually
...@@ -25,7 +25,7 @@ namespace PuzzlePlayer_Namespace ...@@ -25,7 +25,7 @@ namespace PuzzlePlayer_Namespace
set set
{ {
board = value; board = value;
board.Draw(graphics, new Point(220, 30), new Size(400, 400)); this.Invalidate();
} }
} }
...@@ -33,18 +33,26 @@ namespace PuzzlePlayer_Namespace ...@@ -33,18 +33,26 @@ namespace PuzzlePlayer_Namespace
{ {
if (s == default(Size)) s = new Size(800, 500); if (s == default(Size)) s = new Size(800, 500);
this.Size = s; this.Size = s;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.Beige;
graphics = this.CreateGraphics(); graphics = this.CreateGraphics();
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
generatebutton = new Button(); generatebutton = new Button();
hintbutton = new Button(); hintbutton = new Button();
solvebutton = new Button(); solvebutton = new Button();
informationbox = new TextBox(); informationbox = new TextBox();
boardspace = new Rectangle(220, 30, 400, 400);
this.Paint += (object o, PaintEventArgs pea) =>
{
graphics.Clear(this.BackColor);
board.Draw(graphics, boardspace);
//graphics.FillRectangle(Brushes.LightCoral, 220, 30, this.Width - 400, this.Height - 100);
//graphics.FillRectangle(Brushes.DarkRed, 220, 30, boardspace.Width*Board.boardState.GetLength(0), boardspace.Height * Board.boardState.GetLength(1));
};
this.Resize += (object o, EventArgs ea) => UpdateUI();
Board = b; Board = b;
CreateUI(); CreateUI();
UpdateUI(); UpdateUI();
this.Resize += (object sender, EventArgs ea) =>
{
UpdateUI();
};
Board.boardState[1, 1] = 1; Board.boardState[1, 1] = 1;
Board.boardState[2, 2] = 0; Board.boardState[2, 2] = 0;
} }
...@@ -74,7 +82,7 @@ namespace PuzzlePlayer_Namespace ...@@ -74,7 +82,7 @@ namespace PuzzlePlayer_Namespace
solvebutton.MouseClick += (object sender, MouseEventArgs mea) => solvebutton.MouseClick += (object sender, MouseEventArgs mea) =>
{ {
//board = board.Solve(); //board = board.Solve();
board.Draw(graphics, new Point(220, 30), new Size(400, 400)); Board = Board;
}; };
Controls.Add(informationbox); Controls.Add(informationbox);
...@@ -83,13 +91,22 @@ namespace PuzzlePlayer_Namespace ...@@ -83,13 +91,22 @@ namespace PuzzlePlayer_Namespace
informationbox.BackColor = Color.LightGray; informationbox.BackColor = Color.LightGray;
informationbox.Text = board.description; informationbox.Text = board.description;
} }
private void UpdateUI() private void UpdateUI()
{ {
generatebutton.Location = new Point(this.Width - 160, 30); boardspace.Size = FitBoard(new Size(Board.boardState.GetLength(0),Board.boardState.GetLength(1)),new Size(this.Width - 400, this.Height - 100),8);
hintbutton.Location = new Point(this.Width - 160, 130); generatebutton.Location = new Point(boardspace.Right + 30, 30);
solvebutton.Location = new Point(this.Width - 160, 230); hintbutton.Location = new Point(boardspace.Right + 30, 130);
solvebutton.Location = new Point(boardspace.Right + 30, 230);
informationbox.Location = new Point(40, 30); informationbox.Location = new Point(40, 30);
this.Invalidate();
}
public static Size FitBoard(Size gamesize, Size maxboardsize, int drawfactor)
{
int TileLength = Math.Min(maxboardsize.Width / gamesize.Width, maxboardsize.Height / gamesize.Height)/drawfactor*drawfactor;
return new Size(gamesize.Width*TileLength, gamesize.Height*TileLength);
//After each of the divisions in this method, the value should be rounded down,
//as the end value cannot exceed the size given by maxboardsize.
//however, C# rounds divisions of Int values down by default, so this does not have to be explicitly done.
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment