Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help on simple easy program

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help on simple easy program

    Hello guys! I have decided to try to program this really simple program that prints out a game! Basically there are three pieces: slider, diag, and jumper, it is a 5x5 board, and slider can only move one direction in any direction, diag moves diagonal, and jumper moves to a random spot. They don't move if there is a wall. Each turn it prints out a new board(lol i know bad but whatever) and when a piece moves onto another the one that was there before dies, so if jumper moves onto slider, slider dies. My code compiles but when it runs, it prints out a bunch of boards that show random stuff, like sometimes after a piece dies it comes back. here is my very bad code:
    /**
     * The board:
     *        y
     *     1 2 3 4 5
     *  1  * * * * *
     *  2  * * * * *
     *x 3  * * * * *
     *  4  * * * * *
     *  5  * * * * *
    **/
     
    public class Piece{
            static int sx, sy, dx, dy, jx, jy;
            static int pieces = 3;
            static boolean sAlive = true;
            static boolean dAlive = true;
            static boolean jAlive = true;
            public static void printBoard(){
                    for (int x = 1; x <= 5; x++){
                            for (int y = 1; y <= 5; y++){
                                    if (x==jx && y == jy){
                                            if (jAlive){
                                                    System.out.print("J");
                                            }
                                            else{
                                                    System.out.print("-");
                                            }
                                    }
                                    else if (x==dx && y == dy){
                                            if (dAlive){
                                                    System.out.print("D");
                                            }
                                            else{
                                                    System.out.print("-");
                                            }
                                    }
                                    else if (x==sx && y == sy){
                                            if (sAlive){
                                                    System.out.print("S");
                                            }
                                            else{
                                                    System.out.print("-");
                                            }
                                    }
                                    else{
                                            System.out.print("-");
                                    }
                                    System.out.print(" ");
                            }
                            System.out.println();
                    }
            }
            public static void makeNumbers(){
                sx = (int)(Math.random()*5+1);
                    sy = (int)(Math.random()*5+1);
                    dx = (int)(Math.random()*5+1);
                    dy = (int)(Math.random()*5+1);
                    while (dx == sx && dy == sy){
                            dx = (int)(Math.random()*5+1);
                            dy = (int)(Math.random()*5+1);
                    }
                    jx = (int)(Math.random()*5+1);
                    jy = (int)(Math.random()*5+1);
                    while (jx == sx && jy == sy){
                            jx = (int)(Math.random()*5+1);
                            jy = (int)(Math.random()*5+1);
                    }
                    while (dx == jx && dy == jy){
                            jx = (int)(Math.random()*5+1);
                            jy = (int)(Math.random()*5+1);
                    }
            }
            public static void main(String [] args){
                    int direction;
                    makeNumbers();
                    printBoard();
                    Slider s = new Slider();
                    Diag d = new Diag();
                    Jumper j = new Jumper();
                    while (pieces > 0){
                                    if (sAlive){
                                    direction = (int)(Math.random()*4+1);
                                    sx = s.movePieceX(sx, direction);
                                    sy = s.movePieceY(sy, direction);
                                    if (sx == dx && sy == dy){
                                            dAlive = false;
                                            pieces--;
                                    }
                                    if (sx == jx && sx == jy){
                                            jAlive = false;
                                            pieces--;
                                    }
                                    System.out.println();
                                    System.out.println("Sam's turn!");
                                    printBoard();
                            }
                            if (dAlive){
                                    direction = (int)(Math.random()*4+1);
                                    dx = d.movePieceX(dx, direction);
                                    dy = d.movePieceY(dy, direction);
                                    if (dx==0 || dy==0){
                                            dx=0;
                                            dy=0;
                                    }
                                    if (sx == dx && sy == dy){
                                            sAlive = false;
                                            pieces--;
                                    }
                                    if (dx == jx && dx == jy){
                                            jAlive = false;
                                            pieces--;
                                    }
                                    System.out.println();
                                    System.out.println("Donald's turn!");
                                    printBoard();
                            }
                            if (jAlive){
                                    jx = j.movePieceX();
                                    jy = j.movePieceY();
                                    if (jx == dx && jy == dy){
                                            dAlive = false;
                                            pieces--;
                                    }
                                    if (sx == jx && sx == jy){
                                            sAlive = false;
                                            pieces--;
                                    }
                                    System.out.println();
                                    System.out.println("Jimmy's turn!");
                                    printBoard();
                            }
                    }
            }
    }
     
    public class Slider extends Piece{
            public int movePieceX(int x, int direction){
                    if (direction == 1){
                            if (x==1){
                                    return 0;
                            }
                            else{
                                    return x-1;
                            }
                    }
                    else if (direction == 2){
                            if (x==0){
                                    return 1;
                            }
                            else{
                                    return x+1;
                            }
                    }
                    else if (direction == 3){
                            return x;
                    }
                    else{
                            return x;
                    }
            }
            public int movePieceY(int y, int direction){
                    if (direction == 1){
                            return y;
                    }
                    else if (direction == 2){
                            return y;
                    }
                    else if (direction == 3){
                            if (y==1){
                                return 0;
                            }
                            else{
                                    return y-1;
                            }
                    }
                    else{
                            if (y==5){
                                    return 0;
                            }
                            else{
                                    return y+1;
                            }
                    }
            }
    }
     
    public class Diag extends Piece{
            public int movePieceX(int x, int direction){
                    if (direction == 2 || direction == 3){
                            if (x==5){
                                    return 0;
                            }
                            else{
                                    return x+1;
                            }
                    }
                    else{
                            if (x==1){
                                    return 0;
                            }
                            else{
                                    return x-1;
                            }
                    }
            }
            public int movePieceY(int y, int direction){
                    if (direction == 2 || direction == 1){
                            if (y==5){
                                    return 0;
                            }
                            else{
                                    return y+1;
                            }
                    }
                    else{
                            if (y==1){
                                    return 0;
                            }
                            else{
                                    return y-1;
                            }
                    }
            }
    }
     
    public class Jumper extends Piece{
            public int movePieceX(){
                    return (int)(Math.random()*5+1);
            }
            public int movePieceY(){
                    return (int)(Math.random()*5+1);
            }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help on simple easy program

    How are you trying to debug the code to find out why it is not doing what you want it to do?

    Can you copy and paste here some output and add some comments explaining what the program did that was wrong and show what it should have done?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. please help with simple program!!!
    By jokneez in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 10:42 AM
  2. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  3. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM

Tags for this Thread