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); } }