From 98d8f1707735b11d30bfedf3db9e4ef2f58be15d Mon Sep 17 00:00:00 2001 From: pjt5 Date: Wed, 8 Jan 2025 22:15:59 +0530 Subject: [PATCH 1/2] snake and ladder solution --- snake-ladder-practice/README.md | 18 +++++ snake-ladder-practice/src/Board.java | 70 ++++++++++++++++++ snake-ladder-practice/src/Cell.java | 3 + snake-ladder-practice/src/Dice.java | 7 ++ snake-ladder-practice/src/InitializeGame.java | 74 +++++++++++++++++++ snake-ladder-practice/src/Jump.java | 4 + snake-ladder-practice/src/Main.java | 9 +++ snake-ladder-practice/src/Player.java | 9 +++ 8 files changed, 194 insertions(+) create mode 100644 snake-ladder-practice/README.md create mode 100644 snake-ladder-practice/src/Board.java create mode 100644 snake-ladder-practice/src/Cell.java create mode 100644 snake-ladder-practice/src/Dice.java create mode 100644 snake-ladder-practice/src/InitializeGame.java create mode 100644 snake-ladder-practice/src/Jump.java create mode 100644 snake-ladder-practice/src/Main.java create mode 100644 snake-ladder-practice/src/Player.java diff --git a/snake-ladder-practice/README.md b/snake-ladder-practice/README.md new file mode 100644 index 00000000..7c03a532 --- /dev/null +++ b/snake-ladder-practice/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/snake-ladder-practice/src/Board.java b/snake-ladder-practice/src/Board.java new file mode 100644 index 00000000..b899a9b8 --- /dev/null +++ b/snake-ladder-practice/src/Board.java @@ -0,0 +1,70 @@ +// import java.util.ArrayList; +import java.util.Scanner; + +public class Board { + Cell[][] cells; + + // int n = cells.length; + // int m = cells[0].length; + Scanner scn = new Scanner(System.in); + + Board(int boardSize){ + initializeBoard(boardSize); + addSnakes(); + addLadders(); + } + + private void initializeBoard(int size){ + cells = new Cell[size][size]; + for(int i=0;i 0){ + int head = scn.nextInt(); + int tail = scn.nextInt(); + if(head <= tail || head == 100 || getCell(head).jump != null){ + System.err.print("not possible ! Enter again"); + } + else { + Jump snakeJump = new Jump(); + snakeJump.start = head; + snakeJump.end = tail; + Cell headCell = getCell(head); + headCell.jump = snakeJump; + snakesCount--; + } + } + } + + private void addLadders(){ + int laddersCount = scn.nextInt(); + while(laddersCount > 0){ + int start = scn.nextInt(); + int end = scn.nextInt(); + if(start >= end || getCell(start).jump != null){ + System.err.print("not possible ! Enter again"); + } + else { + Jump ladderJump = new Jump(); + ladderJump.start = start; + ladderJump.end = end; + Cell headCell = getCell(start); + headCell.jump = ladderJump; + laddersCount--; + } + } + } + + Cell getCell(int position){ + int row = (position-1)/cells.length; + int column = (position-1)%cells.length; + return cells[row][column]; + } + +} diff --git a/snake-ladder-practice/src/Cell.java b/snake-ladder-practice/src/Cell.java new file mode 100644 index 00000000..b95db1b9 --- /dev/null +++ b/snake-ladder-practice/src/Cell.java @@ -0,0 +1,3 @@ +public class Cell { + Jump jump; +} diff --git a/snake-ladder-practice/src/Dice.java b/snake-ladder-practice/src/Dice.java new file mode 100644 index 00000000..ca566ad3 --- /dev/null +++ b/snake-ladder-practice/src/Dice.java @@ -0,0 +1,7 @@ +import java.util.concurrent.ThreadLocalRandom; + +public class Dice { + public int roll(){ + return ThreadLocalRandom.current().nextInt(1,7); + } +} diff --git a/snake-ladder-practice/src/InitializeGame.java b/snake-ladder-practice/src/InitializeGame.java new file mode 100644 index 00000000..89c206ff --- /dev/null +++ b/snake-ladder-practice/src/InitializeGame.java @@ -0,0 +1,74 @@ +import java.util.Deque; +import java.util.LinkedList; +import java.util.Scanner; + +public class InitializeGame { + + Board board; + Dice dice; + Deque players = new LinkedList<>(); + Player winner; + + InitializeGame(){ + board = new Board(10); + dice = new Dice(); + addPlayers(); + winner = null; + } + + public void startGame(){ + while (winner == null) { + Player currPlayer = players.peek(); + int diceNumber = dice.roll(); + int currPosition = currPlayer.position; + int newPosition = getposition(currPosition, diceNumber); + + if(newPosition <= 100){ + System.out.println(currPlayer.name + " rolled a " + diceNumber + " and moved from "+ currPosition + " to " + newPosition ); + if(newPosition == 100){ + System.out.println(currPlayer.name + " wins the game"); + winner = currPlayer; + } + currPlayer.position = newPosition; + } + else { + System.out.println(currPlayer.name + " rolled a " + diceNumber + " and stays there "); + } + changePlayers(players); + } + } + + private void changePlayers(Deque players){ + Player removeCurrPlayer = players.peek(); + players.pop(); + players.add(removeCurrPlayer); + } + + private int getposition(int currPosition,int diceValue){ + + int newPosition = currPosition + diceValue; + if(newPosition > 100){ + return newPosition; + } + Cell cell = board.getCell(newPosition); + + if(cell.jump != null && cell.jump.start == newPosition){ + newPosition = cell.jump.end; + } + return newPosition; + } + + private void addPlayers(){ + Scanner scanner = new Scanner(System.in); + try { + int numberOfPlayers = scanner.nextInt(); + while (numberOfPlayers > 0) { + players.add(new Player(scanner.next(),0)); + numberOfPlayers--; + } + } + finally { + scanner.close(); + } + } +} diff --git a/snake-ladder-practice/src/Jump.java b/snake-ladder-practice/src/Jump.java new file mode 100644 index 00000000..91b9c9ba --- /dev/null +++ b/snake-ladder-practice/src/Jump.java @@ -0,0 +1,4 @@ +public class Jump { + int start; + int end ; +} diff --git a/snake-ladder-practice/src/Main.java b/snake-ladder-practice/src/Main.java new file mode 100644 index 00000000..5a698233 --- /dev/null +++ b/snake-ladder-practice/src/Main.java @@ -0,0 +1,9 @@ +// import java.util.ArrayList; +// import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + InitializeGame game = new InitializeGame(); + game.startGame(); + } +} diff --git a/snake-ladder-practice/src/Player.java b/snake-ladder-practice/src/Player.java new file mode 100644 index 00000000..956e929b --- /dev/null +++ b/snake-ladder-practice/src/Player.java @@ -0,0 +1,9 @@ +public class Player { + String name; + int position; + + Player(String name,int position){ + this.name = name; + this.position = position; + } +} From 540e728752e1490b2ffee37c3084e715a9e5f467 Mon Sep 17 00:00:00 2001 From: pjt5 Date: Sun, 2 Feb 2025 17:07:31 +0530 Subject: [PATCH 2/2] 2048 game solution --- 2048-game/README.md | 18 +++ 2048-game/src/Board.java | 53 +++++++++ 2048-game/src/Displayboard.java | 16 +++ 2048-game/src/Game.java | 194 ++++++++++++++++++++++++++++++++ 2048-game/src/Main.java | 8 ++ 2048-game/src/Tile.java | 4 + 6 files changed, 293 insertions(+) create mode 100644 2048-game/README.md create mode 100644 2048-game/src/Board.java create mode 100644 2048-game/src/Displayboard.java create mode 100644 2048-game/src/Game.java create mode 100644 2048-game/src/Main.java create mode 100644 2048-game/src/Tile.java diff --git a/2048-game/README.md b/2048-game/README.md new file mode 100644 index 00000000..7c03a532 --- /dev/null +++ b/2048-game/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/2048-game/src/Board.java b/2048-game/src/Board.java new file mode 100644 index 00000000..f9188cdf --- /dev/null +++ b/2048-game/src/Board.java @@ -0,0 +1,53 @@ +// import java.util.ArrayList; +// import java.util.Arrays; +import java.util.concurrent.ThreadLocalRandom; + +public class Board { + int size; + int basevalue; + int numberOfValues; + Tile[][] tiles; + int[] boardValues; + + Board(int size,int base,int count){ + this.size = size; + this.basevalue = base; + this.numberOfValues = count; + initializeValues(base,count); + initializeBoard(size); + } + + private void initializeValues(int base,int numberOfValues){ + boardValues = new int[numberOfValues]; + for(int i=0;i tileValueList; + + Scanner scanner = new Scanner(System.in); + + Game(Board board){ + this.board = board; + size = board.size; + tileValueList = new ArrayList(Arrays.asList(2,4)); + displayboard = new Displayboard(board); + } + + public void startGame(Board board){ + displayboard.showOutput(); + while (filledTiles < size*size && flag == false) { + direction = scanner.nextInt(); + if(direction == 3){ + slideBottom(); + } + else if(direction == 2){ + slideUp(); + } + else if(direction == 1){ + slideRight(); + } + else { + slideLeft(); + } + insertTile(); + displayboard.showOutput(); + checkGame(); + } + if(flag == true){ + System.out.println("Congratulations"); + } + else System.out.println("Game over"); + } + + private void insertTile(){ + int add = 0; + while(add < 1){ + int row = ThreadLocalRandom.current().nextInt(0,size); + int column = ThreadLocalRandom.current().nextInt(0,size); + int number = ThreadLocalRandom.current().nextInt(0,tileValueList.size()); + + if(board.tiles[row][column].value == 0){ + board.tiles[row][column].value = tileValueList.get(number); + add++; + } + } + } + + private void checkGame(){ + filledTiles = 0; + for(int i=0;i=0;i--){ + int k = i; + if(board.tiles[i][j].value > 0){ + while(k < end){ + if(board.tiles[k+1][j].value > 0){ + if(board.tiles[k+1][j].value != board.tiles[k][j].value){ + end = k; + break; + } + else { + board.tiles[k+1][j].value += board.tiles[k][j].value; + board.tiles[k][j].value = 0; + end = k+1; + } + } + else { + board.tiles[k+1][j].value = board.tiles[k][j].value; + board.tiles[k][j].value = 0; + } + k++; + } + } + } + } + } + + private void slideUp(){ + for(int j=0;j 0){ + while(k > end){ + if(board.tiles[k-1][j].value > 0){ + if(board.tiles[k-1][j].value != board.tiles[k][j].value){ + end = k; + break; + } + else { + board.tiles[k-1][j].value += board.tiles[k][j].value; + board.tiles[k][j].value = 0; + end = k-1; + } + } + else { + board.tiles[k-1][j].value = board.tiles[k][j].value; + board.tiles[k][j].value = 0; + } + k--; + } + } + } + } + } + + private void slideRight(){ + for(int i=0;i=0;j--){ + int k = j; + if(board.tiles[i][j].value > 0){ + while(k < end){ + if(board.tiles[i][k+1].value > 0){ + if(board.tiles[i][k+1].value != board.tiles[i][k].value){ + end = k; + break; + } + else { + board.tiles[i][k+1].value += board.tiles[i][k].value; + board.tiles[i][k].value = 0; + end = k+1; + } + } + else { + board.tiles[i][k+1].value = board.tiles[i][k].value; + board.tiles[i][k].value = 0; + } + k++; + } + } + } + } + } + + private void slideLeft(){ + for(int i=0;i 0){ + while(k > end){ + if(board.tiles[i][k-1].value > 0){ + if(board.tiles[i][k-1].value != board.tiles[i][k].value){ + end = k; + break; + } + else { + board.tiles[i][k-1].value += board.tiles[i][k].value; + board.tiles[i][k].value = 0; + end = k-1; + } + } + else { + board.tiles[i][k-1].value = board.tiles[i][k].value; + board.tiles[i][k].value = 0; + } + k--; + } + } + } + } + } +} diff --git a/2048-game/src/Main.java b/2048-game/src/Main.java new file mode 100644 index 00000000..80571001 --- /dev/null +++ b/2048-game/src/Main.java @@ -0,0 +1,8 @@ +// import java.util.Scanner; +public class Main { + public static void main(String[] args) throws Exception { + Board board = new Board(4,2, 10); + Game game = new Game(board); + game.startGame(board); + } +} diff --git a/2048-game/src/Tile.java b/2048-game/src/Tile.java new file mode 100644 index 00000000..b8ff0c05 --- /dev/null +++ b/2048-game/src/Tile.java @@ -0,0 +1,4 @@ + +public class Tile { + int value; +}