Results 1 to 6 of 6

Thread: Game of life in C#

  1. #1
    Join Date
    Nov 2008
    Posts
    20

    Game of life in C#

    I am currently writing the game of life in C#. but i need help writing the code to paint the squares and to start the game. Pls does anyone know what the code for doing this wud b. Thanx for your help

  2. #2
    Join Date
    May 2008
    Posts
    188

    Re: Game of life in C#

    My friend we will not prepare food for you. You need to try the code and then if you have any problem, get back over here and we will help you to solve your difficulty.

  3. #3
    Join Date
    May 2008
    Posts
    1,205

    Re: Game of life in C#

    See this link hope this helps you : http://www.dzone.com/links/selfprint...life_in_c.html

  4. #4
    Join Date
    Oct 2008
    Posts
    49

    Re: Game of life in C#

    public MyControl()
    {

    // Activates double buffering
    SetStyle(ControlStyles.UserPaint, true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.DoubleBuffer, true);
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    }

  5. #5
    Join Date
    May 2008
    Posts
    1,304

  6. #6
    Join Date
    May 2008
    Posts
    21

    Re: Game of life in C#

    Here is the code for Game of life in C#

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
    
            private int SQsize = 25;
            private int[,] paintSQ = new int[50, 50];
            private int shape = 1;
            private int gridLines = 1;
            private int maxRow = 20;
            private int maxCol = 20;
            private int gridSize = 500;
            private int speed = 100;
    
    
    
    
    
            public Form1()
            {
    
                InitializeComponent();
    
    
    
            }
    
    
            private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
    
                //Paint grid
    
                if (gridLines == 1)
                {
                    Graphics grid = e.Graphics;
                    Pen myPen = new Pen(Color.Black, 1); //create a pen object
                    for (int a = 0; a <= this.gridSize; a += this.SQsize)
                    {
                        grid.DrawLine(myPen, 0, a, gridSize, a); //use the DrawLine Horizontal
                        grid.DrawLine(myPen, a, 0, a, gridSize); //use the DrawLine Vertical
                    }
                    myPen.Dispose();
                }
    
                Graphics paint = e.Graphics;
                SolidBrush myBrush = new SolidBrush(colorDialog1.Color);
    
                if (gridSize == 500 && SQsize == 10)
                {
                    this.maxCol = 50;
                    this.maxRow = 50;
                    toolStripStatusLabel1.Text = "Grid Size: Large 50x50";
                }
                if (gridSize == 500 && SQsize == 25)
                {
                    this.maxCol = 20;
                    this.maxRow = 20;
                    toolStripStatusLabel1.Text = "Grid Size: Large 20x20";
                }
                if (gridSize == 500 && SQsize == 50)
                {
                    this.maxCol = 10;
                    this.maxRow = 10;
                    toolStripStatusLabel1.Text = "Grid Size: Large 10x10";
                }
                if (gridSize == 250 && SQsize == 10)
                {
                    this.maxCol = 25;
                    this.maxRow = 25;
                    toolStripStatusLabel1.Text = "Grid Size: Medium 25x25";
                }
                if (gridSize == 250 && SQsize == 25)
                {
                    this.maxCol = 10;
                    this.maxRow = 10;
                    toolStripStatusLabel1.Text = "Grid Size: Medium 10x10";
                }
                if (gridSize == 250 && SQsize == 50)
                {
                    this.maxCol = 5;
                    this.maxRow = 5;
                    toolStripStatusLabel1.Text = "Grid Size: Medium 5x5";
                }
                if (gridSize == 200 && SQsize == 10)
                {
                    this.maxCol = 20;
                    this.maxRow = 20;
                    toolStripStatusLabel1.Text = "Grid Size: Small 20x20";
                }
                if (gridSize == 200 && SQsize == 25)
                {
                    this.maxCol = 8;
                    this.maxRow = 8;
                    toolStripStatusLabel1.Text = "Grid Size: Small 8x8";
                }
                if (gridSize == 200 && SQsize == 50)
                {
                    this.maxCol = 4;
                    this.maxRow = 4;
                    toolStripStatusLabel1.Text = "Grid Size: Small 4x4";
                }
                int cellRow = 0;
    
                while (cellRow < maxRow)
                {
                    int cellCol = 0;
    
                    while (cellCol < maxCol)
                    {
                        if (shape == 1)
                        {
                            if (paintSQ[cellCol, cellRow] == 1)
                            {
    
                                paint.FillRectangle(myBrush, (cellCol * this.SQsize) + 1, (cellRow * this.SQsize) + 1, this.SQsize - 1, this.SQsize - 1);
                            }
                        }
                        if (shape == 0)
                        {
                            if (paintSQ[cellCol, cellRow] == 1)
                            {
                                paint.SmoothingMode = SmoothingMode.HighQuality;
                                paint.FillEllipse(myBrush, new Rectangle((cellCol * this.SQsize), (cellRow * this.SQsize), this.SQsize, this.SQsize));
                            }
                        }
                        cellCol++;
                    }
                    cellRow++;
                }
                myBrush.Dispose();
    
    
            }
    
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                if (e.X < pictureBox1.Width && e.Y < pictureBox1.Height && e.X > 0 && e.Y > 0 && e.Button == MouseButtons.Left)
                {
                    int xMouse = 0;
                    int yMouse = 0;
                    double xMouse1 = (e.X - 1) / SQsize;
                    double yMouse1 = (e.Y - 1) / SQsize;
                    xMouse = (int)System.Math.Ceiling(xMouse1);
                    yMouse = (int)System.Math.Ceiling(yMouse1);
                    this.paintSQ[xMouse, yMouse] = 1;
                    pictureBox1.Refresh();
                }
    
            }
    
            private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.X < pictureBox1.Width && e.Y < pictureBox1.Height && e.X > 0 && e.Y > 0 && e.Button == MouseButtons.Left)
                {
                    int xMouse = 0;
                    int yMouse = 0;
                    double xMouse1 = (e.X - 1) / SQsize;
                    double yMouse1 = (e.Y - 1) / SQsize;
                    xMouse = (int)System.Math.Ceiling(xMouse1);
                    yMouse = (int)System.Math.Ceiling(yMouse1);
                    this.paintSQ[xMouse, yMouse] = 1;
                    pictureBox1.Refresh();
                }
    
    
    
            }
    
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void colourToolStripMenuItem_Click(object sender, EventArgs e)
            {
                colorDialog1.ShowDialog();
            }
    
            private void smallToolStripMenuItem_Click_1(object sender, EventArgs e)
            {
    
                maxRow = 10;
                maxCol = 10;
                gridSize = 200;
                Refresh();
    
            }
    
    
    
            private void mediumToolStripMenuItem_Click(object sender, EventArgs e)
            {
                gridSize = 250;
                maxRow = 25;
                maxCol = 25;
                Refresh();
    
            }
    
            private void largeToolStripMenuItem_Click(object sender, EventArgs e)
            {
                gridSize = 500;
                maxRow = 50;
                maxCol = 50;
                Refresh();
    
            }
    
    
            private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
            {
                Close();
            }
    
            private void helpToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                int[,] tempPaintSQ = new int[50, 50];
                int cellRow = 0;
    
                while (cellRow < maxRow)
                {
                    int cellCol = 0;
    
                    while (cellCol < maxCol)
                    {
    
                        int cellAlive = 0;
    
    
                        if (cellCol - 1 >= 0 && cellRow - 1 >= 0 && cellCol - 1 < this.maxCol && cellRow - 1 < this.maxRow)
                        {
                            if (paintSQ[cellCol - 1, cellRow - 1] == 1)
                            {
                                cellAlive++;
                            }
                        }
    
                        if (cellCol - 1 >= 0 && cellRow >= 0 && cellCol - 1 < this.maxCol && cellRow < this.maxRow)
                        {
                            if (paintSQ[cellCol - 1, cellRow] == 1)
                            {
                                cellAlive++;
                            }
                        }
    
                        if (cellCol - 1 >= 0 && cellRow + 1 >= 0 && cellCol - 1 < this.maxCol && cellRow + 1 < this.maxRow)
                        {
                            if (paintSQ[cellCol - 1, cellRow + 1] == 1)
                            {
                                cellAlive++;
                            }
                        }
    
    
                        if (cellCol >= 0 && cellRow - 1 >= 0 && cellCol < this.maxCol && cellRow - 1 < this.maxRow)
                        {
                            if (paintSQ[cellCol, cellRow - 1] == 1)
                            {
                                cellAlive++;
                            }
                        }
                        if (cellCol >= 0 && cellRow + 1 >= 0 && cellCol < this.maxCol && cellRow + 1 < this.maxRow)
                        {
                            if (paintSQ[cellCol, cellRow + 1] == 1)
                            {
                                cellAlive++;
                            }
                        }
    
                        if (cellCol >= +1 && cellRow - 1 >= 0 && cellCol + 1 < this.maxCol && cellRow - 1 < this.maxRow)
                        {
                            if (paintSQ[cellCol + 1, cellRow - 1] == 1)
                            {
                                cellAlive++;
                            }
                        }
                        if (cellCol + 1 >= 0 && cellRow >= 0 && cellCol + 1 < this.maxCol && cellRow < this.maxRow)
                        {
                            if (paintSQ[cellCol + 1, cellRow] == 1)
                            {
                                cellAlive++;
                            }
                        }
                        if (cellCol + 1 >= 0 && cellRow + 1 >= 0 && cellCol + 1 < this.maxCol && cellRow + 1 < this.maxRow)
                        {
                            if (paintSQ[cellCol + 1, cellRow + 1] == 1)
                            {
                                cellAlive++;
                            }
                        }
    
                        if (paintSQ[cellCol, cellRow] == 1)
                        {
                            if (cellAlive < 2)
                            {
    
                                tempPaintSQ[cellCol, cellRow] = 0;
                            }
                            if (cellAlive == 2 || cellAlive == 3)
                            {
    
                                tempPaintSQ[cellCol, cellRow] = 1;
                            }
    
                            if (cellAlive > 3)
                            {
    
                                tempPaintSQ[cellCol, cellRow] = 0;
                            }
                        }
                        else
                        {
                            if (cellAlive == 3)
                            { tempPaintSQ[cellCol, cellRow] = 1; }
                            else
                            { tempPaintSQ[cellCol, cellRow] = 0; }
    
                        }
    
    
                        cellCol++;
                    }
                    cellRow++;
    
    
    
                }
                this.paintSQ = tempPaintSQ;
                speedBarMove();
                cellcountAliveDead();
                pictureBox1.Refresh();
    
            }
            private void speedBarMove()
            {
                if (speedBar.Value == 0)
                {
                    timer1.Interval = 400;
                }
                if (speedBar.Value == 1)
                {
                    timer1.Interval = 200;
                }
                if (speedBar.Value == 2)
                {
                    timer1.Interval = 100;
                }
                if (speedBar.Value == 3)
                {
                    timer1.Interval = 50;
                }
                if (speedBar.Value == 4)
                {
                    timer1.Interval = 5;
                }
            }
            private void start_Click(object sender, EventArgs e)
            {
                timer1.Enabled = true;
    
            }
    
            private void stop_Click(object sender, EventArgs e)
            {
                timer1.Enabled = false;
            }
    
            private void Clear_Click(object sender, EventArgs e)
            {
                int cellRow = 0;
    
                while (cellRow < maxRow)
                {
                    int cellCol = 0;
    
                    while (cellCol < maxCol)
                    {
    
                        paintSQ[cellCol, cellRow] = 0;
    
                        cellCol++;
                    }
                    cellRow++;
                }
    
    
                pictureBox1.Refresh();
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                colorDialog1.ShowDialog();
    
    
            }
    
            private void onToolStripMenuItem_Click(object sender, EventArgs e)
            {
                gridLines = 1;
                pictureBox1.Refresh();
            }
    
            private void squareToolStripMenuItem_Click(object sender, EventArgs e)
            {
                shape = 1;
                pictureBox1.Refresh();
            }
    
            private void circleToolStripMenuItem_Click(object sender, EventArgs e)
            {
                shape = 0;
                pictureBox1.Refresh();
            }
    
            private void offToolStripMenuItem_Click(object sender, EventArgs e)
            {
                gridLines = 0;
                pictureBox1.Refresh();
            }
    
            private void toolStripMenuItem2_Click(object sender, EventArgs e)
            {
                SQsize = 10;
                pictureBox1.Refresh();
            }
    
            private void mediumToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                SQsize = 25;
                pictureBox1.Refresh();
            }
    
            private void largeToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                SQsize = 50;
                pictureBox1.Refresh();
    
            }
    
            private void slowToolStripMenuItem_Click(object sender, EventArgs e)
            {
                timer1.Interval = 400;
                speed = 400;
                if (speed == 400)
                {
                    speedBar.Value = 0;
                }
            }
    
            private void slowToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                timer1.Interval = 200;
                speed = 200;
                if (speed == 200)
                {
                    speedBar.Value = 1;
                }
    
            }
    
            private void mediumToolStripMenuItem2_Click(object sender, EventArgs e)
            {
                timer1.Interval = 100;
                speed = 100;
                if (speed == 100)
                {
                    speedBar.Value = 2;
                }
            }
    
            private void fastToolStripMenuItem_Click(object sender, EventArgs e)
            {
                timer1.Interval = 50;
                speed = 50;
                if (speed == 50)
                {
                    speedBar.Value = 3;
                }
            }
    
            private void lightSpeedToolStripMenuItem_Click(object sender, EventArgs e)
            {
                timer1.Interval = 5;
                speed = 5;
                if (speed == 5)
                {
                    speedBar.Value = 4;
                }
            }
            private void cellcountAliveDead()
            {
                int currentAlive = 0;
                int currentDead = 0;
    
                int cellRow = 0;
                while (cellRow < maxRow)
                {
                    int cellCol = 0;
    
                    while (cellCol < maxCol)
                    {
    
    
                        if (paintSQ[cellCol, cellRow] == 1)
                        {
                            currentAlive++;
                        }
                        if (paintSQ[cellCol, cellRow] == 0)
                        {
                            currentDead++;
                        }
                        cellCol++;
                    }
                    cellRow++;
                }
    
    
                toolStripStatusLabel2.Text = "Cells Alive: " + currentAlive;
                toolStripStatusLabel3.Text = "Dead Cells: " + currentDead;
    
            }
    
            private void openToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
            private void saveToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
    
        }
    
    
    }

Similar Threads

  1. Do you think Sims 3 town life is nice game
    By BethanyI in forum Video Games
    Replies: 5
    Last Post: 12-07-2011, 10:58 PM
  2. F1 2010 game Vs Real Life
    By Sarasija in forum Video Games
    Replies: 4
    Last Post: 03-12-2010, 06:41 PM
  3. Half Life 2 episode one - cannot connect game
    By Tomthegreat in forum Video Games
    Replies: 4
    Last Post: 22-01-2009, 10:42 PM
  4. Jinsei Game of Life pedometer
    By mauricio in forum Portable Devices
    Replies: 0
    Last Post: 21-07-2008, 08:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,128,749.16484 seconds with 16 queries