Hi Everybody

I am trying to write a small game in Java. I have done few things. But now I want to add some more feature in it.

I am creating Enemies in Game.java using UI.java and EnemyShip.java. Now I want to determine how many enemies to create the number of enemies to be created is read from the user and stored into the variable numEnemies which is decleared in Game.java

Here is the code of all three program
Game.java

public class Game
{
    public static final int HALF_WIDTH = 6;

    private UI ui;
    private EnemyShip enemy1;
    private EnemyShip enemy2;
    private EnemyShip enemy3;
    private PlayerShip player;

    public Game(UI ui)
    {
        this.ui = ui;
        int numEnemies = ui.readNumEnemies();
	  for(int enemiesloop=1; enemiesloop<=numEnemies; enemiesloop++)
	  {
		enemy1 = ui.createEnemy(1);
        	enemy2 = ui.createEnemy(2);
        	enemy3 = ui.createEnemy(3);
	  }
        player = new PlayerShip(this, 0);

        ui.init(this); // This must be the last line of the constructor
    }

    public int getWidth()
    {
        return HALF_WIDTH * 2 + 1;
    }

    public String toString()
    {
        return enemy1 + " " + enemy2 + " " + enemy3 + " " + player;
    }

    public void print()
    {
        ui.updateScreen();
    }

    public void play()
    {
        while (!over())
            executeOneTurn();
        print();
        ui.showGameOver();
    }

    public boolean over()
    {
        return enemy1.isDead() && enemy2.isDead() && enemy3.isDead();
    }

    public void moveEnemies()
    {
        enemy1.move();
        enemy2.move();
        enemy3.move();
    }

    public void executeOneTurn()
    {
        print();
        moveEnemies();
        movePlayer();
    }

    public void movePlayer()
    {
        player.act(ui.readMove());
    }

    public PlayerShip getPlayer()
    {
        return player;
    }

    // This method should return an array of all enemies
    public EnemyShip[] getEnemies()
    {
        EnemyShip[] enemies = { enemy1, enemy2, enemy3 };
        return enemies;
    }

    // You can delete these methods...
    public EnemyShip getEnemy1() { return enemy1; }
    public EnemyShip getEnemy2() { return enemy2; }
    public EnemyShip getEnemy3() { return enemy3; }
}

UI.java

import java.util.*;

public class UI
{
    private static final int HEADER_ROWS = 3;
    private static final int BLANK_ROWS = 2;
    private static final Scanner keyboard = new Scanner(System.in);

    private Game game;

    public void init(Game game)
    {
        this.game = game;
    }

    public int readNumEnemies()
    {
        return promptInt("Enter the number of enemies: ");
    }

    public EnemyShip createEnemy(int number)
    {
        System.out.println("Enemy #" + number);
        int x = promptInt("- Initial x position: ");
        int v = promptInt("- Initial velocity: ");
        return new EnemyShip(x, v);
    }

    private int promptInt(String prompt)
    {
        System.out.print(prompt);
        int value = keyboard.nextInt();
        keyboard.nextLine();
        return value;
    }

    public void updateScreen()
    {
        printLines(HEADER_ROWS);

        printEnemyShip(game.getEnemy1());
        printEnemyShip(game.getEnemy2());
        printEnemyShip(game.getEnemy3());

        printLines(BLANK_ROWS);

        PlayerShip player = game.getPlayer();
        printPlayerShip(player);

        System.out.print(player.getPoints()+" pts. ");
    }

    private void printLines(int n)
    {
        for (int i = 0; i < n; i++)
            System.out.println();
    }

    private void printEnemyShip(EnemyShip e)
    {
        printObject("e", ".", e.isDead(), e.getX());
    }

    private void printPlayerShip(PlayerShip s)
    {
        printObject(s.justFired() ? "Å": "A", "_", false, s.getX());
    }

    private void printObject(String symbol, String bg, boolean dead, int pos)
    {
        if (dead)
            symbol = "X";
        for (int x = -Game.HALF_WIDTH; x <= Game.HALF_WIDTH; x++)
            System.out.print((x == pos) ? symbol : bg);
        System.out.println();
    }

    public int readMove()
    {
        System.out.print("move: ");
        char move = keyboard.nextLine().charAt(0);
        switch (move)
        {
            case 'l': return Action.LEFT;
            case 'r': return Action.RIGHT;
            case 'f': return Action.FIRE;
            default: return Action.NONE;
        }
    }

    public void showGameOver()
    {
        System.out.println("You won!");
    }
}

EnemyShip.java

public class EnemyShip
{
    private int x;
    private int direction;
    private boolean dead;

    public EnemyShip(int x, int direction)
    {
        this.x = x;
        this.direction = direction;
    }

    public int getX()
    {
        return x;
    }

    public boolean isDead()
    {
        return dead;
    }

    public boolean inColumn(int target)
    {
        return !dead && target == x;
    }

    public String toString()
    {
        String s = "Enemy(" + x + ")";
        if (dead)
            s += "*";
        return s;
    }

    public void die()
    {
        dead = true;
    }

    public void move()
    {
        if (dead)
            return;

        x += direction;
        if (Math.abs(x) > Game.HALF_WIDTH)
        {
            x -= 2*direction;
            direction *= -1;
        }
    }
}

Like the comments in the code suggest, you need an array of Enemies, not the hard-coded enenmy1-3. The array can then be a big as needed in each run.

In the definition of class EnemyShip, you may add a static int attribute numEnemieShip from which you may know the number of enemy ships created from time to time. Hence, the constructor is defined as follows:

public EnemyShip(int x, int direction){        
this.x = x;        
this.direction = direction;    
numEnemieShip++;
}

Edited 6 Years Ago by tong1: n/a

In the definition of class EnemyShip, you may add a static int attribute numEnemieShip from which you may know the number of enemy ships created from time to time. Hence, the constructor is defined as follows:

public EnemyShip(int x, int direction){        
this.x = x;        
this.direction = direction;    
numEnemieShip++;
}

Just by adding static int attribute numEnemieShip How is it possible to create enemies at runtime after accepting number of enemies by user

Something like:

EnemyShip[] ships = new EnemyShip[numOfEnemyShips];
for (int i = 0; i < numOfEnemyShips; i++) {
   EnemyShip[i] = new EnemyShip( etc)
}
This article has been dead for over six months. Start a new discussion instead.