Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 10.44 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class Part1 {
  5.     static Scanner sc = new Scanner(System.in);
  6.     static int[][] Table; // the board game
  7.     static int[][] copyTable; // the board game
  8.     static int best = 0; // highest score
  9.     static int score = 0; // the current score
  10.     static boolean gameNotOver = true; // flag that keeps the game status
  11.     static boolean gameIsWonButCont = false; // true if the game won but continued
  12.     static int preScore; // score from last move
  13.     static int prebest; // best score from last move
  14.     static boolean canNotRevers = false; // true if player already used reverse option
  15.  
  16.     public static void main(String[] args) {
  17.         initGameBoard();
  18.         printTable();
  19.         System.out.println("Welcome, would you like to start the game?");
  20.         char input = inputUser();
  21.         if (input == 'y') { // start the game
  22.             startGame();
  23.         } else if (input == 'n') // cancel the game
  24.             endGame();
  25.     }// main
  26.  
  27.     // initialize the game
  28.     public static void startGame() {
  29.         initVars();
  30.         while (gameNotOver) {
  31.             playerMove();
  32.         }
  33.         System.out.println("Game Over");
  34.     }
  35.  
  36.     // initialize the game board and its variables and prints the created board
  37.     public static void initVars() {
  38.         initGameBoard();
  39.         lottery();
  40.         lottery();
  41.         printTable();
  42.         printInformation();
  43.     }
  44.  
  45.     // ends the game
  46.     public static void endGame() {
  47.         System.out.println("game over!");
  48.         initGameBoard();
  49.     }
  50.  
  51.     // take the input from the user
  52.     public static char inputUser() {
  53.         char input = sc.next().charAt(0); // input variable
  54.         return input;
  55.     }
  56.  
  57.     // initialize the board and it's copied board
  58.     public static void initGameBoard() {
  59.         Table = new int[4][4];
  60.         copyTable = new int[4][4];
  61.         gameIsWonButCont = false;
  62.         gameNotOver = true;
  63.         score = 0;
  64.     }
  65.  
  66.     // cast a random number - 2 or 4
  67.     public static void lottery() {
  68.         Random rand = new Random();
  69.         boolean v = false;
  70.         while (v == false) {
  71.             int row = rand.nextInt(4);
  72.             int column = rand.nextInt(4);
  73.             if (Table[row][column] == 0) {
  74.                 if (Math.random() < 0.7) {
  75.                     Table[row][column] = 2;
  76.                     v = true;
  77.                 } else {
  78.                     Table[row][column] = 4;
  79.                     v = true;
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     // prints the current board to the player
  86.     public static void printTable() {
  87.         for (int i = 0; i < 4; i++) { // loop to print the board
  88.             System.out.print("        ");
  89.             for (int j = 0; j < 4; j++) {
  90.                 System.out.print(Table[i][j] + "     ");
  91.             }
  92.             System.out.println();
  93.         }
  94.         System.out.println();
  95.     }
  96.  
  97.     // prints the updated game information
  98.     public static void printInformation() {
  99.         System.out.println("w = up, s = down, a = left, d = right, r = return, e = exit, n = new game");
  100.         System.out.println("score:" + score(0));
  101.         System.out.println("best:" + best());
  102.     }
  103.  
  104.     // initialize the board for the first time
  105.     public static void initBoard() {
  106.         initGameBoard();
  107.         printInformation();
  108.     }
  109.  
  110.     // updates the score after every move
  111.     public static int score(int sum) {
  112.         score = score + sum;
  113.         return score;
  114.     }
  115.  
  116.     // save the best score and updates it
  117.     public static int best() {
  118.         if (score > best) {
  119.             best = score(0);
  120.             return best;
  121.         }
  122.         return best;
  123.     }
  124.  
  125.     // check if the player has won the game - if he reached 2048
  126.     public static boolean checkIfWin() {
  127.         for (int i = 0; i < 4; i++) {
  128.             for (int j = 0; j < 4; j++) {
  129.                 if (Table[i][j] == 2048) {
  130.                     printTable();
  131.                     return true;
  132.                 }
  133.             }
  134.         }
  135.         return false;
  136.     }
  137.  
  138.     // check if the player has lost the game and ask to stay or leave
  139.     public static boolean lose() {
  140.         if (checkIfLose()) {
  141.             System.out.println("Game over! your score is: " + score(0) + " For a new game press y, n if not");
  142.             char choose = inputUser();
  143.             if (choose == 'n') {
  144.                 endGame();
  145.                 return true;
  146.             }
  147.             if (choose == 'y') {
  148.                 score = 0;// מאפס את הניקוד
  149.                 startGame();// מתחיל משחק מחדש
  150.                 return false;
  151.             }
  152.         }
  153.         return false;
  154.     }
  155.  
  156.     // check if there is an option to continue the game. Returns false if the player can continue playing
  157.     public static boolean checkIfLose() {
  158.         for (int i = 0; i < 4; i++) {
  159.             for (int j = 0; j < 3; j++) {
  160.                 if (Table[i][j] == 0 || Table[i][j + 1] == Table[i][j]) {
  161.                     return false;
  162.                 }
  163.             }
  164.         }
  165.         for (int j = 0; j < 4; j++) {
  166.             for (int i = 0; i < 3; i++) {
  167.                 if (Table[i][j] == 0 || Table[i][j] == Table[i + 1][j]) {
  168.                     return false;
  169.                 }
  170.             }
  171.         }
  172.         return true;
  173.     }
  174.  
  175.     // check if there is an option to move in the game. Returns true if the player can't move
  176.     public static boolean canNotMove() {
  177.         boolean error = true;
  178.         for (int i = 0; i < 4; i++) {
  179.             for (int j = 0; j < 4; j++) {
  180.                 if (Table[i][j] != copyTable[i][j]) {
  181.                     error = false;
  182.                     return error;
  183.                 }
  184.             }
  185.         }
  186.         return error;
  187.     }
  188.  
  189.     // move the cells up and adds their value if possible
  190.     public static void moveUp() {
  191.         for (int j = 0; j < 4; j++) {
  192.             boolean sum = true; // flag that prevent double cell merge in the same column
  193.             for (int i = 1; i < 4; i++) {
  194.                 int temp = i;
  195.                 if (Table[i][j] != 0) { // get in only if the cell has value
  196.                     while (temp > 0) { // so the loop goes through all the way down
  197.                         if (Table[temp - 1][j] == 0) { // if the upper cell is 0 merge the cells
  198.                             Table[temp - 1][j] = (Table[temp - 1][j] + Table[temp][j]);
  199.                             Table[temp][j] = 0;
  200.                             temp = temp - 1;
  201.                         } 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
  202.                             Table[temp - 1][j] = (Table[temp - 1][j] + Table[temp][j]);
  203.                             score(Table[temp - 1][j]);
  204.                             Table[temp][j] = 0;
  205.                             temp = temp - 1;
  206.                             if(temp ==1) {
  207.                                 sum = false;
  208.                     }
  209.                         } else { // to continue the loop
  210.                             temp = temp - 1;
  211.                         }
  212.                     } // while
  213.                 }
  214.             }
  215.         }
  216.         checkWrongStep();
  217.         canNotRevers = false;
  218.     }
  219.  
  220.     public static void moveDown() {
  221.         for (int j = 0; j < 4; j++) {
  222.             boolean sum = true;
  223.             for (int i = 2; i >= 0; i--) {
  224.                 int temp = i;
  225.                 if (Table[i][j] != 0) {// אם אני לא אפס
  226.                     while (temp < 3) {
  227.                         if (Table[temp + 1][j] == 0) {// אם מי שמתחתיי שווה לאפס
  228.                             Table[temp + 1][j] = (Table[temp + 1][j] + Table[temp][j]);
  229.                             Table[temp][j] = 0;
  230.                             temp = temp + 1;
  231.                         } else if (Table[temp + 1][j] == Table[temp][j] && sum) {// אם מי שמתחתיי שווה לי
  232.                             Table[temp + 1][j] = (Table[temp + 1][j] + Table[temp][j]);
  233.                             score(Table[temp + 1][j]);
  234.                             Table[temp][j] = 0;
  235.                             temp = temp + 1;
  236.                             if(temp ==2) {
  237.                                 sum = false;
  238.                     }
  239.                         } else {
  240.                             temp = temp + 1;
  241.                         }
  242.                     } // while
  243.                 }
  244.             }
  245.         }
  246.         checkWrongStep();
  247.         canNotRevers = false;
  248.     }
  249.  
  250.     public static void moveLeft() {
  251.         for (int i = 0; i < 4; i++) {
  252.             boolean sum = true;
  253.             for (int j = 1; j < 4; j++) {
  254.                 int temp = j;
  255.                 if (Table[i][j] != 0) {// אם אני לא אפס
  256.                     while (temp > 0) {
  257.                         if (Table[i][temp - 1] == 0) {// אם מי שמשמאלי שווה לאפס
  258.                             Table[i][temp - 1] = (Table[i][temp - 1] + Table[i][temp]);
  259.                             Table[i][temp] = 0;
  260.                             temp = temp - 1;
  261.                         } else if (Table[i][temp - 1] == Table[i][temp] && sum) {// אם מי שמשמאלי שווה לי
  262.                             Table[i][temp - 1] = (Table[i][temp - 1] + Table[i][temp]);
  263.                             score(Table[i][temp - 1]);
  264.                             Table[i][temp] = 0;
  265.                             temp = temp - 1;
  266.                             if(temp ==1) {
  267.                                     sum = false;
  268.                         }
  269.                         } else {
  270.                             temp = temp - 1;
  271.                         }
  272.                     } // while
  273.                 }
  274.             }
  275.         }
  276.         checkWrongStep();
  277.         canNotRevers = false;
  278.     }
  279.  
  280.     public static void moveRight() {
  281.         for (int i = 0; i < 4; i++) {
  282.             boolean sum = true;
  283.             for (int j = 2; j >= 0; j--) {
  284.                 int temp = j;
  285.                 if (Table[i][j] != 0) {// אם אני לא אפס
  286.                     while (temp < 3) {
  287.                         if (Table[i][temp + 1] == 0) {// אם מי שמימיני שווה לאפס
  288.                             Table[i][temp + 1] = (Table[i][temp + 1] + Table[i][temp]);
  289.                             Table[i][temp] = 0;
  290.                             temp = temp + 1;
  291.                         } else if (Table[i][temp + 1] == Table[i][temp] && sum) {// אם מי שמתחתיי שווה לי
  292.                             Table[i][temp + 1] = (Table[i][temp + 1] + Table[i][temp]);
  293.                             score(Table[i][temp + 1]);
  294.                             Table[i][temp] = 0;
  295.                             temp = temp + 1;
  296.                             if(temp ==2) {
  297.                                 sum = false;
  298.                     }
  299.                         } else {
  300.                             temp = temp + 1;
  301.                         }
  302.                     } // while
  303.                 }
  304.             }
  305.         }
  306.         checkWrongStep();
  307.         canNotRevers = false;
  308.     }
  309.     public static void checkWrongStep() {
  310.         if (canNotMove()) {
  311.             System.out.println("eror!!!! Please select another option:");
  312.             printTable();
  313.             printInformation();
  314.         }
  315.     }
  316.  
  317.     public static void revers() {
  318.         for (int i = 0; i < 4; i++) {
  319.             for (int j = 0; j < 4; j++) {
  320.                 Table[i][j] = copyTable[i][j];
  321.             }
  322.         }
  323.         canNotRevers = true;
  324.         score = preScore;
  325.         best = prebest;
  326.     }
  327.  
  328.     public static void copyTheBoard() {
  329.         preScore = score;
  330.         prebest = best;
  331.         for (int i = 0; i < 4; i++) {
  332.             for (int j = 0; j < 4; j++) {
  333.                 copyTable[i][j] = Table[i][j];
  334.             }
  335.         }
  336.     }
  337.  
  338.     public static boolean checkIfGameIsOver() {
  339.         if (!gameIsWonButCont && checkIfWin()) {
  340.             System.out.println("You won! 2048! would you like to continue? Press y if yes, n if not");
  341.             char choose = inputUser();
  342.             if (choose == 'n') {
  343.                 endGame();
  344.                 return true;
  345.             }
  346.             if (choose == 'y') {
  347.                 gameIsWonButCont = true;
  348.                 startGame();
  349.                 return false;
  350.             }
  351.         }
  352.         if (lose()) {
  353.             return true;
  354.         }
  355.         return false;
  356.     }
  357.  
  358.     public static void playerMove() {
  359.         char choose = inputUser();
  360.         if (choose == 'w' || choose == 'a' || choose == 's' || choose == 'd') {
  361.  
  362.             makeMovmentKeys(choose);
  363.             if (!checkIfGameIsOver() && !canNotMove()) {
  364.                 lottery();
  365.                 printTable();
  366.                 printInformation();
  367.             }
  368.         } else if (choose == 'e') {
  369.             endGame();
  370.         } else if (choose == 'r') {
  371.             if (!canNotRevers) {
  372.                 revers();
  373.                 lottery();
  374.                 printTable();
  375.                 printInformation();
  376.             } else {
  377.                 System.out.println("You can not do return. Please select another option:");
  378.                 System.out.println();
  379.                 printTable();
  380.                 printInformation();
  381.             }
  382.         } else if (choose == 'n') {
  383.             score = 0;// מאפס את הניקוד
  384.             initVars();// מתחיל משחק מחדש
  385.         }
  386.     }
  387.  
  388.     public static void makeMovmentKeys(char choose) {
  389.         copyTheBoard();
  390.         if (choose == 'w') { // move up
  391.             moveUp();
  392.         } else if (choose == 's') { // ...
  393.             moveDown();
  394.         } else if (choose == 'a') {
  395.             moveLeft();
  396.         } else if (choose == 'd') {
  397.             moveRight();
  398.         }
  399.     }
  400. }// class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement