%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
Ye Olde Monster Arena!
<%!
/**************calculateBattle()****************
*Calculates the winning monster in the battle***
*and returns a string of A, B, or C where C*****
*means the monsters have tied.******************
***********************************************/
String calculateBattle(Integer monsterAhp,
Integer monsterBhp,
Integer monsterAatk,
Integer monsterBatk,
Integer monsterAdef,
Integer monsterBdef,
Integer monsterAspd,
Integer monsterBspd) {
//calculate total attack power for each monster
Integer totalAatk = (monsterAatk * monsterAspd) - monsterBdef;
Integer totalBatk = (monsterBatk * monsterBspd) - monsterAdef;
//if total attack power is 0 or less, change it to 1
if (totalAatk < 0) {
totalAatk = 1;
}
if (totalBatk < 0) {
totalBatk = 1;
}
//subtract total attack power from respective monster HP until
//either one or both of them are 0 or less
do {
monsterAhp -= totalBatk;
monsterBhp -= totalAatk;
} while (monsterAhp > 0 && monsterBhp > 0);
if (monsterAhp > 0 && monsterBhp < 0) {
return "A";
} else
if (monsterAhp < 0 && monsterBhp > 0) {
return "B";
} else {
return "C";
}
}
%>
<%
/********************Retrieve data from previous page***********************/
String monsterAname = request.getParameter("monsterAname");
String monsterBname = request.getParameter("monsterBname");
Integer monsterAhp = Integer.parseInt(request.getParameter("monsterAhp"));
Integer monsterBhp = Integer.parseInt(request.getParameter("monsterBhp"));
Integer monsterAatk = Integer.parseInt(request.getParameter("monsterAatk"));
Integer monsterBatk = Integer.parseInt(request.getParameter("monsterBatk"));
Integer monsterAdef = Integer.parseInt(request.getParameter("monsterAdef"));
Integer monsterBdef = Integer.parseInt(request.getParameter("monsterBdef"));
Integer monsterAspd = Integer.parseInt(request.getParameter("monsterAspd"));
Integer monsterBspd = Integer.parseInt(request.getParameter("monsterBspd"));
Integer userCash = Integer.parseInt(request.getParameter("userCash"));
Integer userBet = Integer.parseInt(request.getParameter("txtBet"));
String userChoice = request.getParameter("userChoice");
String output = "";
Integer netWinnings;
String cssColor = "";
/****calls calculateBattle() and stores the result of the fight****/
String fightResult = calculateBattle(monsterAhp,
monsterBhp,
monsterAatk,
monsterBatk,
monsterAdef,
monsterBdef,
monsterAspd,
monsterBspd);
/****determines win, tie, or loss and calculates****
*****the net winnings******************************/
if (userChoice.equals(fightResult)) {
output = "You won!";
cssColor = "green";
netWinnings = 2 * userBet;
} else
if (!userChoice.equals(fightResult) && fightResult != "C") {
output = "Sorry, you lost!";
cssColor = "red";
netWinnings = -1 * userBet;
} else {
output = "The monsters have tied!";
netWinnings = userBet;
}
//calculate user cash after battle outcome
userCash += netWinnings;
%>