JacobOlson/Interface/LightsOutModel

From ggc

Jump to: navigation, search

01 package JacobOlson.Interface;
02 
03 import wiki.Wiki;
04 /**
05  * All about my class here.
06  @author Jolson
07  */
08 public class LightsOutModel
09 
10 {
11   private boolean[][] cells;
12 
13   /**
14    * Initializes all of the lights to on.
15    */
16 
17   public LightsOutModel()
18   {
19     cells=new boolean[5][5];
20     for(int r=0; r<cells.length; r++)
21     {
22       for(int c=0; c<cells[0].length; c++)
23       {
24         cells[r][c]=false;
25       }
26     }
27     java.util.Random random=new java.util.Random();
28     for(int k=0; k<10; k++)
29       play(random.nextInt(cells.length),random.nextInt(cells[0].length));
30   }
31 
32   public void play(int row, int col)
33   {
34     cells[row][col]=!cells[row][col];
35     if(row-1>=0)
36       cells[row-1][col]=!cells[row-1][col];
37     if(row+1<=4)
38       cells[row+1][col]=!cells[row+1][col];
39     if(col-1>=0)
40       cells[row][col-1]=!cells[row][col-1];
41     if(col+1<=4)
42       cells[row][col+1]=!cells[row][col+1];
43   }
44 
45   public boolean won()
46   {
47     for(int i=0; i<cells.length; i++)
48     {
49       for(int j=0; j<cells[i].length; j++)
50       {
51         if(cells[i][j]==true)
52         {
53           return false;
54         }
55       }
56     }
57     return true;
58   }
59 
60   public boolean isOn(int row, int col)
61   {
62     return cells[row][col];
63   }
64 
65   public String toString()
66   {
67     String total="";
68     for(int r=0; r<cells.length; r++)
69     {
70       for(int c=0; c<cells[0].length; c++)
71       {
72         total+=cells[r][c]+"\t";
73       }
74       total+="\n";
75     }
76     return total;
77   }
78 }


Download/View JacobOlson/Interface/LightsOutModel.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter