Click here to Skip to main content
15,891,629 members
Articles / Programming Languages / JScript .NET

Star Trek "Galactic Conquest" Game Contest Submission (Java)

Rate me:
Please Sign up or sign in to vote.
4.27/5 (6 votes)
6 Aug 2008CPOL3 min read 53.3K   951   25  
Star Trek "Galactic Conquest" Game Contest Submission (Java)
import java.util.*;
/**
 *  Galaxy constructed of X x Y - Quadrants. 
 * 
 * @author Robert Bettinelli 
 * @version 1.0.0
 */

public class Quadrant
{
    private int spaceSizeX;
    private int spaceSizeY;
    private Sector[][] space;
    
    /**
     * Constructor for objects of class Quadrant
     */
    public Quadrant(int spaceSX,int spaceSY)
    {
        spaceSizeX = spaceSX;
        spaceSizeY = spaceSY;
        space = new Sector[spaceSX+1][spaceSY+1];
        createQuadrant();
        ///util = new Util(5);
    }
    
    /**
    * Initialize Space inside Quadrant. 
    */
    public void createQuadrant()
    {
        int x,y;
        for (x=0;x<=spaceSizeX;x++)
        {
            for (y=0;y<=spaceSizeY;y++)
            {
                space[x][y] = new Sector();
            }
        }
    }
        
    /**
    * Display Quadrant Space Array[0-X][0-Y] 
    */
    public void showQuadrant()
        {
        int x,y;
        String createSpace = "";
        
        for (x=0;x<=spaceSizeX;x++)
        {
            for (y=0;y<=spaceSizeY;y++)
            {
                createSpace += space[x][y].getSector();
            }
            System.out.println(createSpace);
            createSpace = "";
        }
    }
    
    /**
    * Display Quadrant 1 Line Only [0-8]
    * 
    * @param y    Quadrand Line to Display.
    * 
    * @return     String of Quadrant Line.
    */  
    public String showQuadLine(int y, boolean addSpace)
    {
        String createSpace = "";
        String makeSpace = "";
        
        if (addSpace)
        {
            makeSpace = " ";
        }

        for (int x=0;x<=spaceSizeX;x++)
        {
            createSpace += space[y][x].getSector()+makeSpace;
        }
        //System.out.println(createSpace);
        return createSpace;
    }
    /**
    * Clean all Enemies from Quad.
    * 
    * @return     Number of Ships Cleaned
    */ 
    public int wipeEnemeyInQuad()
    {
        int x,y;
        int enemyShip = 0;
        for (x=0;x<=spaceSizeX;x++)
        {
            for (y=0;y<=spaceSizeY;y++)
            {
                if (space[x][y].getSector().equals("X"))
                {
                    enemyShip += 1;
                    space[x][y].setSector("Z");
                }
                if (space[x][y].getSector().equals("V"))
                {
                    enemyShip += 1;
                    space[x][y].setSector("C");
                }
            }
        } 
        return enemyShip;
    }
    /**
    * Count all Enemies from Quad.
    * 
    * @return enemyShip Number of Ships In Quad
    */   
    public int countEnemeyInQuad()
    {
        int x,y;
        int enemyShip = 0;
        for (x=0;x<=spaceSizeX;x++)
        {
            for (y=0;y<=spaceSizeY;y++)
            {
                if ((space[x][y].getSector() == "X")||(space[x][y].getSector() == "V"))
                {
                    enemyShip += 1;
                }
            }
        } 
        return enemyShip;
    }
    /**
    * Display SRS Information. 
    * 
    * @return QuadDetails String of Values displaying contents of Quadrant. 
    */  
    public String getQuadDetails()
    {
        // Set Counters for Each Space Item. 
        //int countSpace = 0;
        int countEnemyShip = 0;
        int countBases = 0;
        int countStars = 0;
        int countPlanets = 0;
        // Loop Through Quad
        for (int x=0;x<=spaceSizeX;x++)
        {
            for (int y=0;y<=spaceSizeY;y++)
            {
                switch(space[x][y].getSector().charAt(0))
                {
                    case 'X':                       // Fighter
                        countEnemyShip += 1;
                        break;
                    case '*':                       // Star
                        countStars += 1;
                        break;
                    case '@':                       // Station
                        countBases += 1;
                        break;
                    case '.':                       // Empty
                        break;
                    case 'Y':                       // Ship
                        break;
                    case 'W':                       // Worm Hole
                        break;
                    case 'S':                       // Sun
                        break;
                    case 'Z':                       // Destroyed Fighter
                        break;
                    case 'C':                       // Destroyed Battleship
                        break;
                    case 'V':                       // BattleShip
                        countEnemyShip += 1;
                        break;
                    default:                        // All else must be planet
                        countPlanets += 1;
                        break;
                }
            } 
        }  
        return ""+countStars+countBases+countPlanets+countEnemyShip;
    }
       
    /**
    * Plot Location for ship jump.
    * 
    * @param   shipLx  Ememy Ship Location X
    * @param   shipLy  Enemy Ship Location Y
    */    
    public void plotEnemy(int shipLx, int shipLy)
    {   
        String shipType = space[shipLx][shipLy].getSector();
        space[shipLx][shipLy].setSector(".");
        int enemySheild = space[shipLx][shipLy].getSheild();
        space[shipLx][shipLy].setSheild(75);
        boolean moveOk = false;
        while  ( moveOk == false)
        {
            int x1 = rNumber(spaceSizeX);
            int y1 = rNumber(spaceSizeY);
            if ( space[x1][y1].getSector() == ".")
            {
                // Set New TEMP Location for Ship.
                if (shipType.equals("X"))
                {
                    space[x1][y1].setSector("<");
                }
                if (shipType.equals("V"))
                {
                    space[x1][y1].setSector(">");
                }
                // Move Sheild with Ship. But increase sheild because it didnt shoot.
                space[shipLx][shipLy].setSheild(enemySheild+10);
                moveOk = true;
            }
        }
    }
    
    /**
    * Execution Of Enemy Jump.
    * 
    * @return jump Output of all Ships jumped.
    */  
    public String jumpEnemy()
    {   
        String jump = "";
        int x,y;
        for (x=0;x<=spaceSizeX;x++)
        {
            for (y=0;y<=spaceSizeY;y++)
            {
                if (space[x][y].getSector() == "<")
                {
                    jump += "Enemy Jump! to ["+x+"],["+y+"]\n";
                    space[x][y].setSector("X");
                }
                if (space[x][y].getSector() == ">")
                {
                    jump += "Enemy Jump! to ["+x+"],["+y+"]\n";
                    space[x][y].setSector("V");
                }
            }
        } 
        return jump;
    }

    /**
    * Set sector of Quadrant. 
    * 
    * @param x   x location of sector
    * @param y   y location of sector
    */
    public void setSpace(int x, int y, String newConstruct)
    {
        space[x][y].setSector(newConstruct);
    }
    
    /**
    * Return sector of Quadrant
    * 
    * @param x   x location of sector
    * @param y   y location of sector
    * 
    * @return    String of Sector Contents.
    */
    public String getSpace(int x, int y)
    {
        return space[x][y].getSector();
    }
    
    /**
     * isEnemy
     * @param x   x location of sector
     * @param y   y location of sector 
     * 
     * @return true or false if enemy is in sector of space.
     */
    public boolean isEnemy(int x, int y)
    {
        return space[x][y].isEnemy();
    }
    
    /**
     * getSheild returns value of Sector
     * 
     * @param x   x location of sector
     * @param y   y location of sector
     *
     * @return value of sheild @ Sector Location 
     */
    public int getSheild(int x, int y)
    {
        return space[x][y].getSheild();
    }

    /**
     * setSheild sets value of Sector
     * 
     * @param x   x location of sector
     * @param y   y location of sector
     * @param sheildValue  Value to reset sheild to
     */
    public void setSheild(int x, int y,int sheildValue)
    {
        space[x][y].setSheild(sheildValue);
    }
    
    /**
     * hitSheild hammer Sheild @ x,y
     * 
     * @param x   x location of sector
     * @param y   y location of sector
     * @param value - Hit Sheild with value amount
     */    
    public void hitSheild(int x, int y, int value)
    {
        space[x][y].hitSheild(value);
    }
    
    /**
     * isDocked - Determine if ship is Docked.
     * @param shipLocX   x location of ship
     * @param shipLocY   y location of ship
     * 
     * @return boolean is ship is Docked. 
     */
    public boolean isDocked(int shipLocX, int shipLocY)
    {
        // Assume that your ship is NOT Docked.
        boolean docked = false;
        // Upper Left Check Limit        
        int x1 = shipLocX-1;
        int y1 = shipLocY-1;
        // Lower Right Check Limit
        int x2 = shipLocX+1; 
        int y2 = shipLocY+1;
        // Check Square for Station.
        for (int x=x1;x<=x2;x++)
        {
            for (int y=y1;y<=y2;y++)
            {
               // Utilize Try because you may be out of Array Limits
               try
               {
                   if(space[x][y].getSector() == "@") 
                   {
                       docked = true;
                   }
                } 
                catch(Exception ie)
                {
                    // ignore error - and move along. (Nothing to see here). :-) 
                }
            }
        } 
        return docked;
    }
    
     /**
     * rNumber - Random Number Routine.
     * 
     * @param uBound - Upper Limit for Random Generation
     * 
     * return randomNumber 
     */
    public int rNumber(int uBound) 
    {
        Random generator = new Random();
        // get a random number between 0 and 1 (Double Length) 
        double r = generator.nextDouble();
        // multiply by X so it's now between 0 and X
        r *= uBound;
        // add 1
        r += 1.0;
        //truncate it to an int
        r = Math.floor(r);
        // handle one special if unlikely case
        if (r == uBound+1) r = uBound;
        // convert to an int and return
        return (int) r;
    }
    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Nottawasaga Valley Conservation Authority
Canada Canada
I have background in programming(many languages), web design, hardware and software. Currently I hold a lead database programming position for the Nottawasaga Valley Conservation Authority... Programming in vb.net and java comes 2nd nature to me and of course I do this for fun. I also used to design in cobol and pascal and run a BBS(anyone remember what this was?). Anyways, drop me a line maybe I can help you.

Comments and Discussions