Advertisement
Guest User

Board Class C# The Game Of Life

a guest
Apr 16th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Test
  12. {
  13.     public partial class Board : Form
  14.     {
  15.         private int BOARDWIDTH;
  16.         private int BOARDHEIGHT;
  17.         private int cellSize = 10;
  18.         private bool Wrap;
  19.         private int refreshRate;
  20.         private World world;
  21.         private Pen wPen = new Pen(Color.Black);
  22.         private SolidBrush bBrush = new SolidBrush(Color.Black);
  23.         private PictureBox pictureBox = new PictureBox();
  24.         private Timer Timer1 = new Timer();
  25.         public Board(World world, int cellsize, bool Wrap, int refreshRate)
  26.         {
  27.             this.BOARDHEIGHT = world.getWorldHeight() * cellsize;
  28.             this.BOARDWIDTH = world.getWorldWidth() * cellsize;
  29.             InitializeComponent();
  30.             SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  31.             pictureBox.SetBounds(0, 0, this.BOARDWIDTH, this.BOARDHEIGHT);
  32.             this.cellSize = cellsize;
  33.             this.world = world;
  34.             this.Wrap = Wrap;
  35.             this.refreshRate = refreshRate;
  36.             InitializeTimer();
  37.  
  38.         }
  39.         private void InitializeTimer()
  40.         {
  41.             Timer1.Interval = refreshRate;
  42.             Timer1.Tick += new EventHandler(Timer1_Tick);
  43.             Timer1.Enabled = true;
  44.  
  45.         }
  46.  
  47.         private void Timer1_Tick(object Sender, EventArgs e)
  48.         {
  49.             Invalidate();
  50.             if (Wrap) world.updateWorldWraparound();
  51.             else world.updateWorld();
  52.         }
  53.         private void Board_Load(object sender, System.EventArgs e)
  54.         {
  55.             pictureBox.Dock = DockStyle.Fill;
  56.             pictureBox.BackColor = Color.White;
  57.             this.Controls.Add(pictureBox);
  58.  
  59.         }
  60.  
  61.         protected override void OnPaint(PaintEventArgs e)
  62.         {
  63.             base.OnPaint(e);
  64.                 for (int i = 0; i < this.world.getWorldHeight(); i++)
  65.                 {
  66.                     for (int j = 0; j < this.world.getWorldWidth(); j++)
  67.                     {
  68.  
  69.                         if (!this.world.getCellState(i, j))
  70.                         {
  71.                                 e.Graphics.DrawRectangle(wPen, i*this.cellSize - 1, j * this.cellSize - 1, this.cellSize, this.cellSize);
  72.  
  73.                         }
  74.                         else
  75.                         {
  76.                                 e.Graphics.FillRectangle(bBrush, i * this.cellSize, j * this.cellSize, this.cellSize, this.cellSize);
  77.  
  78.                         }
  79.                     }
  80.                 }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement