简体   繁体   中英

can't find solution for my Java Game

For school i have to make a game that already exists. this is the link to the game: Game link

a short explanation: each level you receive( on the right side of the screen) a set of arrows that you can place on the screen and when you push the "Go" button the cow starts to move. When he moves on a arrow he will change his direction in the direction of that arrow. So you have to guide the cow with the arrow to his end destination ( a trophy) without touching any obstacles ( like a wall or a tree ... ).

We MUST implement the Model-View-Presenter design pattern. So i started with my model because that's my game logic.

my problem: every level should be read with a simple .txt file here under a example of my level 1:

26,6
xxxxxxxxxxxxxxxxxxxxxxxxxx
xGRGGBGxGGRGGGGGGGGGGBGGTx
xGGGGRGGGGFGxGGGGGGGGGGGGx
xGFGGLGGGGGGGLGGGGGGGGGGGG
xGGGGSGGGxGGGGGGxGGGGGGGGx
xxxxxxxxxxxxxxxxxxxxxxxxxx

x=obstacle 
G=grass
T=trophy
S= Start position of the cow
L= Fixed left arrow  
R= Fixed right arrow
F= fixed front arrow
B= fixed back arrow

i read the file and with a case statement place every char in a line (every char represents an object) in a 2D array.

My problem is: that you have 2 sets of Arrows: a FixedArrow(It's already on the field and you can't change it's position) and a VariableArrow(the arrows on the right side of the screen where the user can place them on the field, if the field is free or with other words if it's on a Grass object. but how can i do this? how can i create an seperate screen where i can put arrows that the user can put on the field?

i know that my explanation is vague so i hope you guys open the link i posted above and play 1 level so you understand better what i mean.

i'll post a part of my code and class diagram here under. path= is the path to the .txt file i explained here above.

My Class Diagramm

public class Board {
private Tile[][] tiles;

    public Board(Path path) {

        try {

            int yRow = 0;
            Scanner scanner = new Scanner(path);
            String[] split = scanner.nextLine().split(",");
            tiles = new Tile[Integer.parseInt(split[1])][Integer.parseInt(split[0])];
            while (scanner.hasNext()) {
                String line = scanner.nextLine();

                for (int xRow = 0; xRow < line.length(); xRow++) {
                    char character = line.charAt(xRow);
                    switch (character) {
                        case 'x':
                            tiles[yRow][xRow] = new Tile(new Obstacle());
                            break;
                        case 'G':
                            tiles[yRow][xRow] = new Tile(new Grass());
                            break;
                        case 'S':
                            tiles[yRow][xRow] = new Tile(new Grass());
                            //cow.setStartPosition(xRow,yRow);
                            break;
                        case 'L':
                            tiles[yRow][xRow] = new Tile(new FixedArrow(Direction.LEFT));
                            break;
                        case 'R':
                            tiles[yRow][xRow] = new Tile(new FixedArrow(Direction.RIGHT));
                            break;
                        case 'F':
                            tiles[yRow][xRow] = new Tile(new FixedArrow(Direction.FRONT));
                            break;
                        case 'B':
                            tiles[yRow][xRow] = new Tile(new FixedArrow(Direction.BACK));
                            break;
                        case 'T':
                            tiles[yRow][xRow] = new Tile(new Trophy());
                            break;
                        //How should i read a set of arrows in without placing them in the game field but on the right side of the screen
                    }


                }

                ++yRow;
            }

        } catch (IOException e) {
            e.getMessage();
        }
    }

}

You only need to set the variable arrows with an input scanner or something else. The variable arrows should be in the two dimension array. You need to compare your variable arrow coordinates from your input with the sign at these points in the array and check if you can put there a new arrow or if its not allowed. If its allowed then replace this sign with your new arrow sign or you reuse your fixed arrow signs.

I hope i've understand your question correctly and this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM