/** * Author: Josiah Yoder et al. * Class: SE1011-011 * Date: 10/30/13 8:44 AM * Lesson: Week 7, Day 1 * * This program relies on the Die class, which you wrote for Lab7. * See http://msoe.us/taylor/se1011/Lab7 for the UML diagram and specification. */ import javax.swing.JOptionPane; public class Lab7State051_8_3 { int enemy=15; int health=15; int enemyRoll; int playerRoll; String input; Die enemyHit=new Die(100); Die hitChance=new Die(100); Die enemyAI=new Die(2); public void playGame() { JOptionPane.showMessageDialog(null, "RULES:\n" + "Attacking allows for a greater chance to damage the enemy\n" + "(successful hit with attack does 3 additional damage, unsuccessful hit takes 2 additional damage)\n" + "Defending allows for a chance to defend yourself\n" + "(successful hit with defend while opponent is in attack mode does 0 additional damage)\n" + "(unsuccessful defend negates 2 damage from attacker)\n" + "At the end of the turn the entity with the higher roll wins and damage values are added\n" + "Ex: Player defends and gets higher roll, Comp attacks and has lower roll\n" + "-Result---> Player gives 0 damage to Comp for defending + 2 damage for the Comp attacking\n" + "If both are defending the one with the higher roll will do one damage to the other\n" + "If both are attacking and the number is equal, both take 1 damage\n" + "If both are defending and the number is equal, neither take damage\n" + "If one is defending and the number is equal, the attacker takes 1 damage"); JOptionPane.showMessageDialog(null, "Welcome to Shootout, press OK to begin."); while(this.enemy>=1&&this.health>=1){ /* * Attacking allows for a greater chance to damage the enemy * (successful hit with attack does 3 additional damage, unsuccessful hit takes 2 additional damage) * Defending allows for a chance to defend yourself * (successful hit with defend while the opponent is in attack mode does 0 additional damage) * (unsuccessful defend negates 2 damage from attacker) * At the end of the turn the entity with the higher roll wins and damage values are added * Ex: Player defends and gets higher roll, Comp attacks and has lower roll * -Result---> Player gives 0 damage to Comp for defending + 2 damage for the Comp attacking * If both are defending the one with the higher roll will do one damage to the other * If both are attacking and the number is equal, both take 1 damage * If both are defending and the number is equal, neither take damage * If one is defending and the number is equal, the attacker takes 1 damage */ this.input=JOptionPane.showInputDialog(null, "Enter 1 for attack, 2 for defense."); if(Integer.parseInt(this.input)==1){ //player attacks if(this.enemyAI.roll()==1){ //enemy attacks this.playerAttacksEnemyAttacks(); } else{ //enemy defends this.playerAttacksEnemyDefends(); } if(this.enemy<0) this.enemy=0; if(this.health<0) this.health=0; JOptionPane.showMessageDialog(null, "You have " + this.health + " health left and\n" + "the enemy has " + this.enemy + " health left."); } else if(Integer.parseInt(this.input)==2){ //player defends if(this.enemyAI.roll()==1){ //enemy attacks this.playerDefendsEnemyAttacks(); } else{ //enemy defends this.playerDefendsEnemyDefends(); } if(this.enemy<0) this.enemy=0; if(this.health<0) this.health=0; JOptionPane.showMessageDialog(null, "You have " + this.health + " health left and\n" + "the enemy has " + this.enemy + " health left."); } else{ //invalid entry JOptionPane.showMessageDialog(null, "Please input a valid entry!"); } } if(this.enemy<=0&&this.health>0){ //player wins JOptionPane.showMessageDialog(null, "You win!"); } else if(this.enemy>0&&this.health<=0){ //enemy wins JOptionPane.showMessageDialog(null, "You lose!"); } else{ //draw (enemy<=0&&health<=0) JOptionPane.showMessageDialog(null, "It's a draw!"); } } public void playerAttacksEnemyAttacks() { JOptionPane.showMessageDialog(null, "The enemy attacks!"); rollDiceForConflict(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); updateHitPoints(5,5,1,1); } private void rollDiceForConflict() { enemyRoll=enemyHit.roll(); playerRoll=hitChance.roll(); } public void playerAttacksEnemyDefends() { JOptionPane.showMessageDialog(null, "The enemy defends!"); rollDiceForConflict(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); updateHitPoints(2,1,1,0); } public void playerDefendsEnemyAttacks() { JOptionPane.showMessageDialog(null, "The enemy attacks!"); rollDiceForConflict(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); updateHitPoints(1,2,0,1); } public void playerDefendsEnemyDefends() { JOptionPane.showMessageDialog(null, "The enemy defends!"); rollDiceForConflict(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); updateHitPoints(1,1,0,0); } private void updateHitPoints(int healthLoss, int enemyLoss, int tieHealthLoss, int tieEnemyLoss) { if(enemyRoll>playerRoll){ //enemy wins roll health-=healthLoss; JOptionPane.showMessageDialog(null, "The enemy hits you for "+healthLoss+" damage!"); } else if (enemyRoll