SlideShare a Scribd company logo
1 of 12
Download to read offline
GameOfLife.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace A03LeeJoe
{
class GameOfLife
{
private int[, ,] grid;
private int displayedGrid = 0;
private int tempGrid = 1;
private const int DEAD = 0;
private const int ALIVE = 1;
private const int TOP = 5;
private const int LEFT = 20;
//constructor
public GameOfLife()
{
grid = CreateGrid();
}
//methods
private int[, ,] CreateGrid()
{
int[, ,] newGrid = new int[25, 40, 2];
for (int col = 14; col < 27; col++)
{
newGrid[13, col, 0] = ALIVE;
}
for (int row = 8; row < 11; row++)
{
newGrid[row, 12, 0] = ALIVE;
newGrid[row, 20, 0] = ALIVE;
newGrid[row, 28, 0] = ALIVE;
}
return newGrid;
}
public void Start()
{
//Display Grid
Console.SetWindowSize(80, 40);
Setup();
//loop until user presses a key
while (!Console.KeyAvailable)
{
Thread.Sleep(1000);
//perform next move
NextMove();
DisplayGrid();
SetColor(1);
Console.WriteLine();
Console.CursorLeft = LEFT;
Console.WriteLine(" Press any key to end ", displayedGrid);
}
}
private void Setup()
{
CreateHeader();
DisplayGrid();
//prompt user to press enter
Console.WriteLine();
Console.CursorLeft = LEFT;
SetColor(1);
Console.WriteLine(" Please press enter to begin ");
Console.ReadLine();
}
private void CreateHeader()
{
Console.SetCursorPosition(LEFT, TOP);
SetColor(1);
Console.WriteLine("{0,40}", " ");
Console.CursorLeft = LEFT;
Console.WriteLine(" G A M E O F L I F E ");
Console.CursorLeft = LEFT;
Console.WriteLine("{0,40}", " ");
}
private void NextMove()
{
//iterate through grid
for (int row = 0; row <= grid.GetUpperBound(0); row++)
{
for (int col = 0; col <= grid.GetUpperBound(1); col++)
{
//each iteration check neighbor count
int neighborCount = GetNeighborCount(row, col);
//determine new life or death and update on temp grid'
grid[row, col, tempGrid] = UpdateCell(grid[row, col, displayedGrid],
neighborCount);
}
}
//switch the grid's 3rd dimension to display new grid
SwitchGrid();
}
private void SwitchGrid()
{
if (displayedGrid == 0)
{
displayedGrid = 1;
tempGrid = 0;
}
else
{
displayedGrid = 0;
tempGrid = 1;
}
}
private int UpdateCell(int stateOfCell, int neighborCount)
{
if (stateOfCell == ALIVE)
{
switch (neighborCount)
{
case 0:
case 1:
return DEAD;
case 2:
case 3:
return ALIVE;
default:
return DEAD;
}
}
else if (neighborCount == 3)
{
return ALIVE;
}
return stateOfCell;
}
private void DisplayGrid()
{
Console.SetCursorPosition(LEFT, TOP + 4);
for (int row = 0; row <= grid.GetUpperBound(0); row++)
{
Console.CursorLeft = LEFT;
for (int col = 0; col <= grid.GetUpperBound(1); col++)
{
SetColor(grid[row, col, displayedGrid]);
Console.Write(".");
}
Console.WriteLine();
}
}
private void SetColor(int state)
{
if (state == 1)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
}
else
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Black;
}
}
private int GetNeighborCount(int row, int col)
{
int neighborCount = 0;
int rowStart = row - 1;
int rowStop = row + 1;
int colStart = col - 1;
int colStop = col + 1;
for (int rowIndex = rowStart; rowIndex <= rowStop; rowIndex++)
{
for (int colIndex = colStart; colIndex <= colStop; colIndex++)
{
if ((rowIndex == row && colIndex == col) ||
(OutOfBounds(rowIndex, colIndex)))
{
continue;
}
if (grid[rowIndex, colIndex, displayedGrid] == ALIVE)
{
neighborCount++;
}
}
}
return neighborCount;
}
private Boolean OutOfBounds(int row, int col)
{
return !((row >= 0 && row <= grid.GetUpperBound(0)) &&
(col >= 0 && col <= grid.GetUpperBound(1)));
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A03LeeJoe
{
class Program
{
static void Main(string[] args)
{
GameOfLife game = new GameOfLife();
game.Start();
}
}
}
Solution
GameOfLife.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace A03LeeJoe
{
class GameOfLife
{
private int[, ,] grid;
private int displayedGrid = 0;
private int tempGrid = 1;
private const int DEAD = 0;
private const int ALIVE = 1;
private const int TOP = 5;
private const int LEFT = 20;
//constructor
public GameOfLife()
{
grid = CreateGrid();
}
//methods
private int[, ,] CreateGrid()
{
int[, ,] newGrid = new int[25, 40, 2];
for (int col = 14; col < 27; col++)
{
newGrid[13, col, 0] = ALIVE;
}
for (int row = 8; row < 11; row++)
{
newGrid[row, 12, 0] = ALIVE;
newGrid[row, 20, 0] = ALIVE;
newGrid[row, 28, 0] = ALIVE;
}
return newGrid;
}
public void Start()
{
//Display Grid
Console.SetWindowSize(80, 40);
Setup();
//loop until user presses a key
while (!Console.KeyAvailable)
{
Thread.Sleep(1000);
//perform next move
NextMove();
DisplayGrid();
SetColor(1);
Console.WriteLine();
Console.CursorLeft = LEFT;
Console.WriteLine(" Press any key to end ", displayedGrid);
}
}
private void Setup()
{
CreateHeader();
DisplayGrid();
//prompt user to press enter
Console.WriteLine();
Console.CursorLeft = LEFT;
SetColor(1);
Console.WriteLine(" Please press enter to begin ");
Console.ReadLine();
}
private void CreateHeader()
{
Console.SetCursorPosition(LEFT, TOP);
SetColor(1);
Console.WriteLine("{0,40}", " ");
Console.CursorLeft = LEFT;
Console.WriteLine(" G A M E O F L I F E ");
Console.CursorLeft = LEFT;
Console.WriteLine("{0,40}", " ");
}
private void NextMove()
{
//iterate through grid
for (int row = 0; row <= grid.GetUpperBound(0); row++)
{
for (int col = 0; col <= grid.GetUpperBound(1); col++)
{
//each iteration check neighbor count
int neighborCount = GetNeighborCount(row, col);
//determine new life or death and update on temp grid'
grid[row, col, tempGrid] = UpdateCell(grid[row, col, displayedGrid],
neighborCount);
}
}
//switch the grid's 3rd dimension to display new grid
SwitchGrid();
}
private void SwitchGrid()
{
if (displayedGrid == 0)
{
displayedGrid = 1;
tempGrid = 0;
}
else
{
displayedGrid = 0;
tempGrid = 1;
}
}
private int UpdateCell(int stateOfCell, int neighborCount)
{
if (stateOfCell == ALIVE)
{
switch (neighborCount)
{
case 0:
case 1:
return DEAD;
case 2:
case 3:
return ALIVE;
default:
return DEAD;
}
}
else if (neighborCount == 3)
{
return ALIVE;
}
return stateOfCell;
}
private void DisplayGrid()
{
Console.SetCursorPosition(LEFT, TOP + 4);
for (int row = 0; row <= grid.GetUpperBound(0); row++)
{
Console.CursorLeft = LEFT;
for (int col = 0; col <= grid.GetUpperBound(1); col++)
{
SetColor(grid[row, col, displayedGrid]);
Console.Write(".");
}
Console.WriteLine();
}
}
private void SetColor(int state)
{
if (state == 1)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
}
else
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Black;
}
}
private int GetNeighborCount(int row, int col)
{
int neighborCount = 0;
int rowStart = row - 1;
int rowStop = row + 1;
int colStart = col - 1;
int colStop = col + 1;
for (int rowIndex = rowStart; rowIndex <= rowStop; rowIndex++)
{
for (int colIndex = colStart; colIndex <= colStop; colIndex++)
{
if ((rowIndex == row && colIndex == col) ||
(OutOfBounds(rowIndex, colIndex)))
{
continue;
}
if (grid[rowIndex, colIndex, displayedGrid] == ALIVE)
{
neighborCount++;
}
}
}
return neighborCount;
}
private Boolean OutOfBounds(int row, int col)
{
return !((row >= 0 && row <= grid.GetUpperBound(0)) &&
(col >= 0 && col <= grid.GetUpperBound(1)));
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A03LeeJoe
{
class Program
{
static void Main(string[] args)
{
GameOfLife game = new GameOfLife();
game.Start();
}
}
}

More Related Content

Similar to GameOfLife.cs using System; using System.Collections.Generic;.pdf

NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docxNewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docxcurwenmichaela
 
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...British Council
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfpoblettesedanoree498
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docxPiersRCoThomsonw
 
import java.awt.;import java.awt.event.;import javax.swing.;.pdf
import java.awt.;import java.awt.event.;import javax.swing.;.pdfimport java.awt.;import java.awt.event.;import javax.swing.;.pdf
import java.awt.;import java.awt.event.;import javax.swing.;.pdfaoneonlinestore1
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfarmcomputers
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfcontact32
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfarchanaemporium
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdffonecomp
 
Please follow the code and comments for description and outputs C.pdf
Please follow the code and comments for description and outputs C.pdfPlease follow the code and comments for description and outputs C.pdf
Please follow the code and comments for description and outputs C.pdfproloyankur01
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdffazilfootsteps
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfcalderoncasto9163
 
import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfADITIEYEWEAR
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical fileMitul Patel
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfasif1401
 

Similar to GameOfLife.cs using System; using System.Collections.Generic;.pdf (16)

NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docxNewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
NewTetrisScore.cppNewTetrisScore.cpp newTetris.cpp  Defines t.docx
 
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
 
import java.awt.;import java.awt.event.;import javax.swing.;.pdf
import java.awt.;import java.awt.event.;import javax.swing.;.pdfimport java.awt.;import java.awt.event.;import javax.swing.;.pdf
import java.awt.;import java.awt.event.;import javax.swing.;.pdf
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
Please follow the code and comments for description and outputs C.pdf
Please follow the code and comments for description and outputs C.pdfPlease follow the code and comments for description and outputs C.pdf
Please follow the code and comments for description and outputs C.pdf
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
implemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdfimplemement the game.cpp by the header file given. And create main.c.pdf
implemement the game.cpp by the header file given. And create main.c.pdf
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdf
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 

More from aravlitraders2012

Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdfaravlitraders2012
 
Electronegativity - Electronegativity is an atom.pdf
                     Electronegativity - Electronegativity is an atom.pdf                     Electronegativity - Electronegativity is an atom.pdf
Electronegativity - Electronegativity is an atom.pdfaravlitraders2012
 
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdfSequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdfaravlitraders2012
 
In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
In the 1st Question, As the polymorphisms occur in the non-coding se.pdfaravlitraders2012
 
I noticed that temperature is not mentioned. A) .pdf
                     I noticed that temperature is not mentioned.  A) .pdf                     I noticed that temperature is not mentioned.  A) .pdf
I noticed that temperature is not mentioned. A) .pdfaravlitraders2012
 
1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdf1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdfaravlitraders2012
 
1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdf1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdfaravlitraders2012
 
argon is inert gas , doesnot chemically reacts wi.pdf
                     argon is inert gas , doesnot chemically reacts wi.pdf                     argon is inert gas , doesnot chemically reacts wi.pdf
argon is inert gas , doesnot chemically reacts wi.pdfaravlitraders2012
 
#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdf#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdfaravlitraders2012
 
Function header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdfFunction header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdfaravlitraders2012
 
With the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdfWith the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdfaravlitraders2012
 
Let n = size of the hash table hashTable = array of size n, w.pdf
Let n = size of the hash table  hashTable = array of size n, w.pdfLet n = size of the hash table  hashTable = array of size n, w.pdf
Let n = size of the hash table hashTable = array of size n, w.pdfaravlitraders2012
 
What is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdfWhat is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdfaravlitraders2012
 
We can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdfWe can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdfaravlitraders2012
 
Wave CharacterMany of the things that light does are only expla.pdf
 Wave CharacterMany of the things that light does are only expla.pdf Wave CharacterMany of the things that light does are only expla.pdf
Wave CharacterMany of the things that light does are only expla.pdfaravlitraders2012
 
The solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdfThe solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdfaravlitraders2012
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdfaravlitraders2012
 
The answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdfThe answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdfaravlitraders2012
 
Solution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdfSolution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdfaravlitraders2012
 
An asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdfAn asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdfaravlitraders2012
 

More from aravlitraders2012 (20)

Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
 
Electronegativity - Electronegativity is an atom.pdf
                     Electronegativity - Electronegativity is an atom.pdf                     Electronegativity - Electronegativity is an atom.pdf
Electronegativity - Electronegativity is an atom.pdf
 
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdfSequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
 
In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
 
I noticed that temperature is not mentioned. A) .pdf
                     I noticed that temperature is not mentioned.  A) .pdf                     I noticed that temperature is not mentioned.  A) .pdf
I noticed that temperature is not mentioned. A) .pdf
 
1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdf1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdf
 
1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdf1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdf
 
argon is inert gas , doesnot chemically reacts wi.pdf
                     argon is inert gas , doesnot chemically reacts wi.pdf                     argon is inert gas , doesnot chemically reacts wi.pdf
argon is inert gas , doesnot chemically reacts wi.pdf
 
#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdf#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdf
 
Function header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdfFunction header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdf
 
With the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdfWith the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdf
 
Let n = size of the hash table hashTable = array of size n, w.pdf
Let n = size of the hash table  hashTable = array of size n, w.pdfLet n = size of the hash table  hashTable = array of size n, w.pdf
Let n = size of the hash table hashTable = array of size n, w.pdf
 
What is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdfWhat is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdf
 
We can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdfWe can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdf
 
Wave CharacterMany of the things that light does are only expla.pdf
 Wave CharacterMany of the things that light does are only expla.pdf Wave CharacterMany of the things that light does are only expla.pdf
Wave CharacterMany of the things that light does are only expla.pdf
 
The solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdfThe solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdf
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 
The answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdfThe answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdf
 
Solution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdfSolution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdf
 
An asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdfAn asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdf
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 

GameOfLife.cs using System; using System.Collections.Generic;.pdf

  • 1. GameOfLife.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace A03LeeJoe { class GameOfLife { private int[, ,] grid; private int displayedGrid = 0; private int tempGrid = 1; private const int DEAD = 0; private const int ALIVE = 1; private const int TOP = 5; private const int LEFT = 20; //constructor public GameOfLife() { grid = CreateGrid(); } //methods private int[, ,] CreateGrid() { int[, ,] newGrid = new int[25, 40, 2]; for (int col = 14; col < 27; col++) { newGrid[13, col, 0] = ALIVE; } for (int row = 8; row < 11; row++) { newGrid[row, 12, 0] = ALIVE; newGrid[row, 20, 0] = ALIVE;
  • 2. newGrid[row, 28, 0] = ALIVE; } return newGrid; } public void Start() { //Display Grid Console.SetWindowSize(80, 40); Setup(); //loop until user presses a key while (!Console.KeyAvailable) { Thread.Sleep(1000); //perform next move NextMove(); DisplayGrid(); SetColor(1); Console.WriteLine(); Console.CursorLeft = LEFT; Console.WriteLine(" Press any key to end ", displayedGrid); } } private void Setup() { CreateHeader(); DisplayGrid(); //prompt user to press enter Console.WriteLine(); Console.CursorLeft = LEFT; SetColor(1); Console.WriteLine(" Please press enter to begin "); Console.ReadLine(); } private void CreateHeader() { Console.SetCursorPosition(LEFT, TOP);
  • 3. SetColor(1); Console.WriteLine("{0,40}", " "); Console.CursorLeft = LEFT; Console.WriteLine(" G A M E O F L I F E "); Console.CursorLeft = LEFT; Console.WriteLine("{0,40}", " "); } private void NextMove() { //iterate through grid for (int row = 0; row <= grid.GetUpperBound(0); row++) { for (int col = 0; col <= grid.GetUpperBound(1); col++) { //each iteration check neighbor count int neighborCount = GetNeighborCount(row, col); //determine new life or death and update on temp grid' grid[row, col, tempGrid] = UpdateCell(grid[row, col, displayedGrid], neighborCount); } } //switch the grid's 3rd dimension to display new grid SwitchGrid(); } private void SwitchGrid() { if (displayedGrid == 0) { displayedGrid = 1; tempGrid = 0; } else { displayedGrid = 0; tempGrid = 1; }
  • 4. } private int UpdateCell(int stateOfCell, int neighborCount) { if (stateOfCell == ALIVE) { switch (neighborCount) { case 0: case 1: return DEAD; case 2: case 3: return ALIVE; default: return DEAD; } } else if (neighborCount == 3) { return ALIVE; } return stateOfCell; } private void DisplayGrid() { Console.SetCursorPosition(LEFT, TOP + 4); for (int row = 0; row <= grid.GetUpperBound(0); row++) { Console.CursorLeft = LEFT; for (int col = 0; col <= grid.GetUpperBound(1); col++) { SetColor(grid[row, col, displayedGrid]); Console.Write("."); } Console.WriteLine(); }
  • 5. } private void SetColor(int state) { if (state == 1) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Yellow; } else { Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.Black; } } private int GetNeighborCount(int row, int col) { int neighborCount = 0; int rowStart = row - 1; int rowStop = row + 1; int colStart = col - 1; int colStop = col + 1; for (int rowIndex = rowStart; rowIndex <= rowStop; rowIndex++) { for (int colIndex = colStart; colIndex <= colStop; colIndex++) { if ((rowIndex == row && colIndex == col) || (OutOfBounds(rowIndex, colIndex))) { continue; } if (grid[rowIndex, colIndex, displayedGrid] == ALIVE) { neighborCount++; } } }
  • 6. return neighborCount; } private Boolean OutOfBounds(int row, int col) { return !((row >= 0 && row <= grid.GetUpperBound(0)) && (col >= 0 && col <= grid.GetUpperBound(1))); } } } Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace A03LeeJoe { class Program { static void Main(string[] args) { GameOfLife game = new GameOfLife(); game.Start(); } } } Solution GameOfLife.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;
  • 7. using System.Threading; namespace A03LeeJoe { class GameOfLife { private int[, ,] grid; private int displayedGrid = 0; private int tempGrid = 1; private const int DEAD = 0; private const int ALIVE = 1; private const int TOP = 5; private const int LEFT = 20; //constructor public GameOfLife() { grid = CreateGrid(); } //methods private int[, ,] CreateGrid() { int[, ,] newGrid = new int[25, 40, 2]; for (int col = 14; col < 27; col++) { newGrid[13, col, 0] = ALIVE; } for (int row = 8; row < 11; row++) { newGrid[row, 12, 0] = ALIVE; newGrid[row, 20, 0] = ALIVE; newGrid[row, 28, 0] = ALIVE; } return newGrid; } public void Start() { //Display Grid
  • 8. Console.SetWindowSize(80, 40); Setup(); //loop until user presses a key while (!Console.KeyAvailable) { Thread.Sleep(1000); //perform next move NextMove(); DisplayGrid(); SetColor(1); Console.WriteLine(); Console.CursorLeft = LEFT; Console.WriteLine(" Press any key to end ", displayedGrid); } } private void Setup() { CreateHeader(); DisplayGrid(); //prompt user to press enter Console.WriteLine(); Console.CursorLeft = LEFT; SetColor(1); Console.WriteLine(" Please press enter to begin "); Console.ReadLine(); } private void CreateHeader() { Console.SetCursorPosition(LEFT, TOP); SetColor(1); Console.WriteLine("{0,40}", " "); Console.CursorLeft = LEFT; Console.WriteLine(" G A M E O F L I F E "); Console.CursorLeft = LEFT; Console.WriteLine("{0,40}", " "); }
  • 9. private void NextMove() { //iterate through grid for (int row = 0; row <= grid.GetUpperBound(0); row++) { for (int col = 0; col <= grid.GetUpperBound(1); col++) { //each iteration check neighbor count int neighborCount = GetNeighborCount(row, col); //determine new life or death and update on temp grid' grid[row, col, tempGrid] = UpdateCell(grid[row, col, displayedGrid], neighborCount); } } //switch the grid's 3rd dimension to display new grid SwitchGrid(); } private void SwitchGrid() { if (displayedGrid == 0) { displayedGrid = 1; tempGrid = 0; } else { displayedGrid = 0; tempGrid = 1; } } private int UpdateCell(int stateOfCell, int neighborCount) { if (stateOfCell == ALIVE) { switch (neighborCount) {
  • 10. case 0: case 1: return DEAD; case 2: case 3: return ALIVE; default: return DEAD; } } else if (neighborCount == 3) { return ALIVE; } return stateOfCell; } private void DisplayGrid() { Console.SetCursorPosition(LEFT, TOP + 4); for (int row = 0; row <= grid.GetUpperBound(0); row++) { Console.CursorLeft = LEFT; for (int col = 0; col <= grid.GetUpperBound(1); col++) { SetColor(grid[row, col, displayedGrid]); Console.Write("."); } Console.WriteLine(); } } private void SetColor(int state) { if (state == 1) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Yellow;
  • 11. } else { Console.BackgroundColor = ConsoleColor.Blue; Console.ForegroundColor = ConsoleColor.Black; } } private int GetNeighborCount(int row, int col) { int neighborCount = 0; int rowStart = row - 1; int rowStop = row + 1; int colStart = col - 1; int colStop = col + 1; for (int rowIndex = rowStart; rowIndex <= rowStop; rowIndex++) { for (int colIndex = colStart; colIndex <= colStop; colIndex++) { if ((rowIndex == row && colIndex == col) || (OutOfBounds(rowIndex, colIndex))) { continue; } if (grid[rowIndex, colIndex, displayedGrid] == ALIVE) { neighborCount++; } } } return neighborCount; } private Boolean OutOfBounds(int row, int col) { return !((row >= 0 && row <= grid.GetUpperBound(0)) && (col >= 0 && col <= grid.GetUpperBound(1))); }
  • 12. } } Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace A03LeeJoe { class Program { static void Main(string[] args) { GameOfLife game = new GameOfLife(); game.Start(); } } }