diff --git a/ChessAttempt/Entities/ChessBoard.java b/ChessAttempt/Entities/ChessBoard.java new file mode 100644 index 00000000..b2b21d7e --- /dev/null +++ b/ChessAttempt/Entities/ChessBoard.java @@ -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(); + } +} diff --git a/ChessAttempt/Entities/InputService.java b/ChessAttempt/Entities/InputService.java new file mode 100644 index 00000000..963d9553 --- /dev/null +++ b/ChessAttempt/Entities/InputService.java @@ -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(); + } + } +} diff --git a/ChessAttempt/Entities/Peice.java b/ChessAttempt/Entities/Peice.java new file mode 100644 index 00000000..20fb8b48 --- /dev/null +++ b/ChessAttempt/Entities/Peice.java @@ -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); + +} diff --git a/ChessAttempt/Entities/PeiceType/Bishop.java b/ChessAttempt/Entities/PeiceType/Bishop.java new file mode 100644 index 00000000..24c204c6 --- /dev/null +++ b/ChessAttempt/Entities/PeiceType/Bishop.java @@ -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; + } +} diff --git a/ChessAttempt/Entities/PeiceType/King.java b/ChessAttempt/Entities/PeiceType/King.java new file mode 100644 index 00000000..90d86ac2 --- /dev/null +++ b/ChessAttempt/Entities/PeiceType/King.java @@ -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; + } +} diff --git a/ChessAttempt/Entities/PeiceType/Knight.java b/ChessAttempt/Entities/PeiceType/Knight.java new file mode 100644 index 00000000..dc96b85d --- /dev/null +++ b/ChessAttempt/Entities/PeiceType/Knight.java @@ -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; + } +} + diff --git a/ChessAttempt/Entities/PeiceType/Pawn.java b/ChessAttempt/Entities/PeiceType/Pawn.java new file mode 100644 index 00000000..2087b8fe --- /dev/null +++ b/ChessAttempt/Entities/PeiceType/Pawn.java @@ -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; + + + + } +} diff --git a/ChessAttempt/Entities/PeiceType/Queen.java b/ChessAttempt/Entities/PeiceType/Queen.java new file mode 100644 index 00000000..c99016ed --- /dev/null +++ b/ChessAttempt/Entities/PeiceType/Queen.java @@ -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; + } +} diff --git a/ChessAttempt/Entities/PeiceType/Rook.java b/ChessAttempt/Entities/PeiceType/Rook.java new file mode 100644 index 00000000..906042da --- /dev/null +++ b/ChessAttempt/Entities/PeiceType/Rook.java @@ -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; + } +} diff --git a/ChessAttempt/Entities/Player.java b/ChessAttempt/Entities/Player.java new file mode 100644 index 00000000..f2e0ecd9 --- /dev/null +++ b/ChessAttempt/Entities/Player.java @@ -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; + } +} diff --git a/ChessAttempt/Main.java b/ChessAttempt/Main.java new file mode 100644 index 00000000..732d7d31 --- /dev/null +++ b/ChessAttempt/Main.java @@ -0,0 +1,8 @@ +import Entities.InputService; + +public class Main { + public static void main(String[] args) { + InputService ip = new InputService(); + ip.startGame(); + } +} \ No newline at end of file diff --git a/SnakeLadderAttempt/Entity/Dice.java b/SnakeLadderAttempt/Entity/Dice.java new file mode 100644 index 00000000..371a5e84 --- /dev/null +++ b/SnakeLadderAttempt/Entity/Dice.java @@ -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; + } +} diff --git a/SnakeLadderAttempt/Entity/Game.java b/SnakeLadderAttempt/Entity/Game.java new file mode 100644 index 00000000..04e0bf00 --- /dev/null +++ b/SnakeLadderAttempt/Entity/Game.java @@ -0,0 +1,66 @@ +package Entity; +import java.util.HashMap; +import java.util.List; + +public class Game { + private boolean isGameOver; + Grid grid; + Dice dice; + public Game(){ + this.grid = new Grid(); + this.isGameOver=false; + } + public Grid getGrid() { + return grid; + } + + + public void Start(){ + int i=0; + List playerList = grid.getPlayers(); + HashMap snakeMap = new HashMap<>(); + HashMapladderMap = new HashMap<>(); + + // Adding Snake and ladders in the hashmap + for(Snake snake : grid.snakes) { + snakeMap.put(snake.getHead(),snake.getTail()); + } + for(Ladder ladder : grid.ladders){ + ladderMap.put(ladder.getStart(),ladder.getEnd()); + } + + while(!isGameOver) { + for(Player p :playerList) { + int throwDice = dice.rollDice(); + String Name = p.getName(); + int initialPostion = p.getPosition(); + + int laterPosition = initialPostion + throwDice; + + // Case if it goes Above 100 + if (laterPosition > 100) { + laterPosition = initialPostion; + } + + // Either of snake or ladder can be there + if (snakeMap.containsKey(laterPosition)) { + laterPosition = snakeMap.get(laterPosition); + } else if (ladderMap.containsKey(laterPosition)) { + laterPosition = ladderMap.get(laterPosition); + } + + //setting the new position + p.setPosition(laterPosition); + + System.out.println(Name + " rolled a " + throwDice + " and moved from " + initialPostion + " to " + laterPosition); + + if (p.getPosition() == 100) { + System.out.println(Name + " Wins"); + isGameOver = true; + break; + } + } + } + } + +} diff --git a/SnakeLadderAttempt/Entity/Grid.java b/SnakeLadderAttempt/Entity/Grid.java new file mode 100644 index 00000000..2582661c --- /dev/null +++ b/SnakeLadderAttempt/Entity/Grid.java @@ -0,0 +1,34 @@ +package Entity; + +import java.util.ArrayList; +import java.util.List; + +public class Grid { + Listplayers; + Listsnakes; + Listladders; + + public Grid(){ + this.players = new ArrayList<>(); + this.snakes = new ArrayList<>(); + this.ladders = new ArrayList<>(); + } + + public List getPlayers() { + return players; + } + + public void addLadder(Ladder l){ + ladders.add(l); + } + + public void addSnake(Snake s){ + snakes.add(s); + } + + public void AddPlayer(Player p){ + players.add(p); + } + + +} diff --git a/SnakeLadderAttempt/Entity/Ladder.java b/SnakeLadderAttempt/Entity/Ladder.java new file mode 100644 index 00000000..5490e32d --- /dev/null +++ b/SnakeLadderAttempt/Entity/Ladder.java @@ -0,0 +1,19 @@ +package Entity; + +public class Ladder { + private int start; + private int end; + + public Ladder(int start, int end){ + this.start = start; + this.end = end; + } + + public int getStart() { + return start; + } + + public int getEnd() { + return end; + } +} diff --git a/SnakeLadderAttempt/Entity/Player.java b/SnakeLadderAttempt/Entity/Player.java new file mode 100644 index 00000000..ea9d2c87 --- /dev/null +++ b/SnakeLadderAttempt/Entity/Player.java @@ -0,0 +1,23 @@ +package Entity; + +public class Player { + private int position; + private String name; + + public Player(int position, String name){ + this.position =position; + this.name = name; + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } + + public void setPosition(int position) { + this.position = position; + } +} diff --git a/SnakeLadderAttempt/Entity/Snake.java b/SnakeLadderAttempt/Entity/Snake.java new file mode 100644 index 00000000..28cf2f6b --- /dev/null +++ b/SnakeLadderAttempt/Entity/Snake.java @@ -0,0 +1,19 @@ +package Entity; + +public class Snake { + private int head; + private int tail; + + public Snake(int head,int tail){ + this.head=head; + this.tail=tail; + } + + public int getHead() { + return head; + } + + public int getTail() { + return tail; + } +} diff --git a/SnakeLadderAttempt/Main.java b/SnakeLadderAttempt/Main.java new file mode 100644 index 00000000..ffbebb8e --- /dev/null +++ b/SnakeLadderAttempt/Main.java @@ -0,0 +1,33 @@ +import Entity.*; +import java.util.Scanner; + + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Game game = new Game(); + int snakeCount = sc.nextInt(); + for(int i=1;i<=snakeCount;i++){ + int head = sc.nextInt(); + int tail = sc.nextInt(); + game.getGrid().addSnake(new Snake(head,tail)); + } + + int ladderCount = sc.nextInt(); + for(int i=1;i<=ladderCount;i++){ + int start = sc.nextInt(); + int end = sc.nextInt(); + game.getGrid().addLadder(new Ladder(start,end)); + } + + int playerCount= sc.nextInt(); + for(int i=1;i<=playerCount;i++){ + String name = sc.next(); + game.getGrid().AddPlayer(new Player(0,name)); + } + + // Start the game + game.Start(); + + } +} \ No newline at end of file diff --git a/TicTacToeAttempt/Entities/Game.java b/TicTacToeAttempt/Entities/Game.java new file mode 100644 index 00000000..f7fe1ebf --- /dev/null +++ b/TicTacToeAttempt/Entities/Game.java @@ -0,0 +1,112 @@ +package Entities; + +import java.util.ArrayList; +import java.util.List; + +public class Game { + Grid grid ; + + public Game(int Row,int Col){ + grid = new Grid(Row,Col); + } + + public Grid getGrid() { + return grid; + } + + public void InitialiseGrid(int row,int col){ + for(int i=0;i players; + Peice peice; + private char [][] board; + + public Grid(int rowSize,int colSize){ + players = new ArrayList<>(); + this.colSize=colSize; + this.rowSize=rowSize; + this.board = new char[rowSize][colSize]; + } + + public List getPlayers() { + return players; + } + + public char getboard(int i, int j){ + return board[i][j]; + } + public void setboard(int i,int j){ + board[i][j]='-'; + } + + public void changeBoard(int i,int j,Player p){ + board[i][j]=p.getPeice(); + } + + + public void addPlayer(Player player){ + players.add(player); + } +} diff --git a/TicTacToeAttempt/Entities/Peice.java b/TicTacToeAttempt/Entities/Peice.java new file mode 100644 index 00000000..11abdab4 --- /dev/null +++ b/TicTacToeAttempt/Entities/Peice.java @@ -0,0 +1,9 @@ +package Entities; + +public class Peice { + String peicetype; + + public Peice(String p){ + this.peicetype = p; + } +} diff --git a/TicTacToeAttempt/Entities/PeiceType.java b/TicTacToeAttempt/Entities/PeiceType.java new file mode 100644 index 00000000..aae7d570 --- /dev/null +++ b/TicTacToeAttempt/Entities/PeiceType.java @@ -0,0 +1,6 @@ +package Entities; + +public enum PeiceType { + X,O; +} + diff --git a/TicTacToeAttempt/Entities/Player.java b/TicTacToeAttempt/Entities/Player.java new file mode 100644 index 00000000..9f93b023 --- /dev/null +++ b/TicTacToeAttempt/Entities/Player.java @@ -0,0 +1,19 @@ +package Entities; + +public class Player { + private String Name; + private Peice peice; + + public Player(String Name,Peice p){ + this.Name = Name; + this.peice = p; + } + + public String getName() { + return Name; + } + + public char getPeice() { + return peice.peicetype.charAt(0); + } +} diff --git a/TicTacToeAttempt/Main.java b/TicTacToeAttempt/Main.java new file mode 100644 index 00000000..ce6daf53 --- /dev/null +++ b/TicTacToeAttempt/Main.java @@ -0,0 +1,67 @@ +import Entities.*; + +import java.util.Objects; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int row = sc.nextInt(); + int col = sc.nextInt(); + Game game = new Game(row,col); + game.InitialiseGrid(row,col); + int players = sc.nextInt(); + for(int i=1;i<=players;i++){ + String peiceType = sc.next(); + String name = sc.next(); + game.getGrid().addPlayer(new Player(name,new Peice(peiceType))); + } + + // Print the grid + game.printGrid(row,col); + + + boolean isChance=true; + while(isChance){ + for(int i=0;i