mirror of
				https://github.com/lov3b/Schack.git
				synced 2025-10-31 05:10:22 +01:00 
			
		
		
		
	Nu funkar schack checken för bönder
This commit is contained in:
		| @@ -33,7 +33,6 @@ public class Pawn extends PieceKnownIfMoved { | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) { |     public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) { | ||||||
|         // TODO: Lösa bugg där bunder på kanterna inte kan röra sig |  | ||||||
|         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 |         // Om bonden har gått en gång, får gå 1 steg, annars 2 | ||||||
| @@ -41,8 +40,6 @@ public class Pawn extends PieceKnownIfMoved { | |||||||
|  |  | ||||||
|         // Kolla om man kan gå rakt frak |         // Kolla om man kan gå rakt frak | ||||||
|         for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { |         for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { | ||||||
|             System.out.println("this.position.x: " + this.position.x); |  | ||||||
|             System.out.println("calced y: " + (this.position.y + (this.white ? -pawnDY : pawnDY))); |  | ||||||
|             Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY)); |             Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY)); | ||||||
|             boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); |             boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected); | ||||||
|             if (shouldBreak) { |             if (shouldBreak) { | ||||||
| @@ -68,7 +65,8 @@ public class Pawn extends PieceKnownIfMoved { | |||||||
|         try { |         try { | ||||||
|             Piece p = pieces[pos.x][pos.y]; |             Piece p = pieces[pos.x][pos.y]; | ||||||
|             if (p.white != this.white) { |             if (p.white != this.white) { | ||||||
|                 movable.add(pos); |                 tryToMoveAndCheckIfCheck(pieces, movable, pos); | ||||||
|  |  | ||||||
|             } |             } | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|         } |         } | ||||||
| @@ -98,7 +96,7 @@ public class Pawn extends PieceKnownIfMoved { | |||||||
|             } |             } | ||||||
|         } catch (NullPointerException npe) { |         } catch (NullPointerException npe) { | ||||||
|             // This is an empty spot |             // This is an empty spot | ||||||
|             movable.add(pos); |             tryToMoveAndCheckIfCheck(pieces, movable, pos); | ||||||
|         } catch (IndexOutOfBoundsException ioobe) { |         } catch (IndexOutOfBoundsException ioobe) { | ||||||
|             // This means that the player is at the edge |             // This means that the player is at the edge | ||||||
|             System.out.println(pos); |             System.out.println(pos); | ||||||
|   | |||||||
| @@ -84,9 +84,32 @@ public abstract class Piece { | |||||||
|                 // Vi kan ta men inte gå längre. |                 // Vi kan ta men inte gå längre. | ||||||
|                 if (!isSelected) { |                 if (!isSelected) { | ||||||
|                     movable.add(pos); |                     movable.add(pos); | ||||||
|  |                 } else { | ||||||
|  |                     tryToMoveAndCheckIfCheck(pieces, movable, pos); | ||||||
|  |                 } | ||||||
|                 return true; |                 return true; | ||||||
|  |  | ||||||
|  |             } | ||||||
|  |         } catch (NullPointerException npe) { | ||||||
|  |             // Detta är en tom plats, vi ska inte breaka | ||||||
|  |             if (!isSelected) { | ||||||
|  |                 movable.add(pos); | ||||||
|  |                 return false; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             tryToMoveAndCheckIfCheck(pieces, movable, pos); | ||||||
|  |             return false; | ||||||
|  |  | ||||||
|  |         } catch (IndexOutOfBoundsException ioobe) { | ||||||
|  |             // This means that the player is at the edge | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             // For good meassure | ||||||
|  |         } | ||||||
|  |         return false; | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     void tryToMoveAndCheckIfCheck(Piece[][] pieces, LinkedHashSet movable, Point pos) { | ||||||
|         // Kom ihåg vart vi var |         // Kom ihåg vart vi var | ||||||
|         Point previousPosition = new Point(this.position); |         Point previousPosition = new Point(this.position); | ||||||
|  |  | ||||||
| @@ -108,43 +131,6 @@ public abstract class Piece { | |||||||
|         if (!inSchack) { |         if (!inSchack) { | ||||||
|             movable.add(pos); |             movable.add(pos); | ||||||
|         } |         } | ||||||
|                 return true; |  | ||||||
|  |  | ||||||
|             } |  | ||||||
|         } catch (NullPointerException npe) { |  | ||||||
|             // Detta är en tom plats, vi ska inte breaka |  | ||||||
|             if (!isSelected) { |  | ||||||
|                 movable.add(pos); |  | ||||||
|                 return false; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Kom ihåg vart vi var |  | ||||||
|             Point previousPosition = new Point(this.position); |  | ||||||
|  |  | ||||||
|             // Testa att flytta |  | ||||||
|             pieces[pos.x][pos.y] = this; |  | ||||||
|             pieces[previousPosition.x][previousPosition.y] = null; |  | ||||||
|             this.position = new Point(pos); |  | ||||||
|  |  | ||||||
|             boolean inSchack = checkIfSchack(pos, pieces); |  | ||||||
|  |  | ||||||
|             // Flytta tillbaka |  | ||||||
|             pieces[previousPosition.x][previousPosition.y] = this; |  | ||||||
|             pieces[pos.x][pos.y] = null; |  | ||||||
|             this.position = new Point(previousPosition); |  | ||||||
|  |  | ||||||
|             if (!inSchack) { |  | ||||||
|                 movable.add(pos); |  | ||||||
|             } |  | ||||||
|             return false; |  | ||||||
|  |  | ||||||
|         } catch (IndexOutOfBoundsException ioobe) { |  | ||||||
|             // This means that the player is at the edge |  | ||||||
|         } catch (Exception e) { |  | ||||||
|             // For good meassure |  | ||||||
|         } |  | ||||||
|         return false; |  | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     boolean checkIfSchack(Point pos, Piece[][] pieces) { |     boolean checkIfSchack(Point pos, Piece[][] pieces) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 loveb
					loveb