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,29 +84,8 @@ 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); | ||||||
|                     return true; |                 } else { | ||||||
|                 } |                     tryToMoveAndCheckIfCheck(pieces, movable, pos); | ||||||
|  |  | ||||||
|                 // Kom ihåg vart vi var |  | ||||||
|                 Point previousPosition = new Point(this.position); |  | ||||||
|  |  | ||||||
|                 // Kom ihåg motståndarpjäs |  | ||||||
|                 Piece opponentPiece = pieces[pos.x][pos.y]; |  | ||||||
|  |  | ||||||
|                 // 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] = opponentPiece; |  | ||||||
|                 this.position = new Point(previousPosition); |  | ||||||
|  |  | ||||||
|                 if (!inSchack) { |  | ||||||
|                     movable.add(pos); |  | ||||||
|                 } |                 } | ||||||
|                 return true; |                 return true; | ||||||
|  |  | ||||||
| @@ -118,24 +97,7 @@ public abstract class Piece { | |||||||
|                 return false; |                 return false; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             // Kom ihåg vart vi var |             tryToMoveAndCheckIfCheck(pieces, movable, pos); | ||||||
|             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; |             return false; | ||||||
|  |  | ||||||
|         } catch (IndexOutOfBoundsException ioobe) { |         } catch (IndexOutOfBoundsException ioobe) { | ||||||
| @@ -147,6 +109,30 @@ public abstract class Piece { | |||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     void tryToMoveAndCheckIfCheck(Piece[][] pieces, LinkedHashSet movable, Point pos) { | ||||||
|  |         // Kom ihåg vart vi var | ||||||
|  |         Point previousPosition = new Point(this.position); | ||||||
|  |  | ||||||
|  |         // Kom ihåg motståndarpjäs | ||||||
|  |         Piece opponentPiece = pieces[pos.x][pos.y]; | ||||||
|  |  | ||||||
|  |         // 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] = opponentPiece; | ||||||
|  |         this.position = new Point(previousPosition); | ||||||
|  |  | ||||||
|  |         if (!inSchack) { | ||||||
|  |             movable.add(pos); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     boolean checkIfSchack(Point pos, Piece[][] pieces) { |     boolean checkIfSchack(Point pos, Piece[][] pieces) { | ||||||
|         boolean ourColor = this.isWhite(); |         boolean ourColor = this.isWhite(); | ||||||
|         Piece selectedPiece = this; |         Piece selectedPiece = this; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 loveb
					loveb