mardi 14 avril 2015

Find A Secret Square


Find A Secret Square


Modify the current program so the user moves the symbol: up, down, left or right, searching for a random winning box. The user gets only 5 moves to find the box



Enter number of rows: 6 (input by user)

Enter number of columns: 7 (input by user)

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

Enter your playing piece : * (input by user)

Enter starting row: 3 (input by user)

Enter starting column: 3

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [*][ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

(U)p, (D)own, (L)eft, (R)ight,or (Q)uit : U (input by user)

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [*][ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

(U)p, (D)own, (L)eft, (R)ight,or (Q)uit : R (input by user)

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [*][ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

[ ] [ ] [ ] [ ] [ ] [ ] [ ]

Congratulations you won!!


If after 5 moves (use a counter), break the loop and display: "Sorry you lost!!"



import acm.program.*;
import acm.util.*;
import java.util.*;

public class matrixSquare extends ConsoleProgram{
public void run(){
int numRows, numCols,currentR,currentC;
String piece= " ",ch=" ";
print("Enter number rows: ");
numRows=readInt();
print("Enter number of columns: ");
numCols=readInt();

String mat[][]=new String[numRows][numCols];
for (int i=0;i<mat.length;i++){
Arrays.fill(mat[i], (" "));
}

for (int r=0;r<mat.length;r++){
for (int c=0;c<mat[r].length;c++){
print("[" + mat[r][c] + "]");
}
println();
}

piece = readLine("Enter your playing piece: ");
currentR = readInt("Enter starting row: ");
currentC = readInt("Enter starting column: ");

do{
for (int i=0;i<mat.length;i++){
Arrays.fill(mat[i], (" "));
}

mat[currentC][currentR] = piece;

for (int r=0;r<mat.length;r++){
for (int c=0;c<mat[r].length;c++){
print("[" + mat[r][c] + "]");
}
println();
}

ch = readLine("(U)p, (D)own, (L)eft, (R)ight, or (Q)uit: ");
if (ch.toLowerCase().equals("q")){
println("Thanks for playing part 1. To be continued...");
break;
}
else if (ch.toLowerCase().equals("u")){
currentC--;
}
else if (ch.toLowerCase().equals("d")){
currentC++;
}
else if (ch.toLowerCase().equals("l")){
currentR--;
}
else if (ch.toLowerCase().equals("r")){
currentR++;
}
else{
println("Unknown Command");
}

}while(true);
}
}




Aucun commentaire:

Enregistrer un commentaire