• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
8%
SHARE
TWEET

# game of life

a guest Jan 7th, 2012 21 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. Matrix class
2.
3. package gameoflife;
4. public class Matrix {
5.     Boolean[][] matrix;
6.     int numRows;
7.     int numCols;
8.
9.    public Matrix(int rows, int cols) {
10.         matrix = new Boolean[rows][cols];
11.
12.         numRows = rows;
13.         numCols = cols;
14.
15.         for(int i = 0; i < cols; i++){
16.             for(int j = 0; j < rows; j++){
17.                 matrix[j][i] = false;
18.             }
19.         }
20.     }
21.     //constructor with all values false
22.
23.     public Matrix(int size) {
24.         matrix = new Boolean[size][size];
25.
26.         numRows = size;
27.         numCols = size;
28.
29.         for(int i = 0; i < size; i++){
30.             for(int j = 0; j < size; j++){
31.                 matrix[j][i] = false;
32.             }
33.         }
34.     }
35.     //constructor
36.
37.     void set(int x, int y, Boolean state){
38.         matrix[x][y] = state;
39.     }
40.     //sets a value within the matrix
41.
42.     Boolean state(int x, int y){
43.         return matrix[x][y];
44.     }
45.     //gets a value within the matrix
46.
47.     int rows(){
48.         return numRows;
49.     }
50.     //gets the number of rows in the matrix
51.
52.     int cols(){
53.         return numCols;
54.     }
55.     //gets the number of columns in the matrix
56. }
57.
58. GameGrid class
59.
60. package gameoflife;
61. public class GameGrid extends Matrix{
62.
63.     public GameGrid(int rows, int cols){
64.         //gameGrid = new Boolean[rows][cols];
65.         super(rows+2, cols+2);
66.     }
67.     //creates a gamegrid with set rows and cols
68.
69.     public GameGrid(int size){
70.         //gameGrid = new Boolean[size][size];
71.         super(size+2) /*me*/;
72.     }
73.     //creates a new gamegrid which is size x size
74.
75.     Boolean isAlive(int x, int y){
76.         return matrix[x][y];
77.     }
78.     //returns the state of a coordinate
79.
80.     short numNeighbors(int x, int y) {
81.         short numNeighbors;
82.         numNeighbors = 0;
83.
84.         if (matrix[x+1][y] == true) numNeighbors++;
85.         if (matrix[x][y+1] == true) numNeighbors++;
86.         if (matrix[x+1][y+1] == true) numNeighbors++;
87.         if (matrix[x][y-1] == true) numNeighbors++;
88.         if (matrix[x-1][y] == true) numNeighbors++;
89.         if (matrix[x+1][y-1] == true) numNeighbors++;
90.         if (matrix[x-1][y+1] == true) numNeighbors++;
91.         if (matrix[x-1][y-1] == true) numNeighbors++;
92.
93.         return numNeighbors;
94.     }
95.     //returns the number of neighbours that a coordinate has
96.
97.     void deactivate(int x, int y){
98.         matrix[x][y] = false;
99.     }
100.     //turns a coord to false
101.
102.     void activate(int x, int y) {
103.         matrix[x][y] = true;
104.     }
105.     //turns a coord to true
106.
107.     void nextGen(){
108.         Boolean[][] newMatrix = new Boolean[rows()][cols()];
109.
110.         for (int i = 1; i < cols()-1; i++){
111.             for (int j = 1; j < rows()-1; j++){
112.                 //if a cell has 3 neighbours, become or stay true
113.                 if (numNeighbors(j, i) == 3) newMatrix[j][i] = true;
114.                 //if it doesn't have 3 neighbours, become or stay false
115.                 else newMatrix[j][i] = false;
116.             }
117.         }
118.
119.         matrix = newMatrix;
120.     }
121.     //makes matrix represent the next generation
122.
123.     String showGrid(){
124.         String arrayOutput = "";
125.        //writes each row as a line, then goes to the next and does the same.
126.         for(int k = 1; k < cols()-1; k++) {
127.             //opens a square bracket on every row of the array
128.             arrayOutput += "[ ";
129.             //lists the row in the square brackets with the correct state in each slot
130.             for(int l = 1; l < rows()-1; l++){
131.                 if (state(l, k))
132.                     {arrayOutput += " * ";}
133.                 else
134.                     {arrayOutput += " . ";}
135.             }
136.             //closes the square brackets and then starts a new line for the next row
137.             arrayOutput += "]\n";
138.         }
139.         return arrayOutput;
140.     }
141.     //outputs the gamegrid
142.
143. }
144.
145. GameOfLife class
146.
147. package gameoflife;
148. import javax.swing.JOptionPane;
149.
150. public class GameOfLife {
151.
152.     public static void main(String args[]) {
153.         int width = Integer.parseInt(JOptionPane.showInputDialog("How many spaces wide do you want your grid to be?"));
154.         int height = Integer.parseInt(JOptionPane.showInputDialog("How many spaces high do you want your grid to be?"));
155.         GameGrid myGrid = new GameGrid(width, height);
156.
157.         int numOrganisms = Integer.parseInt(JOptionPane.showInputDialog("How many organisms do you want to plot?"));
158.
159.         int xcoord;
160.         int ycoord;
161.
162.         for (int i = 0; i < numOrganisms; i++){
163.             xcoord = (1+Integer.parseInt(JOptionPane.showInputDialog("What is the x coordinate of organism #"+(i+1)+"?")));
164.             ycoord = (1+Integer.parseInt(JOptionPane.showInputDialog("What is the y coordinate of organism #"+(i+1)+"?")));
165.             myGrid.activate(xcoord, ycoord);
166.         }
167.
168.         int numGenerations = Integer.parseInt(JOptionPane.showInputDialog("How many generations do you want to be shown?"));
169.
170.         for (int j = 0; j < numGenerations; j++){
171.             JOptionPane.showMessageDialog(null,"generation " + (j+1) + ":\n\n" + myGrid.showGrid());
172.             myGrid.nextGen();
173.         }
174.
175.     }
176. }
RAW Paste Data
Top