Results 1 to 13 of 13
- 02-10-2012, 03:37 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Filling an array using a .txt file
Hello, I'm new to Java and am working on a small project for class.
The project is a simplified version of PacMan using an array[8][8] as the map. I'm required to make the array by reading from a .txt file that looks like this:
... And so on.Java Code:Wall 1 1 Wall 2 3 Wall 2 4 Wall 2 5 Wall 3 3 Wall 3 4 Ghost 2 3 Ghost 5 6
The method I'm supposed to create has to read the text file and insert either a Wall or Ghost in its position according to the coordinates (for example, there's a wall in array[1][1])
Now, i've managed to read from the file, but can't seem to make the walls and ghosts...
This is the first code I tried
However, this isn't working as it doesn't recognize the string as an element. I've been told by a more senior student to use splits instead, and came up with thisJava Code:public static void leer() throws IOException { java.util.Scanner s = new java.util.Scanner(new File("/Users/Viktor/Documents/University/5th Trimester/Data Structure/Projects/Project 1/MetroPacman/src/inicio.txt")); while (s.hasNext()) { if (s.next()=="Wall") { int i = s.nextInt(); int j = s.nextInt(); Tablero[i][j]=new Wall(); } else if (s.next()=="Ghost") { int i = s.nextInt(); int j = s.nextInt(); Tablero[i][j]=new Ghost(); } } }
But I'm stumped from there on...Java Code:{ String[] str = s.nextLine().split("\\s+"); int[] nums = new int[str.length -1]; for(int i = 0; i < nums.length; i++) { nums[i] = Integer.parseInt(str[i]); } int x = nums[0]; int y = nums[1]; Tablero[x][y]=
Any help would be appreciated on how to go about with this.
- 02-10-2012, 04:39 AM #2
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Filling an array using a .txt file
Do not compare value of String objects using ==, because it is reference comparator.
Use equals() method instead.
My suggestion is that You create String object that will represent first token of the row in file, say:
String token = s.next;
After that use if..else and decide what to do in case it is wall or ghost.
Or, another solution is to recieve complete row from file as one token, say,
String token = s.nextLine(); and after that You can operate on String object as You like, say, extract substring, split string etc.
- 02-11-2012, 02:27 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Re: Filling an array using a .txt file
Thanks! That piece of advice did help out a lot, the program reads the .txt, makes the array and shows it, just what I needed.
I hate to be a bother, but I gotta ask for some more advice, this time on moving the character (PacMan).
I've got a PacMan class that has the methods MoveU(), MoveD() and so on, and a class called Player that calls these methods depending on the keyboard input received.
This is what they look like:
PacMan
I didn't copy all the methods, the movement in other directions follow the same pattern as the first one.Java Code:public static int MoverD() { for (int i=0;i<Tablero.length;i++) { for (int j=0;j<Tablero.length;j++) { if (Tablero[i][j] instanceof PacMan) return i-1; } } return 0; } public int getPosicionI() { for (int i=0;i<Tablero.length;i++) { for (int j=0;j<Tablero.length;j++) { if (Tablero[i][j] instanceof PacMan) return i; } } return -1; } public int getPosicionJ() { for (int i=0;i<Tablero.length;i++) { for (int j=0;j<Tablero.length;j++) { if (Tablero[i][j] instanceof PacMan) { return j; } } } return -1; } public void setPosicion(int x, int y) { for (int i=0;i<Tablero.length;i++) { for (int j=0;j<Tablero.length;j++) { if (Tablero[i][j] instanceof PacMan) { Tablero[i][j]=null; } } } Tablero[x][y]=new PacMan(); }
Here's the player class
I think it's somewhat inefficient, but it's what I wrote off the top of my head. What I want to know is how to give the SetPosicion() the two parameters it needs, using the returns from the individual movement methods... Or any other more efficient way would work too...Java Code:public void Movimiento() throws IOException { { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); String mov = read.readLine(); if (mov.equals("w")) { PacMan.setPosicion(PacMan.MoverU()); } else if (mov.equals("s")) { PacMan.MoverD(); } else if (mov.equals("a")) { PacMan.MoverL(); } else if (mov.equals("d")) { PacMan.MoverR(); } } }
Thanks again!
- 02-11-2012, 03:08 AM #4
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Filling an array using a .txt file
This:
I don't think is necessary, since You already know one row(one line) has only two integer values, and only one string literal.Java Code:String[] str = s.nextLine().split("\\s+"); int[] nums = new int[str.length -1]; for(int i = 0; i < nums.length; i++) { nums[i] = Integer.parseInt(str[i]); } int x = nums[0]; int y = nums[1]; Tablero[x][y]=
So You could rather say just:
while str[0] represents string literal, "wall" or "ghost".Java Code:int x = Integer.parseInt(str[1]); int y = Integer.parseInt(str[2]);
-------------
Can You explain, what MoverD() sholud do, because as I can see, it seems no logical for me, as I would expect method MoverD() to set some value, rather than just to return a value?Last edited by diamonddragon; 02-11-2012 at 03:11 AM.
- 02-11-2012, 03:22 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Re: Filling an array using a .txt file
You're absolutely right, I don't know what I was thinking xDCan You explain, what MoverD() sholud do, because as I can see, it seems no logical for me, as I would expect method MoverD() to set some value, rather than just to return a value?
I've changed it, this should do the trick now (i hope
Java Code:public void MoverD() { for (int i=0;i<Tablero.length;i++) { for (int j=0;j<Tablero.length;j++) { if (Tablero[i][j] instanceof PacMan) Tablero[i][j]=null; Tablero[i-1][j]=new PacMan(); } } }
- 02-11-2012, 03:30 AM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Filling an array using a .txt file
I still don't get it what You want to do with that MoverD() method.
- 02-11-2012, 03:31 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
- 02-11-2012, 03:46 AM #8
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
- 02-11-2012, 04:01 AM #9
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Re: Filling an array using a .txt file
Is there a way I can do that with just one method?
I had two methods for getPosition, one for X and one for Y, and two for setPosition...
Then again, the get methods run through the whole array looking for the PacMan element and then return it's horizontal or vertical coordinate... There must be a better way to do this, right?
- 02-11-2012, 04:36 AM #10
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Filling an array using a .txt file
You have 2 dimensional board, represented with 2 dimensional array.
When You loop through that board, and find packman, You have its position which means You have packMan x and y coordinate.
So only one method is enough for retrieving position.
Setting position is simple as cake, as You just have to say to board where You want to put PackMan, say, board[3][6] = "PackMan";
"PackMan" may be represented by string literal, as above, or You could use PackMan object(say instance of Figure Class).
In that case, board would be represented, say, as array of figure objects, where figures are PackMan, Ghost and Wall, say Figures[][] = new Figures[a][b];
a and b represents board length, as in case of chess board a and b would be 8. (7 because array use 0 indexing).
Is You use instances of Figures, You could for each instance set datafield position, which will represent position of figure object on the board.
Say if You have figure PackMan(instance of Figure class), You may get its position like this:
int[] position = figure.getposition();
Where figure is instance of Figure class, say Figure figure = new Figure(int posX, int posY, String type) where type represents type of figure(Wall, Ghost or PackMan).
eg:etc...Java Code:eg. class Figure { private int posX; private int posY; private String type; public Figure(int posX, int posY, String type) { this.posX = posX; etc... } public int[] getPosition() { int[] position = new int[2]; position[0] = this.posX; position[1] = this.posY; return position; } public void setPosition(int posX, int posY) { this.posX = posX; etc... } }Last edited by diamonddragon; 02-11-2012 at 04:47 AM.
- 02-11-2012, 05:59 PM #11
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Re: Filling an array using a .txt file
Ok, I think I get what you mean, and I've tried adding those methods to my program, and while it runs without error, when I try to move PacMan, it simply shows me the same thing, it doesn't move at all...
- 02-11-2012, 09:00 PM #12
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
- 02-11-2012, 09:08 PM #13
Member
- Join Date
- Feb 2012
- Posts
- 7
- Rep Power
- 0
Re: Filling an array using a .txt file
Certainly, taking into account what was said here and some other advice, I was able to get the PacMan movement methods to work. Back in my Element class, the show method looks like this
For some reason, it ALWAYS shows PacMan's initial position as Tablero[0][0].... Am I doing something wrong?Java Code:public static void mostrar(){ PacMan p = new PacMan(6,2); Tablero[PacMan.getY()][PacMan.getX()]=p; for (int i=0;i<Tablero.length;i++) { for (int j=0;j<Tablero.length;j++) { if (Tablero[i][j] instanceof Fantasma) { System.out.print("$"); } else if (Tablero[i][j] instanceof PacMan) { System.out.print("@"); } else if (Tablero[i][j] instanceof Fruta) { System.out.print("+"); } else if (Tablero[i][j] instanceof Simple) { System.out.print("*"); } else if (Tablero[i][j] instanceof Pared) { System.out.print("#"); } else if (Tablero[i][j] == null) { System.out.print(" "); } System.out.print(" "); } System.out.println(" "); }
Similar Threads
-
Filling a multidimensional array, and then change it
By MaceMan in forum New To JavaReplies: 2Last Post: 03-24-2011, 10:43 PM -
Help with filling an array
By MaceMan in forum New To JavaReplies: 6Last Post: 03-23-2011, 03:10 PM -
filling an array of Address objects..?
By hiei_yasha in forum New To JavaReplies: 9Last Post: 02-20-2011, 11:25 PM -
Filling an array from the return value of the function
By alex1988 in forum Java AppletsReplies: 7Last Post: 02-02-2011, 09:29 AM -
Filling 2D Array
By Nakira in forum New To JavaReplies: 3Last Post: 11-12-2008, 12:43 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks