elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  ayuda hacer que aparezca casilla visible sin que el jugador se mueva
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: ayuda hacer que aparezca casilla visible sin que el jugador se mueva  (Leído 1,856 veces)
sheiking

Desconectado Desconectado

Mensajes: 10


Ver Perfil
ayuda hacer que aparezca casilla visible sin que el jugador se mueva
« en: 14 Noviembre 2018, 17:47 pm »

estoy haciendo un pequeño juego en consola que se trata de una búsqueda del tesoro en un cuadricula pero lo que quiero hacer es que el usuario escoja dos opciones moverse o ver lo que hay en la casilla de adelante pero solo tango la opción de mover

Código
  1. package RetoFinal;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class BuscarTesoro {
  6.    public static void main(String[] args) {
  7.        System.out.println("Dame dificultad: ");
  8.        Scanner lector = new Scanner(System.in);
  9.        int dificultad = lector.nextInt();
  10.        Juego partida = new Juego(dificultad);
  11.  
  12.        do{
  13.            partida.imprimir();
  14.            System.out.println("Vida: " + partida.vida);
  15.            System.out.println("Tesoros: " + partida.tesEncontrados);
  16.  
  17.            partida.mover();
  18.  
  19.       }while(partida.vida>=0&&partida.tesEncontrados<partida.numTesoros);
  20.        if(partida.vida<0){
  21.            partida.imprimir();
  22.            System.out.println("GAME OVER");
  23.            System.out.println("Tesoros Encontrados: "+partida.tesEncontrados);
  24.        }else{
  25.            partida.imprimir();
  26.            System.out.println("GANASTE");
  27.        }
  28.    }
  29. }
  30.  
  31.  
  32.  

Código
  1. package RetoFinal;
  2.  
  3. class Comida extends Item{
  4.  
  5.    Comida(int posX, int posY) {
  6.        this.posX=posX;
  7.        this.posY=posY;
  8.        this.afecta=3;
  9.    }
  10.  
  11. }
  12.  
  13.  

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

lo que tengo duda es que al momento de que el jugador haga un movimiento pregunte si se quiere mover o quiere solo ver la casilla si se mueve perderia 2 de vida y si solo ve lo que hay en la casilla perderia 1 vida


« Última modificación: 15 Noviembre 2018, 06:23 am por sheiking » En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: ayuda hacer que aparezca casilla visible sin que el jugador se mueva
« Respuesta #1 en: 14 Noviembre 2018, 23:44 pm »

Hola,

Usa Geshi así, para que te puedan ayudar mas rápido  


que es esto?

Código
  1. public void ver(){
  2.        if(!this.hallazgo){
  3.            map.matriz[player1.posX][player1.posY] = ' ';
  4.        }
  5.        if(ver){
  6.            map.matriz[player1.posX][player1.posY] = '?';
  7.        }
  8.        Scanner lector = new Scanner(System.in);
  9.        char opcion = lector.next().charAt(0);
  10.        switch(opcion){
  11.            case 'w':
  12.                break;
  13.            case 's':
  14.                break;
  15.            case 'a':
  16.                break;
  17.            case 'd':
  18.                break;
  19.        }
  20.        this.encontrarItem();
  21.        this.vida-=1;
  22.    }
  23.  

Sera que estas simplemente copiando codigo? y ni idea  :-\

invoca al metodo ver() y veras que hace te lo dejo como tarea

partida.mover();  :xD



y si  >:D estudia y aprende, quieres que te hagan la tarea @sheiking
como hacerlo ? leer y escribir mucho mucho código

recuerda la tarea pendiente

partida.mover();debes invocar a ver() que es un método


« Última modificación: 15 Noviembre 2018, 04:48 am por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
sheiking

Desconectado Desconectado

Mensajes: 10


Ver Perfil
Re: ayuda hacer que aparezca casilla visible sin que el jugador se mueva
« Respuesta #2 en: 15 Noviembre 2018, 06:07 am »

es que si lo que hice en el metodo ver porque lo que queria hacer era preguntarle al usuario si queria moverse a la casilla que tiene por delante o si queria solo ver que habia pero intente replicar el metodo de mover pero la verdad no tengo idea xd ando un poco perdido pero igual gracias por la ayuda  ;D
En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: ayuda hacer que aparezca casilla visible sin que el jugador se mueva
« Respuesta #3 en: 15 Noviembre 2018, 14:36 pm »

es que si lo que hice en el metodo ver porque lo que queria hacer era preguntarle al usuario si queria moverse a la casilla que tiene por delante o si queria solo ver que habia pero intente replicar el metodo de mover pero la verdad no tengo idea xd ando un poco perdido pero igual gracias por la ayuda  ;D


Y ahora tu clase Juego le borras el método ver?  :xD, por que razón?
En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
sheiking

Desconectado Desconectado

Mensajes: 10


Ver Perfil
Re: ayuda hacer que aparezca casilla visible sin que el jugador se mueva
« Respuesta #4 en: 15 Noviembre 2018, 15:51 pm »

Es que borre técnicamente el método ver() porque era lo mismo que mover() solo que te quitaba 1 de vida  :-\  pero sigo viendo cómo hacerle para que solo se descubra la casilla y que el personaje no avance xd por eso lo borre   :-[ gracias  por enseñarme lo de Geshi si es muy útil
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Hacer que perfil de facebook aparezca en los buscadores
Mensajería
DarkNigth048 2 5,153 Último mensaje 24 Marzo 2011, 16:19 pm
por DarkNigth048
Hacer visible DataGridView asp.net
.NET (C#, VB.NET, ASP)
k4rn13l 0 1,663 Último mensaje 21 Diciembre 2011, 15:53 pm
por k4rn13l
[Python] Hacer que aparezca la ventanita de examinar
Scripting
Trenico 2 3,082 Último mensaje 5 Enero 2012, 20:26 pm
por 0x5d
Hacer Form visible dentro de dll. « 1 2 »
.NET (C#, VB.NET, ASP)
70N1 12 7,138 Último mensaje 7 Diciembre 2012, 15:23 pm
por 70N1
[PYTHON]¿Como puedo hacer que un gif se mueva en tkinter?
Programación General
Mastodonte 3 4,849 Último mensaje 25 Septiembre 2017, 20:42 pm
por tincopasan
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines