Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ferret Code
07-27-2010, 09:30 PM,
Post: #1
Ferret Code
This is for coding practice, so that I can easily show the raw code.

Code:
<?php

$save;
echo "<form action='textbox.php' method='post'>
<textarea type='text' name='type' /></textarea><br />
<input type='submit' value='post'><br />";
$save = $_POST['type'];
mysql_connect("localhost", "Jordan", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query("INSERT INTO text (saved) VALUES('$save')") or die(mysql_error());
mysql_query("SELECT * FROM text WHERE saved=$save");

?>
Reply
07-27-2010, 09:58 PM,
Post: #2
Re: Ferret Code
Code:
<form action='textbox.php' method='post'>
<textarea type='text' name='type' /><?php
$handle = mysql_query("SELECT saved FROM text WHERE saved=$save");
$row = mysql_fetch_array($handle);
$saved = $row[0];
echo $saved;
?></textarea><br />
<input type='submit' value='post'><br />
<?php
$save = $_POST['type'];
if (!$save) exit;
mysql_connect("localhost", "Jordan", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

mysql_query("REPLACE INTO text (saved) VALUES('$save')") or die(mysql_error());
?>
Reply
07-27-2010, 10:19 PM,
Post: #3
Re: Ferret Code
Code:
<?php

echo "<form action='textbox.php?action=test' method='post'>
<textarea type='text' name='type'> </textarea><br /> ";
$save = $_POST['type'];
mysql_connect("localhost", "Jordan", "password") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
mysql_query("INSERT INTO text (saved) VALUES('$save')") or die(mysql_error());
$handle = mysql_query('SELECT saved FROM text WHERE saved="'.$save.'"');
$row = mysql_fetch_array($handle);
$saved = $row[0];
echo $saved;

?>

<input type='submit' value='post'><br />

<?php

if (!$save) exit;
mysql_query("REPLACE INTO text (saved) VALUES('$save')") or die(mysql_error());

?>
Reply
08-09-2010, 01:00 AM,
Post: #4
Re: Ferret Code
Calculator!!!

Code:
<?php

echo "<form action='calc.php?action=calculate' method='post'>
<input type='text' name='numone' /><br /><br />
<input type='text' name='sign' /><br /><br />
<input type='text' name='numtwo' />
<input type='submit' value='enter' />
</form>";

if ($action == "calculate")
{
    $num1 = $_POST['numone'];
    $sign = $_POST['sign'];
    $num2 = $_POST['numtwo'];
    if ($sign == "+")
    {
        $equal = $num1+$num2;
        echo $num1 . $sign . $num2 . "= " . $equal"<br />";
        echo "<a href='calculator.php'>Go back</a>";
    }
    if ($sign == "-")
    {
        $equal = $num1-$num2;
        echo $num1 . $sign . $num2 . "= " . $equal;
        echo "<br /><a href='calculator.php'>Go back</a>";
    }
    if ($sign == "/")
    {    
        $equal = $num1/$num2;
        echo $num1 . $sign . $num2 . "= " . $equal;
        echo "<br /><a href='calculator.php'>Go back</a>";
    }
    if ($sign == "*")
    {
        $equal = $num1*$num2;
        echo $num1 . $sign . $num2 . "= " . $equal;
        echo "<br /><a href='calculator.php'>Go back</a>";
    }
}

?>
Reply
10-25-2010, 02:41 PM,
Post: #5
Re: Ferret Code
Monster testing program with Java

Code:
import java.util.*;
/***************************************************************************
**This class takes functions to make a battle simulator work, using inputs**
***************************************************************************/
public class HW7
{
    public void help() //Prints out all the commands you can use in the program, and what they do
    {
        System.out.println("quit: stops program");
        System.out.println("random: if decided after prompt, random values will be set");
        System.out.println("playerHP: sets players health");
        System.out.println("playerDamage: sets players damage");
        System.out.println("monsterHP: sets monsters health");
        System.out.println("monsterDamage: sets monsters damage");
        System.out.println("battle: engages battle");
    }
    /**
    *@return hp to main function
    **/
    public int playerHP()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter players health>");
        int hp = scan.nextInt(); //variable to take in the value
        return hp;
    }
    /**
    *@return pdamage to main function
    **/
    public int playerDamage()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter players damage value>");
        int pdamage = scan.nextInt(); //variable to take in the value
        return pdamage;
    }
    /**
    *@return mHP to main function
    **/
    public int monsterHP()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter monsters health>");
        int mHP = scan.nextInt(); //variable to take in value
        return mHP;
    }
    /**
    *@return mdamage to main function
    **/
    public int monsterDamage()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter monsters damage value>");
        int mdamage = scan.nextInt(); //variable to hold value
        return mdamage;
    }
    /**
    *@param playerHP the players health
    *@param playerDamage the players damage value
    *@param monsterHP the monsters health
    *@param monsterDamage the monsters damage value
    *@return hp to main function
    **/
    public void battle(int playerHP, int playerDamage, int monsterHP, int monsterDamage)
    {
        int counter = 0; //keeps track of rounds
        while(playerHP > 0 || monsterHP > 0)
        {
            playerHP = playerHP - monsterDamage;
            monsterHP = monsterHP - playerDamage;
            counter++;
            if(playerHP < 0 && monsterHP < 0)
            {
                System.out.println("Player and monster dead on round : "+counter);
                break;
            }
            else if(playerHP <= 0)
            {
                System.out.println("Player dead on round: "+counter);
                System.out.println("Monster health: "+monsterHP);
                break;
            }
            else if(monsterHP <= 0)
            {
                System.out.println("Monster dead on round: "+counter);
                System.out.println("Player health: "+playerHP);
                break;
            }
        }
    }
    public static void main(String[] args)
    {
        System.out.println("Welcome to the interactive battle simulator, type help for a list of commands>");
        Random rand = new Random(); //declares a random variable
        Scanner scan = new Scanner(System.in); //declares the scanner class
        HW7 function = new HW7(); //function for the file
        String random = " "; //variable for the random prompt
        int playerHP = 0; //delcaration of players health
        int playerDamage = 0; //delcaration of players damage value
        int monsterHP = 0; //delcaration of monsters health
        int monsterDamage = 0; //delcaration of monsters damage value
        System.out.println("Do you want random values? Yes/No");
        String answer = scan.nextLine(); //variable to check if random = "random"
        if(answer.equals("yes"))
        {
            random = "random";
        }
        System.out.println("Enter command>");
        String command = scan.nextLine(); //command for the while loop to keep going
        while(!command.equals("quit"))
        {
            if(command.equals("random"))
            {
                random = "random";
            }
            if(command.equals("help"))
            {
                function.help();
            }
            if(command.equals("playerHP"))
            {
                if(random.equals("random"))
                {
                    playerHP = rand.nextInt(9999);
                }
                else
                {
                    playerHP = function.playerHP();
                }
            }
            if(command.equals("playerDamage"))
            {
                if(random.equals("random"))
                {
                    playerDamage = rand.nextInt(9999);
                }
                else
                {
                    playerDamage = function.playerDamage();
                }
            }
            if(command.equals("monsterHP"))
            {
                if(random.equals("random"))
                {
                    monsterHP = rand.nextInt(9999);
                }
                else
                {
                    monsterHP = function.monsterHP();
                }
            }
            if(command.equals("monsterDamage"))
            {
                if(random.equals("random"))
                {
                    monsterDamage = rand.nextInt(9999);
                }
                else
                {
                    monsterDamage = function.monsterDamage();
                }
            }
            if(command.equals("battle"))
            {
                if(playerHP == 0 || playerDamage == 0 || monsterHP == 0 || monsterDamage == 0)
                {
                    System.out.println("Values not set");
                }
                else
                {
                    function.battle(playerHP,playerDamage,monsterHP,monsterDamage);
                }
            }    
            System.out.println("Enter command>");
            command = scan.nextLine();
        }
    }
}
Reply
11-03-2010, 08:47 PM,
Post: #6
Re: Ferret Code
Army battle simluator

Code:
import java.util.*;
public class HW8
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        HW8 function = new HW8();
        Random rand = new Random();
        
        int side = 1;
        int size = 0;
        int low = 0;
        int high = 0;
        double lowarmor = 0;
        boolean check1 = false;
        boolean check2 = false;
        
        int[] army1 = new int[size];
        int[] army2 = new int[size];
        int[] damage1 = new int[size];
        int[] damage2 = new int[size];
        double[] armor1 = new double[size];
        double[] armor2 = new double[size];
        
        System.out.println("Enter command>");
        String command = " ";
        command = scan.nextLine();
        while(!command.equals("quit"))
        {
            if (command.equals("help"))
            {
                System.out.println("Commands: help/size/side/hp/damage/armor/battle>");
            }
            else if (command.equals("side"))
            {
                System.out.println("Choose side> 1/2 (default 1)");
                side = scan.nextInt();
            }
            else if (command.equals("size"))
            {
                if (side == 1)
                {
                    System.out.println("Set army1 size>");
                    size = scan.nextInt();
                    army1 = new int[size];
                }
                else if (side == 2)
                {
                    System.out.println("Set army2 size>");
                    size = scan.nextInt();
                    army2 = new int[size];
                }
                else
                {
                    System.out.println("Side must be either 1 or 2>");
                }
            }
            else if (command.equals("hp"))
            {
                if (side == 1)
                {
                    System.out.println("Enter lower bounds>");
                    low = scan.nextInt();
                    System.out.println("Enter higher bounds>");
                    high = scan.nextInt();
                    for (int i = 0; i<army1.length; i++)
                    {
                        army1[i] = function.hp(low, high);
                    }
                }
                else if (side == 2)
                {
                    System.out.println("Enter lower bounds>");
                    low = scan.nextInt();
                    System.out.println("Enter higher bounds>");
                    high = scan.nextInt();
                    for (int i = 0; i<army2.length; i++)
                    {
                        army2[i] = function.hp(low, high);
                    }
                }
                else
                {
                    System.out.println("Side must be either 1 or 2>");
                }
            }
            else if (command.equals("damage"))
            {
                if (side == 1)
                {
                    damage1 = new int[army1.length];
                    System.out.println("Enter low damage bounds>");
                    low = scan.nextInt();
                    System.out.println("Enter high damage bouds>");
                    high = scan.nextInt();
                    for (int i = 0; i<damage1.length; i++)
                    {
                        damage1[i] = function.damage(low,high);
                    }
                }
                else if (side == 2)
                {
                    damage2 = new int[army2.length];
                    System.out.println("Enter low damage bounds>");
                    low = scan.nextInt();
                    System.out.println("Enter high damage bouds>");
                    high = scan.nextInt();
                    for (int i = 0; i<damage2.length; i++)
                    {
                        damage2[i] = function.damage(low,high);
                    }
                }
                else
                {
                    System.out.println("Side must be either 1 or 2>");
                }
            }
            else if (command.equals("armor"))
            {
                if (side == 1)
                {
                    armor1 = new double[army1.length];
                    System.out.println("Enter low damage bounds>");
                    lowarmor = scan.nextDouble();
                    for (int i = 0; i<armor1.length; i++)
                    {
                        armor1[i] = function.armor(lowarmor);
                    }
                }
                else if (side == 2)
                {
                    armor2 = new double[army2.length];
                    System.out.println("Enter low damage bounds>");
                    lowarmor = scan.nextDouble();
                    for (int i = 0; i<armor2.length; i++)
                    {
                        armor2[i] = function.armor(lowarmor);
                    }
                }
                else
                {
                    System.out.println("Side must be either 1 or 2>");
                }
            }
            else if (command.equals("battle"))
            {
                    function.battle(army1,army2,damage1,damage2,armor1,armor2);
            }
            System.out.println("Enter command>");
            command = scan.nextLine();
        }
    }
    public int hp(int low, int high)
    {
        int hp;
        Random rand = new Random();
        hp = rand.nextInt(high);
        hp += low;
        return hp;
    }
    public int damage(int low, int high)
    {
        int damage;
        Random rand = new Random();
        damage = rand.nextInt(high);
        damage += low;
        return damage;
    }
    public double armor(double lowarmor)
    {
        double armor;
        double low2 = lowarmor;
        Random rand = new Random();
        armor = rand.nextDouble();
        armor += low2;
        return armor;
    }
    public void battle(int army1[], int army2[], int damage1[], int damage2[], double armor1[], double armor2[])
    {
        Scanner scan = new Scanner(System.in);
        int[] temp = new int[army1.length];
        int[] temp2 = new int[army2.length];
        for (int i = 0; i<army1.length; i++)
        {
            temp[i] = army1[i];
        }
        for (int i = 0; i<army2.length; i++)
        {
            temp2[i] = army2[i];
        }
        System.out.println("Enter rounds of fights>");
        int round = scan.nextInt();
        int count = 0;
        int count2 = 0;
        for (int i = 0; i<round; i++)
        {
            for (int x = 0; x<temp.length; x++)
            {
                temp[x] -= damage2[x]-(armor2[x]*damage2[x]);
                if (temp[x] <= 0)
                {
                    count++;
                }
            }
            for (int x = 0; x<temp2.length; x++)
            {
                temp2[x] -= damage1[x]-(armor1[x]*damage1[x]);
                if (temp2[x] <= 0)
                {
                    count2++;
                }
            }
        }
        System.out.println("==========Battle Results Are In!==========");
        System.out.println("Side 1 lost: "+count+" warriors");
        System.out.println("Side 2 lost: "+count2+" warriors");
        if (count > count2)
        {
            System.out.println("Army 2 wins");
        }
        if (count == count2)
        {
            System.out.println("Battle is a draw");
        }
        if (count < count2)
        {
            System.out.println("Army 1 wins");
        }
    }
}
Reply
11-15-2010, 02:28 AM,
Post: #7
Re: Ferret Code
Code:
import java.util.*;
public class lab10
{
    class item
    {
        public int price;
        public String name;
        public boolean status;
        public item()
        {
            price = 0;
            name = " ";
            status = false;
        }
    }
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        item[] store = new item[100];
        lab10 function = new lab10();
        int tally = 0;
        int solditems = 0;
        
        System.out.println("Type a command>");
        String command = scan.nextLine();
        int i = 0;
        int items = 0;
        
        while (!command.equals("quit"))
        {
            if (command.equals("addItem"))
            {
                store[i]=function.addItem(i,store);
                i++;
                items++;
            }
            else if (command.equals("sellItem"))
            {
                tally = function.sellItem(tally, solditems,items,store);
                solditems++;
            }
            else if (command.equals("help"))
            {
                System.out.println("Commands are: addItem/sellItem");
            }
        command = scan.nextLine();
        }
        System.out.println("Store has sold "+solditems+" items");
        System.out.println("Store has made "+tally+ " dollars");
    }
    public item addItem(int i,item[] store)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Item name>");
        store[i] = new item();
        store[i].name=scan.nextLine();
        System.out.println("Item price>");
        store[i].price=scan.nextInt();
        store[i].status = false;
        return store[i];
    }
    public int sellItem(int tally, int solditems, int items, item[] store)
    {
        Random rand = new Random();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter name of item to sell>");
        String sell = scan.nextLine();
        int i = 0;
        int random = 0;
        while (i<items)
        {
            if (sell.equals(store[i].name))
            {
                if(store[i].status == true)
                {
                    System.out.println("Item is already sold");
                }
                else
                {
                    random = rand.nextInt(items);
                    if (random > items*.8)
                    {
                        System.out.println(store[i].name+" has been sold 30% off");
                        store[i].status = true;
                        tally += store[i].price*.7;
                    }
                    else
                    {
                        System.out.println(store[i].name+" has been sold");
                        store[i].status = true;
                        tally += store[i].price;
                    }
                }
            }
            i++;
        }
        return tally;
    }
}
Reply
11-18-2010, 12:50 AM,
Post: #8
Re: Ferret Code
Code:
import java.util.*;
/**
*Class to take in objects for target practice using an array of objects
**/
public class HW9
{
    /**
    *Class object to create a target
    **/
    public class target
    {
        public int x; //x coordinate
        public int y; //y coordinate
        public int length; //length of square
        /**
        *@param a to set the x variable
        *@param b to set the y variable
        *@param square to set the length of square
        *Function to set values to the target
        **/
        public target(int a, int b, int square)
        {
            this.x = a;
            this.y = b;
            this.length = square;
        }
    }
    /**
    *Main function
    **/
    public static void main(String[] args)
    {
        target[] shoot = new target[100]; //array of objects
        Scanner scan = new Scanner(System.in); //scanner class
        HW9 function = new HW9(); //function declaration
        
        System.out.println("Enter command");
        String command = scan.nextLine(); //command string to take in a command
        int targetcount    = 0; //number of targets
        int i = 0; //tracks position of target array
        int score = 0; //score of the game
        while (!command.equals("quit"))
        {
            if (command.equals("addTarget"))
            {
                shoot[i] = function.addTarget(shoot,i,scan);
                targetcount++;
                i++;
            }
            else if (command.equals("moveTarget"))
            {
                function.moveTarget(shoot,scan);
            }
            else if (command.equals("shootTarget"))
            {
                score = function.shootTarget(shoot,scan,score);
            }
            else if (command.equals("score"))
            {
                System.out.println(score);
            }
            System.out.println("Enter command");
            command = scan.nextLine();
        }
    }
    /**
    *@param shoot the array of objects
    *@param i the tombstone for the array
    *@param scan the scanner class
    *@return shoot returns to the main
    *This function adds a target using new target() method
    **/
    public target addTarget(target[] shoot, int i, Scanner scan)
    {
        System.out.println("Enter x coordinates");
        int x = scan.nextInt(); //x coordinate
        System.out.println("Enter y coordinates");
        int y = scan.nextInt(); //y coordinate
        System.out.println("Enter length of target");
        int length = scan.nextInt(); //length of square
        shoot[i] = new target(x,y,length);
        return shoot[i];
    }
    /**
    *@param shoot the array of objects
    *@param scan the scanner class
    *This function moves an existing target
    **/
    public void moveTarget(target[] shoot, Scanner scan)
    {
        System.out.println("Coordinates of what target to move");
        int oldx = scan.nextInt(); //x coordinate of object
        int oldy = scan.nextInt(); //y coordinate of object
        for (int i = 0; i<shoot.length; i++)
        {
            if (oldx == shoot[i].x && oldy == shoot[i].y)
            {
                System.out.println("Enter new coordinates");
                shoot[i].x = scan.nextInt();
                shoot[i].y = scan.nextInt();
            }
            else
            {
                System.out.println("Wrong coordinates");
            }
        }
    }
    /**
    *@param shoot the array of objects
    *@param score the score
    *@param scan the scanner class
    *@return score returns to the main
    *This function asks for coordinates to shoot a target, and returns score if hit is made
    **/
    public int shootTarget(target[] shoot, Scanner scan, int score)
    {
        System.out.println("Enter coordinates to shoot");
        int x = scan.nextInt(); //x coord to shoot
        int y = scan.nextInt(); //y coord to shoot
        for (int a = 0; a<shoot.length; a++)
        {
            if (x == shoot[a].x && y == shoot[a].y)
            {
                System.out.println("Target hit!");
                score++;
            }
            else
            {
                System.out.println("Target missed!");
            }
        }
        return score;
    }
}
Reply
12-02-2010, 04:17 PM,
Post: #9
Re: Ferret Code
Pokemon battle simulator
Code:
import java.util.*;
/**
*The purpose of this program is to create a battle simulation using objects and association
**/
public class HW10    
{
    /**
    *This class is used to declare the trainer object
    **/
    class Trainer
    {
        public String name; //trainers name
        public int nummonsters; //number of monsters the trainer has
        public Monster[] pokemon; //creates the monsters using the function below
        /**
        *This function is used to give values to the trainer and to create the monsters
        *@param a the string to import in the trainer name
        *@param num the int for the number of monsters
        **/
        public Trainer(String a, int num)
        {
            this.name = a;
            this.nummonsters = num;
            pokemon = new Monster[num];
        }
    }
    /**
    *This class is used to create the actual monster values
    **/
    class Monster
    {
        public String name; //monster name
        public String element; //element of attack
        public int hp; //health points
        public int damage; //damage dealt
        /**
        *This function is used to give the monster values
        *@param a the monsters name
        *@param b the monsters element
        *@param x the monsters hp
        *@param y the monsters damage value
        **/
        public Monster(String a, String b, int x, int y)
        {
            this.name = a;
            this.element = b;
            this.hp = x;
            this.damage = y;
        }
    }
    /**
    *The main function
    **/
    public static void main(String[] args)
    {
        HW10 function = new HW10(); //declares the function
        Scanner scan = new Scanner(System.in); //declares scanner class
        Trainer[] player = new Trainer[100]; //declares array of Trainer objects
    
        System.out.println("Enter command");    
        String command = scan.nextLine(); //command for the while loop
        int i = 0; //counter to pass into the variables
        while (!command.equals("quit"))
        {
            if (command.equals("addTrainer"))
            {
                function.addTrainer(player,scan,i);
                i++;
            }
            else if (command.equals("battle"))
            {
                function.battle(player,scan,i,function);
            }
            System.out.println("Enter command");
            command = scan.nextLine();
        }
    }
    /**
    *Function to add the trainer when called
    *@param player the trainer class
    *@param scan the scanner class
    *@param i the counter variable
    **/
    public void addTrainer(Trainer[] player, Scanner scan, int i)
    {
        System.out.println("Enter name and number of monsters");
        String input = scan.nextLine(); //user input for name and number
        String name = input.substring(0, input.indexOf(' ')); //substringed name
        int monsters = Integer.valueOf(input.substring(input.indexOf(' ')+1, input.length())); //substringed number
        player[i] = new Trainer(name, monsters);
        player[i].pokemon = new Monster[player[i].nummonsters];
        for (int a = 0; a<player[i].nummonsters; a++)
        {
            System.out.println("Enter monster name, element, hp and damage");
            String mondetail = scan.nextLine(); //one line input for the monsters values
            Scanner x = new Scanner(mondetail); //scanner for the monster detail
            String monname = x.next(); //monster name
            String monel = x.next(); //monster element
            int monHP = x.nextInt(); //monster hp
            int monDam = x.nextInt(); //monster damage
            player[i].pokemon[a] = new Monster(monname,monel,monHP,monDam);
        }
    }
    /**
    *Function to battle trainers
    *@param player the trainer class
    *@param scan the scanner class
    *@param i the counter variable
    *@param function the function delcaration
    **/
    public void battle(Trainer[] player, Scanner scan, int i, HW10 function)
    {
        System.out.println("What trainers will battle?");
        String battle = scan.nextLine(); //input for which trainers will fight
        Scanner m = new Scanner(battle); //scanner for battle
        String t1 = m.next(); //trainer one
        String t2 = m.next(); //trainer two
        int hp1 = 0; //temp hp value
        int hp2 = 0; //temp hp value
        double dam1 = 0; //temp damage value
        double dam2 = 0; //temp damage value
        boolean out = true; //boolean check if monster is alive and out
        boolean out2 = true; //check for second monster
        int check = 0; //checks for monsters left to fight
        int y = 0; //pokemon tracker1
        int f = 0; //pokemon tracker2
        int z = 0; //trainer tracker
        int x = 0; //trainer tracker2
        System.out.println(t1+" is fighting "+t2);
        for (int a = 0; a<i; a++)
        {
            if (player[a].name.equals(t1))
            {
                z = a;
            }
        }
        for (int b = 0; b<i; b++)
        {
            if (player[b].name.equals(t2) )
            {
                x = b;
            }
        }
        while (y < player[z].nummonsters && f < player[x].nummonsters)
        {
            System.out.println(player[z].pokemon[y].name+" is fighting "+player[x].pokemon[f].name);
            
            hp1 = player[z].pokemon[y].hp;
            dam1 = player[z].pokemon[y].damage;
            hp2 = player[x].pokemon[f].hp;
            dam2 = player[x].pokemon[f].damage;
            int elnum = function.element(player,z,x,y,f); //determines element advantage
            if (elnum == 1)
            {
                dam1 = dam1*1.3;
            }
            else if (elnum == 2)
            {
                dam2 = dam2*1.3;
            }
            while (hp1 > 0 && hp2 >0)
            {
                hp1 -= dam2;
                hp2 -= dam1;
            }
            if (hp1 < 1 && hp2 > 1)
            {
                System.out.println(player[x].pokemon[f].name+" won");
                out = true;
                out2 = false;
            }
            else if (hp2 < 1 && hp1 > 1)
            {
                System.out.println(player[z].pokemon[y].name+" won");
                out = false;
                out2 = true;
            }
            else if (hp2 < 1 && hp1 < 1)
            {
                System.out.println("*DRAW*");
                out = false;
                out2 = false;
            }
            if (out == true)
            {
                y++;
                check = player[z].nummonsters;
                if (y  < check)
                {
                    System.out.println(player[z].name+" is sending out "+player[z].pokemon[y].name);
                }
                else if (check <= y)
                {
                    System.out.println(player[x].name+" has won!");
                }
            }
             if (out2 == true)
            {
                f++;
                check = player[x].nummonsters;
                if (f < check)
                {
                    System.out.println(player[x].name+" is sending out "+player[x].pokemon[f].name);
                }
                else if (f >= check)
                {
                    System.out.println(player[z].name+" has won!");
                }
            }
        }
    }
    /**
    *Function for comparing elements and returning a number to determine advantage in battle
    *@param player the Trainer
    *@param z the Trainer array index
    *@param x the Trainer array index(2)
    *@param y the pokemon z index
    *@param f the pokemon x index
    *@return 1,2,0 returns a number based on the element check
    **/
    public int element(Trainer[] player, int z, int x, int y, int f)
    {
        if (player[z].pokemon[y].element.equals("fire") && player[x].pokemon[f].element.equals("water"))
        {
            return 1;
        }
        else if (player[z].pokemon[y].element.equals("fire") && player[x].pokemon[f].element.equals("earth"))
        {
            return 2;
        }
        else if (player[z].pokemon[y].element.equals("water") && player[x].pokemon[f].element.equals("earth"))
        {
            return 1;
        }
        else if (player[z].pokemon[y].element.equals("water") && player[x].pokemon[f].element.equals("fire"))
        {
            return 2;
        }
        else if (player[z].pokemon[y].element.equals("earth") && player[x].pokemon[f].element.equals("fire"))
        {
            return 1;
        }
        else if (player[z].pokemon[y].element.equals("earth") && player[x].pokemon[f].element.equals("water"))
        {
            return 2;
        }
        else
        {
            return 0;
        }
    }
}
Reply
12-30-2010, 07:34 AM,
Post: #10
Re: Ferret Code
You've got a lot of time on your hands obviously. but great job on the php. clean code might i add. do you wright the code??. maybe put more comments into the code when writing so us small time programmers have a better idea of what were looking at for alteration :p
Reply
06-29-2011, 07:25 PM,
Post: #11
RE: Ferret Code
(12-30-2010, 07:34 AM)ScaleBlade Wrote: You've got a lot of time on your hands obviously. but great job on the php. clean code might i add. do you wright the code??. maybe put more comments into the code when writing so us small time programmers have a better idea of what were looking at for alteration :p

There is enough documentation for a novice coder to understand how and what is going on (It was commented up to par with JavaDoc standards)

However I'd be more than happy to explain what you don't understand.
Reply
06-17-2012, 08:40 PM,
Post: #12
RE: Ferret Code
Here's a small hospital administrative panel I wrote for practice..

Code:
<?php
include "class.mysql_db.php";
error_reporting(E_ALL ^ E_NOTICE);
$mysql_db = new mysql_db();
function viewEmps() {
    global $mysql_db;
    echo "<br /><a href='".$_SERVER['PHP_SELF']."'>Return to main page</a>";
    $emps = $mysql_db->query('SELECT * FROM employee ORDER BY rank asc');
    echo "<div style='text-align:center'><h3>Employee List</h3>";
    echo "<table border=1 align='center'><th>ID#</th><th>Name</th><th>Rank</th><tr>";
    while ($e = mysql_fetch_object($emps)) {
        echo "<td>".$e->eid."</td>";
        echo "<td>".$e->name."</td>";
        echo "<td>".$e->rank."</td></tr>";
    }
    echo "</table></div>";
    echo "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function addPatient($name=false,$address=false,$pay_method=NULL,$cc_num=NULL) {
    global $mysql_db;
    if ($name == false || $address == false) {
        echo "Patient must have a valid name and address.";
        exit;
    }
    $mysql_db->query('INSERT INTO patient (name,address,payment_type,cc_num)
        VALUES ("'.$name.'","'.$address.'","'.$payment_type.'","'.$cc_num.'")');
    echo $name." has been added to the database!";
    echo "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function addEmployee($name=false,$rank=false) {
    global $mysql_db;
    if ($name == false || $rank == false) {
        echo "Field not completely filled out.";
        exit;
    }
    $mysql_db->query("INSERT INTO employee (name,rank) VALUES('".$name."','".$rank."')") or die(mysql_error());
    echo "Employee successfully added to database. Name: ".$name.", Rank: ".$rank;
    echo "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function addEmp() {
    ?>
    <h4>Add an employee!</h4>
    <form action="<?php $_SERVER['PHP_SELF']?>?action=addEmployee" method="POST">
        <input type="text"
            name="name"
            value="Employee Name"
            onclick="if(this.value == 'Employee Name'){this.value = ''}"
            onblur="if(this.value == ''){this.value = 'Employee Name'}" />
        <select name="rank">
            <option value="Doctor">Doctor</option>
            <option value="Nurse">Nurse</option>
        </select>
        <input type="submit"
            value="Add Employee" />
    </form>
    <?php
    echo "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function addPat() {
    ?>
    <h4>Add a patient!</h4>
    <form action="<?php $_SERVER['PHP_SELF']?>?action=addPatient" method="POST">
        <input type="text"
            name="name"
            value="Patient Name"
            onclick="if(this.value == 'Patient Name') {this.value = ''}"
            onblur="if(this.value == ''){this.value = 'Patient Name'}" />
        <input type="text"
            name="address"
            value="Patient Address"
            onclick="if(this.value == 'Patient Address') {this.value = ''}"
            onblur="if(this.value == ''){this.value = 'Patient Address'}" />
        <input type="text"
            name="paytype"
            value="Pay Method Preferred"
            onclick="if(this.value == 'Pay Method Preferred') {this.value = ''}"
            onblur="if(this.value == ''){this.value = 'Pay Method Preferred'}" />
        <input type="text"
            name="ccnum"
            value="Credit Card Number" />
        <input type="submit"
            value="Add Patient" />
    <?php
    echo "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function viewPats() {
    global $mysql_db;
    $patients = $mysql_db->query('SELECT * FROM patient ORDER BY name ASC');
    echo "<div style='text-align:center'><h3>Patient List</h3>";
    echo "<table border=1 align='center'>
        <th>ID#</th>
        <th>Name</th>
        <th>Address</th>
        <th>Balance Due</th>
        <th>Preffered Pay Method</th>
        <th>Credit Card Number</th>
        <th>Staff Notes</th>
        <tr>";
    while ($p = mysql_fetch_object($patients)) {
        echo "<td>".$p->pid."</td>";
        echo "<td>".$p->name."</td>";
        echo "<td>".$p->address."</td>";
        echo "<td>".$p->balance_due."</td>";
        echo "<td>".$p->payment_type."</td>";
        echo "<td>".$p->cc_num."</td>";
        echo "<td>".$p->staff_notes."</td></tr>";
    }
    echo "</table></div>";
    echo "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function createAppointmentLog() {
    global $mysql_db;
    $pnames = $mysql_db->query('SELECT * FROM patient ORDER BY name ASC');
    $dnames = $mysql_db->query('SELECT * FROM employee WHERE rank="Doctor" ORDER BY name ASC');
    $nnames = $mysql_db->query('SELECT * FROM employee WHERE rank="Nurse" ORDER BY name ASC');
    ?>
    <h4>Add an appointment!</h4>
    <form action="<?php $_SERVER['PHP_SELF']?>?action=addLog" method="POST">
        <select name="pname">
        <?php
        echo "<option value='0'> -- Select Patient -- </option>";
        while ($p = mysql_fetch_object($pnames)) {
            echo "<option value=".$p->pid.">".$p->name."</option>";
        }
        ?>
        </select>
        <br />
        <select name="dname">
        <?php
        echo "<option value='0'> -- Select Doctor -- </option>";
        while ($d = mysql_fetch_object($dnames)) {
            echo "<option value=".$d->eid.">".$d->name."</option>";
        }
        ?>
        </select>
        <br />
        <select name="nname">
        <?php
        echo "<option value='0'> -- Select Nurse -- </option>";
        while ($n = mysql_fetch_object($nnames)) {
            echo "<option value=".$n->eid.">".$n->name."</option>";
        }
        ?>
        </select>
        <br />
        <textarea
            rows="10"
            cols="35"
            name="notes">
        </textarea> Enter Record Notes
        <input type="submit"
            value="Add Log" />
    </form>
    <?php
    echo "<a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function addLog($patient=false, $doctor=false, $nurse=false, $notes=NULL) {
    global $mysql_db;
    if ($patient == false || $doctor == false || $nurse==false) {
        echo "A record must contain a patient, doctor, and nurse.";
        exit;
    }
    if ($notes == '') { $notes=NULL; }
    $mysql_db->query('INSERT INTO records (pid,eid,notes,nid) VALUES("'.$patient.'","'.$doctor.'","'.$notes.'","'.$nurse.'")') or die(mysql_error());
    echo "Record successfully entered";
    echo "<br /><br /><a href='".$_SERVER['PHP_SELF']."'>Return to Admin Panel</a>";
}
function mainPage() {
    global $mysql_db;
    echo "<h1 style='text-align:center'>Welcome to the administrative panel!</h1>";
    echo "-<a href='".$_SERVER['PHP_SELF']."?action=viewEmps'>View all employees</a>";
    echo "<br />";
    echo "-<a href='".$_SERVER['PHP_SELF']."?action=viewPatients'>View all patients</a>";
    echo "<br />";
    echo "-<a href='".$_SERVER['PHP_SELF']."?action=createAppointmentLog'>Create an appointment log</a>";
    echo "<br />";
    echo "-<a href='".$_SERVER['PHP_SELF']."?action=addEmp'>Add an employee</a>";
    echo "<br />";
    echo "-<a href='".$_SERVER['PHP_SELF']."?action=addPat'>Add a patient</a>";
}

if (!$_POST) {
    mainPage();
}
if ($_GET['action'] == 'addEmployee') {
    $name = mysql_real_escape_string($_POST['name']);
    $rank = mysql_real_escape_string($_POST['rank']);
    addEmployee($name, $rank);
}
if ($_GET['action'] == 'addPatient') {
    $name = mysql_real_escape_string($_POST['name']);
    $address = mysql_real_escape_string($_POST['address']);
    $pay_method = mysql_real_escape_string($_POST['paytype']);
    $cc_num = mysql_real_escape_string($_POST['ccnum']);
    addPatient($name,$address,$pay_method,$cc_num);
}
if ($_GET['action'] == 'viewEmps') {
    viewEmps();
}
if ($_GET['action'] == 'addEmp') {
    addEmp();
}
if ($_GET['action'] == 'addPat') {
    addPat();
}
if ($_GET['action'] == 'createAppointmentLog') {
    createAppointmentLog();
}
if ($_GET['action'] == 'viewPatients') {
    viewPats();
}
if ($_GET['action'] == 'addLog') {
    date_default_timezone_set(EST);
    $pname = mysql_real_escape_string($_POST['pname']);
    $dname = mysql_real_escape_string($_POST['dname']);
    $nname = mysql_real_escape_string($_POST['nname']);
    $note = mysql_real_escape_string($_POST['notes']);
    addLog($pname,$dname,$nname,$note);
}
?>
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)