Sitzung: Jeden Freitag in der Vorlesungszeit ab 16 Uhr c. t. im MAR 0.005. In der vorlesungsfreien Zeit unregelmäßig (Jemensch da?). Macht mit!

Javakurs/Übungsaufgaben/Doom/Musterlösung

Dies ist eine kurze Musterlösung.

Sie implementiert eine schnellere Abart der Übung, so wurde komplett auf Kapselung und die Terminalklasse verzichtet. Auch könnten einige Kommentare sowie einige schönere CodeConventions fehlen. Bitte fühlt euch ermächtigt änderungen einzupflegen ...

MFG Mario


import java.io.*; 
public class ASCIIDoom{
	protected class Position {
		public int x;
		public int y;
		
		public Position(int _x, int _y)	{
			this.x = _x;
			this.y = _y;
		}
		
		public int toIndex(int iWidth) {
			return x + y*iWidth;
		}
	}
	
	protected class GameTile {
		public Position pos;
		
		public GameTile() {
			pos = new Position(0,0);
		}
		
		public String toString() {
			return "~";
		}
		
		public String draw(String s, int worldLength) {
			int index = pos.toIndex( worldLength );
			char[] chars = s.toCharArray();
			chars[index] = this.toString().charAt(0);
			return new String(chars);
		}
	}
	
	protected class WallTile extends GameTile {
		public String toString() {
			return "#";
		}
	}

	protected class EmptyTile extends GameTile {
		public String toString() {
			return " ";
		}
	}
		
	protected class Player extends GameTile {
		public String toString() {
			return "@";
		}
		public void moveLeft() {
			pos.x--;
		}
		public void moveRight() {
			pos.x++;
		}
		public void moveUp() {
			pos.y--;
		}
		public void moveDown() {
			pos.y++;
		}
	}
	
	protected class GameField {
		public GameTile[][] field;
		public int width;
		public int height;
		
		GameField( int _width, int _height, String stringField )
		{
			this.width=_width;
			this.height=_height;
			field = new GameTile[width][height];
			
			for( int y = 0; y < height; ++y) {
				for( int x = 0; x < width; ++x) {
					switch( stringField.charAt(x + y*width) ){
						case '#':
							field[x][y] = new WallTile();
							break;
						case ' ':
						default:
							field[x][y] = new EmptyTile();
							break;
					}
				}
			}
		} // constructor
		
		public String toString() {
			String result="";
			for( int y = 0; y < height; ++y) {
				for( int x = 0; x < width; ++x) {
					result += field[x][y].toString();
				}
			}
			
			return result;
		}
	}
	
	protected class World {
		public GameField field;
		public Player player;
		
		public World() {
			field = new GameField(3, 5, "#### ## ## ####");
			player = new Player();
			player.pos.x = 1;
			player.pos.y = 1;
		}
		
		public void keyPressed(String s) {
			if( s.isEmpty() ) {
				return;
			}
			switch( s.charAt(0) ) {
				case 'w':
					player.moveUp();
					break;
				case 's':
					player.moveDown();
					break;
				case 'a': 
					player.moveLeft();
					break;
				case 'd':
					player.moveRight();
					break;
				default:
					System.out.println("Key not found");
					break;
			}
		}
		
		public String toString() {
			String s = field.toString();
			s = player.draw(s,field.width);
			
			String result = "\\ ";
			for( int column = 0; column < field.width; ++column) {
				result += (column % 10);
			}
			result += "\n";
			
			for( int row = 0; row < s.length()/field.width; ++row) {
				result += row%10 + " " + s.substring( row*field.width, (row+1)*field.width) + "\n";
			}
			return result;
		}
	}
				

	public static void main(String [] argv) throws IOException
	{
		java.io.DataInputStream in = new java.io.DataInputStream(System.in);
		test t = new test();
		World w = t.new World();

		System.out.println("ESC+ENTER to exit");    		
		String key = in.readLine();

    		while(key != "q") {
			w.keyPressed(key);
			System.out.println(w);
			key = in.readLine();
		}	
	}
 }