/** * 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 game.Die class, which you wrote for game.game. * See http://msoe.us/taylor/se1011/game.game for the UML diagram and specification. */ package game; import javax.swing.JOptionPane; public class Lab7State011_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, "Welcome to Shootout, press OK to begin."); 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"); while(enemy>=1&&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 */ input=JOptionPane.showInputDialog(null, "Enter 1 for attack, 2 for defense."); if(Integer.parseInt(input)==1){ //player attacks if(enemyAI.roll()==1){ //enemy attacks playerAttacksEnemyAttacks(); } else{ //enemy defends playerAttacksEnemyDefends(); } if(enemy<0) enemy=0; if(health<0) health=0; JOptionPane.showMessageDialog(null, "You have " + health + " health left and\n" + "the enemy has " + enemy + " health left."); } else if(Integer.parseInt(input)==2){ //player defends if(enemyAI.roll()==1){ //enemy attacks playerDefendsEnemyAttacks(); } else{ //enemy defends playerDefendsEnemyDefends(); } if(enemy<0) enemy=0; if(health<0) health=0; JOptionPane.showMessageDialog(null, "You have " + health + " health left and\n" + "the enemy has " + enemy + " health left."); } else{ //invalid entry JOptionPane.showMessageDialog(null, "Please input a valid entry!"); } } if(enemy<=0&&health>0){ //player wins JOptionPane.showMessageDialog(null, "You win!"); } else if(enemy>0&&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!"); rollForWar(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); // this.updateHealth(); identical to next line updateHealth(5,5,1,1); } public void playerAttacksEnemyDefends() { JOptionPane.showMessageDialog(null, "The enemy defends!"); rollForWar(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); updateHealth(2,2,1,0); } public void playerDefendsEnemyAttacks() { JOptionPane.showMessageDialog(null, "The enemy attacks!"); rollForWar(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); updateHealth(1,2,0,1); } public void playerDefendsEnemyDefends() { JOptionPane.showMessageDialog(null, "The enemy defends!"); rollForWar(); JOptionPane.showMessageDialog(null, "The enemy rolls " + enemyRoll + " and you roll a " + playerRoll + "."); updateHealth(1,2,0,1); } private void rollForWar() { enemyRoll=enemyHit.roll(); playerRoll=hitChance.roll(); } public void updateHealth(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