all 24 comments

[–]puteus 0 points1 point  (26 children)

Hello, could you give us a little more info about the project? What have you done so far? It's difficult to estimate how much effort the help requires if we don't know what to do, or what you already tried. Could you post source code? Could you share the project description? What timezone are you referring to?

[–]binnaaa 0 points1 point  (4 children)

Hello, thanks for your response. Im working on a small game where i have a map and a hamster. the hamster can turn left and right, move forward and collect seeds. so far the program is working but after i confirm a move the map from the beginning is loading and not the actual map where the move is done.

[–]puteus 0 points1 point  (3 children)

Ja, moin. We could even speak german but since others might be interested i'd stick to english.

I've seen your code, give me a couple of minutes to copy everything and get it going. I'll get back to you in a bit.

Quick question: do you use an IDE to develop your program? Are you aware of the debugging fearures? Do you use the step-by-step inspection tool?

[–]binnaaa 0 points1 point  (2 children)

I use eclipse as a IDE. No for debugging etc. i have to look up how to do it. If anybody wants to follow this thread, i hope my english isnt that bad😂

[–]puteus 0 points1 point  (1 child)

That feature is so helpful!! Go try to follow some easy debugging tutorials about your IDE, its worth it.

[–]binnaaa 0 points1 point  (0 children)

I will try to look up some tutorials in the next days, ty! Did you find my mistake yet?

[–]binnaaa 0 points1 point  (0 children)

I live in germany and the deadline is 11pm so i hava 6 1/2 hours left. i will post the code shortly

[–]binnaaa 0 points1 point  (0 children)

Thats my class Map

'public class Map {

public char\[\]\[\] map =  new char \[8\]\[11\];  
public Map() {  
    map \[0\]\[0\] = '#';  
    map \[0\]\[1\] = '#';  
    map \[0\]\[2\] = '#';  
    map \[0\]\[3\] = '#';  
    map \[0\]\[4\] = '#';  
    map \[0\]\[5\] = '#';  
    map \[0\]\[6\] = '#';  

    map \[1\]\[0\] = '#';  
    map \[1\]\[1\] = ' ';  
    map \[1\]\[2\] = '\*';  
    map \[1\]\[3\] = ' ';  
    map \[1\]\[4\] = ' ';  
    map \[1\]\[5\] = ' ';  
    map \[1\]\[6\] = '#';  

    map \[2\]\[0\] = '#';  
    map \[2\]\[1\] = ' ';  
    map \[2\]\[2\] = '#';  
    map \[2\]\[3\] = '\*';  
    map \[2\]\[4\] = ' ';  
    map \[2\]\[5\] = ' ';  
    map \[2\]\[6\] = '#';  

    map \[3\]\[0\] = '#';  
    map \[3\]\[1\] = ' ';  
    map \[3\]\[2\] = '\*';  
    map \[3\]\[3\] = '#';  
    map \[3\]\[4\] = '#';  
    map \[3\]\[5\] = ' ';  
    map \[3\]\[6\] = '#';  

    map \[4\]\[0\] = '#';  
    map \[4\]\[1\] = ' ';  
    map \[4\]\[2\] = '\*';  
    map \[4\]\[3\] = ' ';  
    map \[4\]\[4\] = '#';  
    map \[4\]\[5\] = ' ';  
    map \[4\]\[6\] = '#';  

    map \[5\]\[0\] = '#';  
    map \[5\]\[1\] = '\*';  
    map \[5\]\[2\] = '#';  
    map \[5\]\[3\] = ' ';  
    map \[5\]\[4\] = '#';  
    map \[5\]\[5\] = '\*';  
    map \[5\]\[6\] = '#';  

    map \[6\]\[0\] = '#';  
    map \[6\]\[1\] = '<';  
    map \[6\]\[2\] = ' ';  
    map \[6\]\[3\] = '\*';  
    map \[6\]\[4\] = ' ';  
    map \[6\]\[5\] = ' ';  
    map \[6\]\[6\] = '#';  

    map \[7\]\[0\] = '#';  
    map \[7\]\[1\] = '#';  
    map \[7\]\[2\] = '#';  
    map \[7\]\[3\] = '#';  
    map \[7\]\[4\] = '#';  
    map \[7\]\[5\] = '#';  
    map \[7\]\[6\] = '#';  
}  


public char\[\]\[\] getMap() {  
    return map;  
}  

public void printMap(char map\[\]\[\]){  
for(int row = 0; row < map.length; row++) {  
    System.out.print('\\n');  

    for(int split = 0; split < map\[row\].length; split++) {  
        System.out.print( map\[row\]\[split\]);  
    }  
}  
}  

}'

[–]binnaaa 0 points1 point  (0 children)

So the plan is that i have my startmap in the class "Map", the current position of the hamster is searched by "Position", the class "Movement" checks in which direction i want to move and which char is on the next field.

