(miniProject) Java를 이용한 WordGameProgram

2021. 9. 3. 15:06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.java.tango;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;
 
public class TangoManager {
    
    private HashMap<StringString> tango;
    private ArrayList<String> tangoKey;
    private ArrayList<String> tangoValue;
    
    //constructor
    public TangoManager() {
        tango         = new HashMap<>();
        tangoKey     = new ArrayList<>();
        tangoValue     = new ArrayList<>();
    }
    
    //getter setter
    public HashMap<StringString> getTango() {
        return tango;
    }
    public void setTango(HashMap<StringString> tango) {
        this.tango = tango;
    }
    public ArrayList<String> getTangoKey() {
        return tangoKey;
    }
    public void setTangoKey(ArrayList<String> tangoKey) {
        this.tangoKey = tangoKey;
    }
    public ArrayList<String> getTangoValue() {
        return tangoValue;
    }
    public void setTangoValue(ArrayList<String> tangoValue) {
        this.tangoValue = tangoValue;
    }
 
    
    public void saveMachine(int num) {
        Scanner scan = new Scanner(System.in);
        
        for (int i = 1; i < num + 1; i++) {
            System.out.print(i + "번째 ) 단어는?    : ");
            String key = scan.next();
            System.out.print(i + "번째 ) 뜻은?        : ");
            String value = scan.next();
            
            if(tango.get(key) != null) {
                System.out.print("이미 정해진 단어가 있습니다. 덮어 쓰겠습니까?\n덮어쓰려면 1, 아니면 그 외의 값을 입력하시오.");
                
                if(!scan.next().equals("1")) {
                    System.out.println("귀하가 입력한 단어는 덮어쓰지 않았습니다. 다시 입력하시오.");
                    continue;
                }
                
            }
            tango.put(key, value);
            tangoKey.add(key);
            tangoValue.add(value);
        }
    }
    
    public HashMap<StringString> printMachine() {        
        return tango;
    }
    
    public String magicBox(int num) {        
        Random r     = new Random();
        int random     = r.nextInt(tango.size());
        switch(num) {
            case 1    : return tangoKey.get(random);
            case 2    : return tangoValue.get(random);
            default : return null;
        }
    }
    
    public boolean valueFinder(String question, String answer) {//key값을 받아 value값을 찾는다.
        String answerQuest = null;
        Set<Entry<StringString>> entrySet = tango.entrySet();
        
        for (Entry<StringString> entry : entrySet) {
            if (entry.getKey().equals(question)) {
                answerQuest = entry.getValue();
                break;
            }
        }
        
        if (answerQuest.equals(answer)) {
            return true;
        } else {
            return false;
        }
        
    }
    
    public boolean keyFinder(String question, String answer) { //value값을 받아 key를 찾는다.
        String yourChoice = tango.get(answer);
        
        if (yourChoice == null) {
            return false;
            }
        
        if(question.equals(yourChoice)) {
            return true;
        } else {
            return false;
        }
    }        
}
cs

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.java.tango;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Set;
 
public class TangoUI {
    
    private Scanner scan;
    private int choice;
    private TangoManager tm;
    
    public TangoUI() {
        
        scan     = new Scanner(System.in);
        tm         = new TangoManager();
        
        while (true) {
            printMenu();
    
            try {
                choice = scan.nextInt();
            } catch (Exception e) {
                System.out.println("입력값 에러! 다시 입력하세요 ㅠㅠ");
                scan.next();
                continue;
            }
            
            switch (choice) {
                case 1     :     saveTango();         break;
                case 2     :     printTango();        break;
                case 3     :     playTangoKey();        break;
                case 4     :     playTangoValue();    break;
                case 0     :     System.out.println("종료되었습니다."); System.exit(0); break;
                default :     System.out.println("입력값 에러! 다시 입력하세요!");
            }
        }
    }
 
    public void saveTango() {
        int num = 0;
        while (true) {
            System.out.print("몇 개의 단어를 저장하실? ㅎㅎ: ");
            try {
                num = scan.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("잘못된 숫자를 입력했습니다. 다시 입력하세요.");
                scan.next();
                continue;
            }
            break;
        }
        tm.saveMachine(num);
    }
    
    public void printTango() {
        System.out.println("**************");
        HashMap<StringString> tango = tm.printMachine();
        Set<String> keys = tango.keySet();
        for (String s : keys) {
            String key = s;
            String value = tango.get(key);
            System.out.println(key + " 의 뜻은 " + value + "입니다!");
        }
        System.out.println("**************");
    }
    
    public void playTangoKey() {    // 무작위 key를 제시하고 그 key에 따른 value를 맞추는 게임        
        System.out.print("게임을 몇 회 진행하시겠습니까? : ");
        int round = scan.nextInt();
        for (int i = 1; i < round + 1 ; i++) {
            String keyQuest = tm.magicBox(1);
            System.out.println(i + "회차 입니다." + keyQuest + "는 무슨 뜻(value)일까요?");
            Boolean result     = tm.valueFinder(keyQuest, scan.next());
            if (result == false) {                         
                System.out.println("틀렸습니다 ㅠㅠ");
            } else {
                System.out.println("축하합니다! 정답입니다!");        
            }
        }    
    }
    
    public void playTangoValue() {    // 무작위 Value를 제시하고 그 Value에 따른 Key를 맞추는 게임        
        System.out.print("게임을 몇 회 진행하시겠습니까? : ");
        int round = scan.nextInt();
        for (int i = 1; i < round + 1 ; i++) {
            String valueQuest = tm.magicBox(2);
            System.out.println(i + "회차 입니다." + valueQuest + "는 무슨 단어(key)일까요?");
            Boolean result     = tm.keyFinder(valueQuest, scan.next());
            if (result == false) {                         
                System.out.println("틀렸습니다 ㅠㅠ");
            } else {
                System.out.println("축하합니다! 정답입니다!");        
            }
        }    
    }
    
    public void printMenu() {
        System.out.println("========단어 저장 시스템========");
        System.out.println("원하는 작업을 선택하세요!");
        System.out.println("1. 단어(key)와 뜻(value) 저장하기");
        System.out.println("2. 단어장(k = value) 출력하기");
        System.out.println("3. key를 이용해 value를 찾는 게임");
        System.out.println("4. value를 이용해 key를 찾는 게임");
        System.out.println("0. 종료");
        System.out.println("===========================");
        System.out.print("선택하기 > ");
    }
    
    public static void main(String[] args) {
        new TangoUI();
    }
    
}
cs

 

 

BELATED ARTICLES

more