Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.48 KB | None | 0 0
  1. public class Main {
  2.  
  3.  
  4. public static void main(String[] args) {
  5.  
  6. new Settingsboard();
  7.  
  8. }
  9.  
  10. }
  11.  
  12. public class Settingsboard extends JFrame{
  13.  
  14. JFrame Frame2 = new JFrame();
  15. JLabel label1, label2, value1, value2;
  16. JTextField x_Value, y_Value;
  17. JButton button1, button2, button3, clear;
  18. static JCheckBox random;
  19. int run = 0;
  20. static JSlider howManyTimes;
  21. int delay = 1000; // 1000 ms == 1 second
  22. javax.swing.Timer myTimer = new javax.swing.Timer(delay,
  23. new MyTimerActionListener());
  24.  
  25. public Settingsboard(){
  26.  
  27. Frame2.setSize(400,400);
  28. JPanel thePanel = new JPanel();
  29. //JPanel thePanel2 = new JPanel();
  30.  
  31. label1 = new JLabel("Set Size Of Board: ");
  32. thePanel.add(label1);
  33.  
  34. value1 = new JLabel(" Width: ");
  35. thePanel.add(value1);
  36. x_Value = new JTextField("", 5);
  37. thePanel.add(x_Value);
  38.  
  39. value2 = new JLabel(" Height: ");
  40. thePanel.add(value2);
  41. y_Value = new JTextField("", 5);
  42. thePanel.add(y_Value);
  43.  
  44. button1 = new JButton("Generate Board");
  45. button1.addActionListener(
  46. new ActionListener() {
  47. public void actionPerformed(ActionEvent e) {
  48. if(e.getSource() == button1){
  49. if(run == 0){
  50. int number1 = Integer.parseInt(x_Value.getText());
  51. int number2 = Integer.parseInt(y_Value.getText());
  52.  
  53. new Board(number1,number2);
  54. run = 1;
  55. }
  56. else{JOptionPane.showMessageDialog(null, "Board is already running", "InfoBox: ", JOptionPane.INFORMATION_MESSAGE);}
  57. }
  58. }
  59. }
  60. );
  61. thePanel.add(button1);
  62.  
  63.  
  64.  
  65. button2 = new JButton("Play");
  66. button2.addActionListener(
  67. new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69. if(e.getSource() == button2){
  70.  
  71. myTimer.start();
  72.  
  73. }
  74. }
  75. }
  76. );
  77. thePanel.add(button2);
  78.  
  79.  
  80.  
  81. button3 = new JButton("Stop");
  82. button3.addActionListener(
  83. new ActionListener() {
  84. public void actionPerformed(ActionEvent e) {
  85. if(e.getSource() == button3){
  86. myTimer.stop();
  87.  
  88. }
  89. }
  90. }
  91. );
  92. thePanel.add(button3);
  93. random = new JCheckBox("Randomize?");
  94. thePanel.add(random);
  95.  
  96. label2 = new JLabel(" Speed of loop (in milliseconds): ");
  97. thePanel.add(label2);
  98.  
  99. howManyTimes = new JSlider(0, 1000, 1000);
  100. howManyTimes.setMinorTickSpacing(50);
  101. howManyTimes.setMajorTickSpacing(250);
  102. howManyTimes.setPaintTicks(true);
  103. howManyTimes.setPaintLabels(true);
  104. ListenForSlider lForSlider = new ListenForSlider();
  105. howManyTimes.addChangeListener(lForSlider);
  106. thePanel.add(howManyTimes);
  107.  
  108.  
  109. clear = new JButton("Reset board");
  110. clear.addActionListener(
  111. new ActionListener() {
  112. public void actionPerformed(ActionEvent e) {
  113. if(e.getSource() == clear){
  114.  
  115.  
  116.  
  117.  
  118. for(int y=0; y<Board.lengthT; y++){
  119. for(int x=0; x<Board.widthT; x++){
  120.  
  121. if(Settingsboard.random.isSelected()) {
  122. int random = (int )(Math.random() * 2);
  123. if(random == 0){
  124. Board.grid[x][y].setBackground(Color.red);
  125. Board.boolBoard[x][y] = false;
  126. }
  127. else if(random == 1){
  128. Board.grid[x][y].setBackground(Color.blue);
  129. Board.boolBoard[x][y] = true;
  130. }
  131. }
  132. else{
  133. Board.grid[x][y].setBackground(Color.red);
  134. Board.boolBoard[x][y] = false;
  135. }
  136.  
  137. }
  138. }
  139.  
  140.  
  141.  
  142.  
  143. }
  144. }
  145. }
  146. );
  147. thePanel.add(clear);
  148.  
  149.  
  150.  
  151. Frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  152. Frame2.add(thePanel);
  153. Frame2.setVisible(true);
  154. }
  155.  
  156.  
  157. private class ListenForSlider implements ChangeListener{
  158.  
  159. @Override
  160. public void stateChanged(ChangeEvent e) {
  161.  
  162. // Check if the source of the event was the button
  163.  
  164. if(e.getSource() == howManyTimes){
  165.  
  166. delay = howManyTimes.getValue();
  167. String speed = Integer.toString(delay);
  168. System.out.print(speed);
  169. if(delay == 0){
  170. System.out.print("hit");
  171. speed = "As fast as it will go!";
  172. }
  173. label2.setText(" Speed of loop(in milliseconds): " + speed );
  174. myTimer.setDelay(delay);
  175. }
  176.  
  177. }
  178.  
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. }
  187.  
  188. public class Board extends JFrame{
  189.  
  190.  
  191. JFrame Frame = new JFrame(); //creates frame
  192.  
  193. static JButton[][] grid; //names the grid of buttons
  194. JTextArea textArea1;
  195. static int lengthT = 0;;
  196. static int widthT = 0;
  197. static Boolean[][] boolBoard;
  198.  
  199. List<String> onXY_Values = new ArrayList<String>();
  200.  
  201. public Board(int width, int length){ //constructor
  202.  
  203. boolBoard = new Boolean [width][length];
  204.  
  205. Frame.setSize(1000,1000);
  206.  
  207. Frame.setLocationRelativeTo(null);
  208.  
  209. Frame.setTitle("The Game of Life");
  210. ListenForKeys lForKeys = new ListenForKeys();
  211. widthT = width;
  212. lengthT = length;
  213. Frame.setLayout(new GridLayout(width,length)); //set layout
  214. grid = new JButton[width][length]; //allocate the size of grid
  215. for(int y=0; y<length; y++){
  216. for(int x=0; x<width; x++){
  217. grid[x][y]=new JButton("("+x+","+y+")"); //creates new button
  218. Frame.add(grid[x][y]); //adds button to grid
  219. grid[x][y].setBorderPainted(false);
  220. grid[x][y].setContentAreaFilled(false);
  221.  
  222.  
  223.  
  224. if(Settingsboard.random.isSelected()) {
  225. int random = (int )(Math.random() * 2);
  226. if(random == 0){
  227. grid[x][y].setBackground(Color.red);
  228. boolBoard[x][y] = false;
  229. }
  230. else if(random == 1){
  231. grid[x][y].setBackground(Color.blue);
  232. boolBoard[x][y] = true;
  233. }
  234. }
  235. else{
  236. grid[x][y].setBackground(Color.red);
  237. boolBoard[x][y] = false;
  238. }
  239.  
  240.  
  241.  
  242.  
  243. grid[x][y].setForeground(Color.black);
  244. grid[x][y].setOpaque(true);
  245. grid[x][y].setPreferredSize(new Dimension(30, 30));
  246. grid[x][y].setBorder(BorderFactory.createLineBorder(Color.black, 2));
  247. grid[x][y].setMargin(new Insets(0, 0, 0, 0));
  248. ListenForButton lForButton = new ListenForButton();
  249. grid[x][y].addActionListener(lForButton);
  250. grid[x][y].addKeyListener(lForKeys);
  251.  
  252. }
  253. }
  254. Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  255. Frame.pack(); //sets appropriate size for frame
  256. Frame.setVisible(true); //makes frame visible
  257.  
  258. }
  259.  
  260. public static void main(String[] args) {
  261.  
  262.  
  263. }
  264.  
  265.  
  266.  
  267.  
  268. private class ListenForButton implements ActionListener{
  269.  
  270. public void actionPerformed(ActionEvent e){
  271.  
  272. String xString = e.getActionCommand();
  273. String yString = e.getActionCommand();
  274.  
  275. int index = xString.indexOf(',');
  276. xString = xString.substring(0, index);
  277. String yStringRemove = xString;
  278. xString = xString.replace("(", "");
  279.  
  280. int x = Integer.parseInt(xString);
  281.  
  282. yString = yString.replace(yStringRemove, "");
  283. yString = yString.replace(",", "");
  284. yString = yString.replace(")", "");
  285.  
  286. int y = Integer.parseInt(yString);
  287.  
  288. ((JButton)e.getSource()).setEnabled(true);
  289.  
  290. if(e.getSource() == grid[x][y]){
  291.  
  292. grid[x][y].getActionCommand();
  293. if(!boolBoard[x][y]){//turning on
  294. grid[x][y].setBackground(Color.blue);
  295. onXY_Values.add(x + "," + y);
  296. boolBoard[x][y] = true;
  297. }
  298.  
  299. else if(boolBoard[x][y]){//turning off
  300. grid[x][y].setBackground(Color.red);
  301. onXY_Values.remove(x + "," + y);
  302. boolBoard[x][y] = false;
  303. }
  304.  
  305. }
  306.  
  307. }
  308. }
  309.  
  310.  
  311. private class ListenForKeys implements KeyListener{
  312.  
  313. @Override
  314. public void keyPressed(KeyEvent e) {
  315. // TODO Auto-generated method stub
  316. }
  317.  
  318. @Override
  319. public void keyReleased(KeyEvent e) {
  320. // TODO Auto-generated method stub
  321. if( e.getKeyChar() == 10 ){
  322. Logic.logic(lengthT,widthT,grid,boolBoard);
  323. }
  324.  
  325. }
  326.  
  327. @Override
  328. public void keyTyped(KeyEvent e) {
  329. // TODO Auto-generated method stub
  330. }
  331. }
  332. }
  333.  
  334. public class MyTimerActionListener implements ActionListener {
  335.  
  336. @Override
  337.  
  338.  
  339. public void actionPerformed(ActionEvent e) {
  340. int widthT = Board.widthT;
  341. int lengthT = Board.lengthT;
  342. JButton[][] grid = Board.grid;
  343. Boolean [][] boolBoard = Board.boolBoard;
  344. Logic.logic(lengthT, widthT, grid, boolBoard);
  345. }
  346.  
  347. }
  348.  
  349. public class Logic {
  350.  
  351.  
  352.  
  353.  
  354.  
  355. public static void logic(int lengthT, int widthT, JButton[][] grid, Boolean[][] boolBoard) {
  356.  
  357. List<Integer> turnOnX = new ArrayList<Integer>();
  358. List<Integer> turnOnY = new ArrayList<Integer>();
  359. List<Integer> turnOffX = new ArrayList<Integer>();
  360. List<Integer> turnOffY = new ArrayList<Integer>();
  361. int fullspace = 0;
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368. for(int w = 0; w < 1; w++){//right bottom piece
  369. int j = lengthT - 1;
  370. int i = widthT - 1;
  371. if(!boolBoard[i][j]){//off
  372. fullspace = 0;
  373. if(boolBoard[i- 1][j-1]){
  374. fullspace++;
  375. }
  376.  
  377. if(boolBoard[i- 1][j]){
  378. fullspace++;
  379. }
  380.  
  381. if(boolBoard[i][j-1]){
  382. fullspace++;
  383. }
  384.  
  385. if(boolBoard[i-widthT+1][j-lengthT+1]){
  386. fullspace++;
  387. }
  388.  
  389. if(boolBoard[i-widthT+1][j]){
  390. fullspace++;
  391. }
  392.  
  393. if(boolBoard[i][j-lengthT+1]){
  394. fullspace++;
  395. }
  396.  
  397. if(boolBoard[i-widthT+1][j-1]){
  398. fullspace++;
  399. }
  400.  
  401. if(boolBoard[i-1][j-lengthT+1]){
  402. fullspace++;
  403. }
  404.  
  405.  
  406. if(fullspace == 3){
  407. turnOnX.add(i);
  408. turnOnY.add(j);
  409. }
  410. }
  411.  
  412.  
  413. else if(boolBoard[i][j]){
  414. fullspace = 0;
  415. if(boolBoard[i- 1][j-1]){
  416. fullspace++;
  417. }
  418.  
  419. if(boolBoard[i- 1][j]){
  420. fullspace++;
  421. }
  422.  
  423. if(boolBoard[i][j-1]){
  424. fullspace++;
  425. }
  426.  
  427. if(boolBoard[i-widthT+1][j-lengthT+1]){
  428. fullspace++;
  429. }
  430.  
  431. if(boolBoard[i-widthT+1][j]){
  432. fullspace++;
  433. }
  434.  
  435. if(boolBoard[i][j-lengthT+1]){
  436. fullspace++;
  437. }
  438.  
  439. if(boolBoard[i-widthT+1][j-1]){
  440. fullspace++;
  441. }
  442.  
  443. if(boolBoard[i-1][j-lengthT+1]){
  444. fullspace++;
  445. }
  446.  
  447.  
  448. if(fullspace < 2){
  449. turnOffX.add(i);
  450. turnOffY.add(j);
  451. }
  452. else if(fullspace > 3){
  453. turnOffX.add(i);
  454. turnOffY.add(j);
  455. }
  456. }
  457. }
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464. for(int w = 0; w < 1; w++){//left bottom piece
  465. int j = lengthT - 1;
  466. int i = w;
  467. if(!boolBoard[i][j]){//off
  468. fullspace = 0;
  469. if(boolBoard[i+widthT- 1][j-1]){
  470. fullspace++;
  471. }
  472.  
  473. if(boolBoard[i+widthT- 1][j]){
  474. fullspace++;
  475. }
  476.  
  477. if(boolBoard[i][j-1]){
  478. fullspace++;
  479. }
  480.  
  481. if(boolBoard[i+1][j-lengthT+1]){
  482. fullspace++;
  483. }
  484.  
  485. if(boolBoard[i+1][j]){
  486. fullspace++;
  487. }
  488.  
  489. if(boolBoard[i][j-lengthT+1]){
  490. fullspace++;
  491. }
  492.  
  493. if(boolBoard[i+1][j-1]){
  494. fullspace++;
  495. }
  496.  
  497. if(boolBoard[i+widthT-1][j-lengthT+1]){
  498. fullspace++;
  499. }
  500.  
  501. if(fullspace == 3){
  502. turnOnX.add(i);
  503. turnOnY.add(j);
  504. }
  505. }
  506.  
  507.  
  508. else if(boolBoard[i][j]){
  509. fullspace = 0;
  510. if(boolBoard[i+widthT- 1][j-1]){
  511. fullspace++;
  512. }
  513.  
  514. if(boolBoard[i+widthT- 1][j]){
  515. fullspace++;
  516. }
  517.  
  518. if(boolBoard[i][j-1]){
  519. fullspace++;
  520. }
  521.  
  522. if(boolBoard[i+1][j-lengthT+1]){
  523. fullspace++;
  524. }
  525.  
  526. if(boolBoard[i+1][j]){
  527. fullspace++;
  528. }
  529.  
  530. if(boolBoard[i][j-lengthT+1]){
  531. fullspace++;
  532. }
  533.  
  534. if(boolBoard[i+1][j-1]){
  535. fullspace++;
  536. }
  537.  
  538. if(boolBoard[i+widthT-1][j-lengthT+1]){
  539. fullspace++;
  540. }
  541.  
  542.  
  543. if(fullspace < 2){
  544. turnOffX.add(i);
  545. turnOffY.add(j);
  546. }
  547. else if(fullspace > 3){
  548. turnOffX.add(i);
  549. turnOffY.add(j);
  550. }
  551. }
  552. }
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561. for(int w = 0; w < 1; w++){//right top piece
  562. int j = 0;
  563. int i = widthT - 1;
  564. if(!boolBoard[i][j]){//off
  565. fullspace = 0;
  566. if(boolBoard[i- 1][j+lengthT-1]){
  567. fullspace++;
  568. }
  569.  
  570.  
  571. if(boolBoard[i- 1][j]){
  572. fullspace++;
  573. }
  574.  
  575.  
  576. if(boolBoard[i][j+lengthT-1]){
  577. fullspace++;
  578. }
  579.  
  580.  
  581. if(boolBoard[i-widthT+1][j+1]){
  582. fullspace++;
  583. }
  584.  
  585.  
  586. if(boolBoard[i-widthT+1][j]){
  587. fullspace++;
  588. }
  589.  
  590.  
  591. if(boolBoard[i][j+1]){
  592. fullspace++;
  593. }
  594.  
  595. if(boolBoard[i-widthT+1][j+lengthT-1]){
  596. fullspace++;
  597. }
  598.  
  599. if(boolBoard[i-1][j+1]){
  600. fullspace++;
  601. }
  602.  
  603. if(fullspace == 3){
  604. turnOnX.add(i);
  605. turnOnY.add(j);
  606. }
  607. }
  608.  
  609.  
  610. else if(boolBoard[i][j]){
  611. fullspace = 0;
  612. if(boolBoard[i- 1][j+lengthT-1]){
  613. fullspace++;
  614. }
  615.  
  616. if(boolBoard[i- 1][j]){
  617. fullspace++;
  618. }
  619.  
  620. if(boolBoard[i][j+lengthT-1]){
  621. fullspace++;
  622. }
  623.  
  624. if(boolBoard[i-widthT+1][j+1]){
  625. fullspace++;
  626. }
  627.  
  628. if(boolBoard[i-widthT+1][j]){
  629. fullspace++;
  630. }
  631.  
  632. if(boolBoard[i][j+1]){
  633. fullspace++;
  634. }
  635.  
  636. if(boolBoard[i-widthT+1][j+lengthT-1]){
  637. fullspace++;
  638. }
  639.  
  640. if(boolBoard[i-1][j+1]){
  641. fullspace++;
  642. }
  643.  
  644. if(fullspace < 2){
  645. turnOffX.add(i);
  646. turnOffY.add(j);
  647. }
  648. else if(fullspace > 3){
  649. turnOffX.add(i);
  650. turnOffY.add(j);
  651. }
  652. }
  653. }
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663. for(int w = 0; w < 1; w++){//left top piece
  664. int j = 0;
  665. int i = w;
  666. if(!boolBoard[i][j]){//off
  667. fullspace = 0;
  668. if(boolBoard[i + widthT - 1][j+lengthT-1]){
  669. fullspace++;
  670. }
  671.  
  672.  
  673. if(boolBoard[i + widthT- 1][j]){
  674. fullspace++;
  675. }
  676.  
  677.  
  678. if(boolBoard[i][j+lengthT-1]){
  679. fullspace++;
  680. }
  681.  
  682.  
  683. if(boolBoard[i+1][j+1]){
  684. fullspace++;
  685. }
  686.  
  687.  
  688. if(boolBoard[i+1][j]){
  689. fullspace++;
  690. }
  691.  
  692.  
  693. if(boolBoard[i][j+1]){
  694. fullspace++;
  695. }
  696.  
  697.  
  698. if(boolBoard[i+1][j+lengthT-1]){
  699. fullspace++;
  700. }
  701.  
  702.  
  703. if(boolBoard[i+ widthT-1][j+1]){
  704. fullspace++;
  705. }
  706.  
  707. if(fullspace == 3){
  708. turnOnX.add(i);
  709. turnOnY.add(j);
  710. }
  711. }
  712.  
  713.  
  714. else if(boolBoard[i][j]){
  715. fullspace = 0;
  716. if(boolBoard[i+ widthT - 1][j+lengthT-1]){
  717. fullspace++;
  718. }
  719.  
  720. if(boolBoard[i+ widthT- 1][j]){
  721. fullspace++;
  722. }
  723.  
  724. if(boolBoard[i][j+lengthT-1]){
  725. fullspace++;
  726. }
  727.  
  728. if(boolBoard[i+1][j+1]){
  729. fullspace++;
  730. }
  731.  
  732. if(boolBoard[i+1][j]){
  733. fullspace++;
  734. }
  735.  
  736. if(boolBoard[i][j+1]){
  737. fullspace++;
  738. }
  739.  
  740. if(boolBoard[i+1][j+lengthT-1]){
  741. fullspace++;
  742. }
  743.  
  744. if(boolBoard[i+ widthT-1][j+1]){
  745. fullspace++;
  746. }
  747.  
  748.  
  749. if(fullspace < 2){
  750. turnOffX.add(i);
  751. turnOffY.add(j);
  752. }
  753. else if(fullspace > 3){
  754. turnOffX.add(i);
  755. turnOffY.add(j);
  756. }
  757. }
  758. }
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766. for(int w = 1; w < widthT - 1; w++){//top bar
  767. int j = 0;
  768. int i = w;
  769. if(!boolBoard[i][j]){//off
  770. fullspace = 0;
  771. if(boolBoard[i - 1][j+lengthT-1]){
  772. fullspace++;
  773. }
  774.  
  775.  
  776. if(boolBoard[i - 1][j]){
  777. fullspace++;
  778. }
  779.  
  780.  
  781. if(boolBoard[i][j+lengthT-1]){
  782. fullspace++;
  783. }
  784.  
  785. if(boolBoard[i+1][j+1]){
  786. fullspace++;
  787. }
  788.  
  789. if(boolBoard[i+1][j]){
  790. fullspace++;
  791. }
  792.  
  793.  
  794. if(boolBoard[i][j+1]){
  795. fullspace++;
  796. }
  797.  
  798.  
  799. if(boolBoard[i+1][j+lengthT-1]){
  800. fullspace++;
  801. }
  802.  
  803.  
  804. if(boolBoard[i-1][j+1]){
  805. fullspace++;
  806. }
  807.  
  808. if(fullspace == 3){
  809. turnOnX.add(i);
  810. turnOnY.add(j);
  811. }
  812. }
  813.  
  814.  
  815.  
  816.  
  817.  
  818. else if(boolBoard[i][j]){
  819. fullspace = 0;
  820. if(boolBoard[i - 1][j+lengthT-1]){
  821. fullspace++;
  822. }
  823.  
  824. if(boolBoard[i - 1][j]){
  825. fullspace++;
  826. }
  827.  
  828. if(boolBoard[i][j+lengthT-1]){
  829. fullspace++;
  830. }
  831.  
  832. if(boolBoard[i+1][j+1]){
  833. fullspace++;
  834. }
  835.  
  836. if(boolBoard[i+1][j]){
  837. fullspace++;
  838. }
  839.  
  840. if(boolBoard[i][j+1]){
  841. fullspace++;
  842. }
  843.  
  844. if(boolBoard[i+1][j+lengthT-1]){
  845. fullspace++;
  846. }
  847.  
  848. if(boolBoard[i-1][j+1]){
  849. fullspace++;
  850. }
  851.  
  852.  
  853. if(fullspace < 2){
  854. turnOffX.add(i);
  855. turnOffY.add(j);
  856. }
  857. else if(fullspace > 3){
  858. turnOffX.add(i);
  859. turnOffY.add(j);
  860. }
  861. }
  862. }
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870. for(int w = 1; w < widthT - 1; w++){//bottom bar
  871. int j = lengthT - 1;
  872. int i = w;
  873. if(!boolBoard[i][j]){//off
  874. fullspace = 0;
  875. if(boolBoard[i - 1][j - 1]){
  876. fullspace++;
  877. }
  878.  
  879.  
  880. if(boolBoard[i - 1][j]){
  881. fullspace++;
  882. }
  883.  
  884.  
  885. if(boolBoard[i][j - 1]){
  886. fullspace++;
  887. }
  888.  
  889.  
  890. if(boolBoard[i+1][j-lengthT+1]){
  891. fullspace++;
  892. }
  893.  
  894.  
  895. if(boolBoard[i+1][j]){
  896. fullspace++;
  897. }
  898.  
  899.  
  900. if(boolBoard[i][j-lengthT+1]){
  901. fullspace++;
  902. }
  903.  
  904.  
  905. if(boolBoard[i+1][j - 1]){
  906. fullspace++;
  907. }
  908.  
  909.  
  910. if(boolBoard[i-1][j-lengthT+1]){
  911. fullspace++;
  912. }
  913.  
  914. if(fullspace == 3){
  915. turnOnX.add(i);
  916. turnOnY.add(j);
  917. }
  918. }
  919.  
  920.  
  921.  
  922. else if(boolBoard[i][j]){
  923. fullspace = 0;
  924. if(boolBoard[i - 1][j-1]){
  925. fullspace++;
  926. }
  927.  
  928. if(boolBoard[i - 1][j]){
  929. fullspace++;
  930. }
  931.  
  932. if(boolBoard[i][j-1]){
  933. fullspace++;
  934. }
  935.  
  936. if(boolBoard[i+1][j-lengthT+1]){
  937. fullspace++;
  938. }
  939.  
  940. if(boolBoard[i+1][j]){
  941. fullspace++;
  942. }
  943.  
  944. if(boolBoard[i][j-lengthT+1]){
  945. fullspace++;
  946. }
  947.  
  948. if(boolBoard[i+1][j-1]){
  949. fullspace++;
  950. }
  951.  
  952. if(boolBoard[i-1][j-lengthT+1]){
  953. fullspace++;
  954. }
  955.  
  956.  
  957. if(fullspace < 2){
  958. turnOffX.add(i);
  959. turnOffY.add(j);
  960. }
  961. else if(fullspace > 3){
  962. turnOffX.add(i);
  963. turnOffY.add(j);
  964. }
  965. }
  966. }
  967.  
  968.  
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975. for(int l = 1; l < lengthT - 1; l++){//left bar
  976. int i = 0;
  977. int j = l;
  978. if(!boolBoard[i][j]){//off
  979. fullspace = 0;
  980. if(boolBoard[i + widthT -1][j-1]){
  981. fullspace++;
  982. }
  983.  
  984.  
  985. if(boolBoard[i + widthT -1][j]){
  986. fullspace++;
  987. }
  988.  
  989.  
  990. if(boolBoard[i][j-1]){
  991. fullspace++;
  992. }
  993.  
  994.  
  995. if(boolBoard[i+1][j+1]){
  996. fullspace++;
  997. }
  998.  
  999.  
  1000. if(boolBoard[i+1][j]){
  1001. fullspace++;
  1002. }
  1003.  
  1004.  
  1005. if(boolBoard[i][j+1]){
  1006. fullspace++;
  1007. }
  1008.  
  1009.  
  1010. if(boolBoard[i+1][j-1]){
  1011. fullspace++;
  1012. }
  1013.  
  1014.  
  1015. if(boolBoard[i + widthT -1][j+1]){
  1016. fullspace++;
  1017. }
  1018.  
  1019. if(fullspace == 3){
  1020. turnOnX.add(i);
  1021. turnOnY.add(j);
  1022. }
  1023. }
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029. else if(boolBoard[i][j]){
  1030. fullspace = 0;
  1031. if(boolBoard[i + widthT -1][j-1]){
  1032. fullspace++;
  1033. }
  1034.  
  1035. if(boolBoard[i + widthT -1][j]){
  1036. fullspace++;
  1037. }
  1038.  
  1039. if(boolBoard[i][j-1]){
  1040. fullspace++;
  1041. }
  1042.  
  1043. if(boolBoard[i+1][j+1]){
  1044. fullspace++;
  1045. }
  1046.  
  1047. if(boolBoard[i+1][j]){
  1048. fullspace++;
  1049. }
  1050.  
  1051. if(boolBoard[i][j+1]){
  1052. fullspace++;
  1053. }
  1054.  
  1055. if(boolBoard[i+1][j-1]){
  1056. fullspace++;
  1057. }
  1058.  
  1059. if(boolBoard[i + widthT -1][j+1]){
  1060. fullspace++;
  1061. }
  1062.  
  1063.  
  1064. if(fullspace < 2){
  1065. turnOffX.add(i);
  1066. turnOffY.add(j);
  1067. }
  1068. else if(fullspace > 3){
  1069. turnOffX.add(i);
  1070. turnOffY.add(j);
  1071. }
  1072. }
  1073. }
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082. for(int l = 1; l < lengthT - 1; l++){//right bar
  1083. int i = widthT -1;
  1084. int j = l;
  1085. if(!boolBoard[i][j]){//off
  1086. fullspace = 0;
  1087. if(boolBoard[i-1][j-1]){
  1088. fullspace++;
  1089. }
  1090.  
  1091.  
  1092. if(boolBoard[i-1][j]){
  1093. fullspace++;
  1094. }
  1095.  
  1096.  
  1097. if(boolBoard[i][j-1]){
  1098. fullspace++;
  1099. }
  1100.  
  1101.  
  1102. if(boolBoard[i-widthT+1][j+1]){
  1103. fullspace++;
  1104. }
  1105.  
  1106.  
  1107. if(boolBoard[i-widthT+1][j]){
  1108. fullspace++;
  1109. }
  1110.  
  1111.  
  1112. if(boolBoard[i][j+1]){
  1113. fullspace++;
  1114. }
  1115.  
  1116.  
  1117. if(boolBoard[i-widthT+1][j-1]){
  1118. fullspace++;
  1119. }
  1120.  
  1121.  
  1122. if(boolBoard[i-1][j+1]){
  1123. fullspace++;
  1124. }
  1125.  
  1126. if(fullspace == 3){
  1127. turnOnX.add(i);
  1128. turnOnY.add(j);
  1129. }
  1130. }
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136. else if(boolBoard[i][j]){
  1137. fullspace = 0;
  1138. if(boolBoard[i-1][j-1]){
  1139. fullspace++;
  1140. }
  1141.  
  1142. if(boolBoard[i-1][j]){
  1143. fullspace++;
  1144. }
  1145.  
  1146. if(boolBoard[i][j-1]){
  1147. fullspace++;
  1148. }
  1149.  
  1150. if(boolBoard[i-widthT+1][j+1]){
  1151. fullspace++;
  1152. }
  1153.  
  1154. if(boolBoard[i-widthT+1][j]){
  1155. fullspace++;
  1156. }
  1157.  
  1158. if(boolBoard[i][j+1]){
  1159. fullspace++;
  1160. }
  1161.  
  1162. if(boolBoard[i-widthT+1][j-1]){
  1163. fullspace++;
  1164. }
  1165.  
  1166. if(boolBoard[i-1][j+1]){
  1167. fullspace++;
  1168. }
  1169.  
  1170.  
  1171. if(fullspace < 2){
  1172. turnOffX.add(i);
  1173. turnOffY.add(j);
  1174. }
  1175. else if(fullspace > 3){
  1176. turnOffX.add(i);
  1177. turnOffY.add(j);
  1178. }
  1179. }
  1180. }
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188. for(int i = 1; i < widthT-1; i++)//checking empty spaces *main body*
  1189. {
  1190. for(int j = 1; j < lengthT-1; j++)
  1191. {
  1192. if(!boolBoard[i][j]){//off
  1193. fullspace = 0;
  1194. if(boolBoard[i - 1][j-1]){
  1195. fullspace++;
  1196. }
  1197.  
  1198.  
  1199. if(boolBoard[i - 1][j]){
  1200. fullspace++;
  1201. }
  1202.  
  1203.  
  1204. if(boolBoard[i][j-1]){
  1205. fullspace++;
  1206. }
  1207.  
  1208.  
  1209. if(boolBoard[i+1][j+1]){
  1210. fullspace++;
  1211. }
  1212.  
  1213.  
  1214. if(boolBoard[i+1][j]){
  1215. fullspace++;
  1216. }
  1217.  
  1218.  
  1219. if(boolBoard[i][j+1]){
  1220. fullspace++;
  1221. }
  1222.  
  1223.  
  1224. if(boolBoard[i+1][j-1]){
  1225. fullspace++;
  1226. }
  1227.  
  1228.  
  1229. if(boolBoard[i-1][j+1]){
  1230. fullspace++;
  1231. }
  1232.  
  1233.  
  1234. if(fullspace == 3){
  1235. turnOnX.add(i);
  1236. turnOnY.add(j);
  1237. }
  1238. }
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244. else if(boolBoard[i][j]){
  1245. fullspace = 0;
  1246. if(boolBoard[i - 1][j-1]){
  1247. fullspace++;
  1248. }
  1249.  
  1250. if(boolBoard[i - 1][j]){
  1251. fullspace++;
  1252. }
  1253.  
  1254. if(boolBoard[i][j-1]){
  1255. fullspace++;
  1256. }
  1257.  
  1258. if(boolBoard[i+1][j+1]){
  1259. fullspace++;
  1260. }
  1261.  
  1262. if(boolBoard[i+1][j]){
  1263. fullspace++;
  1264. }
  1265.  
  1266. if(boolBoard[i][j+1]){
  1267. fullspace++;
  1268. }
  1269.  
  1270. if(boolBoard[i+1][j-1]){
  1271. fullspace++;
  1272. }
  1273.  
  1274. if(boolBoard[i-1][j+1]){
  1275. fullspace++;
  1276. }
  1277.  
  1278.  
  1279. if(fullspace < 2){
  1280. turnOffX.add(i);
  1281. turnOffY.add(j);
  1282. }
  1283. else if(fullspace > 3){
  1284. turnOffX.add(i);
  1285. turnOffY.add(j);
  1286. }
  1287. }
  1288. }
  1289. }
  1290.  
  1291.  
  1292. for (int i = 0; i < turnOnX.size(); i++){
  1293. grid[ turnOnX.get(i) ][ turnOnY.get(i) ].setBackground(Color.blue);
  1294. boolBoard[turnOnX.get(i)][turnOnY.get(i)] = true;
  1295. }
  1296.  
  1297. for (int i = 0; i < turnOffX.size(); i++){
  1298. grid[ turnOffX.get(i) ][ turnOffY.get(i) ].setBackground(Color.red);
  1299. boolBoard[turnOffX.get(i)][turnOffY.get(i)] = false;
  1300. }
  1301.  
  1302. }
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309. }
  1310.  
  1311. private static final int[][] offsets = {
  1312. {-1, -1}, // up left
  1313. {-1, 0}, // left
  1314. {-1, 1}, // dn left
  1315. { 0, -1}, // up
  1316. { 0, 1}, // dn
  1317. { 1, -1}, // up right
  1318. { 1, 0}, // right
  1319. { 1, 1} // dn right
  1320. }
  1321.  
  1322. private static final int getAliveNeighbors(int x, int y, int width, int height, Boolean[][] boolboard) {
  1323.  
  1324. int alive = 0;
  1325. // look around us.... how many cells are alive
  1326. for (int[] offset : offsets) {
  1327. if (boolboard[(x + offset[0] + width) % width][(y + offset[1] + height) % height]) {
  1328. alive++;
  1329. }
  1330. }
  1331. return alive;
  1332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement