Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Model/Board.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
56 changes: 56 additions & 0 deletions Model/King.java
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 22 additions & 0 deletions Model/Knight.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions Model/Piece.java
Original file line number Diff line number Diff line change
@@ -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);
}
46 changes: 46 additions & 0 deletions Model/Spot.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
44 changes: 44 additions & 0 deletions Model/soundsystem/soundMakerwaw.java
Original file line number Diff line number Diff line change
@@ -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();
}





}

Binary file added Model/soundsystem/sounds/sax.mp3
Binary file not shown.