Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Student
  4.  * Date: 13.11.2018
  5.  * Time: 15:12
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Windows.Forms;
  13.  
  14. namespace wall_lab
  15. {
  16.     /// <summary>
  17.     /// Description of MainForm.
  18.     /// </summary>
  19.     public partial class MainForm : Form
  20.     {
  21.         Random rand = new Random();
  22.            
  23.         public bool ValueInPointArray(bool[][] array, Point compare) {
  24.             return array[compare.Y][compare.X];
  25.         }
  26.        
  27.         public Point PointMod(Point pt, int x, int y) {
  28.             pt.X += x;
  29.             pt.Y += y;
  30.             return pt;
  31.         }
  32.        
  33.         public void SetWallsMove(ref bool[][] walls_w, ref bool[][] walls_h, Point from, Point to) {
  34.             to.X -= from.X;
  35.             to.Y -= from.Y;
  36.            
  37.             if(to.X != 0) {
  38.                 walls_w[from.Y][from.X+(to.X==1?1:0)] = false;
  39.                 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) );
  40.             }
  41.             else if(to.Y != 0) {
  42.                 walls_h[from.Y+(to.Y==1?1:0)][from.X] = false;
  43.                 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) );
  44.             }
  45.            
  46.         }
  47.        
  48.         public byte[] moves = new byte[4];
  49.         byte counter = 0;
  50.        
  51.         public bool CanMakeMove(int width, int height, ref bool[][] oldpos, ref Point[] old, ref int old_length) {
  52.             //no more moves
  53.             if(old_length < 1) return false;
  54.            
  55.             counter = 0;
  56.             if(old[old_length-1].Y > 0 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], 0, -1))) moves[0] = ++counter;
  57.             else moves[0] = 0;
  58.             if(old[old_length-1].X < width-1 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], 1, 0))) moves[1] = ++counter;
  59.             else moves[1] = 0;
  60.             if(old[old_length-1].Y < height-1 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], 0, 1))) moves[2] = ++counter;
  61.             else moves[2] = 0;
  62.             if(old[old_length-1].X > 0 && !ValueInPointArray(oldpos, PointMod(old[old_length-1], -1, 0))) moves[3] = ++counter;
  63.             else moves[3] = 0;
  64.             return counter > 0;
  65.         }
  66.        
  67.         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) {
  68.            
  69.             //move available?
  70.             if(CanMakeMove(width, height, ref oldpos, ref old, ref old_length) && counter >= 1) {
  71.                 //make move
  72.                 counter = (byte)(rand.Next(1, (int)counter+1));
  73.                 for(int i = 0; i < 4; i++) {
  74.                     if(moves[i] == counter) {
  75.                         old[old_length] = PointMod(old[old_length-1], (i%2==1?(i==1?1:-1):0), (i%2==0?(i==0?-1:1):0));
  76.                         oldpos[old[old_length].Y][old[old_length].X] = true;
  77.                        
  78.                         SetWallsMove(ref walls_w, ref walls_h, old[old_length-1], old[old_length]);
  79.                        
  80.                         old_length += 1;
  81.                         return true;
  82.                     }
  83.                 }
  84.             }
  85.             return --old_length > 0;
  86.         }
  87.        
  88.         public Panel p = new Panel();
  89.         public int width = 80;
  90.         public int height = 60;
  91.         public int w_x, w_y;
  92.         public bool[][] walls_w, walls_h;
  93.         public bool[][] oldpos;
  94.         public Point[] old;
  95.         public int pl_len = 1;
  96.        
  97.         public System.Windows.Forms.Timer drawer = new System.Windows.Forms.Timer();
  98.        
  99.         public SolidBrush b = new SolidBrush(Color.White);
  100.         public Pen pen = new Pen(Color.Black);
  101.         public Pen wpen = new Pen(Color.White);
  102.         public Graphics g;
  103.    
  104.         public MainForm()
  105.         {
  106.             //
  107.             // The InitializeComponent() call is required for Windows Forms designer support.
  108.             //
  109.             InitializeComponent();
  110.             this.FormBorderStyle = FormBorderStyle.FixedSingle;
  111.            
  112.             //this.SetClientSizeCore(800, 600);
  113.             this.ClientSize = new Size(800, 600);
  114.            
  115.            
  116.             p.Location = new Point(0, 0);
  117.             p.Size = new Size(800, 600);
  118.             p.BackColor = Color.White;
  119.             this.Controls.Add(p);
  120.             g = p.CreateGraphics();
  121.            
  122.             walls_w = new bool[height + 1][];
  123.             walls_h = new bool[height + 1][];
  124.             for( int i = 0; i < height + 1; i++ ) {
  125.                 walls_w[i] = new bool[width + 1];
  126.                 walls_h[i] = new bool[width + 1];
  127.                 for(int j = 0; j <= width; j++) { walls_w[i][j] = true; walls_h[i][j] = true; }
  128.             }
  129.            
  130.             oldpos = new bool[height][];
  131.             for( int i = 0; i < height; i++ ) {
  132.                 oldpos[i] = new bool[width];
  133.             }
  134.            
  135.             old = new Point[width * height];
  136.             old[0] = new Point(rand.Next(width), rand.Next(height));
  137.             oldpos[old[0].Y][old[0].X] = true;
  138.            
  139.             w_x = 800/(width);
  140.             w_y = 600/(height);
  141.            
  142.             pen.Width = 6;
  143.             wpen.Width = 6;
  144.             drawer.Tick += new EventHandler(TimerEvent);
  145.             drawer.Start();
  146.             drawer.Interval = 1;
  147.         }
  148.        
  149.         public void PaintMap() {
  150.             for(int y = 0; y <= height; y++) {
  151.                 for(int x = 0; x <= width; x++) {
  152.                     if( walls_w[y][x] ) g.DrawLine(pen, x * (w_y), y * (w_x), x * (w_y), (y + 1) * (w_x) );
  153.                     if( walls_h[y][x] ) g.DrawLine(pen, x * (w_y), y * (w_x), (x + 1) * (w_y), y * (w_x) );
  154.                 }
  155.             }
  156.         }
  157.        
  158.         public void ClearMap() {
  159.             //b.Color = Color.Red;
  160.             g.FillRectangle(b, new RectangleF(0, 0, 800, 600));
  161.         }
  162.        
  163.         public bool firstrun = true;
  164.         public void TimerEvent(object sender, EventArgs e) {
  165.             //PaintMap();
  166.             if(firstrun) {
  167.                 ClearMap();
  168.                 PaintMap();
  169.                 firstrun = false;
  170.             }
  171.             if(!MakeMove(width, height, ref walls_w, ref walls_h, ref oldpos, ref old, ref pl_len)) {
  172.                 ClearMap();
  173.                 PaintMap();
  174.                 drawer.Stop();
  175.                 g.Dispose();
  176.                 pen.Dispose();
  177.                 wpen.Dispose();
  178.             }
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement