Java - ayuda generar un metodo para que solo se descubra casilla y el jugador no avance

 
Vista:
sin imagen de perfil
Val: 5
Ha aumentado su posición en 7 puestos en Java (en relación al último mes)
Gráfica de Java

ayuda generar un metodo para que solo se descubra casilla y el jugador no avance

Publicado por Jesus Alberto (4 intervenciones) el 17/11/2018 20:52:21
hola que tal ando haciendo un juego en consola que es de buscar un tesoro pero el problema que tengo es que en el juego quiero poner que le pregunte al jugador si se quiere mover a una casilla o si solo la quiere descubrir al momento de que el jugador se mueve a la casilla pierde dos vidas y si solo la quiere descubrir el jugador no se mueve pero si se descubre la casilla y pierde una vida

aquí les dejo lo que tengo ya avanzado

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package RetoFinal;
 
import java.util.Scanner;
import java.util.InputMismatchException;
 
public class BuscarTesoro {
    public static void main(String[] args) {
        try{
        System.out.println("hello this program is a search of the treasure");
        System.out.println("choose dificulty to playing ");
        Scanner lector = new Scanner(System.in);
        int dificultad = lector.nextInt();
        Juego partida = new Juego(dificultad);
 
 
        do{
            partida.imprimir();
            System.out.println("Lives: " + partida.vida);
            System.out.println("Treasures: " + partida.tesEncontrados);
 
            partida.mover();
 
       }while(partida.vida>=0&&partida.tesEncontrados<partida.numTesoros);
        if(partida.vida<0){
            partida.imprimir();
            System.out.println("GAME OVER");
            System.out.println("Treasures found: "+partida.tesEncontrados);
        }else{
            partida.imprimir();
            System.out.println("YOU WIN");
        }
      }catch(InputMismatchException e){
            System.out.println("only use numbers please");
      }
 
    }
  }
1
2
3
4
5
6
7
8
9
10
11
package RetoFinal;
 
class Comida extends Item{
 
    Comida(int posX, int posY) {
        this.posX=posX;
        this.posY=posY;
        this.afecta=3;
    }
 
}
1
2
3
4
5
6
package RetoFinal;
 
public class Elemento {
    int posX;
    int posY;
}
1
2
3
4
5
6
7
8
9
10
11
package RetoFinal;
 
class Enemigo extends Item{
 
    Enemigo(int posX, int posY) {
        this.posX=posX;
        this.posY=posY;
        this.afecta=-5;
    }
 
}
1
2
3
4
5
package RetoFinal;
 
class Item extends Elemento{
    int afecta;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package RetoFinal;
 
import java.util.Random;
import java.util.Scanner;
 
public class Juego {
    boolean continuar=true;
    int tesEncontrados = 0;
    private boolean hallazgo;
    private boolean ver;
    int numTesoros;
    private int numEnemigos;
    private int numComida;
    int vida;
    private int mapDimension;
    private Jugador player1;
    private final Mapa map;
    private Enemigo[] enemies;
    private Comida[] food;
    private Tesoro[] chest;
 
    public Juego(int dificultad) {
 
        switch(dificultad){
            case 1:
                this.mapDimension=5;
                this.vida = 50;
                this.numTesoros=1;
                this.numEnemigos=2;
                this.numComida=8;
                break;
            case 2:
                this.mapDimension=8;
                this.numTesoros=2;
                this.vida = 40;
                this.numEnemigos=4;
                this.numComida=6;
                break;
            case 3:
                this.numTesoros=4;
                this.numEnemigos=8;
                this.numComida=4;
                this.vida = 30;
                this.mapDimension=12;
                break;
            default:
 
                break;
        }
        map = new Mapa(this.mapDimension);
        this.generarJugador();
        this.generarEnemigos();
        this.generarComida();
        this.generarTesoro();
    }
 
    private void generarJugador() {
        Random generadorAleatorios = new Random();
        int posX = 0+generadorAleatorios.nextInt(this.mapDimension);
        int posY = 0+generadorAleatorios.nextInt(this.mapDimension);
        player1 = new Jugador(posX,posY);
    }
 
    private void generarEnemigos() {
        this.enemies = new Enemigo[this.numEnemigos];
        for(int i=0;i<this.numEnemigos;i++){
            Random generadorAleatorios = new Random();
            int posX = 0+generadorAleatorios.nextInt(this.mapDimension);
            int posY = 0+generadorAleatorios.nextInt(this.mapDimension);
            int numeroAleatorio = 1+generadorAleatorios.nextInt(5);
            this.enemies[i] = new Enemigo(posX, posY);
        }
    }
 
    private void generarComida() {
        this.food = new Comida[this.numComida];
        for(int i=0;i<this.numComida;i++){
            Random generadorAleatorios = new Random();
            int posX = 0+generadorAleatorios.nextInt(this.mapDimension);
            int posY = 0+generadorAleatorios.nextInt(this.mapDimension);
            int numeroAleatorio = 1+generadorAleatorios.nextInt(5);
            food[i] = new Comida(posX, posY);
        }
    }
 
    private void generarTesoro() {
        this.chest = new Tesoro[this.numTesoros];
        for(int i=0;i<this.numTesoros;i++){
            Random generadorAleatorios = new Random();
            int posX = 0+generadorAleatorios.nextInt(this.mapDimension);
            int posY = 0+generadorAleatorios.nextInt(this.mapDimension);
            int numeroAleatorio = 1+generadorAleatorios.nextInt(5);
            chest[i] = new Tesoro(posX, posY);
        }
    }
 
    public void imprimir() {
        map.matriz[player1.posX][player1.posY] = '@';
        map.imprimir();
        this.generarItem();
    }
 
    public void mover() {
        if(!this.hallazgo){
            map.matriz[player1.posX][player1.posY] = ' ';
        }
        if(ver){
            map.matriz[player1.posX][player1.posY] = '?';
        }
        System.out.println("controls:");
        System.out.println("West  North  South  East");
        System.out.println("a      w      s    d");
 
        Scanner lector = new Scanner(System.in);
        char opcion = lector.next().charAt(0);
        switch(opcion){
            case 'a':
                player1.moverX(-1);
                break;
            case 'w':
                player1.moverY(-1);
                break;
            case 's':
                player1.moverY(1);
                break;
            case 'd':
                player1.moverX(1);
                break;
 
        }
        this.encontrarItem();
        this.vida-=2;
    }
    public void see(){
 
    }
 
    private void generarItem() {
        int i;
        this.hallazgo=false;
        for(i=0;i<this.numTesoros;i++){
            if(player1.posX==chest[i].posX&&player1.posY==chest[i].posY){
                System.out.println("Encontraste Un Tesoro! Felicidades!");
                map.matriz[player1.posX][player1.posY] = '$';
                this.hallazgo=true;
            }
        }
        for(i=0;i<this.numComida;i++){
            if(player1.posX==food[i].posX&&player1.posY==food[i].posY){
                System.out.println("Encontraste Comida! Salud +3");
                map.matriz[player1.posX][player1.posY] = '+';
                this.hallazgo=true;
            }
        }
        for(i=0;i<this.numEnemigos;i++){
            if(player1.posX==enemies[i].posX&&player1.posY==enemies[i].posY){
                System.out.println("Encontraste Un Enemigo! Salud -5");
                map.matriz[player1.posX][player1.posY] = '!';
                this.hallazgo=true;
            }
        }
    }
 
    private void encontrarItem() {
        int i;
        for(i=0;i<this.numTesoros;i++){
            if(player1.posX==chest[i].posX&&player1.posY==chest[i].posY){
                this.tesEncontrados++;
            }
        }
        for(i=0;i<this.numComida;i++){
            if(player1.posX==food[i].posX&&player1.posY==food[i].posY){
                this.vida+=3;
            }
        }
        for(i=0;i<this.numEnemigos;i++){
            if(player1.posX==enemies[i].posX&&player1.posY==enemies[i].posY){
                this.vida-=5;
            }
        }
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package RetoFinal;
 
class Jugador extends Elemento{
    int vida;
 
    Jugador(int posX, int posY) {
        this.posX=posX;
        this.posY=posY;
    }
    public void moverX(int i){
        this.posY += i;
    }
    public void moverY(int j){
        this.posX += j;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package RetoFinal;
 
class Mapa {
 
    char[][] matriz;
    private final int dimension;
 
    public Mapa(int d) {
        this.dimension = d;
        this.matriz = new char[d][d];
        for(int i=0;i<this.dimension;i++){
            for(int j=0;j<this.dimension;j++){
                this.matriz[i][j]='?';
            }
        }
    }
 
    void imprimir() {
        for(int i=0;i<this.dimension;i++){
            for(int j=0;j<this.dimension;j++){
                System.out.print(this.matriz[i][j]+" ");
            }
            System.out.println();
        }
    }
 
}
1
2
3
4
5
6
7
8
9
10
11
package RetoFinal;
 
 
class Tesoro extends Elemento{
 
    Tesoro(int posX, int posY) {
        this.posX=posX;
        this.posY=posY;
    }
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder