Mkim/Lightout

From ggc

Jump to: navigation, search

001 package Mkim;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 /**from summer class(also in API)*/
007 import grid.*;
008 /** implementing the lights out model that Dr.Jenkins created*/
009 import Mkim.*;
010 
011 /**
012  * lights out. point of the game either turn on all the lights 
013  * or turn off aghts to win the game.
014  * got helped from Jason. Jason helped me in implementing the class and gave me the idea to
015  * use the array of 5X5
016  @author min.
017  */
018 public class Lightout extends Game
019 {
020   /**Field Declaration of Sprite named board*/
021   private Sprite board;
022   /** OvalSprite Declaration named light*/
023   private OvalSprite light;
024   /**array declaration new Sprite 5X5*/
025   private Sprite[][] lights  = new Sprite[5][5];
026   /** int of row*/
027   private int row;
028   /** int of column*/
029   private int col;
030   /** model that Dr. jenkins created*/
031   private LightsOutModel gameWork;
032   /**make help*/
033   private void helps()
034   {
035     String helpText=
036         "first click to turn on the random light "+
037         "if you turn off all the lights than you win";
038     setHelpText(helpText);
039   }
040   /**makes the board 5 by 5 grid Grid(first value goes down, second value goes across,
041     size of the grid)*/
042   private void makeBoard()
043   {
044     board=new Grid(550.06);
045     board.setScale(1);
046     board.setLocation(0.50.5);
047     board.setColor(Palette.getColor("blue"));
048     addSprite(board);
049   }
050   /**Array of Light OvalSprite
051   used the base loop for square grid */
052   private void makeLight()
053   {
054     int lightAcross=5;
055     int lightDown=5;
056     for(int i=0; i<lightAcross; i++)
057     {
058       for(int j=0; j<lightAcross; j++)
059       {
060         light=new OvalSprite(11);
061         light.setSize(0.18);
062         double x=.5/lightAcross+1.0/lightAcross*(i);
063         double y=.5/lightAcross+(j+0.0)/lightAcross;
064         light.setLocation(x, y);
065         light.setColor(Palette.getColor("green"));
066         light.setVisible(false);
067         lights[i][j] = light;
068         addSprite(light);
069       }
070     }
071   }
072   /** this will set the light on or off depending on the boolean
073       *since the cells is a boolean it will return true or false
074        *so it will either turn off the light or on (Jason gave me the idea.)*/
075   private void updateBox()
076   {
077     for(int = 0; i < lights.length; i++)
078     {
079       for(int = 0; j < lights.length; j++)
080       {
081         lights[i][j].setVisible(gameWork.cells[i][j]);
082       }
083     }
084   }
085   /**sets up the game*/
086   public void setup()
087   {
088     /**makes the grid calling it*/
089     makeBoard();
090     /** make the array of a oval 25 lights calling it*/
091     makeLight();
092     /** calling the radom lights to be on*/
093     gameWork = new LightsOutModel();
094     /** getting the random lights to turn on from the lights out model
095     that Dr.J wrote*/
096     gameWork.randomPlay();
097     /** sets up the help method*/
098     helps();
099   }
100   /**handle input and game events*/
101   public void advance()
102   {
103     Point2D.Double click = getPlayer().getMouse().getClickLocation();
104     if(click !=null)
105     {
106       for(int = 0; i < lights.length; i++)
107       {
108         for(int = 0; j < lights.length; j++)
109         {
110 
111           if (lights[i][j].intersects(click))
112           {
113             row=i;
114             col=j;
115             gameWork.play(row,col);
116             updateBox();
117           }
118         }
119       }
120     }
121   }
122 }


Download/View Mkim/Lightout.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