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
22 changes: 22 additions & 0 deletions GITCOMMANDS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#### Branch. Lag alltid en ny branch hver gang du jobber med noe nytt.
- git branch // Viser de tilgjengelige branchene
- git checkout -b <navn-på-branch> // lager ny branch
- git branch -d <branch_name> // sletter branch

#### Navn på branch
- Kun små bokstaver og "-" indikerer mellomrom
- initialler/navn-på-branch
- Eksempel: hnl/oppdatere-readme

#### Hvordan pulle(hente) endringer. Gjøres hver gang man skal jobbe.
- git status
- git switch main
- git pull
- git switch <ønsket branch>
- git main //sørge for at din branch er oppdatert med main

#### Hvordan pushe endringer.
- git add . // legger til endringer i staging area
- git commit -m “Hva du har gjort” // lagrer add endringene i den lokale git historien din.
- git push -u origin <branch_navn> //sender de lokale endringene i git historien til de eksterne repositoriet. Nå kan andre hente endringene. Husk å lage en pull request. Bare git push hvis man har pushet branchen før.
- Lag en pull request på github hvor du spør en annen om å godkjenne hva du har gjort
46 changes: 46 additions & 0 deletions Model/Bishop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package Model;

// Bishop.java
public class Bishop extends Piece {

public Bishop(Color color, int row, int col) {
super(color, row, col, color == Color.WHITE ? "resources/white_bishop.png" : "resources/black_bishop.png");
}

@Override
public boolean isValidMove(int startRow, int startCol, int endRow, int endCol, Board board) {
int rowDiff = Math.abs(endRow - startRow);
int colDiff = Math.abs(endCol - startCol);

if (rowDiff != colDiff) {
return false; // Bishops move diagonally
}

// Check for obstructions in the path
int rowStep = (endRow > startRow) ? 1 : -1;
int colStep = (endCol > startCol) ? 1 : -1;
int row = startRow + rowStep;
int col = startCol + colStep;

while (row != endRow) {
if (board.getPiece(row, col) != null) {
return false; // Path is blocked
}
row += rowStep;
col += colStep;
}

// Check if the destination square has a piece of the same color
Piece destinationPiece = board.getPiece(endRow, endCol);
if (destinationPiece != null && destinationPiece.getColor() == getColor()) {
return false;
}

return true;
}

@Override
public PieceType getType() {
return PieceType.BISHOP;
}
}
90 changes: 90 additions & 0 deletions Model/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package Model;

// Board.java
public class Board {
private Piece[][] board;
private static final int BOARD_SIZE = 8;

public Board() {
board = new Piece[BOARD_SIZE][BOARD_SIZE];
initializeBoard();
}

public void initializeBoard() {
// Initialize white pieces
board[7][0] = new Rook(Piece.Color.WHITE, 7, 0);
board[7][1] = new Knight(Piece.Color.WHITE, 7, 1);
board[7][2] = new Bishop(Piece.Color.WHITE, 7, 2);
board[7][3] = new Queen(Piece.Color.WHITE, 7, 3);
board[7][4] = new King(Piece.Color.WHITE, 7, 4);
board[7][5] = new Bishop(Piece.Color.WHITE, 7, 5);
board[7][6] = new Knight(Piece.Color.WHITE, 7, 6);
board[7][7] = new Rook(Piece.Color.WHITE, 7, 7);
for (int i = 0; i < 8; i++) {
board[6][i] = new Pawn(Piece.Color.WHITE, 6, i);
}

// Initialize black pieces
board[0][0] = new Rook(Piece.Color.BLACK, 0, 0);
board[0][1] = new Knight(Piece.Color.BLACK, 0, 1);
board[0][2] = new Bishop(Piece.Color.BLACK, 0, 2);
board[0][3] = new Queen(Piece.Color.BLACK, 0, 3);
board[0][4] = new King(Piece.Color.BLACK, 0, 4);
board[0][5] = new Bishop(Piece.Color.BLACK, 0, 5);
board[0][6] = new Knight(Piece.Color.BLACK, 0, 6);
board[0][7] = new Rook(Piece.Color.BLACK, 0, 7);
for (int i = 0; i < 8; i++) {
board[1][i] = new Pawn(Piece.Color.BLACK, 1, i);
}

// Initialize empty squares
for (int row = 2; row < 6; row++) {
for (int col = 0; col < 8; col++) {
board[row][col] = null;
}
}
}

public Piece getPiece(int row, int col) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
return null; // Out of bounds
}
return board[row][col];
}

public void setPiece(int row, int col, Piece piece) {
board[row][col] = piece;
}

public void movePiece(int startRow, int startCol, int endRow, int endCol) {
Piece piece = getPiece(startRow, startCol);
if (piece != null) {
board[endRow][endCol] = piece;
board[startRow][startCol] = null;
piece.setRow(endRow); // Update the piece's position
piece.setCol(endCol);
}
}

public static int getBoardSize(){
return BOARD_SIZE;
}

public void printBoard() {
for (int row = 0; row < BOARD_SIZE; row++) {
System.out.print(8 - row + " |"); // Print row number for better readability

for (int col = 0; col < BOARD_SIZE; col++) {
Piece piece = getPiece(row, col);
if (piece == null) {
System.out.print(" . "); // Empty square
} else {
System.out.print(" " + piece + " "); // Print piece representation
}
}
System.out.println("|");
}
System.out.println(" --------------------------------");
System.out.println(" a b c d e f g h"); // Print column letters
}
}
112 changes: 112 additions & 0 deletions Model/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package Model;

// Game.java
import java.util.Scanner;

public class Game {
private Board board;
private Player player1;
private Player player2;
private Player currentPlayer;
private boolean gameOver;

public Game(String player1Name, String player2Name) {
board = new Board();
player1 = new Player(player1Name, Piece.Color.WHITE);
player2 = new Player(player2Name, Piece.Color.BLACK);
currentPlayer = player1;
gameOver = false;
}

public void startGame() {
Scanner scanner = new Scanner(System.in);

while (!gameOver) {
board.printBoard();
System.out.println(currentPlayer.getName() + "'s turn (" + currentPlayer.getColor() + ")");
System.out.print("Enter move (e.g., 'a2 a4'): ");
String moveStr = scanner.nextLine();

if (moveStr.equalsIgnoreCase("quit")) {
System.out.println("Game ended.");
break;
}

try {
Move move = parseMove(moveStr);

if (isValidMove(move)) {
executeMove(move);
switchTurn();
} else {
System.out.println("Invalid move. Try again.");
}
} catch (IllegalArgumentException e) {
System.out.println("Invalid move format. Use 'a2 a4' format.");
}
}

scanner.close();
}

private Move parseMove(String moveStr) {
String[] parts = moveStr.split(" ");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid move format");
}

String startSq = parts[0].trim().toLowerCase();
String endSq = parts[1].trim().toLowerCase();

int startCol = startSq.charAt(0) - 'a';
int startRow = 8 - Integer.parseInt(startSq.substring(1)); // Assuming row input is 1-8
int endCol = endSq.charAt(0) - 'a';
int endRow = 8 - Integer.parseInt(endSq.substring(1));

Piece movedPiece = board.getPiece(startRow, startCol);
Piece capturedPiece = board.getPiece(endRow, endCol);

if (movedPiece == null) {
throw new IllegalArgumentException("No piece at starting square.");
}
return new Move(startRow, startCol, endRow, endCol, movedPiece, capturedPiece);
}

private boolean isValidMove(Move move) {
int startRow = move.getStartRow();
int startCol = move.getStartCol();
int endRow = move.getEndRow();
int endCol = move.getEndCol();
Piece piece = move.getMovedPiece();

if (piece.getColor() != currentPlayer.getColor()) {
System.out.println("Not your piece.");
return false;
}

if (endRow < 0 || endRow >= Board.getBoardSize() || endCol < 0 || endCol >= Board.getBoardSize()) {
System.out.println("Out of bounds move.");
return false;
}

return piece.isValidMove(startRow, startCol, endRow, endCol, board);
}

private void executeMove(Move move) {
int startRow = move.getStartRow();
int startCol = move.getStartCol();
int endRow = move.getEndRow();
int endCol = move.getEndCol();
Piece piece = move.getMovedPiece();

board.movePiece(startRow, startCol, endRow, endCol);

//TODO: Implement promotion logic

//Check for game over conditions (checkmate, stalemate) - not implemented in this example
}

private void switchTurn() {
currentPlayer = (currentPlayer == player1) ? player2 : player1;
}
}
65 changes: 65 additions & 0 deletions Model/King.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package Model;

// King.java
public class King extends Piece {

private boolean hasMoved; // Flag to track if the King has moved (for castling)

public King(Color color, int row, int col) {
super(color, row, col, color == Color.WHITE ? "resources/white_king.png" : "resources/black_king.png");
this.hasMoved = false;
}

public boolean hasMoved() {
return hasMoved;
}

public void setHasMoved(boolean hasMoved) {
this.hasMoved = hasMoved;
}

@Override
public boolean isValidMove(int startRow, int startCol, int endRow, int endCol, Board board) {
int rowDiff = Math.abs(endRow - startRow);
int colDiff = Math.abs(endCol - startCol);

if (rowDiff > 1 || colDiff > 1) {
//Check for castling
if (!hasMoved && rowDiff == 0 && colDiff == 2){
//Check that the spaces are empty
int direction = (endCol > startCol) ? 1 : -1;
for (int col = startCol + direction; col != endCol; col += direction){
if (board.getPiece(startRow, col) != null){
return false;
}
}

//Check that the rook is there and hasn't moved
int rookCol = (direction == 1) ? 7 : 0;
Piece rook = board.getPiece(startRow, rookCol);
if (rook instanceof Rook){
Rook realRook = (Rook) rook;
//TODO: Implement hasMoved for rook
return true;
}
return false;
}
return false; // King can only move one square in any direction
}

// Check if the destination square has a piece of the same color
Piece destinationPiece = board.getPiece(endRow, endCol);
if (destinationPiece != null && destinationPiece.getColor() == getColor()) {
return false;
}

//TODO: Add Check checking logic

return true;
}

@Override
public PieceType getType() {
return PieceType.KING;
}
}
32 changes: 32 additions & 0 deletions Model/Knight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Model;

// Knight.java
public class Knight extends Piece {

public Knight(Color color, int row, int col) {
super(color, row, col, color == Color.WHITE ? "resources/white_knight.png" : "resources/black_knight.png");
}

@Override
public boolean isValidMove(int startRow, int startCol, int endRow, int endCol, Board board) {
int rowDiff = Math.abs(endRow - startRow);
int colDiff = Math.abs(endCol - startCol);

if (!((rowDiff == 2 && colDiff == 1) || (rowDiff == 1 && colDiff == 2))) {
return false; // Knights move in an L-shape
}

// Check if the destination square has a piece of the same color
Piece destinationPiece = board.getPiece(endRow, endCol);
if (destinationPiece != null && destinationPiece.getColor() == getColor()) {
return false;
}

return true;
}

@Override
public PieceType getType() {
return PieceType.KNIGHT;
}
}
9 changes: 9 additions & 0 deletions Model/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Model;

// Main.java
public class Main {
public static void main(String[] args) {
Game game = new Game("Alice", "Bob");
game.startGame();
}
}
Loading