private static void battle(Adventurer player, AbstractCreature opponent) {
    System.out.println("You're battling a " + opponent.toString());

    Scanner scanner = new Scanner(System.in);

    while (player.getHealth() > 0 && opponent.getHealth() > 0) {
      String action = null;

      while (!VALID_ACTIONS.contains(action)) {
        System.out.println("What would you like to do?");
        for (String validAction : VALID_ACTIONS) {
          System.out.println("\t" + validAction);
        }

        action = scanner.next();

        /*
            TODO:
            If the action is "attack", call the player's attack function
            on the opponent.
            If the action is "check_inventory", call the player's
            print_inventory function.
            If the action is "check_health", call the player's print_health
            function
            If the command isn't one of these, tell the user that
            the command is invalid and that they should try again.

            There are two ways to write this. Either will work :D
        */
      }

      if (opponent.getHealth() <= 0) {
        // TODO: The player should loot the opponent
        System.out.println("Congratuations! You slayed " + opponent.toString());
        return;
      }

      opponent.attack(player);

      if (player.getHealth() <= 0) {
        // TODO: Print to the screen that the player died
        System.exit(0);
      }
    }

    scanner.close();
  }
Exemple #2
0
    public static void combat(Adventurer[] player, Adventurer opp){
	Scanner in = new Scanner(System.in);
	for (int i=0;i<(player.length);i++){
	    while (true){
		System.out.println("Choose an action: \n A: attack \n S: special attack");
		String act2 = in.next();
		if (act2.equals("A")){
		    player[i].attack(opp);
		    break;
		}else if (act2.equals("S")){
		    player[i].specialAttack(opp);
		    break;
		}else{
		    System.out.println("Please input choice again.");
		}
	    }
	    if (opp.getHP() <= 0){
		break;
	    }
	}
	Random rand = new Random();
	Random rand2 = new Random();
	int target = rand2.nextInt(3);
	while (opp.getHP() > 0){
	    if (player[target].getHP() > 0){
		if (rand.nextDouble() < .3){
		    opp.specialAttack(player[target]);
		    System.out.println();
		    break;
		}else{
		    opp.attack(player[target]);
		    System.out.println();
		    break;
		}
	    }else{
		target = rand2.nextInt(3);
	    }
	}
    }
Exemple #3
0
    public static void main(String[]args){
	Adventurer opp;
	Adventurer[] player;
	player = new Adventurer [3];
	Scanner in = new Scanner(System.in);
	player[0] = new Warrior("Barrett");
	player[1] = new Wizard("Terra");
	player[2] = new Rogue("Zidane");
	/*while (true){
   	    System.out.println("Choose a class: \n A: Warrior \n B: Wizard \n C: Rogue");
	    String charselect = in.next();
	    if (charselect.equals("A")){
		player = new Warrior("Player");
		break;
	    }else if (charselect.equals("B")){
		player = new Wizard("Player");
		break;
	    }else if (charselect.equals("C")){
		player = new Rogue("Player");
		break;
	    }else{
		System.out.println("Please input choice again.");
	    }
	}
	int statpts = 30;
	while (true){
	    Scanner inSTR = new Scanner(System.in);
	    System.out.println("Enter how many points in STR:");
	    int STRpts = inSTR.nextInt();
	    Scanner inINT = new Scanner(System.in);
	    System.out.println("Enter how many points in INT:");
	    int INTpts = inINT.nextInt();
	    Scanner inDEX = new Scanner(System.in);
	    System.out.println("Enter how many points in DEX:");
	    int DEXpts = inDEX.nextInt();
	    if (STRpts+INTpts+DEXpts <= 30){
		player.setSTR(STRpts);
		player.setINT(INTpts);
		player.setDEX(DEXpts);
		break;
	    }else{
		System.out.println("Total points exceed 30. Go back and enter values so the sum is less than 30.");
	    }
	    }*/
	Random comp = new Random();
	int comp2 = comp.nextInt(3);
	if (comp2 == 1){
	    opp = new Warrior("Opponent");
	}else if (comp2 == 2){
	    opp = new Wizard("Opponent");
	}else{
	    opp = new Rogue("Opponent");
	}
	while (player[0].getHP() > 0 && player[1].getHP() >0 && player[2].getHP() > 0 && opp.getHP() > 0){
	    for(int i=0;i<3;i++){
		System.out.println(player[i].getStats());
	    }
	    System.out.println();
	    System.out.println(opp.getStats());
	    System.out.println();
	    combat(player,opp);
	    }
	   
	if (opp.getHP() < 1){
	    System.out.println("Victory! *cue victory music*");
	    System.out.println();
	    for(int i=0;i<3;i++){
		System.out.println(player[i].getStats());}
	    System.out.println();
	    System.out.println(opp.getStats());
	}else{
	    System.out.println("Defeat. Better luck next time.");
	    System.out.println();
	    for(int i=0;i<3;i++){
		System.out.println(player[i].getStats());}
	    System.out.println();
	    System.out.println(opp.getStats());
	}
    }