Gå til innhold

Trenger nye ting å lære, hvor hente motivasjon?


Anbefalte innlegg

Har lest igjennom "introduction to java programming", av Liang og jeg føler jeg kan basics nå. Men hva mer er det å lære seg? Er det bare masse bibliotek jeg skal pugge nå?

 

Jeg vet ikke hva mer jeg kan lære om java. Hvor kan jeg hente motivasjon/oppgaver? :)

Endret av oransjeFugl
Lenke til kommentar
Videoannonse
Annonse

Fulgte rådet ditt og lagde sokoban. Var litt gøy :)

 

Sokoban.java:

package Sokoban;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Sokoban extends JFrame {
static final char PLAYER = '@';
static final char WALL = '#';
static final char BOX = '$';
static final char FLOOR = ' ';
static final char GOAL = '.';
static final char PLAYER_ON_GOAL = '+';
static final char BOX_ON_GOAL = '*';

static final String testLevel = "#####|#@$.#|#####";
static final String testLevel2 = 
											"#######|"+
											"#.@ # #|"+
											"#$* $ #|"+
											"#   $ #|"+
											"# ..  #|"+
											"#  *  #|"+
											"#######";

private GameLogic gl;
private JPanel p = new JPanel();

public Sokoban(String level) {
	setTitle("sokoban ");
	setSize(300, 300);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));

	gl = new GameLogic(level);
	printMap();

	addKeyListener(new KeyAdapter() {
		public void keyTyped(KeyEvent e) {
			char c = e.getKeyChar();
			if(c == 'w')
				gl.doMove(-1, 0);
			else if(c == 's')
				gl.doMove(1, 0);
			else if(c == 'a')
				gl.doMove(0, -1);
			else if(c == 'd')
				gl.doMove(0, 1);
			else if(c == 'q')
				System.exit(0);

			printMap();
		}
	});


	setVisible(true);
}

// console print and GUI update
public void printMap() {
	char[][] map = gl.getMap();
	int longest = gl.getLongest();
	String path = "src/Sokoban/images/";
	String path2 = "";
	ImageIcon pic;

	p.removeAll();
	p = new JPanel();
	p.setLayout(new GridLayout(map.length, longest));

	for(int i=0;i < map.length;i++) {
		for(int k=0;k < longest;k++) {
			if(map[i][k] == PLAYER)
				path2 = "mover16x16.png";
			else if(map[i][k] == WALL)
				path2 = "wall16x16.png";
			else if(map[i][k] == BOX)
				path2 = "movable16x16.png";
			else if(map[i][k] == GOAL)
				path2 = "target16x16.png";
			else if(map[i][k] == FLOOR)
				path2 = "blank16x16.png";
			else if(map[i][k] == PLAYER_ON_GOAL)
				path2 = "mover_on_target16x16.png";
			else if(map[i][k] == BOX_ON_GOAL)
				path2 = "movable_on_target16x16.png";

				pic = new ImageIcon(path+path2);

			p.add(new JLabel(pic));

			System.out.print(map[i][k]);
		}

		System.out.println();
	}
	add(p);
	validate(); // repaints the JLabels

	if(gl.checkWin()) {
		JOptionPane.showMessageDialog(this, "Congrats!");
		System.exit(0);
	}
}

public void recvInput() {

	Scanner in = new Scanner(System.in);
	String input = in.next();
	while(! input.equals("q")) {
		for(int i=0;i < input.length();i++) {
			char move = input.charAt(i);
			if(move == 'a')
				gl.doMove(0, -1);
			else if(move == 'd')
				gl.doMove(0, 1);
			else if(move == 'w')
				gl.doMove(-1, 0);
			else if(move == 's')
				gl.doMove(1, 0);
			else
				System.out.println("illegal input");
		}
		printMap(); 
		input = in.next();
	}
}

public static void main(String[] args) {
	Sokoban game1 = new Sokoban(testLevel2);
	// game1.recvInput();   no more console input.
}
}

 

