<repo_name>whanata/pacman_no_ui<file_sep>/Pacman/Player.java

/**
 * Player Class
 * 
 * @author Whanata
 * @version 20:41 25/03/2014
 */
public class Player
{
    //Player's fields
    private int x;
    private int y;
    private int collectedDots;
    private boolean life;

    //Constructor for objects of class Player
    Player(int initialX,int initialY)
    {
        collectedDots = 0;
        this.x = initialX;
        this.y = initialY;
        this.life = true;       
    }   

    public boolean alive()
    {
        return this.life;
    }

    public void died()
    {
        this.life = false;
    }
            
    //Player changing position 
    public void changePosition(int changeX,int changeY)
    {
        this.x += changeX;
        this.y += changeY;
    }

    //Player's x position    
    public int getX()
    {
        return this.x;
    }

    //Player's y position
    public int getY()
    {
        return this.y;
    }
    
    public String end()
    {
        if (this.life)
        {
            return "You win!";
        }
        else
        {
            return "You lose!";
        }                   
    }
    
    //Increase collectedDots by 1
    public void collectDot()
    {
        collectedDots++;
    }

    public boolean allDots()
    {
        return (collectedDots == 3);
    }
    
    public String dots()
    {
        int i;
        String star = "";
        for (i = 0; i < collectedDots; i++)
        {
            star += "*";           
        }
        return star;
    }

    //Return to string  
    public String toString()
    {
        String dots = "Player[" + this.dots() + "](";
        if (this.life)
        {
            return dots + this.x + "," + this.y + ")";
        }
        else
        {
            return dots + "-,-)";
        }
    }
}
<file_sep>/Pacman/Enemy.java

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy
{
    private int x;
    private int y;
    private String direction;

    Enemy(int initialX,int initialY)
    {
        x = initialX;
        y = initialY;
        direction = "neutral";
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }    

    public void movement()
    {
        if (direction == "right")
        {
            this.x++;

        }
        else if (direction == "left")
        {
            this.x--;

        }
        else if (direction == "down")
        {
            this.y++;

        }
        else if (direction == "up")
        {
            this.y--;

        }

    }    
    
    public int distance(int player, int enemy)
    {
        return player - enemy;
    }
    
    public void decision(Player player)
    {            
        int playerX = player.getX();
        int playerY = player.getY();        
        int xDistance = distance(playerX,this.x);
        int yDistance = distance(playerY,this.y);
        boolean rightDistance = this.absDecision(xDistance - 1,xDistance);
        boolean leftDistance = this.absDecision(xDistance + 1,xDistance);
        boolean downDistance = this.absDecision(yDistance - 1,yDistance);
        boolean upDistance = this.absDecision(yDistance + 1,yDistance);
        if (direction == "right" && rightDistance || direction == "left" && leftDistance || direction == "down" && downDistance || direction == "up" && upDistance || direction == "neutral")
        {
            this.decisionMaking(xDistance,yDistance);
        }
        
        this.movement();
        if (this.check(playerX,playerY))
        {
            player.died();
        }
    }
    
    public boolean check(int playerX,int playerY)
    {
        return distance(playerX,this.x) == 0 && distance(playerY,this.y) == 0;
    }
    
    public boolean absDecision(int a,int b)
    {
        return Math.abs(a) > Math.abs(b);
    }
    
    public void decisionMaking(int xDistance,int yDistance)
    {
        if (this.absDecision(xDistance,yDistance))
        {           
            if (xDistance > 0)
            {                
                direction = "right";
            }
            else
            {
                direction = "left";

            }
        }
        else if (this.absDecision(yDistance,xDistance))
        {
            if (yDistance > 0)
            {
                direction = "down";
            }
            else
            {
                direction = "up";

            }
        }
        else
        {
            direction = "neutral";
        }
    }
    
    public String toString()
    {
        return "Enemy(" + this.x + "," + this.y + ")";
    }
}
<file_sep>/README.md
pacman_no_ui
============
<file_sep>/Pacman/Dot.java

/**
 * Class Dot
 * 
 * @author Whanata
 * @version 20:40 25/03/2014
 */
public class Dot
{
    // Dot's field
    private int x;
    private int y;
    private boolean exists;

    //Constructor for objects of Class Dot
    Dot(int initialX,int initialY)
    {
        this.exists = true;
        this.x = initialX;
        this.y = initialY;
    }

    //The dot will disappear
    public void disappear()
    {
        this.exists = false;
    }
    
    public void check(Player player)
    {
        if (player.getX() == this.x && player.getY() == this.y && this.getExists())
        {
            player.collectDot();
            this.disappear();
        }
    }
    
    //Dot's state
    public boolean getExists()
    {
        return this.exists;
    }
    
    //Return to string
    public String toString()
    {   
        if (exists)
        {
            return "Dot(" + this.x + "," + this.y +")";
        }
        else
        {
            return "Dot(-,-)";            
        }
    }
}
<file_sep>/Pacman/Game.java
import java.util.*;
/**
 * Class Game
 * 
 * @author Whanata
 * @version 20:40 25/03/2014
 */
public class Game
{
    //Game's fields
    private Player player;
    private Dot dot1;
    private Dot dot2;
    private Dot dot3;
    private Exit exit;
    private Enemy enemy;
    private boolean advanced;

    //Constructor for objects of class Game
    Game(int xPosition,int yPosition)
    {
        // initialise instance variables
        player = new Player(xPosition,yPosition);
        dot1 = new Dot(1,1);
        dot2 = new Dot(2,2);
        dot3 = new Dot(3,3);
        exit = new Exit(4,4);
        enemy = new Enemy(5,5);
        if (xPosition == 5)
        {
            this.advanced = true;
        }
        else
        {
            this.advanced = false;
        }
    }

    public void advanceMode()
    {
        enemy.decision(player);
    }

    public void start()
    {
        do
        {
            System.out.println(this);
            
            this.input();
            if (this.advanced)
            {
                this.advanceMode();
            }
        }
        while (exit.check(player) && player.alive());

        System.out.println(this);

        System.out.println(player.end());
    }

    public void input()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Move (l/r/u/d): ");
        String movement = keyboard.nextLine(); 

        switch (movement.charAt(0))
        {
            case 'l': move(-1,0);
                break;

            case 'r': move(1,0);
                break;

            case 'u': move(0,-1);
                break;

            case 'd': move(0,1);
                break;

            default: System.out.println("Invalid move");
                break;
        }
    }

    //Player moves around by adding dx and dy to original position, Checks if player is on dot
    public void move(int dx,int dy)
    {   
        player.changePosition(dx,dy);
        dot1.check(player);
        dot2.check(player);
        dot3.check(player);
        exit.check(player);
    }
    
    //Return to string
    public String toString()
    {
        String normal = player + " " + dot1 + " " + dot2 + " " + dot3 + " " + exit;
        if (this.advanced)
        {
            return normal + " " + enemy;
        }
        else
        {
            return normal;
        }
    }
}
