In: Computer Science
Please take this c++ code and make it into java code.
/* RecursionPuzzleSolver
* ------------
* This program takes a puzzle board and returns true if the
* board is solvable and false otherwise
*
* Example: board 3 6 4 1 3 4 2 5 3 0
* The goal is to reach the 0. You can move the number of spaces of your
* current position in either the positive / negative direction
* Solution for this game is +3, -1, +4, +2, -3, +4
*/
#include <iostream>
#include <vector> // for vector
using namespace std;
/* Solvable
* --------
* Solvable returns true if there is a solution to the puzzle
* false otherwise.
*
*/
bool Solvable(int start, vector<int> & squares){
if (squares[start] == 0) return true;
else{
int nextForward = start + squares[start];
int nextBackward = start - squares[start];
if( nextForward<=squares.size()-1 && nextBackward>=0 ){
return ( Solvable(nextForward, squares) || Solvable(nextBackward,squares) );
}
if ( nextForward>squares.size()-1 && nextBackward>=0) {
return ( Solvable(nextBackward, squares));
}
if (nextForward<=squares.size()-1 && nextBackward<0 && squares[start]!=squares[nextForward]){
return ( Solvable(nextForward, squares));
}
if ( squares[start] == squares[nextForward] &&
nextBackward<0 &&
nextForward+squares[nextForward]>squares.size()-1){
return false;
}
else return false;
}
}
/* boolToString
* --------
* boolToString returns string "true" if the argument is true or 1
* returns string "false" otherwise
*
*/
string boolToString(bool boolean){
if(boolean)
{
return "true";
}
else
{
return "false";
}
}
int main(){
vector<int> puzzle1 {3, 6, 4, 1, 3 ,4, 2, 5, 3, 0};
vector<int> puzzle2 {3, 1, 2, 3, 0};
vector<int> puzzle3 {1,0};
vector<int> puzzle4 {1, 3, 2, 2, 1, 0};
cout << boolToString( Solvable(0, puzzle1) ) << endl; // true
cout << boolToString( Solvable(0, puzzle2) ) << endl; // false
cout << boolToString( Solvable(0, puzzle3) ) << endl; // true
cout << boolToString( Solvable(0, puzzle4) ) << endl; // true
return 0;
}
Please hit upvote(class name is Main so save as Main.java)
import java.util.*;
public class Main
{
static boolean Solvable (int start, Vector < Integer >
squares)
{
if (squares.get (start) == 0)
return true;
else
{
int nextForward = start + squares.get (start);
int nextBackward = start - squares.get (start);
if (nextForward <= squares.size () - 1
&& nextBackward >= 0)
{
return (Solvable (nextForward, squares)
|| Solvable (nextBackward,
squares));
}
if (nextForward > squares.size () - 1 &&
nextBackward >= 0)
{
return (Solvable (nextBackward, squares));
}
if (nextForward <= squares.size () - 1
&& nextBackward < 0
&& squares.get (start) != squares.get
(nextForward))
{
return (Solvable (nextForward, squares));
}
if (squares.get (start) == squares.get
(nextForward) &&
nextBackward < 0 &&
nextForward + squares.get (nextForward) >
squares.size () - 1)
{
return false;
}
else
return false;
}
}
static String boolToString (boolean bool)
{
if (bool)
{
return "true";
}
else
{
return "false";
}
}
public static void main (String[]args)
{
Vector < Integer > puzzle1 =
new Vector <> (Arrays.asList (3, 6, 4, 1, 3, 4, 2, 5, 3,
0));
Vector < Integer > puzzle2 =
new Vector <> (Arrays.asList (3, 1, 2, 3, 0));
Vector < Integer > puzzle3 = new Vector <> (Arrays.asList (1, 0));
Vector < Integer > puzzle4 =
new Vector <> (Arrays.asList (1, 3, 2, 2, 1, 0));
System.out.println (boolToString (Solvable (0, puzzle1)));
System.out.println (boolToString (Solvable (0, puzzle2)));
System.out.println (boolToString (Solvable (0, puzzle3)));
System.out.println (boolToString (Solvable (0, puzzle4)));
}
}