GameLogic.java

package Sokoban;

public class GameLogic {
static final char PLAYER = '@';
static final char WALL = '#';
static final char BOX = '$';
static final char FLOOR = ' ';
static final char GOAL = '.';
static final char PLAYER_ON_GOAL = '+';
static final char BOX_ON_GOAL = '*';

private char PREV_PIECE;

private char[][] map;
private boolean[][] goals;

private int longest; // the longest line in the map. Used in array initialization
private int playerY, playerX;
private char prevMove = ' ';

public GameLogic(String level) {

	String[] parts = level.split("\\|");

	// figure out which line is the longest(for array creation)
	longest = 1;
	for(int i=0;i < parts.length;i++)
		if(parts[i].length() > longest)
			longest = parts[i].length();

	map = new char[parts.length][longest];
	goals = new boolean[parts.length][longest];

	// fill arrays with values
	for(int i=0;i < parts.length;i++) {
		String line = parts[i];
		for(int k=0;k < line.length();k++) {
			map[i][k] = line.charAt(k);

			// player position
			if(line.charAt(k) == '@') {
				playerY = i;
				playerX = k;
			}
			// goals
			if(line.charAt(k) == '.')
				goals[i][k] = true;
		}
	}
}

public void set(char c, int y, int x) {
	map[y][x] = c;
}

public char get(int y, int x) {
	if(y < 0 || x < 0 || x >= longest || y >= map.length)
		return 'X'; // random char different than FLOOR and GOAL

	return map[y][x];
}



public int getLongest() {
	return longest;
}

public void doMove(int dy, int dx) {
	int nextY = playerY+dy;
	int nextX = playerX+dx;

	// inside array?
	if(nextY < map.length && nextX < longest && nextY > -1 && nextX > -1) { // array bounds check
		char next = get(nextY, nextX);
		char next2 = get(nextY+dy, nextX+dx); // the next piece after the next piece. E.g. the wall behind the box.

		if(next != WALL ) { // walls?

			PREV_PIECE = ' ';

			if(prevMove == GOAL || prevMove == BOX_ON_GOAL)
				PREV_PIECE = '.';

			/* from here on there are many possible situations */

			if(next == FLOOR) {
				prevMove = get(playerY+dy, playerX+dx);
				set(PLAYER, playerY+dy, playerX+dx);
				set(PREV_PIECE, playerY, playerX);
				updatePos(dy, dx);					
			}
			else if(next == GOAL) {
				prevMove = get(playerY+dy, playerX+dx);
				set(PLAYER_ON_GOAL, playerY+dy, playerX+dx);
				set(PREV_PIECE, playerY, playerX);
				updatePos(dy, dx);
			}
			else if(next == BOX || next == BOX_ON_GOAL) {
				if(next2 == FLOOR) {
					prevMove = get(playerY+dy, playerX+dx);
					set(PLAYER, playerY+dy, playerX+dx);
					set(BOX, playerY+dy+dy, playerX+dx+dx);
					set(PREV_PIECE, playerY, playerX);
					updatePos(dy, dx);
				}
				else if(next2 == GOAL) {
					prevMove = get(playerY+dy, playerX+dx);
					set(PLAYER, playerY+dy, playerX+dx);
					set(BOX_ON_GOAL, playerY+dy+dy, playerX+dx+dx);
					set(PREV_PIECE, playerY, playerX);
					updatePos(dy, dx);						
				}
			}				
		}
		else {
			System.out.println("wall error");
		}
	}
}

public boolean checkWin() {
	for(int i=0;i < map.length;i++) {
		for(int k=0;k < longest;k++) {
			if(goals[i][k] == true && map[i][k] != BOX_ON_GOAL)
				return false;
		}
	}
	return true;
}
public void updatePos(int dy, int dx) {
	playerY += dy;
	playerX += dx;
}

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

}

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...