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
61 changes: 61 additions & 0 deletions ChessAttempt/Entities/ChessBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package Entities;

import Entities.PeiceType.*;

public class ChessBoard {
private Peice[][] board;
public ChessBoard(){
this.board = new Peice[8][8];
initialiseBoard();
}
public void initialiseBoard(){
board[0][0]=new Rook(false,"BR");
board[0][1]=new Knight(false,"BN");
board[0][2]=new Bishop(false,"BB");
board[0][3]=new Queen(false,"BQ");
board[0][4]=new King(false,"BK");
board[0][5]=new Bishop(false,"BB");
board[0][6]=new Knight(false,"BN");
board[0][7]=new Rook(false,"BR");
for(int j=0;j<8;j++){
board[1][j]=new Pawn(false,"BP");
}



board[7][0]=new Rook(true,"WR");
board[7][1]=new Knight(true,"WN");
board[7][2]=new Bishop(true,"WB");
board[7][3]=new Queen(true,"WQ");
board[7][4]=new King(true,"WK");
board[7][5]=new Bishop(true,"WB");
board[7][6]=new Knight(true,"WN");
board[7][7]=new Rook(true,"WR");
for(int j=0;j<8;j++){
board[6][j]=new Pawn(true,"WP");
}
}

public Peice getPeice(int i,int j){
return board[i][j];
}

public void setPeice(int i,int j,Peice p){
board[i][j]=p;
}


public void showBoard(){
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(board[i][j]==null){
System.out.print("-- ");
}else {
System.out.print(board[i][j].getName() + " ");
}
}
System.out.println();
}
System.out.println();
}
}
43 changes: 43 additions & 0 deletions ChessAttempt/Entities/InputService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Entities;

import java.util.Objects;
import java.util.Scanner;

public class InputService {

public InputService(){
}

public void startGame(){
ChessBoard chessgame = new ChessBoard();
Player player = new Player(true);
chessgame.showBoard();
boolean isGameRunning=true;
Scanner sc = new Scanner(System.in);
while(isGameRunning){
String start = sc.next();
if(Objects.equals(start, "exit")){
System.out.println("Game Over");
break;
}
String end = sc.next();

int startX = 8 - (start.charAt(1)-'0');
int startY = start.charAt(0)-'a';
int endX = 8 - (end.charAt(1)-'0');
int endY = end.charAt(0)-'a';

Peice p = chessgame.getPeice(startX,startY);
if(p!=null && player.isWhite()==p.isWhite() && p.isValidMove(chessgame,startX,startY,endX,endY)) {
chessgame.setPeice(endX, endY, p);
chessgame.setPeice(startX, startY, null);
chessgame.showBoard();
}else{
System.out.println("Invalid Move");
continue;
}
// change the player
player.switchPlayer();
}
}
}
23 changes: 23 additions & 0 deletions ChessAttempt/Entities/Peice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Entities;

public abstract class Peice {
private boolean isWhite;
private String name;

public Peice(boolean isWhite,String name){
this.isWhite =isWhite;
this.name=name;
}

public boolean isWhite(){
return isWhite;
}

public String getName(){
return name;
}


public abstract boolean isValidMove(ChessBoard board, int startX, int startY, int endX, int endY);

}
38 changes: 38 additions & 0 deletions ChessAttempt/Entities/PeiceType/Bishop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Entities.PeiceType;

import Entities.ChessBoard;
import Entities.Peice;

public class Bishop extends Peice {
public Bishop(boolean isWhite,String name){
super(isWhite,name);
}

public boolean isValidMove(ChessBoard board, int startX, int startY, int endX, int endY){
int x = Math.abs(startX-endX);
int y = Math.abs(startY-endY);

if(x==y){
if(!checkPathClear(board,startX,startY,endX,endY)) return false;
Peice p = board.getPeice(endX,endY);
return p == null || p.isWhite() != isWhite();
}
return false;
}

private boolean checkPathClear(ChessBoard board, int startRow, int startCol, int endRow, int endCol) {
int rowStep = Integer.compare(endRow, startRow);
int colStep = Integer.compare(endCol, startCol);
int currentRow = startRow + rowStep;
int currentCol = startCol + colStep;

while (currentRow != endRow || currentCol != endCol) {
if (board.getPeice(currentRow, currentCol) != null) {
return false; // Path is blocked
}
currentRow += rowStep;
currentCol += colStep;
}
return true;
}
}
21 changes: 21 additions & 0 deletions ChessAttempt/Entities/PeiceType/King.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Entities.PeiceType;

import Entities.ChessBoard;
import Entities.Peice;

public class King extends Peice {
public King(boolean isWhite,String name){
super(isWhite,name);
}

public boolean isValidMove(ChessBoard board, int startX, int startY, int endX, int endY){
int x = Math.abs(startX-endX);
int y = Math.abs(startY-endY);

if(x<=1 && y<=1){
Peice p = board.getPeice(endX,endY);
return p == null || p.isWhite() != isWhite();
}
return false;
}
}
22 changes: 22 additions & 0 deletions ChessAttempt/Entities/PeiceType/Knight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Entities.PeiceType;

import Entities.ChessBoard;
import Entities.Peice;

public class Knight extends Peice {
public Knight(boolean isWhite,String name){
super(isWhite,name);
}

public boolean isValidMove(ChessBoard board, int startX, int startY, int endX, int endY){
int x = Math.abs(startX-endX);
int y = Math.abs(startY-endY);

if ((x== 2 && y == 1) || (x== 1 && y == 2)) {
Peice p = board.getPeice(x,y);
return p == null || p.isWhite() != isWhite();
}
return false;
}
}

36 changes: 36 additions & 0 deletions ChessAttempt/Entities/PeiceType/Pawn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package Entities.PeiceType;

import Entities.ChessBoard;
import Entities.Peice;

public class Pawn extends Peice {
public Pawn(boolean isWhite,String name){
super(isWhite,name);
}
public boolean isValidMove(ChessBoard board, int startX, int startY, int endX, int endY){
int direction = isWhite()?-1:1;

if (startY == endY) {
// Move forward
if (startX + direction == endX && board.getPeice(endX, endY) == null) {
return true;
}
// First move: two steps
if ((startX == 1 && !isWhite()) || (startX == 6 && isWhite())) {
if (startX + 2 * direction == endX && board.getPeice(endX, endY) == null) {
return true;
}
}
}else if(Math.abs(startY - endY) == 1 && startX+direction == endX){
// dioganally capture
Peice p = board.getPeice(endX,endY);
if(p!=null && isWhite()!=p.isWhite()){
return true;
}
}
return false;



}
}
41 changes: 41 additions & 0 deletions ChessAttempt/Entities/PeiceType/Queen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package Entities.PeiceType;

import Entities.ChessBoard;
import Entities.Peice;

public class Queen extends Peice {
public Queen(boolean isWhite,String name){
super(isWhite,name);
}

public boolean isValidMove(ChessBoard board, int startX, int startY, int endX, int endY){

// queen is like bishop and rook
int x = Math.abs(startX-endX);
int y = Math.abs(startY-endY);

if(x==y || startX == endX || startY == endY){
if(checkPathClear(board,startX,startY,endX,endY)){
Peice p = board.getPeice(endX,endY);
return p == null || p.isWhite() != isWhite();
}
}
return false;
}

private boolean checkPathClear(ChessBoard board, int startRow, int startCol, int endRow, int endCol) {
int rowStep = Integer.compare(endRow, startRow);
int colStep = Integer.compare(endCol, startCol);
int currentRow = startRow + rowStep;
int currentCol = startCol + colStep;

while (currentRow != endRow || currentCol != endCol) {
if (board.getPeice(currentRow, currentCol) != null) {
return false; // Path is blocked
}
currentRow += rowStep;
currentCol += colStep;
}
return true;
}
}
38 changes: 38 additions & 0 deletions ChessAttempt/Entities/PeiceType/Rook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package Entities.PeiceType;

import Entities.ChessBoard;
import Entities.Peice;

public class Rook extends Peice {
public Rook(boolean isWhite,String name){
super(isWhite,name);
}

public boolean isValidMove(ChessBoard board, int startX, int startY, int endX, int endY){

if(startX == endX || startY == endY){
if(!checkPathClear(board,startX,startY,endX,endY))return false;
else {
Peice p = board.getPeice(endX,endY);
return p == null || p.isWhite() != isWhite();
}
}
return false;

}
private boolean checkPathClear(ChessBoard board, int startRow, int startCol, int endRow, int endCol) {
int rowStep = Integer.compare(endRow, startRow);
int colStep = Integer.compare(endCol, startCol);
int currentRow = startRow + rowStep;
int currentCol = startCol + colStep;

while (currentRow != endRow || currentCol != endCol) {
if (board.getPeice(currentRow, currentCol) != null) {
return false; // Path is blocked
}
currentRow += rowStep;
currentCol += colStep;
}
return true;
}
}
15 changes: 15 additions & 0 deletions ChessAttempt/Entities/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Entities;

public class Player {
private boolean isWhite;

public Player(boolean isWhite){
this.isWhite= isWhite;
}
public void switchPlayer(){
this.isWhite = !isWhite;
}
public boolean isWhite(){
return isWhite;
}
}
8 changes: 8 additions & 0 deletions ChessAttempt/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Entities.InputService;

public class Main {
public static void main(String[] args) {
InputService ip = new InputService();
ip.startGame();
}
}
18 changes: 18 additions & 0 deletions SnakeLadderAttempt/Entity/Dice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Entity;

import java.util.Random;

public class Dice {
private int numberOfDices;

Dice(int numberOfDices){
this.numberOfDices=numberOfDices;
}

// Roll Dice
public static int rollDice(){
Random rand = new Random();
int k = rand.nextInt(6)+1;
return k;
}
}
Loading