/* * LifeGrid.java * * Version: * $Id$ * * Revisions: * $Log$ */ import java.uti
ID: 3549472 • Letter: #
Question
/*
* LifeGrid.java
*
* Version:
* $Id$
*
* Revisions:
* $Log$
*/
import java.util.Random;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* A grid for the game of life. The grid can be written to and read
* from a file.
*
* @author Scott Liu (sxl0436@cs.rit.edu)
* @author Paul Tymann (ptt@cs.rit.edu)
*/
public class LifeGrid {
private int numRows; // Number of rows in the grid
private int numCols; // Number of columns in the grid
private int generation; // Number of the current generation
private boolean cells[][]; // Where the cells live
private Random rng; // Source of randomness
/**
* Construct a GameOfLife object with a specified number of
* rows and columns.
*
* @param row the number of rows in the grid.
* @param cols the number of columns in the grid.
*/
public LifeGrid( int rows, int cols ) {
numRows = rows + 2;
numCols = cols + 2;
rng = new Random();
reset();
}
/**
* Construct a LifeGrid based on the contents of a file.
*
* @param fileName the name of the file.
*/
public LifeGrid( String fileName ) throws IOException {
readGrid( fileName );
}
/**
* Set the cell to the specified value. If the row or col values are out
* of bounds, the call is ignored.
*
* @param row the row of the cell to set.
* @param col the column of the row to set.
* @param value the value to set the cell to.
*/
public void setCell( int row, int col, boolean value ) {
// ADD YOUR CODE HERE
}
/**
* Get the cell value. The return value is false if the
* row or column are out of range.
*
* @return The value of the cell if the row and column are
* valid and false otherwise.
*/
public boolean isAlive( int row, int col ) {
boolean retValue = false;
// ADD YOUR CODE HERE
return retValue;
}
/**
* Determine the number of living neighbors of the cell at the
* given location.
*
* @param row the row of the cell to check.
* @param col the column of the cell to check.
*
* @return the number of living neighbors of the given cell.
*/
public int livingNeighbors( int row, int col ) {
int neighbors = 0;
// ADD YOUR CODE HERE
return neighbors;
}
/**
* Calculate the next generation of the grid. Each cell in the grid
* is examined. A living cell stays alive if it is surrounded by
* either 2 or 3 other living cells. A dead cell becomes a live
* cell in the next generation if it is surrounded by exactly 3 living
* cells. Otherwise the cell dies or stays dead.
*/
public void nextGeneration() {
// Array used to hold the next generation
boolean next[][] = new boolean[ numRows ][ numCols ];
// All of the cells are supposed to change at the same
// time and we can only examine the cells one at a time.
// In order to do this, we look at the environment in the
// array 'cell' and place the results into the array 'next'.
// After we have completed the next generation, we set the
// 'cells' array to the 'next' array.
// ADD YOUR CODE HERE
// Do not change or remove any of the lines in this method
// Update the number of generations
generation = generation + 1;
// Make the next array the one used for the simulation
cells = next;
}
/**
* Returns the number of generations that have past
*
* @return the number of generations that have past
*/
public int getGeneration() {
return generation;
}
/**
* Return the number of rows in the grid.
*
* @return the number of rows in the grid.
*/
public int getNumRows() {
return numRows - 2;
}
/**
* Return the number of columns in the grid.
*
* @return the number of columns in the grid.
*/
public int getNumCols() {
return numCols -2;
}
/**
* Resets the cells back to their original state and set the
* generation to 0.
*/
public void reset() {
cells = new boolean[ numRows ][ numCols ];
generation = 0;
}
/**
* Fill the grid with random values. The threshold determines
* the approximate percentage of cells that will be living. For
* example a threshold of 30 will set approximately 30% of the
* cells to living.
*
* @param threshold the approximate percentange of cells that will
* set to living.
*/
public void randomFill( int threshold ) {
// Step through each of the cells in the grid
for ( int i = 1; i < numRows - 1; i = i++ ) {
for ( int j = 1; j < numCols - 1; j++ ) {
// Get a random number between 0 and 99. If less than
// the threshold set the cells to living.
cells[ i ][ j ] = ( rng.nextInt( 100 ) < threshold );
}
}
}
/**
* Read the cells from a file.
*
* @param fileName the name of the file to read.
*
* @throws IOException if an error occurs while reading the file.
*/
public void readGrid( String fileName ) throws IOException {
ObjectInputStream in =
new ObjectInputStream( new FileInputStream( fileName ) );
numRows = in.readInt();
numCols = in.readInt();
generation = in.readInt();
try {
cells = (boolean[][])in.readObject();
}
catch ( ClassNotFoundException e ) {
throw new IOException( e.getMessage() );
}
}
/**
* Output the cells to a file
*
* @param fileName the name of the file to output the results to
*
* @throws IOException if an error occurs while writing the file.
*/
public void writeGrid( String fileName ) throws IOException {
ObjectOutputStream out =
new ObjectOutputStream( new FileOutputStream( fileName ) );
out.writeInt( numRows - 2 );
out.writeInt( numCols -2);
out.writeInt( generation );
out.writeObject( cells );
}
/**
* Return a string representation of the grid.
*
* @return a string representation of this grid.
*/
public String toString() {
StringBuffer retVal = new StringBuffer();
// Step through the cells
for ( int i = 0; i < numRows; i = i + 1 ) {
for ( int j = 0; j < numCols; j = j + 1 ) {
// If the cell is alive write a '*' otherwise
// write a ' '
if ( cells[ i ][ j ] ) {
retVal.append( "*" );
}
else {
retVal.append( " " );
}
}
// Put newlines at the end of each row
retVal.append( " " );
}
return retVal.toString();
}
} // LifeGrid
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector> // For this program I am using a vector
#include <algorithm>
using namespace std;
//--------------------------------------------------------------------------
// Constant global unchangeable variables...
const int MaxEmployees = 5;
//--------------------------------------------------------------------------
// class (Employee) declaration...
class Employee
{
public:
int ID; // public access data member...
// Automatic inline functions, less typing
void SetAge(int ItsAge){Age = ItsAge;}
int GetAge() const {return Age;}
void SetName(string ItsName) {Name = ItsName;}
string GetName() const{return Name;}
void SetAddress(string ItsAddress) {Address = ItsAddress;}
string GetAddress() const {return Address;}
// Outline function. This function will be passed as an argument to the
// for_each() to calculate the sum. I calculated the average externally
// to the classes; I only used the average age in the main function and
// for display purposes only.
friend bool compare_id(const Employee &emp1, const Employee &emp2);
private:
int Age;
string Name;
string Address;
};
bool compare_id(const Employee &emp1, const Employee &emp2)
{
if(emp1.ID < emp2.ID)
return true;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.