Home > OS >  New to Programming (Java) - dont know how to fix an error with a small Console Game simular to Snake
New to Programming (Java) - dont know how to fix an error with a small Console Game simular to Snake

Time:10-07

I just started to learn Java and am working on a small project which is supposed to create a gameField in a 2 dimensional String Array Field Borders are marked with a "#". In this Field I want to spawn a player who is marked as ">". This player can then turn left or right (∧ for looking up for example) and then has the option to move forward one space. I currently have the gameField created and now want to spawn the player into that 2 dimensional String Array for which I have made a spawnPlayer method/function in the class Player. I now want to add / open that method/function in the main method. This is where I am getting the error messages and dont know what to do.

public class KonsolenWanderer {




    public static void main(String[] args) {
        
        Field field1 = new Field();
        field1.createField();
        
        Player player1 = new Player();
        //Error is here!!!
        player1.spawnPlayer();
        
        
        

    }

}


public class Field {
    
    private String [][] fieldSize = new String [10][10]; 
        
    
    public void createField() {
        
        for(int i = 0; i < fieldSize.length; i  ) {
            
            for(int j = 0; j < fieldSize[i].length; j  ) {
                
                //Creating Field Border
                if(i == 0 || i == 9 || j == 0 || j == 9) {
                    fieldSize[i][j] = "#";  
                }
                else {
                    fieldSize[i][j] = " ";
                }
                
                System.out.print(fieldSize[i][j]);
            }
            
            System.out.println();
        }
        
    }
    

    public String[][] getFieldSize() {
        return fieldSize;
    }

    public void setFieldSize(String[][] fieldSize) {
        this.fieldSize = fieldSize;
    }


    
        
    
}



public class Player {
    
    private static int xPosition;
    private static int yPosition;
    private String up = "∧";
    private String down = "∨";
    private String left = "<";
    private String right = ">";
    private String currentDirection;
    
    Player() {
        xPosition = 4;
        yPosition = 4;
    }
    
    public void spawnPlayer(String[][]fieldSize) {
        
        fieldSize[xPosition][yPosition] = ">";
        
    }
    
    
    public static void moveForward() {
        
        
    }
    
    public static void turnPlayerLeft() {
        
        
    }
    
    public static void turnPlayerRight() {
        
        
    }

}

CodePudding user response:

You need to set the position inside the field object or expose fieldSize from within Field class.

  public class KonsolenWanderer {

    public static void main(String[] args) {

        Field field = new Field();
        field.createField();

        Player player1 = new Player();
        //Error is here!!!
        player1.spawnPlayer(new String[8][8], field);

        field.showField();

    }
}

class Field {

    private String [][] fieldSize = new String [10][10];

    public void createField() {

        for(int i = 0; i < fieldSize.length; i  ) {

            for(int j = 0; j < fieldSize[i].length; j  ) {

                //Creating Field Border
                if(i == 0 || i == 9 || j == 0 || j == 9) {
                    fieldSize[i][j] = "#";
                }
                else {
                    fieldSize[i][j] = " ";
                }

                System.out.print(fieldSize[i][j]);
            }

            System.out.println();
        }

    }
    public void showField() {

        for(int i = 0; i < fieldSize.length; i  ) {
            for(int j = 0; j < fieldSize[i].length; j  ) {
                System.out.print(fieldSize[i][j]);
            }
            System.out.println();
        }

    }


    public String[][] getFieldSize() {
        return fieldSize;
    }

    public void setFieldSize(String[][] fieldSize) {
        this.fieldSize = fieldSize;
    }

    public void setPlayerPosition(int i, int j) {
        i  ;
        j  ;
        fieldSize[i][j] = ">";
    }
}

class Player {

    private static int xPosition;
    private static int yPosition;
    private String up = "∧";
    private String down = "∨";
    private String left = "<";
    private String right = ">";
    private String currentDirection;

    Player() {
        xPosition = 4;
        yPosition = 4;
    }

    public void spawnPlayer(String[][] fieldSize, Field field) {
        for(int i = 0; i < fieldSize.length; i  ) {
            if (i == fieldSize.length -1){
                for(int j = 0; j < fieldSize[i].length; j  ) {
                    if (j == fieldSize.length - 1 )
                        field.setPlayerPosition(i, j);
                }
            }
        }
    }


    public static void moveForward() {


    }

    public static void turnPlayerLeft() {


    }

    public static void turnPlayerRight() {


    }

}
  • Related