Read attached file
start with ‘X’ in array[0][0]
Move to Right if end is reach and still Right, ‘X’ appears on left side.
Move to Left if end is reach and still Left, ‘X’ appears on Right side.
Move to Up if end is reach and still Up, ‘X’ appears from bottom.
Move to Down if end is reach and still Down, ‘X’ appears from top.
CODE
import java.io.*;
import java.util.*;
class Ass{
public static void main(String[] args)throws IOException{
// Thread t = Thread.currentThread();
int maxcol = 60;
int maxrow = 20;
int colpos = 0;
int rowpos = 0;
char[][]array = new char[20][60];
Scanner in = new Scanner(new FileReader("obstruct.txt"));
int i =0;
int j =0;
System.out.print(array[i][j]='X');
for (i =1; i<array.length;i++){
String ss = in.nextLine();
for(j = 1; j<array[i].length;j++){
array[i][j]= ss.charAt(j);
System.out.print(array[i][j]);
}
System.out.println(" ");
}
int ans = check();
if (ans==1){
right(array,rowpos, colpos,ans,maxcol);
PrintResults(array);
}else if(ans ==2){
left(array,rowpos, colpos,ans,maxcol);
PrintResults(array);
} else if (ans ==3){
up(array,rowpos, colpos,ans,maxrow,maxcol);
PrintResults(array);
}else if(ans ==4){
down(array,rowpos, colpos,ans,maxcol,maxrow);
PrintResults(array);
}else{
Quit(ans);
}
}//end main
public static int check(){
Scanner in = new Scanner(System.in);
System.out.println("1.Right"+" "+ "2. Left"+" "+"3. Up"+" "+"4. Down"+" "+"5.Quit");
int choice = in.nextInt();
return choice;
}
public static boolean isEmpty(char[][]arr, int rpos,int cpos){
return ((arr[rpos][cpos])==0);
}
public static void right(char[][]ar, int rpos, int cpos, int answ, int mcol){
if((rpos == 0)&&(cpos>=0)&&(cpos<mcol-1)){
cpos++;
if(isEmpty(ar,rpos, cpos)){
ar[rpos][cpos] = 'X';
System.out.print(ar[rpos][cpos]);
}else if(!isEmpty(ar,rpos, cpos)){
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}
public static void left(char[][]ary,int ropos, int copos,int answer, int mxcol){
if((ropos >=0)&&(copos>=0)&&(copos<mxcol)){
copos = mxcol-1;
if(isEmpty(ary,ropos, copos)){
ary[ropos][copos] = 'X';
System.out.print(ary[ropos][copos]);
}else if(!isEmpty(ary,ropos, copos)){
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}
public static void up(char[][]arry,int rrpos, int ccpos,int answ, int mxrow, int mxcol){
if((rrpos < mxrow)&&(ccpos>=0)&&(ccpos<mxcol)){
rrpos = 0;
if(isEmpty(arry,rrpos, ccpos)){
arry[rrpos][ccpos] = 'X';
//System.out.print(arry[rrpos][ccpos]);
}else if(!isEmpty(arry,rrpos, ccpos)){
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}
public static void down(char[][]array1,int rowpos1, int colpos1,int answer1, int mxcol1, int mxrow1){
if((rowpos1 <= 0)&&(colpos1>=0)&&(colpos1<mxcol1)){
rowpos1 = mxrow1 - 1;
if(isEmpty(array1,rowpos1, colpos1)){
array1[rowpos1][colpos1] = 'X';
System.out.print(array1[rowpos1][colpos1]);
}else if(!isEmpty(array1,rowpos1, colpos1)){
java.awt.Toolkit.getDefaultToolkit().beep();
array1[rowpos1][colpos1++]='X';
System.out.print(array1[rowpos1][colpos1]);
}
}
}
public static void Quit(int ansChoice){
System.exit(0);
}
public static void PrintResults(char [][]a){
for (int i =1; i<a.length;i++){
for(int j = 1; j<a[i].length;j++){
System.out.print(a[i][j]);
}
System.out.println(" ");
}
}
}//end class
Description of problem
I am trying to move the charcter around but, the progrma is not working the way it should, for example when I try to move Right, the character moves Right but does not go back to the left when it gets to the end.