mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-11-03 22:50:24 +01:00 
			
		
		
		
	Pawn funkar förutom enpassant
This commit is contained in:
		@@ -6,6 +6,8 @@ import java.util.LinkedHashSet;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
public class Pawn extends Piece {
 | 
					public class Pawn extends Piece {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private boolean hasMoved = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public Pawn(boolean isWhite, Point startingPosition) throws IOException {
 | 
					    public Pawn(boolean isWhite, Point startingPosition) throws IOException {
 | 
				
			||||||
        super(isWhite, startingPosition);
 | 
					        super(isWhite, startingPosition);
 | 
				
			||||||
        setPieceIcon("Pawn");
 | 
					        setPieceIcon("Pawn");
 | 
				
			||||||
@@ -19,8 +21,68 @@ public class Pawn extends Piece {
 | 
				
			|||||||
    public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
 | 
					    public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
 | 
				
			||||||
        LinkedHashSet<Point> movable = new LinkedHashSet<>();
 | 
					        LinkedHashSet<Point> movable = new LinkedHashSet<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Om bonden har gått en gång, får gå 1 steg, annars 2
 | 
				
			||||||
 | 
					        final int upTo = hasMoved ? 1 : 2;
 | 
				
			||||||
 | 
					        // Kolla om man kan gå rakt frak
 | 
				
			||||||
 | 
					        for (int pawnX = 1; pawnX <= upTo; pawnX++) {
 | 
				
			||||||
 | 
					            Point pos = new Point(this.position.x, this.position.y - pawnX);
 | 
				
			||||||
 | 
					            boolean shouldBreak = checkMove(pos, movable, pieces);
 | 
				
			||||||
 | 
					            if (shouldBreak) {
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return new LinkedHashSet<>();
 | 
					        // Logik för att ta
 | 
				
			||||||
 | 
					        for (int pawnX : new int[]{-1, 1}) {
 | 
				
			||||||
 | 
					            // Position vi kollar just nu, snett upp åt höger & vänster
 | 
				
			||||||
 | 
					            Point pos = new Point(this.position.x + pawnX, this.position.y - 1);
 | 
				
			||||||
 | 
					            Piece p = pieces[pos.x][pos.y];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Ifall det är en pjäs som står här och den inte är samma färg som oss, lägg till
 | 
				
			||||||
 | 
					            if (p != null && p.isWhite != this.isWhite) {
 | 
				
			||||||
 | 
					                movable.add(pos);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return movable;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    protected boolean checkMove(Point pos, LinkedHashSet movable, Piece[][] pieces) {
 | 
				
			||||||
 | 
					        // Instead of checking index and null, try-catch
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            // Ifall vi kollar utanför brädet kommer detta att faila
 | 
				
			||||||
 | 
					            Piece p = pieces[pos.x][pos.y];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Ifall pjäsen här har samma färg som oss, break
 | 
				
			||||||
 | 
					            // Ifall det inte är någon pjäs här kommer det att gå ner till
 | 
				
			||||||
 | 
					            // catch(NullPointerException) och då lägger vi till detta drag i listan
 | 
				
			||||||
 | 
					            // Ifall det är inte är en pjäs här, kasta ett NullPointerException
 | 
				
			||||||
 | 
					            // Detta är för att vara så lik super-implementationen som möjligt
 | 
				
			||||||
 | 
					            if (p == null) {
 | 
				
			||||||
 | 
					                throw new NullPointerException();
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                // Detta betyder att det finns en pjäs här
 | 
				
			||||||
 | 
					                // Vi kan ta men inte gå längre.
 | 
				
			||||||
 | 
					                return true;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } catch (NullPointerException npe) {
 | 
				
			||||||
 | 
					            // This is an empty spot
 | 
				
			||||||
 | 
					            movable.add(pos);
 | 
				
			||||||
 | 
					        } catch (IndexOutOfBoundsException ioobe) {
 | 
				
			||||||
 | 
					            // This means that the player is at the edge
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            // For good meassure
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void move(Piece[][] pieces, Point toMove, Point selected) {
 | 
				
			||||||
 | 
					        // Detta är för att veta ifall vi kan gå 2 steg eller inte
 | 
				
			||||||
 | 
					        hasMoved = true;
 | 
				
			||||||
 | 
					        super.move(pieces, toMove, selected);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user