두더지 잡기 게임
in Diary
자바로 만드는 두더지 잡기 게임
자바공부 한달차에 만든건데 지금보니 개판이다.
그래도 미래의 나를 위해서 업로드는 해둬야지..
DB가 뭔지 모를때 만든거라 기록은 IOStream
을 이용해 txt파일로 Save&Load 되는 구조다.
맵은 2차원 배열로 구현하였음.
public class MoleMain {
public static void main(String[] args) throws InterruptedException {
MoleGame mg = new MoleGame();
mg.moleGame();
}
}
public class MoleGame {
static private int[][] map = {
{3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4},
{3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}};
static private void callMap() {
for(int i = 0; i < 18; i++) {
for(int j = 0; j < 20; j++) {
if(map[i][j] == 4) {
System.out.print("▩");
}
else if(map[i][j] == 3) {
System.out.print(" ");
}
else if(map[i][j] == 2) {
System.out.print("∩");
}
else if(map[i][j] == 1) {
System.out.print("_");
}
else if(map[i][j] == 0) {
System.out.print("♨");
}
}
System.out.println("");
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
static private void clearScreen() {
try {
if(System.getProperty("os.name").contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
else {
Runtime.getRuntime().exec("clear");
}
}
catch(IOException | InterruptedException ex) {
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
public void moleGame() throws InterruptedException {
DecimalFormat formatter = new DecimalFormat("###,###");
Scanner sc = new Scanner(System.in);
String name, sel, selM, show_score;
Record rc;
double time;
long start_time = 0, end_time = 0;
int input = 0, score = 0, cnt = 1;
boolean trg0 = false, trg1 = false, trg2 = false, trg3 = false, trg4 = false;
clearScreen();
System.out.println("\n\n\n ♨♨♨ 두더지 잡기 게임 ♨♨♨\n");
System.out.println(" <<<<< 입력 >>>>>");
System.out.println("좌상 : 1 / 우상 : 2 / 좌하 : 3 / 우하 : 4\n");
System.out.println("두더지를 잘못 잡을 경우 점수가 감소합니다.\n\n\n");
System.out.println(" 게임을 시작하려면 y를 입력하세요\n\n");
callMap();
sel = sc.nextLine();
switch(sel) {
case "y":
start_time = System.currentTimeMillis();
break;
default:
clearScreen();
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 게임을 종료 합니다 ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++");
Thread.sleep(4000);
clearScreen();
System.exit(0);
}
do {
map[4][6] = (int) Math.round(Math.random() + 1);
map[4][15] = (int) Math.round(Math.random() + 1);
map[13][6] = (int) Math.round(Math.random() + 1);
map[13][15] = (int) Math.round(Math.random() + 1);
if(map[4][6] == 1 && map[4][15] == 1 && map[13][6] == 1 && map[13][15] == 1) {
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| Lucky! 추가점수 획득! ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++");
score += 3;
Thread.sleep(500);
continue;
}
clearScreen();
System.out.println("\n\n\n ♨♨♨ 두더지 잡기 게임 ♨♨♨\n");
System.out.printf(" STAGE %d\n\n", cnt);
System.out.println(" <<<<< 입력 >>>>>");
System.out.println("좌상 : 1 / 우상 : 2 / 좌하 : 3 / 우하 : 4\n");
callMap();
trg1 = map[4][6] == 0 || map[4][6] == 1 ? true : false;
trg2 = map[4][15] == 0 || map[4][15] == 1 ? true : false;
trg3 = map[13][6] == 0 || map[13][6] == 1 ? true : false;
trg4 = map[13][15] == 0 || map[13][15] == 1 ? true : false;
trg0 = trg1 == true && trg2 == true && trg3 == true && trg4 == true ? true : false;
while(trg0 == false) {
input = sc.nextInt();
if(input == 1 && trg1 == true) {
System.out.println("땡! >ㅆ<");
score -= 5;
}
if(input == 2 && trg2 == true) {
System.out.println("땡! >ㅆ<");
score -= 5;
}
if(input == 3 && trg3 == true) {
System.out.println("땡! >ㅆ<");
score -= 5;
}
if(input == 4 && trg4 == true) {
System.out.println("땡! >ㅆ<");
score -= 5;
}
if(input < 1 || input > 4) {
System.out.println("땡! >ㅆ<");
score -= 5;
}
if(map[4][6] == 2 && input == 1) {
map[4][6] = 0;
clearScreen();
score += 1;
System.out.println("\n\n\n ♨♨♨ 두더지 잡기 게임 ♨♨♨\n");
System.out.printf(" STAGE %d\n\n", cnt);
System.out.println(" <<<<< 입력 >>>>>");
System.out.println("좌상 : 1 / 우상 : 2 / 좌하 : 3 / 우하 : 4\n");
callMap();
trg1 = map[4][6] == 0 || map[4][6] == 1 ? true : false;
}
else if(map[4][15] == 2 && input == 2) {
map[4][15] = 0;
clearScreen();
score += 1;
System.out.println("\n\n\n ♨♨♨ 두더지 잡기 게임 ♨♨♨\n");
System.out.printf(" STAGE %d\n\n", cnt);
System.out.println(" <<<<< 입력 >>>>>");
System.out.println("좌상 : 1 / 우상 : 2 / 좌하 : 3 / 우하 : 4\n");
callMap();
trg2 = map[4][15] == 0 || map[4][15] == 1 ? true : false;
}
else if(map[13][6] == 2 && input == 3) {
map[13][6] = 0;
clearScreen();
score += 1;
System.out.println("\n\n\n ♨♨♨ 두더지 잡기 게임 ♨♨♨\n");
System.out.printf(" STAGE %d\n\n", cnt);
System.out.println(" <<<<< 입력 >>>>>");
System.out.println("좌상 : 1 / 우상 : 2 / 좌하 : 3 / 우하 : 4\n");
callMap();
trg3 = map[13][6] == 0 || map[13][6] == 1 ? true : false;
}
else if(map[13][15] == 2 && input == 4) {
map[13][15] = 0;
clearScreen();
score += 1;
System.out.println("\n\n\n ♨♨♨ 두더지 잡기 게임 ♨♨♨\n");
System.out.printf(" STAGE %d\n\n", cnt);
System.out.println(" <<<<< 입력 >>>>>");
System.out.println("좌상 : 1 / 우상 : 2 / 좌하 : 3 / 우하 : 4\n");
callMap();
trg4 = map[13][15] == 0 || map[13][15] == 1 ? true : false;
}
trg0 = trg1 == true && trg2 == true && trg3 == true && trg4 == true ? true : false;
}
cnt++;
Thread.sleep(200);
} while(!(cnt == 20));
end_time = System.currentTimeMillis();
time = (end_time - start_time) / 1000.0;
show_score = formatter.format(score * 123456);
System.out.print("\n\n ■");
for(int i = 0; i < 17; i++) {
Thread.sleep(200);
System.out.print("■");
}
Thread.sleep(1000);
clearScreen();
System.out.println("\n+-----------------------------------------------+");
System.out.println("| 결 과 |");
System.out.println("+-----------------------------------------------+");
System.out.println("| |");
System.out.printf("| 잡은 두더지 : %3d 마리 |\n", score);
System.out.println("| |");
System.out.println("+-----------------------------------------------+");
System.out.println("| |");
System.out.printf("| 경과 시간 : %5.2f 초 |\n", time);
System.out.println("| |");
System.out.println("+-----------------------------------------------+");
System.out.println("| |");
System.out.printf("| 점수 : %10s 점 |\n", show_score);
System.out.println("| |");
System.out.println("+-----------------------------------------------+\n\n");
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 기록을 저장하시겠습니까? ||");
System.out.println("\t|| y / AnyKey ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++");
sc.nextLine();
selM = sc.nextLine();
switch(selM) {
case "y":
clearScreen();
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 이름을 입력하세요 ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++\n");
System.out.print("이름 : ");
name = sc.nextLine();
while(name.matches("^[가-힣]*$") || name.length() > 12) {
clearScreen();
System.out.println("\n\n\t++---------------------------------------------------++");
System.out.println("\t++---------------------------------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 한글이 포함되었거나 이름이 너무 깁니다 ||");
System.out.println("\t|| 이름을 다시 입력하세요 ||");
System.out.println("\t|| ||");
System.out.println("\t++---------------------------------------------------++");
System.out.println("\t++---------------------------------------------------++\n");
System.out.print("이름 : ");
name = sc.nextLine();
}
rc = new Record(name, show_score, score, time);
rc.loadandsaveRecord();
clearScreen();
rc.showRecord();
Thread.sleep(4000);
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 게임을 재시작 할까요? ||");
System.out.println("\t|| y / AnyKey ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++");
selM = sc.nextLine();
switch(selM) {
case "y":
clearScreen();
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 게임을 재시작 합니다 ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++");
Thread.sleep(3000);
clearScreen();
moleGame();
break;
default:
clearScreen();
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 게임을 종료 합니다 ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++");
Thread.sleep(3000);
clearScreen();
System.exit(0);
break;
}
default:
clearScreen();
System.out.println("\n\n\t++----------------------------++");
System.out.println("\t++----------------------------++");
System.out.println("\t|| ||");
System.out.println("\t|| 게임을 종료 합니다 ||");
System.out.println("\t|| ||");
System.out.println("\t++----------------------------++");
System.out.println("\t++----------------------------++");
Thread.sleep(3000);
clearScreen();
System.exit(0);
break;
}
sc.close();
}
}
public class Record {
private int score;
private double time;
private String name, show_score, scoreborad, format, arraybean;
private ArrayList<String> forloadscoreboard = new ArrayList<>();
private String compare_one, compareto_one, compare_two, compareto_two;
private String[] copyArray, to_one, to_two;
public Record(String name, String show_score, int score, double time) {
this.name = name;
this.show_score = show_score;
this.score = score;
this.time = time;
this.format = String.format("| %12s | %5s | %8s | %10s |", this.name, this.score, this.time,
this.show_score);
this.scoreborad = this.format;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
@SuppressWarnings("unchecked")
public void loadandsaveRecord() {
try {
FileInputStream fisrecord = new FileInputStream("D:/java/ws/java_proj/src/game_src/molerecord.ser");
BufferedInputStream bisrecord = new BufferedInputStream(fisrecord);
ObjectInputStream inrecord = new ObjectInputStream(bisrecord);
Object objrecord = inrecord.readObject();
inrecord.close();
ArrayList<String> loadrecord = (ArrayList<String>) objrecord;
for(String read_scoreboard : loadrecord) {
forloadscoreboard.add(read_scoreboard);
}
}
catch(Exception e) {
System.out.printf("파일 불러오기 오류 : %s", e.getMessage());
}
forloadscoreboard.add(this.scoreborad);
copyArray = new String[forloadscoreboard.size()];
int size = 0;
for(String temp : forloadscoreboard) {
copyArray[size++] = temp;
}
for(int i = 0; i < copyArray.length; i++) {
for(int j = i + 1; j < copyArray.length; j++) {
compare_one = copyArray[i].replace(" ", "");
compareto_one = compare_one.replace("|", " ");
compareto_one = compareto_one.trim();
to_one = compareto_one.split(" ");
to_one[3] = to_one[3].replace(",", "");
compare_two = copyArray[j].replace(" ", "");
compareto_two = compare_two.replace("|", " ");
compareto_two = compareto_two.trim();
to_two = compareto_two.split(" ");
to_two[3] = to_two[3].replace(",", "");
if(Integer.parseInt(to_one[3]) < Integer.parseInt(to_two[3])) {
arraybean = copyArray[i];
copyArray[i] = copyArray[j];
copyArray[j] = arraybean;
}
}
}
ArrayList<String> saveList = new ArrayList<>();
for(String temp : copyArray) {
saveList.add(temp);
}
try {
FileOutputStream fosrecord = new FileOutputStream("D:/java/ws/java_proj/src/game_src/molerecord.ser");
BufferedOutputStream bosrecord = new BufferedOutputStream(fosrecord);
ObjectOutputStream outrecord = new ObjectOutputStream(bosrecord);
outrecord.writeObject(saveList);
outrecord.close();
}
catch(Exception e) {
System.out.printf("파일 저장 오류 : %s", e.getMessage());
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
@SuppressWarnings("unchecked")
public void showRecord() {
try {
FileInputStream fisrecord = new FileInputStream("D:/java/ws/java_proj/src/game_src/molerecord.ser");
BufferedInputStream bisrecord = new BufferedInputStream(fisrecord);
ObjectInputStream inrecord = new ObjectInputStream(bisrecord);
Object objrecord = inrecord.readObject();
inrecord.close();
ArrayList<String> loadrecord = (ArrayList<String>) objrecord;
System.out.println("+---------------------------------------------------------+");
System.out.println("| 이 름 | 두더지 | 시간 | 점수 |");
System.out.println("+---------------------------------------------------------+");
for(String show_scoreboard : loadrecord) {
System.out.println(show_scoreboard);
}
System.out.println("+---------------------------------------------------------+");
}
catch(Exception e) {
System.out.printf("파일 불러오기 오류 : %s", e.getMessage());
}
}
}