Final Project: Conway’s Game of Life The final project for ENRG-1800 is to creat
ID: 3576416 • Letter: F
Question
Final Project: Conway’s Game of Life
The final project for ENRG-1800 is to create a working Conway’s Game of Life. This is a player-less game that will be executed by MATLAB. The level of complexity and features that are presented in this document are minimal requirements, but of course, you can add as much complexity as you prefer (some suggestions of additions are included at the bottom). In order to be considered a final project, the game must play and the code (script and or function(s)) must be properly annotated and be self sufficient (all the functions should either be built in functions in MATLAB or be included as part of the submission). Once you consider your project is ready, upload it to Blackboard under the assignment called Final Project.
Game setup:
The game starts with a given configuration consisting on a logical matrix (suggested size is 100 x 100) where the 0 elements are known as “dead cells” and the 1 elements are known as “live cells”. The game should display this initial matrix as a figure.
Playing the game:
The game continues by looping through a routine that calculates the matrix of the new generation according to the rules bellow and then displays the result as a figure. This routine should continue indefinitely or until a set pre-condition is reached (these are decisions for you, the game designer).
Rules of the game:
In calculating the next generation, these rules should be followed:
1) If the cell is alive and in contact with 4 or more other live cells, it will be dead in the next generation, as if by starvation (local resources depletion).
2) If the cell is alive and in contact with less than 2 live cells, it will be dead in the next generation, as if by isolation (lack of enough cooperation to harness the local resources).
3) If a cell is dead and in contact with exactly 3 live cells, it will spawn into life in the next generation, as if by reproduction.
Ideas to Improve your game:
1) Allow the user to choose between different starts.
2) Allow the user to change the size of the playing field.
3) Allow the user to change the speed of the game.
4) Allow the user to change the color scheme of the game.
5) Make the game into a function so that the above values can be passed to the game.
6) Make a GUI for your game that allows in-game changes.
Explanation / Answer
using UnityEngine; using System.Collections; namespace Completed { using System.Collections.Generic; //Allows us to use Lists. using UnityEngine.UI; //Allows us to use UI. public class GameManager : MonoBehaviour { public float levelStartDelay = 2f; //Time to wait before starting level, in seconds. public float turnDelay = 0.1f; //Delay between each Player turn. public int playerFoodPoints = 100; //Starting value for Player food points. public static GameManager instance = null; //Static instance of GameManager which allows it to be accessed by any other script. [HideInInspector] public bool playersTurn = true; //Boolean to check if it's players turn, hidden in inspector but public. private Text levelText; //Text to display current level number. private GameObject levelImage; //Image to block out level as levels are being set up, background for levelText. private BoardManager boardScript; //Store a reference to our BoardManager which will set up the level. private int level = 1; //Current level number, expressed in game as "Day 1". private List enemies; //List of all Enemy units, used to issue them move commands. private bool enemiesMoving; //Boolean to check if enemies are moving. private bool doingSetup = true; //Boolean to check if we're setting up board, prevent Player from moving during setup. //Awake is always called before any Start functions void Awake() { //Check if instance already exists if (instance == null) //if not, set instance to this instance = this; //If instance already exists and it's not this: else if (instance != this) //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager. Destroy(gameObject); //Sets this to not be destroyed when reloading scene DontDestroyOnLoad(gameObject); //Assign enemies to a new List of Enemy objects. enemies = new List(); //Get a component reference to the attached BoardManager script boardScript = GetComponent(); //Call the InitGame function to initialize the first level InitGame(); } //This is called each time a scene is loaded. void OnLevelWasLoaded(int index) { //Add one to our level number. level++; //Call InitGame to initialize our level. InitGame(); } //Initializes the game for each level. void InitGame() { //While doingSetup is true the player can't move, prevent player from moving while title card is up. doingSetup = true; //Get a reference to our image LevelImage by finding it by name. levelImage = GameObject.Find("LevelImage"); //Get a reference to our text LevelText's text component by finding it by name and calling GetComponent. levelText = GameObject.Find("LevelText").GetComponent(); //Set the text of levelText to the string "Day" and append the current level number. levelText.text = "Day " + level; //Set levelImage to active blocking player's view of the game board during setup. levelImage.SetActive(true); //Call the HideLevelImage function with a delay in seconds of levelStartDelay. Invoke("HideLevelImage", levelStartDelay); //Clear any Enemy objects in our List to prepare for next level. enemies.Clear(); //Call the SetupScene function of the BoardManager script, pass it current level number. boardScript.SetupScene(level); } //Hides black image used between levels void HideLevelImage() { //Disable the levelImage gameObject. levelImage.SetActive(false); //Set doingSetup to false allowing player to move again. doingSetup = false; } //Update is called every frame. void Update() { //Check that playersTurn or enemiesMoving or doingSetup are not currently true. if(playersTurn || enemiesMoving || doingSetup) //If any of these are true, return and do not start MoveEnemies. return; //Start moving enemies. StartCoroutine (MoveEnemies ()); } //Call this to add the passed in Enemy to the List of Enemy objects. public void AddEnemyToList(Enemy script) { //Add Enemy to List enemies. enemies.Add(script); } //GameOver is called when the player reaches 0 food points public void GameOver() { //Set levelText to display number of levels passed and game over message levelText.text = "After " + level + " days, you starved."; //Enable black background image gameObject. levelImage.SetActive(true); //Disable this GameManager. enabled = false; } //Coroutine to move enemies in sequence. IEnumerator MoveEnemies() { //While enemiesMoving is true player is unable to move. enemiesMoving = true; //Wait for turnDelay seconds, defaults to .1 (100 ms). yield return new WaitForSeconds(turnDelay); //If there are no enemies spawned (IE in first level): if (enemies.Count == 0) { //Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none. yield return new WaitForSeconds(turnDelay); } //Loop through List of Enemy objects. for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.