<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@page import="java.sql.*" %> Ye Olde Monster Arena!
Ye Olde Monster Arena

Welcome to Ye Olde Monster Arena! The premise here is simple: after evaluating the statistics of the two randomly generated monsters, decide which you think would win in a battle. Then, select that monster, place your bet, and click the submit button! You start with $100 and can bet between 1 and 100. If you win, you get double your bet back. If you lose, you only lose your original bet.

A couple important notes:

GOOD LUCK!

<%! /***************getNumRecords()****************** *This function gets the total number of records** *in any given table. In essence, it returns the* *highest primary key so that I can use it later** *to generate a random record from the table****** ************************************************/ Integer getNumRecords(Connection con, String theTable) { int numRecords = -1; try { Statement stmt = con.createStatement(); String sql = "SELECT COUNT(*) FROM " + theTable; ResultSet rs = stmt.executeQuery(sql); rs.next(); numRecords = rs.getInt(1); } catch(SQLException e) { numRecords = -1; } //end catch return numRecords; } //end getNumRecords %> <%! /*************generateRandMonster()*************** *Grabs the corresponding monster from the monster* *database and stores it into a variable of the**** *monster class, then returns that variable.******* *************************************************/ class Monster { String name; Integer HP; Integer ATK; Integer DEF; Integer SPD; String special; } Monster generateRandMonster(Connection con, Integer rand) { Monster monster = new Monster(); try { Statement stmt = con.createStatement(); String sql = "SELECT name, hitpoints, attack, defense, speed, special FROM monsters WHERE monstersID = " + rand; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { monster.name = rs.getString("name"); monster.HP = rs.getInt("hitpoints"); monster.ATK = rs.getInt("attack"); monster.DEF = rs.getInt("defense"); monster.SPD = rs.getInt("speed"); monster.special = rs.getString("special"); } //end while } catch(SQLException e) { monster.name = "name"; monster.HP = 0; monster.ATK = 0; monster.DEF = 0; monster.SPD = 0; monster.special = "special"; } //end catch return monster; } %> <%! /*************generateRandWeapon()*************** *Grabs the corresponding weapon from the weapons* *database and stores it into a variable of the*** *weapon class, then returns that variable.******* ************************************************/ class Weapon { String name; Integer ATK; } Weapon generateRandWeapon(Connection con, Integer rand) { Weapon weapon = new Weapon(); try { Statement stmt = con.createStatement(); String sql = "SELECT name, attack FROM weapons WHERE weaponsID = " + rand; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { weapon.name = rs.getString("name"); weapon.ATK = rs.getInt("attack"); } //end while } catch(SQLException e) { weapon.name = "name"; weapon.ATK = 0; } //end catch return weapon; } %> <%! /*************generateRandArmor()*************** *Grabs the corresponding armor from the armor*** *database and stores it into a variable of the** *armor class, then returns that variable.******* ***********************************************/ class Armor { String name; Integer DEF; } Armor generateRandArmor(Connection con, Integer rand) { Armor armor = new Armor(); try { Statement stmt = con.createStatement(); String sql = "SELECT name, defense FROM armor WHERE armorID = " + rand; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { armor.name = rs.getString("name"); armor.DEF = rs.getInt("defense"); } //end while } catch(SQLException e) { armor.name = "name"; armor.DEF = 0; } //end catch return armor; } %> <%! /****************generateRandAccessory()***************** *Grabs the corresponding accessory from the accessories** *database and stores it into a variable of the accessory* *class, then returns that variable.********************** ********************************************************/ class Accessory { String name; Integer HP; Integer ATK; Integer DEF; Integer SPD; } Accessory generateRandAccessory(Connection con, Integer rand) { Accessory accessory = new Accessory(); try { Statement stmt = con.createStatement(); String sql = "SELECT name, hitpoints, attack, defense, speed FROM accessories WHERE accessoriesID = " + rand; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { accessory.name = rs.getString("name"); accessory.HP = rs.getInt("hitpoints"); accessory.ATK = rs.getInt("attack"); accessory.DEF = rs.getInt("defense"); accessory.SPD = rs.getInt("speed"); } //end while } catch(SQLException e) { accessory.name = "name"; accessory.HP = 0; accessory.ATK = 0; accessory.DEF = 0; accessory.SPD = 0; } //end catch return accessory; } %> <% Connection con = null; Class.forName("org.gjt.mm.mysql.Driver"); con = DriverManager.getConnection("jdbc:mysql:///chclarke_db","chclarke", "chclarke"); //Create current player money Integer userCash = null; if (userCash == null) { userCash = 100; } else { userCash = Integer.parseInt(request.getParameter("userCash")); } //Declare and instantiate custom class variables Monster monsterA = new Monster(); Monster monsterB = new Monster(); Weapon weaponA = new Weapon(); Weapon weaponB = new Weapon(); Armor armorA = new Armor(); Armor armorB = new Armor(); Accessory accessoryA = new Accessory(); Accessory accessoryB = new Accessory(); //Bonus variables for equipment String hpBonusA = new String(""); String hpBonusB = new String(""); String atkBonusA = new String(""); String atkBonusB = new String(""); String defBonusA = new String(""); String defBonusB = new String(""); String spdBonusA = new String(""); String spdBonusB = new String(""); Integer totalATKbonusA; Integer totalATKbonusB; Integer totalDEFbonusA; Integer totalDEFbonusB; /***********numRecords[]*********** **0 is number of Monster records*** **1 is number of Weapon records**** **2 is number of Armor records***** **3 is number of Accessory records* **********************************/ int [] numRecords = new int [4]; //get the number of records for each table numRecords[0] = getNumRecords(con, "monsters"); numRecords[1] = getNumRecords(con, "weapons"); numRecords[2] = getNumRecords(con, "armor"); numRecords[3] = getNumRecords(con, "accessories"); /***************Generate random Monsters******************/ int randMonster = 1 + (int)(Math.random() * numRecords[0]); monsterA = generateRandMonster(con, randMonster); randMonster = 1 + (int)(Math.random() * numRecords[0]); monsterB = generateRandMonster(con, randMonster); /***************Generate random Weapons******************/ int randWeapon = 1 + (int)(Math.random() * numRecords[1]); weaponA = generateRandWeapon(con, randWeapon); randWeapon = 1 + (int)(Math.random() * numRecords[1]); weaponB = generateRandWeapon(con, randWeapon); /***************Generate random Armors******************/ int randArmor = 1 + (int)(Math.random() * numRecords[2]); armorA = generateRandArmor(con, randArmor); randArmor = 1 + (int)(Math.random() * numRecords[2]); armorB = generateRandArmor(con, randArmor); /***************Generate random Accessories*****************/ int randAccessory = 1 + (int)(Math.random() * numRecords[3]); accessoryA = generateRandAccessory(con, randAccessory); randAccessory = 1 + (int)(Math.random() * numRecords[3]); accessoryB = generateRandAccessory(con, randAccessory); /***************Determine bonuses*****************/ if (accessoryA.HP > 0) { hpBonusA = "(+"; hpBonusA += accessoryA.HP.toString(); hpBonusA += ")"; } if (accessoryB.HP > 0) { hpBonusB = "(+"; hpBonusB += accessoryB.HP.toString(); hpBonusB += ")"; } if (accessoryA.SPD > 0) { spdBonusA = "(+"; spdBonusA += accessoryA.SPD.toString(); spdBonusA += ")"; } if (accessoryB.SPD > 0) { spdBonusB = "(+"; spdBonusB += accessoryB.SPD.toString(); spdBonusB += ")"; } totalATKbonusA = weaponA.ATK + accessoryA.ATK; totalATKbonusB = weaponB.ATK + accessoryB.ATK; totalDEFbonusA = armorA.DEF + accessoryA.DEF; totalDEFbonusB = armorB.DEF + accessoryB.DEF; atkBonusA = "(+"; atkBonusA += totalATKbonusA.toString(); atkBonusA += ")"; atkBonusB = "(+"; atkBonusB += totalATKbonusB.toString(); atkBonusB += ")"; defBonusA = "(+"; defBonusA += totalDEFbonusA.toString(); defBonusA += ")"; defBonusB = "(+"; defBonusB += totalDEFbonusB.toString(); defBonusB += ")"; //output the data in tabular format out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" "); out.println(""); out.println("
Monster AMonster B
Name" + monsterA.name + "" + monsterB.name + "
HP" + monsterA.HP + " " + hpBonusA + "" + monsterB.HP + " " + hpBonusB + "
ATK" + monsterA.ATK + " " + atkBonusA + "" + monsterB.ATK + " " + atkBonusB + "
DEF" + monsterA.DEF + " " + defBonusA + "" + monsterB.DEF + " " + defBonusB + "
SPD" + monsterA.SPD + " " + spdBonusA + "" + monsterB.SPD + " " + spdBonusB + "
Special" + monsterA.special + "" + monsterB.special + "
Weapon" + weaponA.name + "" + weaponB.name + "
Armor" + armorA.name + "" + armorB.name + "
Accessory" + accessoryA.name + "" + accessoryB.name + "
You bet on:
"); %>
Current cash: <% out.println(userCash);%>
Place your bet (1-100): 


Source Code


SQL Source Code