Lektion 3

Nu kan man gå med kung
This commit is contained in:
loveb 2022-03-08 16:19:36 +01:00
parent f00f0af75d
commit f7d601491c
9 changed files with 195 additions and 48 deletions

View File

@ -56,10 +56,6 @@ public class Bishop extends Piece {
}
}
@Override
public void move(Piece[][] pieces) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public String toString() {

View File

@ -5,15 +5,20 @@ import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import javax.swing.JPanel;
public class Board extends JPanel {
public class Board extends JPanel implements MouseListener {
public static final int SIZE_OF_TILE = 100;
private Piece[][] pieces = new Piece[8][8];
private LinkedHashSet<Point> validMovesToDraw = new LinkedHashSet<>();
private Point selectedPiece = new Point();
private Color moveableColor = new Color(200, 200, 200);
public Board() throws IOException {
@ -30,7 +35,7 @@ public class Board extends JPanel {
{new Bishop(false, new Point(2, 0)), null, null, null, null, null, null, new Bishop(true, new Point(2, 7))},
{new Queen(false, new Point(3, 0)), null, null, null, null, null, null, new Queen(true, new Point(3, 7))},
{new King(false), null, null, null, null, null, null, new King(true)},
{null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, new King(false, new Point(5, 7))},
{null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null}
};
@ -70,13 +75,19 @@ public class Board extends JPanel {
Graphics2D g2 = (Graphics2D) g;
drawSquares(g2);
// måla alla ställen man kan ¨till
validMovesToDraw.forEach(point -> {
if (point != null) {
g2.setColor(moveableColor);
g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE);
}
});
// Draw piece
Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
if (piece != null) {
piece.draw(g2);
}
}));
// Check valid moves method
// Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
// if (piece != null) {
@ -88,23 +99,24 @@ public class Board extends JPanel {
// System.out.println("x:" + piece.position.x + ", y:" + piece.position.y + ": " + validMoves.size());
// }
// }));
// Check valid moves method
for (int y = 0; y < pieces.length; y++) {
for (int x = 0; x < pieces.length; x++) {
Piece p = pieces[y][x];
if (p == null) {
continue;
// draw valid moves
// for (int y = 0; y < pieces.length; y++) {
// for (int x = 0; x < pieces.length; x++) {
// Piece p = pieces[y][x];
// if (p == null) {
// continue;
// }
//
// LinkedHashSet<Point> validMoves = p.validMoves(pieces);
// Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
// g2.setColor(c);
// validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
// }
// }
}
LinkedHashSet<Point> validMoves = p.validMoves(pieces);
Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
g2.setColor(c);
validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
}
}
}
private void printPieces(Piece[][] pieces) {
public static void printPieces(Piece[][] pieces) {
System.out.println("");
for (int loopX = 0; loopX < pieces.length; loopX++) {
for (int loopY = 0; loopY < pieces.length; loopY++) {
@ -149,4 +161,60 @@ public class Board extends JPanel {
}
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
Point clicked = new Point(mouseCoordinateX, mouseCoordinateY);
// Ifall vi har tryckt en pjäs och sedan ska dit
if (validMovesToDraw.contains(clicked)) {
try {
Piece p = pieces[selectedPiece.x][selectedPiece.y];
p.move(pieces, clicked, selectedPiece);
validMovesToDraw.clear();
System.out.println("came here");
} catch (Exception e) {
}
} else {
selectedPiece = new Point(clicked);
validMovesToDraw.clear();
}
System.out.println("X: " + mouseCoordinateX + ", Y: " + mouseCoordinateY);
try {
Piece p = pieces[mouseCoordinateX][mouseCoordinateY];
LinkedHashSet validMoves = p.validMoves(pieces);
System.out.println("valid moves " + validMoves);
moveableColor = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
validMovesToDraw.addAll(validMoves);
System.out.println("valid moves to draw " + validMovesToDraw);
} catch (Exception e) {
validMovesToDraw.clear();
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}

21
src/schack/Horse.java Normal file
View File

@ -0,0 +1,21 @@
package schack;
import java.awt.Point;
import java.io.IOException;
import java.util.LinkedHashSet;
public class Horse extends Piece {
public Horse(boolean isWhite, boolean isLeft, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("Horse");
}
@Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
return new LinkedHashSet<>();
}
}

View File

@ -13,10 +13,7 @@ public final class King extends Piece {
public King(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
String colorName = isWhite ? "White" : "Black";
String fileName = colorName + "King.png";
InputStream is = King.class.getResourceAsStream("../img/" + fileName);
icon = ImageIO.read(is);
setPieceIcon("King");
}
public King(boolean isWhite) throws IOException {
@ -63,12 +60,6 @@ public final class King extends Piece {
}
@Override
public void move(Piece[][] pieces) {
eglibleForCastling = false;
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public String toString() {
return "Piece{" + "eglibleForCastling=" + eglibleForCastling + "position=" + position + ", isWhite=" + isWhite + '}';

View File

@ -21,10 +21,6 @@ public class Pawn extends Piece {
return new LinkedHashSet<>();
}
@Override
public void move(Piece[][] pieces) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public String toString() {
return "Pawn{" + "position=" + position + ", isWhite=" + isWhite + '}';

View File

@ -16,7 +16,7 @@ public abstract class Piece extends Component {
public boolean isWhite;
protected BufferedImage icon;
public Piece(boolean isWhite, Point startingPosition) {
public Piece(boolean isWhite, Point startingPosition) throws IOException {
this.isWhite = isWhite;
this.position = startingPosition;
}
@ -48,7 +48,23 @@ public abstract class Piece extends Component {
);
}
public abstract void move(Piece[][] pieces);
public void move(Piece[][] pieces, Point toMove, Point selected) {
try {
System.out.println("toMove: " + toMove);
pieces[toMove.x][toMove.y] = this; //new Rook(true,new Point(toMove));
pieces[selected.x][selected.y] = null;
// varför funkar det nu? det borde inte funka nu.
System.out.println("equals: " + selected.equals(this.position));
this.position = new Point(toMove);
Board.printPieces(pieces);
} catch (Exception e) {
System.out.println("jmgfmhyfhm");
}
}
@Override
public String toString() {

20
src/schack/Queen.java Normal file
View File

@ -0,0 +1,20 @@
package schack;
import java.awt.Point;
import java.io.IOException;
import java.util.LinkedHashSet;
public class Queen extends Piece {
public Queen(boolean isWhite, Point point) throws IOException {
super(isWhite,point);
setPieceIcon("Queen");
}
@Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
return new LinkedHashSet<>();
}
}

20
src/schack/Rook.java Normal file
View File

@ -0,0 +1,20 @@
package schack;
import java.awt.Point;
import java.io.IOException;
import java.util.LinkedHashSet;
public class Rook extends Piece {
public Rook(boolean isWhite, Point startingPosition) throws IOException {
super(isWhite, startingPosition);
setPieceIcon("Rook");
}
@Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) {
return new LinkedHashSet<>();
}
}

View File

@ -1,10 +1,11 @@
package schack;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
@ -15,13 +16,16 @@ import javax.swing.JOptionPane;
*
* @author Love Billenius & Simon Hansson
*/
public class Schack extends JFrame {
public class Schack extends JFrame implements Runnable {
public Schack() throws IOException {
setTitle("Schack");
setAlwaysOnTop(false);
setResizable(false);
setContentPane(new Board());
Board board = new Board();
setContentPane(board);
getContentPane().addMouseListener(board);
Thread thread = new Thread(this);
// Create menu
JMenuBar menuBar = new JMenuBar();
@ -62,6 +66,7 @@ public class Schack extends JFrame {
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.run();
}
@ -70,4 +75,18 @@ public class Schack extends JFrame {
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(12);
} catch (InterruptedException ex) {
Logger.getLogger(Schack.class.getName()).log(Level.SEVERE, null, ex);
}
repaint();
}
}
}