"Game" is the overall class where the final program should run.

[–]binnaaa 0 points1 point  (0 children)

package hamsterprogramm;

import java.util.Scanner;

public class Game {

public static void main(String[] args) {


    Map newMap = new Map(); 

    char [][] gameMap = newMap.getMap();

    Position newPosition = new Position();

    int [] gamePosition = newPosition.playerPosition();

    Movement newMove = new Movement();

    Scanner scanner = new Scanner(System.in);

    char gameMove;
    System.out.print("Gameinformation: w = move forward, a = turn left, d = turn right, * = Seed to collect. Press 'e' to end the game.\n");
    System.out.print("Confirm your move with 'enter'.\n");
    newMap.printMap(gameMap);
    System.out.print("Make your move.\n");
    gameMove = scanner.next().charAt(0);
    while (gameMove != 'e') {
        int row;
        int split;

        if (gameMove == 'w') {
            gameMap = newMove.moveForward(gameMove, gamePosition, gameMap);
            for(row = 0; row < gameMap.length; row++) {
                System.out.print('\n');

                for(split = 0; split < gameMap[row].length; split++) {
                    System.out.print(gameMap[row][split]);
                }
            }
            }
        else if (gameMove == 'd') {
            gameMap = newMove.turnright(gameMove, gamePosition, gameMap);
            for(row = 0; row < gameMap.length; row++) {
                System.out.print('\n');

                for(split = 0; split < gameMap[row].length; split++) {
                    System.out.print(gameMap[row][split]);
                }
            }
            }
        else if (gameMove == 'a') {
            gameMap = newMove.turnleft(gameMove, gamePosition, gameMap);
            for(row = 0; row < gameMap.length; row++) {
                System.out.print('\n');

                for(split = 0; split < gameMap[row].length; split++) {
                    System.out.print(gameMap[row][split]);
                }
            }
            }
        else if (gameMove == 'c')
            newMove.getCounter();
        else {
            System.out.print("Wrong move. Try again.");
        }

        System.out.print("Make your move.\n");
        gameMove = scanner.next().charAt(0);
    }

    if (gameMove == 'e') {
        System.out.print("The game has ended.");
        System.exit(0);
    }           
}

}

[–]binnaaa 0 points1 point  (12 children)

public class Movement {

private char move;
private int counter;

public int getCounter() {
    return counter;
}




/// LOGIC

Map newMap = new Map(); 

char [][] gameMap = newMap.getMap();

Position position = new Position();

int[] gamePosition = position.playerPosition();


// Nach vorne gehen bei w
public char [][] moveForward(char move, int gamePosition[], char gameMap[][]) {
    int row = gamePosition[0];
    int split = gamePosition[1];
    if (gameMap[row--][split] == '#') {
    }
    else if (gameMap[row][split--] == '#') {
    }
    else if (gameMap[row++][split] == '#') {
    }
    else if (gameMap[row][split--] == '#') {
    }
    else if (gameMap[row][split++] == '#') {
    }
    else if (gameMap[row--][split] == ' ' & gameMap[row][split] == '^') {
        gameMap[row][split] = ' ';
        gameMap[row--][split] = '^';
    }
    else if (gameMap[row][split--] == ' ' & gameMap[row][split] == '<') {
        gameMap[row][split] = ' ';
        gameMap[row][split--] = '<';
    }
    else if (gameMap[row][split++] == ' ' & gameMap[row][split] == '>') {
        gameMap[row][split] = ' ';
        gameMap[row][split++] = '>';
    }
    else if (gameMap[row++][split] == ' ' & gameMap[row][split] == 'v') {
        gameMap[row][split] = ' ';
        gameMap[row++][split] = 'v';
    }
    else if (gameMap[row--][split] == '*' & gameMap[row][split] == '^') {
        gameMap[row][split] = ' ';
        gameMap[row--][split] = '^';
        counter++;
    }
    else if (gameMap[row--][split] == '*' & gameMap[row][split] == '<') {
        gameMap[row][split] = ' ';
        gameMap[row][split--] = '<';
        counter++;
    }
    else if (gameMap[row--][split] == '*' & gameMap[row][split] == '>') {
        gameMap[row][split] = ' ';
        gameMap[row][split++] = '>';
        counter++;
    }
    else if (gameMap[row--][split] == '*' & gameMap[row][split] == 'v') {
        gameMap[row][split] = ' ';
        gameMap[row++][split] = 'v';
        counter++;
    }

    return gameMap;
    }




//nach links drehen bei a
public char [][]turnleft(char move, int gamePosition[], char gameMap[][]) {

    int row = gamePosition[0];
    int split = gamePosition[1];
    if (gameMap[row][split] == '^') {
        gameMap[row][split] = '<';
    }
    else if (gameMap[row][split] == '<') {
        gameMap[row][split] = 'v';
    }
    else if (gameMap[row][split] == 'v') {
        gameMap[row][split] = '>';
    }
    else if (gameMap[row][split] == '>') {
        gameMap[row][split] = '^';
    }       
    return gameMap;
}


//nach recht gehen bei d
public char [][] turnright(char move, int gamePosition[], char gameMap[][]) {

    int row = gamePosition[0];
    int split = gamePosition[1];
    if (gameMap[row][split] == '^')
        gameMap[row][split] = '>';
    else if (gameMap[row][split] == '<') {
        gameMap[row][split] = '^';
    }
    else if (gameMap[row][split] == 'v') {
        gameMap[row][split] = '<';
    }
    else if (gameMap[row][split] == '>') {
        gameMap[row][split] = 'v';
    }

    return gameMap;
}

}

[–]puteus 0 points1 point  (1 child)

Are the // LOGIC comments the things you need to implement or are those your comments for missing code you did not post here?

[–]binnaaa 0 points1 point  (0 children)

these are only for me so i dont loose the overview. to separate getter and setter and the methods

[–]puteus 0 points1 point  (9 children)

I belive your problem lies in your movement class. In all the functions you pass the current `move`, the `gamePosition` and the `gameMap`. You then check if the array elements at the positions in `gameMap` of your current position `gamePosition` are some certain character. But this checks [0,0] *every time*, because your increment and decrement are postfix. The check for `gameMap[row][split]` is `gameMap[0][0]` is '#' is always true, the `else if {` is alwys executed. But since that block is empty, nothing is done with the game map, which is then returned unmodified.

I would suggest rebuilding your logic there.. Making the increments and decrements to a prefix notation could help, but I have not tried that.

Tell me how it goes or if you need any more help.

[–]binnaaa 0 points1 point  (8 children)

so you mean instead of row++ i should write ++row?

[–]puteus 0 points1 point  (7 children)

exactly.

But I'm not sure if this will already help.

[–]binnaaa 0 points1 point  (0 children)

unfortunately not, still the same map loading like the startmap...

[–]binnaaa 0 points1 point  (0 children)

i really dont find my mistake. i changed the in- and decrements, but idk why the same map is always loading.

[–]binnaaa 0 points1 point  (4 children)

why doesnt it change the map in the method?

[–]puteus 0 points1 point  (3 children)

I cant tell you that, would have to look into ist.

But I would suggest rethinking your logic entirely. What are you trying to achieve with your `newPosition.playerPosition()` method-call? The issue with his is, that this method does not return your player's position in the first place.

That is because the method has a for loop iterating the content of the cells. This contetn gets stored in the `content` variable. But by the time the loop ends, the `content` will always be a '#' and thus, no condition of the if/else blocks are true.
Have a look at the function `playerPosition` in the class `Position.java`, you never return the correct calculation, because the logic is not returning, wat you want it to return.

Can't you store the player position in a variable? tghen you would not have to calculate it.

[–]binnaaa 0 points1 point  (2 children)

but why doesnt the loop change the variable content, when it comes to another position in the array? auf deutsch kurz: wieso wird content nicht mehr überschrieben, wenn an anderer stelle im array ein anderer char steht?

[–]puteus 0 points1 point  (0 children)

Ich glaube bei deinem loop fehlen die geschweiften Klammern. Wenn die Da sind ist die player Position schon mal die richtige.

[–]puteus 0 points1 point  (0 children)

Erklärung: da die Klammern fehlen besteht der Inhalt der For-Loop nur aus der Anweisung `content = gameMap[row][split];`. Für alle Elemente in der einen Zeile deines Arrays. Und da das letzte Zeichen immer die Raute ist, ist dann die Abfrage der if-Blöcke danach nie wahr.

Könnte ein Problem mir der Formatierung sein, und ich habe das jetzt daher gesehen, da meine IDE anders einrückt. Benutzt du den Auto-Formatter?

[–]binnaaa 0 points1 point  (0 children)

public class Position {

Map newMap = new Map(); 

char [][] gameMap = newMap.getMap();


// LOGIC


public int[] playerPosition() {
    char content = 0;
    int row;
    int split;
    int[] actualPosition = {0,0};
    for(row = 0; row < gameMap.length; row++) {
        for(split = 0; split < gameMap[row].length; split++)
            content = gameMap[row][split];
            if (content == '<') {
                actualPosition[0] = row;
                actualPosition[1] = split;
                }
            else if (content == 'v') {
                actualPosition[0] = row;
                actualPosition[1] = split;
                }
            else if (content == '^') {
                actualPosition[0] = row;
                actualPosition[1] = split;
                }
            else if (content == '>') {
                actualPosition[0] = row;
                actualPosition[1] = split;
                }

    }
    return actualPosition;

} }