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
74 changes: 74 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 156 additions & 0 deletions 2048.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <conio.h> // for getch() on Windows

using namespace std;

const int SIZE = 4;
int grid[SIZE][SIZE];

// Initialize grid
void initGrid() {
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
grid[i][j] = 0;
}

// Add random tile (2 or 4)
void addRandomTile() {
int empty[SIZE * SIZE][2];
int count = 0;
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
if (grid[i][j] == 0) {
empty[count][0] = i;
empty[count][1] = j;
count++;
}
if (count > 0) {
int r = rand() % count;
grid[empty[r][0]][empty[r][1]] = (rand() % 2 + 1) * 2;
}
}

// Print grid
void printGrid() {
cout << "\n";
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (grid[i][j] == 0)
cout << setw(5) << ".";
else
cout << setw(5) << grid[i][j];
}
cout << "\n\n";
}
}

// Slide left
bool slideLeft() {
bool moved = false;
for (int i = 0; i < SIZE; i++) {
int temp[SIZE] = {0};
int idx = 0;
for (int j = 0; j < SIZE; j++) {
if (grid[i][j] != 0) {
if (temp[idx] == 0) {
temp[idx] = grid[i][j];
} else if (temp[idx] == grid[i][j]) {
temp[idx++] *= 2;
moved = true;
} else {
temp[++idx] = grid[i][j];
}
if (j != idx) moved = true;
}
}
for (int j = 0; j < SIZE; j++)
grid[i][j] = temp[j];
}
return moved;
}

// Rotate grid 90° clockwise
void rotateGrid() {
int temp[SIZE][SIZE];
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
temp[j][SIZE - 1 - i] = grid[i][j];
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
grid[i][j] = temp[i][j];
}

// Move tiles based on arrow key
bool move(int key) {
bool moved = false;
switch(key) {
case 72: // up arrow
rotateGrid(); rotateGrid(); rotateGrid();
moved = slideLeft();
rotateGrid();
break;
case 80: // down arrow
rotateGrid();
moved = slideLeft();
rotateGrid(); rotateGrid(); rotateGrid();
break;
case 75: // left arrow
moved = slideLeft();
break;
case 77: // right arrow
rotateGrid(); rotateGrid();
moved = slideLeft();
rotateGrid(); rotateGrid();
break;
}
return moved;
}

// Check if game is over
bool gameOver() {
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
if (grid[i][j] == 0) return false;

for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE-1; j++)
if (grid[i][j] == grid[i][j+1]) return false;

for (int j = 0; j < SIZE; j++)
for (int i = 0; i < SIZE-1; i++)
if (grid[i][j] == grid[i+1][j]) return false;

return true;
}

int main() {
srand(time(0));
initGrid();
addRandomTile();
addRandomTile();
printGrid();

while (true) {
cout << "Use arrow keys to move tiles.\n";
int ch = _getch(); // first input
if (ch == 224) { // arrow keys are detected in 2 steps
int arrow = _getch(); // second input gives actual key code
if (move(arrow)) {
addRandomTile();
printGrid();
} else {
cout << "Invalid move or no tiles moved.\n";
}
}

if (gameOver()) {
cout << "Game Over!\n";
break;
}
}

return 0;
}
Binary file added Main.class
Binary file not shown.
103 changes: 103 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**

* @Filename- Main.java
* @Description- For taking user input and calling string functions
* @Author- Disha Toshniwal
*/
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Mystring myStringFunctions = new Mystring();

System.out.println("Enter a string : ");
String mainString = scanner.nextLine();
System.out.println("Choose an operation : ");
System.out.println("1. Append ");
System.out.println("2. Count Words ");
System.out.println("3. Replace ");
System.out.println("4. isPalindrome ");
System.out.println("5. Splice ");
System.out.println("6. Split ");
System.out.println("7. Max Repeat ");
System.out.println("8. Sort ");
System.out.println("9. Shift ");
System.out.println("10. Reverse ");
System.out.println("11. Exit ");

System.out.print("Enter your choice (1 – 11) : ");
int userChoice = scanner.nextInt();
scanner.nextLine(); // clear buffer

switch (userChoice) {
case 1:
System.out.println("Enter a string to append : ");
String appendString = scanner.nextLine();
myStringFunctions.append(mainString, appendString);
break;

case 2:
System.out.println("Enter string to count total number of words : ");
String wordCountString = scanner.nextLine();
myStringFunctions.countWords(wordCountString);
break;

case 3:
System.out.println("Enter character you want to replace : ");
char oldCharacter = scanner.next().charAt(0);
System.out.println("Enter new character : ");
char newCharacter = scanner.next().charAt(0);
myStringFunctions.replace(mainString, oldCharacter, newCharacter);
break;

case 4:
myStringFunctions.isPalindrome(mainString);
break;

case 5:
System.out.println("Enter starting index : ");
int startIndex = scanner.nextInt();
System.out.println("Enter length to remove : ");
int removeLength = scanner.nextInt();
myStringFunctions.splice(mainString, startIndex, removeLength);
break;

case 6:
scanner.nextLine(); // clear buffer
System.out.println("Enter pattern to split by : ");
String splitPattern = scanner.nextLine();
myStringFunctions.split(mainString, splitPattern);
break;

case 7:
myStringFunctions.maxrepeat(mainString);
break;

case 8:
myStringFunctions.sort(mainString);
break;

case 9:
System.out.println("Enter shift count : ");
int shiftCount = scanner.nextInt();
myStringFunctions.shift(mainString, shiftCount);
break;

case 10:
myStringFunctions.reverse(mainString);
break;

case 11:
System.out.println("Exiting... ");
break;

default:
System.out.println("Invalid choice! Please enter a number between 1 and 11. ");
}

scanner.close();
}


}
Loading