Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need Game of Life codes in C# I have to complete codes as seen below using

ID: 3660896 • Letter: H

Question

Hi I need Game of Life codes in C# I have to complete codes as seen below using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace hw4_GameOfLife { public partial class GameOfLifeForm : Form { // The game board: byte[,] board = new byte[45, 80]; // All elements are initialized to zero, meaning empty board. public GameOfLifeForm() { // Constructor. InitializeComponent(); // // DO NOT CHANGE THE CONTENTS OF THIS METHOD // } private void Form1_Load(object sender, EventArgs e) { // You don't need to write anything here but you can write something if you wish } private void Form1_Paint(object sender, PaintEventArgs e) { // You don't need to change the contents of this method. // Get the Graphics object of the form so that shapes can be drawn on the form Graphics g = this.CreateGraphics(); int rows = 45; // Number of rows int cols = 80; // Number of columns int w = 10; // Width and height of the cells int left = 10; // Unused space on the left int top = 30; // Unused space on the top // Draw horizontal lines: for (int i = 0; i <= rows; i++) { g.DrawLine(Pens.Black, left, top + i * w, left + cols * w, top + i * w); } // Draw vertical lines: for (int j = 0; j <= cols; j++) { g.DrawLine(Pens.Black, left + j * w, top, left + j * w, top + rows * w); } // Fill the cells according to the contents of the board for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i, j] == 1) { // Living cell g.FillRectangle(Brushes.Black, left + j * w + 1, top + i * w + 1, 9, 9); } else { // Empty cell g.FillRectangle(Brushes.White, left + j * w + 1, top + i * w + 1, 9, 9); } } } } private void btnStartPause_Click(object sender, EventArgs e) { // You don't need to change the contents of this method if (btnStartPause.Text == "Start") { // Start the game timer1.Enabled = true; btnStartPause.Text = "Pause"; } else { // Pause the game timer1.Enabled = false; btnStartPause.Text = "Start"; } } private void Form1_MouseClick(object sender, MouseEventArgs e) { if (btnStartPause.Text == "Pause") { // The game is running, don't change the board. return; } // If the execution of the program comes here, it means that // the game is paused, and the board can be changed. Graphics g = this.CreateGraphics(); int rows = 45; // Number of rows int cols = 80; // Number of columns int w = 10; // Width and height of the cells int left = 10; // Unused space on the left int top = 30; // Unused space on the top // Draw horizontal lines: for (int i = 0; i <= rows; i++) { g.DrawLine(Pens.Black, left, top + i * w, left + cols * w, top + i * w); } // Draw vertical lines: for (int j = 0; j <= cols; j++) { g.DrawLine(Pens.Black, left + j * w, top, left + j * w, top + rows * w); } // Fill the cells according to the contents of the board for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i, j] == 1) { // Living cell g.FillRectangle(Brushes.Black, left + j * w + 1, top + i * w + 1, 9, 9); } else { // Empty cell g.FillRectangle(Brushes.White, left + j * w + 1, top + i * w + 1, 9, 9); } } } // TODO: Match the point clicked by mouse on the form to the board's row and column values // TODO: If the clicked point is living cell, make it empty. If empty cell, make it living. // (a) Change the value in the board array // (b) Fill or clear the square clicked on the form } int NumOfLivingNeighbors(int r, int c) { // TODO: Compute number of alive neighbors of the cell at r-th row and c-th column int count = 0; return count; } private void timer1_Tick(object sender, EventArgs e) { // TODO: Compute the next generation and draw on the form } } }

Explanation / Answer

100 % working code

using System;

using System.Text;
namespace ArraysOnSteriod
{
    class Program
    {
        static void Main(string[] args)
        {
            Board board = new Board(20, 20);
            board.Randomize(new Random().NextDouble());
            bool exit = false;
            bool first = true;
            while (!exit)
            {
                Console.Clear();
                if (!first)
                    board.Step();
                first = false;
                Console.Write(board.ToString());
                Console.Write("Continue, randomize or quit? (c/r/q)");
                var inp = Console.ReadKey(false);
                if (inp.KeyChar == 'r')
                {
                    board.Randomize(new Random().NextDouble());
                    first = true;
                    continue;
                }
                exit = inp.KeyChar != 'c';
            }
        }
    }
    public class Board
    {
        bool[] nowStates;        // Current population
        bool[] newStates;        // New population
        int rows;                // Rows
        int cols;                // Columns
        int cells;                // Total cell count, not much used though...
        public Board(int columns, int rows)
        {
            this.cols = columns;
            this.rows = rows;
            this.cells = columns * rows;
            nowStates = new bool[cells];
            newStates = new bool[cells];
        }
        public void Randomize(double probability)
        {
            Random rand = new Random();
            for (int i = 0; i < nowStates.Length; i++)
                nowStates[i] = rand.NextDouble() < probability;
        }
        private void SwapGrids() { Swap<bool[]>(ref nowStates, ref newStates); }

        private static void Swap<T>(ref T a, ref T b) // reusable generic method

        {
            T x = a;
            a = b;
            b = x;
        }
        public void Step()
        {
            //AdvancePopulation(1, 1, cols - 1, rows - 1);
            AdvancePopulation(0, 0, cols, rows);
            SwapGrids();
        }
        private void AdvancePopulation(int c1, int r1, int c2, int r2)
        {
            for (int y = 0; y < rows; y++)
            {
                for (int x = 0; x < cols; x++)
                {
                    if (x >= c1 && x < c2 && y >= r1 && y < r2) // r2 and c2 are exclusive, it simplifies logic when calling the method
                    {
                        int neighbours = GetNeighbours(x, y);
                        if (nowStates[x + y * this.cols]) // is populated
                        {
                            if (neighbours <= 1 || neighbours >= 4) // if less than 2 or more than 3
                                newStates[x + y * this.cols] = false;
                            else // survive
                                newStates[x + y * this.cols] = true;
                        }
                        else if (neighbours == 3) // become populated
                            newStates[x + y * this.cols] = true;
                        else
                            newStates[x + y * this.cols] = nowStates[x + y * this.cols];
                    }
                    else
                        newStates[x + y * this.cols] = nowStates[x + y * this.cols];
                }
            }
        }
        private int GetNeighbours(int x, int y)
        {
            // Sum up on true states, traverse in clockwise direction
            return
                (x > 0 && y > 0 ? (nowStates[x - 1 + (y - 1) * this.cols] ? 1 : 0) : 0) +
                (y > 0 ? (nowStates[x + (y - 1) * this.cols] ? 1 : 0) : 0) +
                (x + 1 < cols && y > 0 ? (nowStates[x + 1 + (y - 1) * this.cols] ? 1 : 0) : 0) +
                (x + 1 < cols ? (nowStates[x + 1 + y * this.cols] ? 1 : 0) : 0) +
                (x + 1 < cols && y + 1 < rows ? (nowStates[x + 1 + (y + 1) * this.cols] ? 1 : 0) : 0) +
                (y + 1 < rows ? (nowStates[x + (y + 1) * this.cols] ? 1 : 0) : 0) +
                (x > 0 && y + 1 < rows ? (nowStates[x - 1 + (y + 1) * this.cols] ? 1 : 0) : 0) +
                (x > 0 ? (nowStates[x - 1 + y * this.cols] ? 1 : 0) : 0);
            /* Sorry about the obfuscation, but I needed to compress the code,
            * or it would span some 30 lines instead. */
        }
        public bool GetState(int column, int row)
        {
            if (column < 0 || column >= cols) throw new ArgumentOutOfRangeException("column");
            if (row < 0 || row >= rows) throw new ArgumentOutOfRangeException("row");
            return nowStates[row + column * this.cols];
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            for (int y = 0; y < rows; y++)
            {
                for (int x = 0; x < cols; x++)
                {
                    //sb.Append(nowStates[x + y * this.cols] ? 'O' : ' ');
                    sb.Append(
                        nowStates[x + y * this.cols] ?
                        GetNeighbours(x, y).ToString() :
                        " ");
                }
                sb.AppendLine();
            }
            return sb.ToString();
        }
        public int Columns { get { return cols; } }
        public int Rows { get { return rows; } }
        public int CellCount { get { return cells; } }
    }
}