Java oop пытается уменьшить тип объекта в игровом приложении - PullRequest
0 голосов
/ 19 января 2020

https://github.com/cesarMark/psGame?files=1

Таким образом, у каждого игрока есть очки здоровья, но проблема в том, что я не могу уменьшить, например, если противник атакует другого противника. и затем попытался разыграть 'i' как жертву, на которую будут нападать

Character[] gamePlayers = new Character[3];
    gamePlayers[0] = new Bob();
    gamePlayers[1] = new Alice();
    gamePlayers[2] = new EvilTom();
        for(Character i:gamePlayers) {
            if(i instanceof Player) {
                System.out.println("Hello "+i.getClass().getName()+"!"+" Here is your status \n"+i.toString());
                System.out.println("What would you like to do?");
                System.out.println("1)shoot\n2)reload\n3)sleep");
                System.out.print("Enter an integer: ");
                int number = input.nextInt();
                switch(number) {
                case 1:
                        System.out.println("Hi "+((Player) i).getName()+" Who would you like to shoot at?");
                        for(Character j:gamePlayers) {
                            System.out.println(j.toString());
                        }
                        int attackChoice = input.nextInt();
                        switch(attackChoice) {
                            case 1:
                                if(i instanceof Alice) {
                                    ((Alice) i).fire();
                                    System.out.println("BOB");
                                    //Now in this line our case Bob is getting shot by Alice . Now in the class of Bob I specified int health meaning that I'm trying to reduce his life like objectOfBob.health-- how can I do it after Alice shoots? (BTW the fire() comes from an interface that the players implement 
                                }

;

Как вы можете видеть, где я положил комментарий, Алиса застрелила Боба. Теперь в классе Боба я указал int health, что означает, что я пытаюсь сократить его жизнь, как objectOfBob.health-- как я могу сделать это после того, как Алиса стреляет? (Кстати, огонь () исходит из интерфейса, который реализуют игроки) enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...