Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.78 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6.  
  7. public class krehal2lab13
  8. {
  9. public static void main(String args[])
  10. {
  11. int xsize, ysize = -1;
  12. int xpos, ypos = -1;
  13. File f = new File (args[0]);
  14. Scanner sc = null;
  15.  
  16. try
  17. {
  18. sc = new Scanner (f);
  19. }
  20. catch (FileNotFoundException fnfe)
  21. {
  22. System.out.println ("File did not exist");
  23. return;
  24. }
  25.  
  26. xsize = sc.nextInt();
  27. ysize = sc.nextInt();
  28.  
  29. // create the grid of 25 rows and 35 columns
  30. GridDisplay disp = new GridDisplay(xsize, ysize);
  31.  
  32. int[][] currentGen = new int[xsize][ysize];
  33. int[][] nextGen = new int[xsize][ysize];
  34.  
  35. for(int i = 0; i < xsize; i++)
  36. {
  37. for(int j = 0; j < ysize; j++)
  38. {
  39. currentGen[i][j] = 0;
  40. nextGen[i][j] = 0;
  41. disp.setColor(i, j, Color.WHITE);
  42. }
  43. }
  44.  
  45. // loop until all integers are read from the file
  46. while (sc.hasNextInt())
  47. {
  48. xpos = sc.nextInt();
  49. ypos = sc.nextInt();
  50.  
  51. currentGen[xpos][ypos] = 1;
  52. }
  53.  
  54. for(int i = 0; i < xsize; i++)
  55. {
  56. for(int j = 0; j < ysize; j++)
  57. {
  58. if(currentGen[i][j] == 0)
  59. disp.setColor(i, j, Color.WHITE);
  60. else if (currentGen[i][j] == 1)
  61. disp.setColor(i, j, Color.BLACK);
  62. }
  63. }
  64.  
  65. while(true)
  66. {
  67. for(int i = 0; i < xsize; i++)
  68. {
  69. for(int j = 0; j < ysize; j++)
  70. {
  71. if(nextGen[i][j] == 0)
  72. disp.setColor(i, j, Color.WHITE);
  73. else if (nextGen[i][j] == 1)
  74. disp.setColor(i, j, Color.BLACK);
  75. }
  76. }
  77. setNextGen(currentGen, nextGen, xsize, ysize);
  78. mySleep(500);
  79. System.out.println("hi");
  80. }
  81. }
  82.  
  83. // puts the current thread to sleep for some number of milliseconds to allow for "animation"
  84. public static void mySleep( int milliseconds)
  85. {
  86. try
  87. {
  88. Thread.sleep(milliseconds);
  89. }
  90. catch (InterruptedException ie)
  91. {
  92. }
  93. }
  94.  
  95. public static void setNextGen(int[][] currentGen, int[][] nextGen, int xsize, int ysize)
  96. {
  97. for(int i = 0; i < xsize; i++)
  98. {
  99. for(int j = 0; j < ysize; j++)
  100. {
  101. int aliveNeighbors = 0;
  102. /*
  103. 1. Any live cell with less than two live neighbors dies, as if caused by under-population.
  104. 2. Any live cell with two or three live neighbors lives on to the next generation.
  105. 3. Any live cell with more than three live neighbors dies, as if by overcrowding.
  106. 4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
  107. */
  108.  
  109. //if it is a corner cell
  110. //top left corner
  111. if(i == 0 && j == 0)
  112. {
  113. if(currentGen[i][j+1] == 1)
  114. aliveNeighbors++;
  115. if(currentGen[i+1][j] == 1)
  116. aliveNeighbors++;
  117. if(currentGen[i+1][j+1] == 1)
  118. aliveNeighbors++;
  119. }
  120. //top right corner
  121. else if(i == 0 && j == ysize - 1)
  122. {
  123. if(currentGen[i][j-1] == 1)
  124. aliveNeighbors++;
  125. if(currentGen[i+1][j] == 1)
  126. aliveNeighbors++;
  127. if(currentGen[i+1][j-1] == 1)
  128. aliveNeighbors++;
  129. }
  130. //bottom left corner
  131. else if(i == xsize - 1 && j == 0)
  132. {
  133. if(currentGen[i-1][j] == 1)
  134. aliveNeighbors++;
  135. if(currentGen[i-1][j+1] == 1)
  136. aliveNeighbors++;
  137. if(currentGen[i][j+1] == 1)
  138. aliveNeighbors++;
  139. }
  140. //bottom right corner
  141. else if(i == xsize - 1 && j == ysize - 1)
  142. {
  143. if(currentGen[i][j-1] == 1)
  144. aliveNeighbors++;
  145. if(currentGen[i-1][j-1] ==1)
  146. aliveNeighbors++;
  147. if(currentGen[i-1][j] == 1)
  148. aliveNeighbors++;
  149. }
  150. //else if it is a top row cell
  151. else if(i == 0)
  152. {
  153. if(currentGen[i][j-1] == 1)
  154. aliveNeighbors++;
  155. if(currentGen[i+1][j-1] == 1)
  156. aliveNeighbors++;
  157. if(currentGen[i+1][j] == 1)
  158. aliveNeighbors++;
  159. if(currentGen[i+1][j+1] == 1)
  160. aliveNeighbors++;
  161. if(currentGen[i][j+1] == 1)
  162. aliveNeighbors++;
  163. }
  164. //else if it is a bottom row cell
  165. else if(i == xsize - 1)
  166. {
  167. if(currentGen[i][j-1] == 1)
  168. aliveNeighbors++;
  169. if(currentGen[i-1][j-1] == 1)
  170. aliveNeighbors++;
  171. if(currentGen[i-1][j] == 1)
  172. aliveNeighbors++;
  173. if(currentGen[i-1][j+1] == 1)
  174. aliveNeighbors++;
  175. if(currentGen[i][j+1] == 1)
  176. aliveNeighbors++;
  177. }
  178. //else if it is a left column cell
  179. else if(j == 0)
  180. {
  181. if(currentGen[i-1][j] == 1)
  182. aliveNeighbors++;
  183. if(currentGen[i-1][j+1] == 1)
  184. aliveNeighbors++;
  185. if(currentGen[i][j+1] == 1)
  186. aliveNeighbors++;
  187. if(currentGen[i+1][j+1] == 1)
  188. aliveNeighbors++;
  189. if(currentGen[i+1][j] == 1)
  190. aliveNeighbors++;
  191. }
  192. //else if it is a right column cell
  193. else if(j == ysize - 1)
  194. {
  195. if(currentGen[i-1][j] == 1)
  196. aliveNeighbors++;
  197. if(currentGen[i-1][j-1] == 1)
  198. aliveNeighbors++;
  199. if(currentGen[i][j-1] == 1)
  200. aliveNeighbors++;
  201. if(currentGen[i+1][j-1] == 1)
  202. aliveNeighbors++;
  203. if(currentGen[i+1][j] == 1)
  204. aliveNeighbors++;
  205. }
  206.  
  207. //else it is a center cell
  208. else
  209. {
  210. if(currentGen[i-1][j-1] == 1)
  211. aliveNeighbors++;
  212. if(currentGen[i-1][j] == 1)
  213. aliveNeighbors++;
  214. if(currentGen[i-1][j+1] == 1)
  215. aliveNeighbors++;
  216. if(currentGen[i][j-1] == 1)
  217. aliveNeighbors++;
  218. if(currentGen[i][j+1] == 1)
  219. aliveNeighbors++;
  220. if(currentGen[i+1][j-1] == 1)
  221. aliveNeighbors++;
  222. if(currentGen[i+1][j] == 1)
  223. aliveNeighbors++;
  224. if(currentGen[i+1][j+1] == 1)
  225. aliveNeighbors++;
  226. }
  227.  
  228. //If the cell is alive
  229. if(currentGen[i][j] == 1)
  230. {
  231. if(aliveNeighbors < 2)
  232. nextGen[i][j] = 0;
  233. else if(aliveNeighbors == 2 || aliveNeighbors == 3)
  234. nextGen[i][j] = 1;
  235. else if(aliveNeighbors > 3)
  236. nextGen[i][j] = 0;
  237. }
  238.  
  239. //If the cell is dead
  240. else
  241. {
  242. if(aliveNeighbors == 3)
  243. nextGen[i][j] = 1;
  244. }
  245. }
  246. }
  247. //copy everything from nextGen to currentGen, since we are printing from currentGen
  248. for(int x = 0; x < xsize; x++)
  249. {
  250. for(int y = 0; y < ysize; y++)
  251. {
  252. currentGen[x][y] = nextGen[x][y];
  253. }
  254. }
  255. //return nextGen;
  256. }
  257. }
  258.  
  259. class GridDisplay extends JFrame
  260. {
  261. private JLabel labels[];
  262.  
  263. private Container container;
  264. private GridLayout grid1;
  265. int rowCount;
  266. int colCount;
  267.  
  268. // set up GUI
  269. public GridDisplay(int rows, int cols)
  270. {
  271. super( "GridDisplay for CS211" );
  272. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  273.  
  274. // set up grid layout struture of the display
  275. rowCount = rows;
  276. colCount = cols;
  277. grid1 = new GridLayout( rows, cols );
  278. container = getContentPane();
  279. container.setLayout( grid1 );
  280.  
  281. // create and add buttons
  282. labels = new JLabel[ rows * cols ];
  283.  
  284. for ( int count = 0; count < labels.length; count++ ) {
  285. labels[ count ] = new JLabel( " " );
  286. labels[count].setOpaque(true);
  287. container.add( labels[ count ] );
  288. }
  289.  
  290. // set up the size of the window and show it
  291. setSize( cols * 15 , rows * 15 );
  292. setVisible( true );
  293.  
  294. } // end constructor GridLayoutDemo
  295.  
  296. // display the given char in the (row,col) position of the display
  297. public void setChar (int row, int col, char c)
  298. {
  299. if ((row >= 0 && row < rowCount) && (col >= 0 && col < colCount) )
  300. {
  301. int pos = row * colCount + col;
  302. labels [pos].setText("" + c);
  303. }
  304. }
  305.  
  306. // display the given color in the (row,col) position of the display
  307. public void setColor (int row, int col, Color c)
  308. {
  309. if ((row >= 0 && row < rowCount) && (col >= 0 && col < colCount) )
  310. {
  311. int pos = row * colCount + col;
  312. labels [pos].setBackground(c);
  313. }
  314. }
  315. } // end class GridDisplay
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement