반응형
앞의 버전의 2048 ver1.0은 말했듯이 기능이 매우 제한적이였다. 그래서 이번에는 조금 기능을 추가해 보았다.
기능을 추가해봤더니 코드가 570줄...ㄷㄷ...
이렇게 많이 나올줄 알았으면 처음부터 분리해서 코드를 짤걸 이라는 후회가 든다... 시간이 나면 다시 싹 뜯어 고쳐야겠다.
<ver 1.0과 달라진 점>
움직임을 입력받을 때 숫자가 아닌 w, a, s, d로 입력받게 하였다.
reset, save, load 기능 추가
점수 표시 및 undo 기능 추가 (단, undo는 10번만 가능하다.)
<다음 버전에서 수정해야 할 점>
undo는 10번만 가능하지만 save를 하면 전체 값이 저장되어서 용량의 낭비가 생기므로 그 이후의 값들은 삭제하는 방안을 이용해야겠다.
code가 너무 지저분하다...(아무리 생각을 해보더라도..) 코드를 나눠서 쓰던지 아니면 간결한 표현으로 바꿔야겠다.
아래는 소스코드이다.
- import java.util.Scanner;
- import java.util.Vector;
- import java.io.*;
- public class game {
- public static int score;
- public static int undo;
- public static int[] arr_score = new int [10];
- /*********** This is element function ***********/
- public static int getRand(int min, int max) {
- return i;
- }
- public static void syncArr(int [][] arr, int [][] barr) {
- if(arr.length != barr.length)
- return;
- for(int i = 0; i < arr.length; i++) {
- if(arr[i].length != barr[i].length)
- return;
- for(int j = 0; j < arr[i].length; j++)
- barr[i][j] = arr[i][j];
- }
- }
- public static void syncArr(int []arr, int [] barr) {
- if(arr.length != barr.length)
- return;
- for(int i = 0; i < arr.length; i++) {
- barr[i] = arr[i];
- }
- }
- // if arr[i][j] and arr[k][l] are same, then merge it at arr[i][j]
- public static void merge(int [][] arr, int i, int j, int k, int l) {
- if(arr[i][j] == arr[k][l])
- {
- arr[i][j] = arr[i][j] + arr[k][l];
- arr[k][l] = 0;
- score = score + arr[i][j];
- }
- return;
- }
- // if arr[i][j] is 0, then move arr[k][l] to arr[i][j]
- public static void shift(int [][] arr, int i, int j, int k, int l) {
- if(arr[i][j] == 0)
- {
- arr[i][j] = arr[k][l];
- arr[k][l] = 0;
- }
- }
- public static void randNum(int [][] arr) {
- int i = 0; int j = 0; // i, j is index number of array
- if(check_Lose(arr)) {
- show_Lose();
- return;
- }
- while(true) {
- i = getRand(0, arr.length - 1);
- j = getRand(0, arr[i].length - 1);
- if(arr[i][j] == 0)
- break;
- }
- arr[i][j] = getRand(0, 1) * 2 + 2;
- }
- /*********** This is moving function ***********/
- public static void L_Move(int [][] arr) {
- int k, l;
- for(int i = 0; i < arr.length; i++) {
- for(int j = 0; j < arr[i].length-1; j++) {
- k = i; l = j;
- while(l >=0) {
- shift(arr, k, l, k, l+1);
- l--;
- }
- }
- }
- for(int i = 0; i < arr.length; i++) {
- for(int j =0; j < arr[i].length-1; j++)
- {
- merge(arr, i, j, i, j+1);
- }
- }
- for(int i = 0; i < arr.length; i++) {
- for(int j = 0; j < arr[i].length-1; j++) {
- k = i; l = j;
- while(l >=0) {
- shift(arr, k, l, k, l+1);
- l--;
- }
- }
- }
- }
- public static void R_Move(int [][] arr) {
- int k, l;
- for(int i = 0; i < arr.length; i++) {
- for(int j = arr[i].length - 1; j > 0; j--) {
- k = i; l = j;
- while(l < arr[i].length ) {
- shift(arr, k, l, k, l-1);
- l++;
- }
- }
- }
- for(int i = 0; i < arr.length; i++) {
- for(int j = arr[i].length - 1; j > 0; j--) {
- merge(arr, i, j, i, j-1);
- }
- }
- for(int i = 0; i < arr.length; i++) {
- for(int j = arr[i].length - 1; j > 0; j--) {
- k = i; l = j;
- while(l < arr[i].length ) {
- shift(arr, k, l, k, l-1);
- l++;
- }
- }
- }
- }
- public static void T_Move(int [][] arr) {
- int k, l;
- for(int j = 0; j < arr.length; j++) {
- for(int i = 1; i < arr.length; i++) {
- k = i; l = j;
- while(k > 0 ) {
- shift(arr, k-1, l, k, l);
- k--;
- }
- }
- }
- for(int j = 0; j < arr.length; j++) {
- for(int i = 1; i < arr.length; i++) {
- merge(arr, i-1, j, i, j);
- }
- }
- for(int j = 0; j < arr.length; j++) {
- for(int i = 1; i < arr.length; i++) {
- k = i; l = j;
- while(k > 0 ) {
- shift(arr, k-1, l, k, l);
- k--;
- }
- }
- }
- }
- public static void B_Move(int [][] arr) {
- int k, l;
- for(int j = 0; j < arr.length; j++) {
- for(int i = arr.length - 2; i >= 0; i--) {
- k = i; l = j;
- while(k < arr.length -1 ) {
- shift(arr, k+1, l, k, l);
- k++;
- }
- }
- }
- for(int j = 0; j < arr.length; j++) {
- for(int i = arr.length - 2; i >= 0; i--) {
- merge(arr, i+1, j, i, j);
- }
- }
- for(int j = 0; j < arr.length; j++) {
- for(int i = arr.length - 2; i >= 0; i--) {
- k = i; l = j;
- while(k < arr.length -1 ) {
- shift(arr, k+1, l, k, l);
- k++;
- }
- }
- }
- }
- int [][] Barr = new int[arr.length][arr.length];
- syncArr(arr, Barr);
- if(opt.equalsIgnoreCase("w")) {
- T_Move(arr);
- } else if (opt.equalsIgnoreCase("s")) {
- B_Move(arr);
- } else if (opt.equalsIgnoreCase("d")) {
- R_Move(arr);
- } else if (opt.equalsIgnoreCase("a")) {
- L_Move(arr);
- } else if (opt.equalsIgnoreCase("r") || opt.equalsIgnoreCase("e") ||
- opt.equalsIgnoreCase("u") || opt.equalsIgnoreCase("h") ||
- opt.equalsIgnoreCase("v") || opt.equalsIgnoreCase("l")) {
- return false;
- } else {
- return false;
- }
- // If it can move, then insert new number. If it cann't, don't insert new number.
- for(int i = 0; i < arr.length; i++) {
- for(int j = 0; j < arr[i].length; j++) {
- if(Barr[i][j] != arr[i][j]) {
- randNum(arr);
- show_arr(arr);
- return true;
- }
- }
- }
- show_arr(arr);
- return false;
- }
- /*********** This is showing function ***********/
- public static void show_option() {
- + "e : end game, u : undo, h : view high score, v : save, l : load");
- }
- public static void show_arr(int [][] arr) {
- for(int i = 0; i < arr.length; i++) {
- for(int j = 0; j < arr[i].length; j++) {
- }
- }
- }
- public static void show_Win() {
- }
- public static void show_Lose() {
- }
- /*********** checking the game end ***********/
- public static boolean check_Same(int [][] arr, int [][] barr) {
- if(arr.length != barr.length)
- return false;
- for(int i = 0; i < arr.length; i++) {
- if(arr[i].length != barr[i].length)
- return false;
- for(int j = 0; j < arr[i].length; j++)
- if(barr[i][j] != arr[i][j])
- return false;
- }
- return true;
- }
- public static boolean check_Win(int [][] arr, int maxnum) {
- for(int i = 0; i < arr.length; i++) {
- for(int j = 0; j < arr[i].length; j++) {
- if(maxnum <= arr[i][j]){
- HighScore("score.txt", score);
- show_Win();
- return true;
- }
- }
- }
- return false;
- }
- public static boolean check_Lose(int [][] arr) {
- int [][] barr = new int[arr.length][arr.length];
- syncArr(arr, barr);
- for(int i = 0; i < arr.length; i++) {
- for(int j = 0; j < arr[i].length; j++) {
- if(arr[i][j] == 0)
- return false;
- }
- }
- L_Move(barr);
- if (!check_Same(barr, arr))
- return false;
- syncArr(arr, barr);
- R_Move(barr);
- if(!check_Same(barr, arr))
- return false;
- syncArr(arr, barr);
- T_Move(barr);
- if(!check_Same(barr, arr))
- return false;
- syncArr(arr, barr);
- B_Move(barr);
- if(!check_Same(barr, arr))
- return false;
- show_Lose();
- HighScore("score.txt", score);
- return true;
- }
- /*********** This is setting game ***********/
- public static void setGame(int [][] arr) {
- randNum(arr);
- randNum(arr);
- show_arr(arr);
- score = 0; undo = 10;
- }
- public static void restart(int [][] arr) {
- for(int i = 0; i < arr.length; i++) {
- for(int j = 0; j < arr[i].length; j++) {
- arr[i][j] = 0;
- }
- }
- randNum(arr);
- randNum(arr);
- show_arr(arr);
- score = 0; undo = 10;
- }
- /*********** This is additional code. ***********/
- public static void Save_Arr(Vector<int [][]> vec, int [][] arr) {
- int [][] barr = new int [arr.length][arr.length];
- syncArr(arr, barr);
- vec.add(barr);
- }
- int [][] arr = new int[4][4];
- try {
- for(int i = 0; i < score_arr.length; i++)
- fw.write(score_arr[i] + " ");
- fw.write("\n" + undo + "\n");
- for(int i = 0; i < vec.size(); i++) {
- syncArr(vec.elementAt(i), arr);
- for(int j = 0; j < arr.length; j++) {
- for(int k = 0; k < arr.length; k++) {
- fw.write(arr[j][k] + " ");
- }
- }
- }
- fw.close();
- e.printStackTrace();
- }
- }
- try {
- int nextSpace; String line;
- Vector<Integer> vec = new Vector<Integer>();
- if((line = br.readLine()) != null && !line.equals("")) {
- line = line.trim();
- nextSpace = line.indexOf(" ");
- while(nextSpace > 0) {
- line = line.substring(nextSpace).trim();
- nextSpace = line.indexOf(" ");
- }
- nextSpace = line.indexOf(" ");
- br.close();
- for(int i = 0; i < arr.length && vec.size() - i > 0; i++) {
- arr[i] = (int) vec.elementAt(vec.size()-i-1);
- }
- }
- e.printStackTrace();
- }
- }
- int [][] arr = new int[4][4];
- vec.clear();
- try {
- if(!file.exists()) {
- return false;
- }
- String line;
- int x; int i = 0, j = 0;
- Read_Txt(fileName, arr_score);
- int nextSpace;
- line = br.readLine();
- if((line = br.readLine()) != null && !line.equals("")) {
- line = line.trim();
- nextSpace = line.indexOf(" ");
- }
- if((line = br.readLine()) != null && !line.equals("")) {
- line = line.trim();
- nextSpace = line.indexOf(" ");
- while(nextSpace > 0) {
- arr[i%4][j%4] = x;
- j++;
- if( (j%4) == 0 ) {
- i++;
- }
- if(i == 4 && j%4 == 0) {
- int [][] barr = new int [4][4];
- syncArr(arr, barr);
- vec.add(barr);
- i = i%4;
- }
- line = line.substring(nextSpace).trim();
- nextSpace = line.indexOf(" ");
- }
- nextSpace = line.indexOf(" ");
- int [][] barr = new int [4][4];
- syncArr(arr, barr);
- vec.add(barr);
- }
- score = arr_score[9];
- br.close();
- return true;
- e.printStackTrace();
- return false;
- }
- }
- public static void saveundoScore (int [] arr, int Score) {
- int [] barr = new int[arr.length];
- for(int i = 0; i < barr.length; i++)
- barr[i] = arr[i];
- arr[0] = Score;
- for(int i = 1; i < arr.length; i++) {
- arr[i] = barr[i-1];
- }
- }
- public static void BubbleSort(int [] arr) {
- for(int i = 0; i < arr.length - 1; i++) {
- for(int j = 0; j < arr.length -1 - i; j++) {
- if(arr[j] < arr[j+1]) {
- int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- }
- int [] highScore = new int[5];
- try {
- if(!file.exists()) {
- fw.write("0 0 0 0 0");
- fw.close();
- }
- Read_Txt(fileName, highScore);
- BubbleSort(highScore);
- if(highScore[4] < score) {
- highScore[4] = score;
- }
- BubbleSort(highScore);
- for(int i = 0; i < 5; i++) {
- fw.write(x + " ");
- }
- fw.close();
- e.printStackTrace();
- }
- }
- try {
- int x = (int) tokens.nval;
- }
- fr.close();
- e.printStackTrace();
- }
- }
- public static boolean Undo_Game(Vector<int [][]> vec, int [][] arr) {
- if(undo <= 0 || vec.size() <= 1 || check_Same(arr, vec.elementAt(0))) {
- return false;
- }
- int [][] barr = new int [arr.length][arr.length];
- vec.removeElementAt(vec.size()-1);
- barr = vec.elementAt(vec.size()-1);
- vec.removeElementAt(vec.size()-1);
- syncArr(barr, arr);
- undo--;
- for(int i = 1; i < arr_score.length; i++) {
- arr_score[i-1] = arr_score[i];
- }
- arr_score[9] = 10;
- score = arr_score[0];
- return true;
- }
- /*********** This is main part ***********/
- /* "score.txt" file is save the score. And "save.txt" file is save the game. */
- int [][] arr = new int[4][4];
- String opt = " ";
- int num = 2048; // If make 'num' then the game ends.
- Vector<int [][]> vec = new Vector<int [][]>();
- setGame(arr);
- while(!opt.equalsIgnoreCase("e")) {
- show_option();
- opt = scan.nextLine();
- if(opt.equalsIgnoreCase("r")) {
- vec.clear();
- HighScore("score.txt", score);
- restart(arr);
- }
- if(opt.equalsIgnoreCase("u") && undo >= 0) {
- Undo_Game(vec, arr);
- Save_Arr(vec, arr);
- show_arr(arr);
- }
- if(opt.equalsIgnoreCase("h")) {
- show_HighScore("score.txt");
- }
- if(opt.equalsIgnoreCase("v"))
- Save_Game("save.txt", vec, arr_score);
- if(opt.equalsIgnoreCase("l")) {
- if(Load_Game("save.txt", vec)) {
- arr = vec.elementAt(vec.size()-1);
- show_arr(arr);
- }
- }
- if(MoveArr(arr, opt)) {
- if(!check_Win(arr, num) && !check_Lose(arr)) {
- Save_Arr(vec, arr);
- saveundoScore(arr_score, score);
- } else {
- + "If you don't, type 'e'");
- opt = scan.nextLine();
- while(!opt.equalsIgnoreCase("r") && !opt.equalsIgnoreCase("e")) {
- opt = scan.nextLine();
- }
- if(opt.equalsIgnoreCase("r")) {
- vec.clear();
- restart(arr);
- }
- }
- }
- }
- scan.close();
- }
- }
반응형
'컴퓨터 > Java' 카테고리의 다른 글
MongoDB에서 Between 쿼리 사용하기 (0) | 2024.04.23 |
---|---|
도서 바코드 데이터 추출 알고리즘 (2) | 2016.01.27 |
[프로그래밍, 자료구조] 큐(Queue) (2) | 2014.08.25 |
Fibonacci number (0) | 2014.06.04 |
2048 ver 1.0 (0) | 2014.06.04 |