Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: Student
- * Date: 13.11.2018
- * Time: 15:12
- *
- * To change this template use Tools | Options | Coding | Edit Standard Headers.
- */
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace wall_lab
- {
- /// <summary>
- /// Description of MainForm.
- /// </summary>
- public partial class MainForm : Form
- {
- Random rand = new Random();
- public bool ValueInPointArray(bool[][] array, Point compare) {
- return array[compare.Y][compare.X];
- }
- public Point PointMod(Point pt, int x, int y) {
- pt.X += x;
- pt.Y += y;
- return pt;
- }
- public void SetWallsMove(ref bool[][] walls_w, ref bool[][] walls_h, Point from, Point to) {
- to.X -= from.X;
- to.Y -= from.Y;
- if(to.X != 0) {
- walls_w[from.Y][from.X+(to.X==1?1:0)] = false;
- g.DrawLine(wpen, (from.X + (to.X==1?1:0)) * (w_y), from.Y * (w_x), (from.X + (to.X==1?1:0)) * (w_y), (from.Y + 1) * (w_x) );
- }
- else if(to.Y != 0) {
- walls_h[from.Y+(to.Y==1?1:0)][from.X] = false;
- g.DrawLine(wpen, (from.X) * (w_y), (from.Y+(to.Y==1?1:0)) * (w_x), (from.X+1) * (w_y), (from.Y+(to.Y==1?1:0)) * (w_x) );
- }
- }
- public byte[] moves = new byte[4];
- byte counter = 0;
- public bool CanMakeMove(int width, int height, ref bool[][] oldpos, ref Point[] old, ref int old_length) {
- //no more moves
- if(old_length < 1) return false;
- counter = 0;
- if(old[old_length-1].Y > 0 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], 0, -1))) moves[0] = ++counter;
- else moves[0] = 0;
- if(old[old_length-1].X < width-1 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], 1, 0))) moves[1] = ++counter;
- else moves[1] = 0;
- if(old[old_length-1].Y < height-1 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], 0, 1))) moves[2] = ++counter;
- else moves[2] = 0;
- if(old[old_length-1].X > 0 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], -1, 0))) moves[3] = ++counter;
- else moves[3] = 0;
- return counter > 0;
- }
- public bool MakeMove(int width, int height, ref bool[][] walls_w, ref bool[][] walls_h, ref bool[][] oldpos, ref Point[] old, ref int old_length) {
- //move available?
- if(CanMakeMove(width, height, ref oldpos, ref old, ref old_length) && counter >= 1) {
- //make move
- counter = (byte)(rand.Next(1, (int)counter+1));
- for(int i = 0; i < 4; i++) {
- if(moves[i] == counter) {
- old[old_length] = PointMod(old[old_length-1], (i%2==1?(i==1?1:-1):0), (i%2==0?(i==0?-1:1):0));
- oldpos[old[old_length].Y][old[old_length].X] = true;
- SetWallsMove(ref walls_w, ref walls_h, old[old_length-1], old[old_length]);
- old_length += 1;
- return true;
- }
- }
- }
- return --old_length > 0;
- }
- public Panel p = new Panel();
- public int width = 80;
- public int height = 60;
- public int w_x, w_y;
- public bool[][] walls_w, walls_h;
- public bool[][] oldpos;
- public Point[] old;
- public int pl_len = 1;
- public System.Windows.Forms.Timer drawer = new System.Windows.Forms.Timer();
- public SolidBrush b = new SolidBrush(Color.White);
- public Pen pen = new Pen(Color.Black);
- public Pen wpen = new Pen(Color.White);
- public Graphics g;
- public MainForm()
- {
- //
- // The InitializeComponent() call is required for Windows Forms designer support.
- //
- InitializeComponent();
- this.FormBorderStyle = FormBorderStyle.FixedSingle;
- //this.SetClientSizeCore(800, 600);
- this.ClientSize = new Size(800, 600);
- p.Location = new Point(0, 0);
- p.Size = new Size(800, 600);
- p.BackColor = Color.White;
- this.Controls.Add(p);
- g = p.CreateGraphics();
- walls_w = new bool[height + 1][];
- walls_h = new bool[height + 1][];
- for( int i = 0; i < height + 1; i++ ) {
- walls_w[i] = new bool[width + 1];
- walls_h[i] = new bool[width + 1];
- for(int j = 0; j <= width; j++) { walls_w[i][j] = true; walls_h[i][j] = true; }
- }
- oldpos = new bool[height][];
- for( int i = 0; i < height; i++ ) {
- oldpos[i] = new bool[width];
- }
- old = new Point[width * height];
- old[0] = new Point(rand.Next(width), rand.Next(height));
- oldpos[old[0].Y][old[0].X] = true;
- w_x = 800/(width);
- w_y = 600/(height);
- pen.Width = 6;
- wpen.Width = 6;
- drawer.Tick += new EventHandler(TimerEvent);
- drawer.Start();
- drawer.Interval = 1;
- }
- public void PaintMap() {
- for(int y = 0; y <= height; y++) {
- for(int x = 0; x <= width; x++) {
- if( walls_w[y][x] ) g.DrawLine(pen, x * (w_y), y * (w_x), x * (w_y), (y + 1) * (w_x) );
- if( walls_h[y][x] ) g.DrawLine(pen, x * (w_y), y * (w_x), (x + 1) * (w_y), y * (w_x) );
- }
- }
- }
- public void ClearMap() {
- //b.Color = Color.Red;
- g.FillRectangle(b, new RectangleF(0, 0, 800, 600));
- }
- public bool firstrun = true;
- public void TimerEvent(object sender, EventArgs e) {
- //PaintMap();
- if(firstrun) {
- ClearMap();
- PaintMap();
- firstrun = false;
- }
- if(!MakeMove(width, height, ref walls_w, ref walls_h, ref oldpos, ref old, ref pl_len)) {
- ClearMap();
- PaintMap();
- drawer.Stop();
- g.Dispose();
- pen.Dispose();
- wpen.Dispose();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement