Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Part1 {
- static Scanner sc = new Scanner(System.in);
- static int[][] Table; // the board game
- static int[][] copyTable; // the board game
- static int best = 0; // highest score
- static int score = 0; // the current score
- static boolean gameNotOver = true; // flag that keeps the game status
- static boolean gameIsWonButCont = false; // true if the game won but continued
- static int preScore; // score from last move
- static int prebest; // best score from last move
- static boolean canNotRevers = false; // true if player already used reverse option
- public static void main(String[] args) {
- initGameBoard();
- printTable();
- System.out.println("Welcome, would you like to start the game?");
- char input = inputUser();
- if (input == 'y') { // start the game
- startGame();
- } else if (input == 'n') // cancel the game
- endGame();
- }// main
- // initialize the game
- public static void startGame() {
- initVars();
- while (gameNotOver) {
- playerMove();
- }
- System.out.println("Game Over");
- }
- // initialize the game board and its variables and prints the created board
- public static void initVars() {
- initGameBoard();
- lottery();
- lottery();
- printTable();
- printInformation();
- }
- // ends the game
- public static void endGame() {
- System.out.println("game over!");
- initGameBoard();
- }
- // take the input from the user
- public static char inputUser() {
- char input = sc.next().charAt(0); // input variable
- return input;
- }
- // initialize the board and it's copied board
- public static void initGameBoard() {
- Table = new int[4][4];
- copyTable = new int[4][4];
- gameIsWonButCont = false;
- gameNotOver = true;
- score = 0;
- }
- // cast a random number - 2 or 4
- public static void lottery() {
- Random rand = new Random();
- boolean v = false;
- while (v == false) {
- int row = rand.nextInt(4);
- int column = rand.nextInt(4);
- if (Table[row][column] == 0) {
- if (Math.random() < 0.7) {
- Table[row][column] = 2;
- v = true;
- } else {
- Table[row][column] = 4;
- v = true;
- }
- }
- }
- }
- // prints the current board to the player
- public static void printTable() {
- for (int i = 0; i < 4; i++) { // loop to print the board
- System.out.print(" ");
- for (int j = 0; j < 4; j++) {
- System.out.print(Table[i][j] + " ");
- }
- System.out.println();
- }
- System.out.println();
- }
- // prints the updated game information
- public static void printInformation() {
- System.out.println("w = up, s = down, a = left, d = right, r = return, e = exit, n = new game");
- System.out.println("score:" + score(0));
- System.out.println("best:" + best());
- }
- // initialize the board for the first time
- public static void initBoard() {
- initGameBoard();
- printInformation();
- }
- // updates the score after every move
- public static int score(int sum) {
- score = score + sum;
- return score;
- }
- // save the best score and updates it
- public static int best() {
- if (score > best) {
- best = score(0);
- return best;
- }
- return best;
- }
- // check if the player has won the game - if he reached 2048
- public static boolean checkIfWin() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (Table[i][j] == 2048) {
- printTable();
- return true;
- }
- }
- }
- return false;
- }
- // check if the player has lost the game and ask to stay or leave
- public static boolean lose() {
- if (checkIfLose()) {
- System.out.println("Game over! your score is: " + score(0) + " For a new game press y, n if not");
- char choose = inputUser();
- if (choose == 'n') {
- endGame();
- return true;
- }
- if (choose == 'y') {
- score = 0;// מאפס את הניקוד
- startGame();// מתחיל משחק מחדש
- return false;
- }
- }
- return false;
- }
- // check if there is an option to continue the game. Returns false if the player can continue playing
- public static boolean checkIfLose() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 3; j++) {
- if (Table[i][j] == 0 || Table[i][j + 1] == Table[i][j]) {
- return false;
- }
- }
- }
- for (int j = 0; j < 4; j++) {
- for (int i = 0; i < 3; i++) {
- if (Table[i][j] == 0 || Table[i][j] == Table[i + 1][j]) {
- return false;
- }
- }
- }
- return true;
- }
- // check if there is an option to move in the game. Returns true if the player can't move
- public static boolean canNotMove() {
- boolean error = true;
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (Table[i][j] != copyTable[i][j]) {
- error = false;
- return error;
- }
- }
- }
- return error;
- }
- // move the cells up and adds their value if possible
- public static void moveUp() {
- for (int j = 0; j < 4; j++) {
- boolean sum = true; // flag that prevent double cell merge in the same column
- for (int i = 1; i < 4; i++) {
- int temp = i;
- if (Table[i][j] != 0) { // get in only if the cell has value
- while (temp > 0) { // so the loop goes through all the way down
- if (Table[temp - 1][j] == 0) { // if the upper cell is 0 merge the cells
- Table[temp - 1][j] = (Table[temp - 1][j] + Table[temp][j]);
- Table[temp][j] = 0;
- temp = temp - 1;
- } else if (Table[temp - 1][j] == Table[temp][j] && sum) {// if the upper cell and current cell are equals merge them to the upper cell
- Table[temp - 1][j] = (Table[temp - 1][j] + Table[temp][j]);
- score(Table[temp - 1][j]);
- Table[temp][j] = 0;
- temp = temp - 1;
- if(temp ==1) {
- sum = false;
- }
- } else { // to continue the loop
- temp = temp - 1;
- }
- } // while
- }
- }
- }
- checkWrongStep();
- canNotRevers = false;
- }
- public static void moveDown() {
- for (int j = 0; j < 4; j++) {
- boolean sum = true;
- for (int i = 2; i >= 0; i--) {
- int temp = i;
- if (Table[i][j] != 0) {// אם אני לא אפס
- while (temp < 3) {
- if (Table[temp + 1][j] == 0) {// אם מי שמתחתיי שווה לאפס
- Table[temp + 1][j] = (Table[temp + 1][j] + Table[temp][j]);
- Table[temp][j] = 0;
- temp = temp + 1;
- } else if (Table[temp + 1][j] == Table[temp][j] && sum) {// אם מי שמתחתיי שווה לי
- Table[temp + 1][j] = (Table[temp + 1][j] + Table[temp][j]);
- score(Table[temp + 1][j]);
- Table[temp][j] = 0;
- temp = temp + 1;
- if(temp ==2) {
- sum = false;
- }
- } else {
- temp = temp + 1;
- }
- } // while
- }
- }
- }
- checkWrongStep();
- canNotRevers = false;
- }
- public static void moveLeft() {
- for (int i = 0; i < 4; i++) {
- boolean sum = true;
- for (int j = 1; j < 4; j++) {
- int temp = j;
- if (Table[i][j] != 0) {// אם אני לא אפס
- while (temp > 0) {
- if (Table[i][temp - 1] == 0) {// אם מי שמשמאלי שווה לאפס
- Table[i][temp - 1] = (Table[i][temp - 1] + Table[i][temp]);
- Table[i][temp] = 0;
- temp = temp - 1;
- } else if (Table[i][temp - 1] == Table[i][temp] && sum) {// אם מי שמשמאלי שווה לי
- Table[i][temp - 1] = (Table[i][temp - 1] + Table[i][temp]);
- score(Table[i][temp - 1]);
- Table[i][temp] = 0;
- temp = temp - 1;
- if(temp ==1) {
- sum = false;
- }
- } else {
- temp = temp - 1;
- }
- } // while
- }
- }
- }
- checkWrongStep();
- canNotRevers = false;
- }
- public static void moveRight() {
- for (int i = 0; i < 4; i++) {
- boolean sum = true;
- for (int j = 2; j >= 0; j--) {
- int temp = j;
- if (Table[i][j] != 0) {// אם אני לא אפס
- while (temp < 3) {
- if (Table[i][temp + 1] == 0) {// אם מי שמימיני שווה לאפס
- Table[i][temp + 1] = (Table[i][temp + 1] + Table[i][temp]);
- Table[i][temp] = 0;
- temp = temp + 1;
- } else if (Table[i][temp + 1] == Table[i][temp] && sum) {// אם מי שמתחתיי שווה לי
- Table[i][temp + 1] = (Table[i][temp + 1] + Table[i][temp]);
- score(Table[i][temp + 1]);
- Table[i][temp] = 0;
- temp = temp + 1;
- if(temp ==2) {
- sum = false;
- }
- } else {
- temp = temp + 1;
- }
- } // while
- }
- }
- }
- checkWrongStep();
- canNotRevers = false;
- }
- public static void checkWrongStep() {
- if (canNotMove()) {
- System.out.println("eror!!!! Please select another option:");
- printTable();
- printInformation();
- }
- }
- public static void revers() {
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- Table[i][j] = copyTable[i][j];
- }
- }
- canNotRevers = true;
- score = preScore;
- best = prebest;
- }
- public static void copyTheBoard() {
- preScore = score;
- prebest = best;
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- copyTable[i][j] = Table[i][j];
- }
- }
- }
- public static boolean checkIfGameIsOver() {
- if (!gameIsWonButCont && checkIfWin()) {
- System.out.println("You won! 2048! would you like to continue? Press y if yes, n if not");
- char choose = inputUser();
- if (choose == 'n') {
- endGame();
- return true;
- }
- if (choose == 'y') {
- gameIsWonButCont = true;
- startGame();
- return false;
- }
- }
- if (lose()) {
- return true;
- }
- return false;
- }
- public static void playerMove() {
- char choose = inputUser();
- if (choose == 'w' || choose == 'a' || choose == 's' || choose == 'd') {
- makeMovmentKeys(choose);
- if (!checkIfGameIsOver() && !canNotMove()) {
- lottery();
- printTable();
- printInformation();
- }
- } else if (choose == 'e') {
- endGame();
- } else if (choose == 'r') {
- if (!canNotRevers) {
- revers();
- lottery();
- printTable();
- printInformation();
- } else {
- System.out.println("You can not do return. Please select another option:");
- System.out.println();
- printTable();
- printInformation();
- }
- } else if (choose == 'n') {
- score = 0;// מאפס את הניקוד
- initVars();// מתחיל משחק מחדש
- }
- }
- public static void makeMovmentKeys(char choose) {
- copyTheBoard();
- if (choose == 'w') { // move up
- moveUp();
- } else if (choose == 's') { // ...
- moveDown();
- } else if (choose == 'a') {
- moveLeft();
- } else if (choose == 'd') {
- moveRight();
- }
- }
- }// class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement