Advertisement
eldieck

Untitled

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