diff --git a/Model/Board.java b/Model/Board.java new file mode 100644 index 0000000..b56f5a5 --- /dev/null +++ b/Model/Board.java @@ -0,0 +1,50 @@ +package Model; + + + +public class Board { + Spot[][] boxes; + + public Board() + { + this.resetBoard(); + } + + public Spot getBox(int x, int y) + { + + if (x < 0 || x > 7 || y < 0 || y > 7) { + throw new Exception("Index out of bound"); + } + + return boxes[x][y]; + } + + public void resetBoard() + { + // initialize white pieces + boxes[0][0] = new Spot(0, 0, new Rook(true)); + boxes[0][1] = new Spot(0, 1, new Knight(true)); + boxes[0][2] = new Spot(0, 2, new Bishop(true)); + //... + boxes[1][0] = new Spot(1, 0, new Pawn(true)); + boxes[1][1] = new Spot(1, 1, new Pawn(true)); + //... + + // initialize black pieces + boxes[7][0] = new Spot(7, 0, new Rook(false)); + boxes[7][1] = new Spot(7, 1, new Knight(false)); + boxes[7][2] = new Spot(7, 2, new Bishop(false)); + //... + boxes[6][0] = new Spot(6, 0, new Pawn(false)); + boxes[6][1] = new Spot(6, 1, new Pawn(false)); + //... + + // initialize remaining boxes without any piece + for (int i = 2; i < 6; i++) { + for (int j = 0; j < 8; j++) { + boxes[i][j] = new Spot(i, j, null); + } + } + } +} diff --git a/Model/King.java b/Model/King.java new file mode 100644 index 0000000..592c279 --- /dev/null +++ b/Model/King.java @@ -0,0 +1,56 @@ +package Model; + +public class King extends Piece { + private boolean castlingDone = false; + + public King(boolean white) + { + super(white); + } + + public boolean isCastlingDone() + { + return this.castlingDone; + } + + public void setCastlingDone(boolean castlingDone) + { + this.castlingDone = castlingDone; + } + + @Override + public boolean canMove(Board board, Spot start, Spot end) + { + // we can't move the piece to a Spot that + // has a piece of the same color + if (end.getPiece().isWhite() == this.isWhite()) { + return false; + } + + int x = Math.abs(start.getX() - end.getX()); + int y = Math.abs(start.getY() - end.getY()); + if (x + y == 1) { + // check if this move will not result in the king + // being attacked if so return true + return true; + } + + return this.isValidCastling(board, start, end); + } + + private boolean isValidCastling(Board board, Spot start, Spot end) + { + + if (this.isCastlingDone()) { + return false; + } + + // Logic for returning true or false + } + + public boolean isCastlingMove(Spot start, Spot end) + { + // check if the starting and + // ending position are correct + } +} diff --git a/Model/Knight.java b/Model/Knight.java new file mode 100644 index 0000000..42084e1 --- /dev/null +++ b/Model/Knight.java @@ -0,0 +1,22 @@ +package Model; + +public class Knight extends Piece { + public Knight(boolean white) + { + super(white); + } + + @Override + public boolean canMove(Board board, Spot start, Spot end) + { + // we can't move the piece to a spot that has + // a piece of the same colour + if (end.getPiece().isWhite() == this.isWhite()) { + return false; + } + + int x = Math.abs(start.getX() - end.getX()); + int y = Math.abs(start.getY() - end.getY()); + return x * y == 2; + } +} diff --git a/Model/Piece.java b/Model/Piece.java new file mode 100644 index 0000000..7cb4d25 --- /dev/null +++ b/Model/Piece.java @@ -0,0 +1,34 @@ +package Model; + +public abstract class Piece { + + private boolean killed = false; + private boolean white = false; + + public Piece(boolean white) + { + this.setWhite(white); + } + + public boolean isWhite() + { + return this.white; + } + + public void setWhite(boolean white) + { + this.white = white; + } + + public boolean isKilled() + { + return this.killed; + } + + public void setKilled(boolean killed) + { + this.killed = killed; + } + + public abstract boolean canMove(Board board, Spot start, Spot end); +} diff --git a/Model/Spot.java b/Model/Spot.java new file mode 100644 index 0000000..73aba73 --- /dev/null +++ b/Model/Spot.java @@ -0,0 +1,46 @@ +package Model; + + + +public class Spot { + private Piece piece; + private int x; + private int y; + + public Spot(int x, int y, Piece piece) + { + this.setPiece(piece); + this.setX(x); + this.setY(y); + } + + public Piece getPiece() + { + return this.piece; + } + + public void setPiece(Piece p) + { + this.piece = p; + } + + public int getX() + { + return this.x; + } + + public void setX(int x) + { + this.x = x; + } + + public int getY() + { + return this.y; + } + + public void setY(int y) + { + this.y = y; + } +} diff --git a/Model/soundsystem/soundMakerwaw.java b/Model/soundsystem/soundMakerwaw.java new file mode 100644 index 0000000..d075860 --- /dev/null +++ b/Model/soundsystem/soundMakerwaw.java @@ -0,0 +1,44 @@ +package Model.soundsystem; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +public class soundMakerwaw { + + private Clip clip; + + // Hentet fra: https://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java + // hentet 23.04.2024, svart av Bilesh Ganguly, modifisert videre med hjelp av medstudent Jesper Hammer + + public soundMakerwaw(String filename) { + try { + this.clip = AudioSystem.getClip(); + AudioInputStream inputStream = AudioSystem.getAudioInputStream( + soundMakerwaw.class.getResourceAsStream("/sounds/" + filename)); + this.clip.open(inputStream); + } catch (Exception e) { + System.err.println(e); + } + } + + + public void play(){ + this.clip.setFramePosition(0); + this.clip.start(); + } + + public void loop(){ + this.clip.loop(-1); + this.play(); + } + + public void stop(){ + this.clip.stop(); + } + + + + + + } + diff --git a/Model/soundsystem/sounds/sax.mp3 b/Model/soundsystem/sounds/sax.mp3 new file mode 100644 index 0000000..d57bb9b Binary files /dev/null and b/Model/soundsystem/sounds/sax.mp3 differ