-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.java
146 lines (126 loc) · 3.93 KB
/
AI.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.util.*;
/**
* Die Klasse AI raet Woerter, welche vom Spieler eingegeben wurden oder
* laesst den Spieler Woerter aus einer Liste raten.
*
* @author Goetz und Dominik
* @version 9.11
*/
public class AI extends Player
{
public List used_chars = new ArrayList<String>();
public List word_list = new ArrayList<String>();
public Map table;
public int word_list_len = 0;
public String list_filename = new String();
public AI()
{
table = this.create_table();
Out.println("1: Wortliste 1");
Out.println(" Selbst zusammengestellt, auf Hangman angepasst");
Out.println("");
Out.println("2: Wortliste 2");
Out.println(" Komplette Wortliste der deutschen Sprache, auch Beugungen möglich");
Out.println("");
Out.println("3: Wortliste 3");
Out.println(" Umfangreiche Wortliste, nur Substantive");
Out.println("");
Out.println("Suchen Sie sich eine Liste aus");
String list = In.readWord();
this.list_filename = "wortliste" + list + ".txt";
word_list_input();
}
public void set_name(){
this.name = "Computer";
}
public void word_list_input(){
In.open(this.list_filename);
String tmpWord = In.readWord();
while (In.done()){
if (tmpWord.length() <= 10){
word_list.add(tmpWord);
this.word_list_len += 1;
}
tmpWord = In.readWord();
}
In.close();
Out.println("Anzahl Woerter: " + this.word_list_len);
}
public String word_input()
{
Random random = new Random();
int index = random.nextInt(this.word_list_len);
String word = (String) this.word_list.get(index);
word = word.toUpperCase();
MSWord W = new MSWord(word);
int rating = W.get_rating();
int diff = W.get_difficulty();
return word;
}
public Map create_table(){
Map<String, Integer> table = new HashMap<String, Integer>();
String[] parts;
String chr;
Integer value;
In.open("ht.txt");
String tmp = In.readWord();
while(In.done()){
parts = tmp.split(";");
chr = parts[0];
value = Integer.valueOf(parts[1]);
table.put(chr, value);
tmp = In.readWord();
}
In.close();
return table;
}
public String generate_alphabet() {
StringBuilder alphabet = new StringBuilder();
table = this.table;
String key;
int value;
Iterator it = table.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
key = (String) pair.getKey();
value = (Integer) pair.getValue();
if ( (value / 100) == 0){
value = 1;
}
else{
value = value / 100;
}
for (int i = 0; i <= value; i++){
alphabet.append(key);
}
it.remove();
}
String result = alphabet.toString();
this.table = this.create_table();
return result;
}
public String guess()
{
String Alphabet;
Random random = new Random();
String tmp = new String();
Alphabet = this.generate_alphabet();
this.create_table();
int index = 0;
boolean running = true;
while (running){
Alphabet = this.generate_alphabet();
index = random.nextInt(Alphabet.length());
tmp = Alphabet.substring(index, index+1);
if (used_chars.contains(tmp)){
running = true;
}
else{
used_chars.add(tmp);
running = false;
break;
}
}
return tmp;
}
}