Advertisement
eldieck

Untitled

Feb 23rd, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. public class lab3 {
  2.  
  3.     /**
  4.      * final variable for locked switch
  5.      */
  6.     private static final int LOCKED = 0;
  7.  
  8.     /**
  9.      * final variable for unlocked switch
  10.      */
  11.     private static final int UNLOCKED = 1;
  12.  
  13.     /**
  14.      * final variable for the number of switches spot on the array
  15.      */
  16.     private static final int SWITCH_NUM = 0;
  17.  
  18.     /**
  19.      * final variable for the number of lock steps spot on the array
  20.      */
  21.     private static final int LOCK_STEPS = 1;
  22.  
  23.     /**
  24.      * final variable for the number of unlock steps spot on the array
  25.      */
  26.     private static final int UNLOCK_STEPS = 2;
  27.  
  28.     /**
  29.      * final variable for the total number of steps spot on the array
  30.      */
  31.     private static final int TOTAL_STEPS = 3;
  32.  
  33.     /**
  34.      * counter start
  35.      */
  36.     private static final int DEFAULT = 0;
  37.  
  38.     /**
  39.      * even start
  40.      */
  41.     private static final int EVEN_START = 2;
  42.  
  43.     /**
  44.      * odd start
  45.      */
  46.     public static final int ODD_START = 1;
  47.  
  48.     /**
  49.      * data size final variable
  50.      */
  51.     private static final int DATA_SIZE = 4;
  52.    
  53.     private static int[] spinOut;
  54.    
  55.     private static int[] compArray;
  56.  
  57.     public static void main(String[] args) {
  58.  
  59.         int[] toDisplay = compute(3);
  60.  
  61.         for (int i = 0; i < toDisplay.length; i++) {
  62.             System.out.println(toDisplay[i]);
  63.         }
  64.  
  65.     }
  66.  
  67.     /**
  68.      * This method will compute the amount of steps it takes to play Spin Out
  69.      *
  70.      * @param numOfSwitches
  71.      *            the amount of switches in the game
  72.      * @return int array with the steps info for the game
  73.      */
  74.     public static int[] compute(int numOfSwitches) {
  75.  
  76.         compArray = new int[DATA_SIZE];
  77.         spinOut = new int[numOfSwitches];
  78.         compArray[SWITCH_NUM] = numOfSwitches;
  79.         int index = numOfSwitches;
  80.  
  81.         unlockGame(index);
  82.  
  83.         compArray[TOTAL_STEPS] = compArray[LOCK_STEPS]
  84.                 + compArray[UNLOCK_STEPS];
  85.         return compArray;
  86.     }
  87.  
  88.     /**
  89.      * This method will play through the game, acquiring step data
  90.      *
  91.      * @param compArray
  92.      *            array were data will be stored
  93.      * @param spinOut
  94.      *            array with values for locked or unlocked switches
  95.      * @param index
  96.      *            spot on the array to start unlocking
  97.      */
  98.     public static void unlockGame(int index) {
  99.  
  100.         if(Math.abs(DEFAULT - index) % 2 == 0 && !isSolved()){
  101.             index --;
  102.             flip(index);
  103.             if(isSolved()){
  104.                
  105.             }
  106.            
  107.         }
  108.         else {
  109.             flip(index);
  110.            
  111.         }
  112.        
  113.     }
  114.    
  115.    
  116.  
  117.     /**
  118.      * Checks to see if the game has been solved
  119.      *
  120.      * @param spinOut
  121.      *            array of switches
  122.      * @return boolean true or false depending on whether the game has been
  123.      *         solved
  124.      */
  125.     public static boolean isSolved() {
  126.  
  127.         for (int i = DEFAULT; i < spinOut.length; i++) {
  128.             if (spinOut[i] != UNLOCKED) {
  129.                 return false;
  130.             }
  131.         }
  132.         return true;
  133.     }
  134.    
  135.     public static void flip(int index){
  136.        
  137.         if(spinOut[index] == UNLOCKED){
  138.             spinOut[index] = LOCKED;
  139.         }
  140.         else {
  141.             spinOut[index] = UNLOCKED;
  142.         }
  143.     }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement