3
\$\begingroup\$

I need to calculate the minimum number of jumps to reach the end of an Array with dice throw.

Array values may be negative/positive:

  • When positive - move forward
  • When negative - go back

The array may also contain an R value, which means that the player have to throw the dice again.

The start position is marked on our array with S and end position with E. The start position is not always the first element of the array and the end position is not always at the end (it can even be before S).

Example:

Array = {4, S, -2,1, R, 4,3,4,3,-5,2,-4, E}

The player starts on the S position the fastest way to reach E:

  • Throwing the dice to have 3 and reach the R case (first move)
  • Throwing the dice again and having 6 to reach the 2 case (second movement)
  • Jumping 2 cases to reach E (third move)

So the best solution for this example is: 3 moves

I'm using a BufferReader to read files that contain my array elements. I wrote this working solution but I need some advice to improve my code.

public static int compute(BufferedReader br) throws IOException{
   //Retriving Total node Number    
   final int TotalCaseCount = GetCaseCount(br);
    //pushing Bufferreader to an Array
    final String CaseArray[]=new String[TotalCaseCount];
    BufferToArray(br, CaseArray);
    //Catching Start Position
StartPosition=GetStartPosition(CaseArray);
    //Array to mark Visited node statut
    final boolean visited[ ]=new boolean[CaseArray.length]; 
    int MinimumNumberOfMove = 0;
    //List that will contain all reachable route
    final List<Integer> ReachableList = new ArrayList<Integer>();
    //Calculating all reachable route
             for (int i=1;i<=6;i++)
                {
                    VisitedInitilise(visited);
                    if(StartPosition+i<TotalCaseCount&&StartPosition+i>0)
                    {
                    GetMinimumNumberOfMoves(CaseArray,visited, StartPosition+i,0,ReachableList);
                    }
                }

 //Retriving Minimum number of move from all reachble route
if(ReachableList.isEmpty())
  {
    MinimumNumberOfMove=Constants.IMPOSSIBLE;
  }

else

  {
    MinimumNumberOfMove=ReachableList.get(0);
   for (int i=0;i<ReachableList.size();i++)
      {
         if(ReachableList.get(i)<MinimumNumberOfMove)
         {
            MinimumNumberOfMove=ReachableList.get(i);
         }

      }

   }

return MinimumNumberOfMove;


}

static List<Integer> GetMinimumNumberOfMoves( String[] Plateau,boolean[] visited ,final int current_index, int Current_N_of_moves,  List<Integer> List )
{

    Boolean EndIsReached=false;
    Boolean Impossible=false;
    visited[StartPosition]=true;
    //Checking if the current index index is negativ 
    if(current_index<0)
    {
        Impossible=true;
    }


    while(EndIsReached==false&&Impossible==false&&visited[current_index]==false&&current_index<Plateau.length)
    {
    try {

        switch (Plateau[current_index]){
        case "E":
        {
            //if end is reached , pushing number of move into our list 
            EndIsReached=true;
            List.add(Current_N_of_moves+1);
            break;

        }


        case "R":
        {
            //Marking node as visited
            visited[current_index]=true;

                for (int i=1;i<=6;i++)
                {
                    //Marking all case after R case as non visited 
                    for(int j=current_index+1;j<visited.length;j++)
                    {
                        visited[j]=false;
                    }
                    //Calculating number of move after R case
                    if(current_index+i<Plateau.length&&current_index>0)
                    {
                    GetMinimumNumberOfMoves(Plateau,visited, current_index+i,Current_N_of_moves+1,List);
                    }
                }



                break;
        }
        default:
        {
             //Cheking if node was already visited
            if(visited[current_index]==true)
            {
                //Marking all node as non visited 
                VisitedInitilise(visited);

                Impossible=true;
                break;
            }
            else
               {
                //when the node was not visited before , catch the jump value
                int JumpValue=Integer.parseInt( Plateau[current_index]);
                    //cheking that the next node is not bigger than node number and not negativ
                    if(current_index+JumpValue>Plateau.length||current_index<0)
                     {
                      Impossible=true;
                      break;
                     }
                         else
                         {
                          //Marking node as visited 
                          visited[current_index]=true;
                         //calculating minimum number of move starting from this node
                          GetMinimumNumberOfMoves(Plateau,visited,current_index + JumpValue, Current_N_of_moves + 1,List);
                         break;
                         }
                }

        }

}


} 
    catch (NumberFormatException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    break;
    }
    if(Impossible==true)
    {
        Current_N_of_moves=0;
    }

        return List;






}

//A Method to count total Node 
static int GetCaseCount (BufferedReader br)

{
    int i = 0;
      try 
       {
       i = Integer.parseInt(br.readLine());
       } 
          catch (NumberFormatException e) 
          {
          e.printStackTrace();
          } 
             catch (IOException e) 
             {
             e.printStackTrace();
             }
return i;
}

 //A method to push bufferreader to String Array
static void BufferToArray(BufferedReader br,String[] Plateau)
{
    String line;
    int i = 0;
       try 
       {
          while((line=br.readLine())!=null)
          {
            Plateau[i]=line;
            i++;
          }
       } catch (IOException e) 
       {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
}



//A method to mark all node as non visited
static void VisitedInitilise(boolean visited[])
{
    for(int i =0;i<=visited.length-1;i++)
    {
        visited[i]=false;
    }
}

//A method to Get the S Position
static int GetStartPosition(String[] Plateau)
{

    int Startindex = 0;
    for(int i=0;i<=Plateau.length-1;i++)
    {
        if( Plateau[i].equals("S")) 
        {
            Startindex=i;
            break;
        }

    }

    return Startindex;
}
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers. Your edit fixes point addressed in an answer; this makes the answer outdated and doesn't allow others to learn from your code. \$\endgroup\$
    – Pimgd
    Commented Mar 18, 2016 at 16:36
  • \$\begingroup\$ ... so please don't edit the code in your question. If you want to provide an improved version, ask a follow up question. \$\endgroup\$
    – Pimgd
    Commented Mar 18, 2016 at 16:48

1 Answer 1

3
\$\begingroup\$

A few things to get started:

  • follow the language convention: in Java, variables and methods start in lower case: final int TotalCaseCount = GetCaseCount(br); should be final int totalCaseCount = getCaseCount(br); - as a side effect, once you do that, the auto-formatting on this site will work better.
  • try to use self-documenting code. Instead of:

    //Retriving Total node Number    
    final int totalCaseCount = getCaseCount(br);
    

    why not write:

    final int totalCaseCount = getTotalNodeNumber(br);
    

    and get rid of the comment?

  • indent your code properly and put opening braces on the same line (Java convention):

    static int GetCaseCount (BufferedReader br)
    
    {
        int i = 0;
          try 
           {
           i = Integer.parseInt(br.readLine());
           } 
              catch (NumberFormatException e) 
              {
              e.printStackTrace();
              } 
                 catch (IOException e) 
                 {
                 e.printStackTrace();
                 }
    return i;
    }
    

    would look better like this:

    static int GetCaseCount(BufferedReader br) {
      int i = 0;
      try {
        i = Integer.parseInt(br.readLine());
      } catch (NumberFormatException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return i;
    }
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.