-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.java~
119 lines (109 loc) · 3.12 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
import java.util.*;
/**
* @author Goetz und Dominik
* @version 9.11
*/
public class AI extends Player
{
public List used_chars = new ArrayList<String>();
public Map table;
public AI()
{
table = this.create_table();
}
public void set_name(){
this.name = "Computer";
}
public String word_input()
{
// Hier werden die Worte aus einer Wortliste gelesen!
List word_list = new ArrayList<String>();
int word_list_len = 0;
In.open("wortliste.txt");
String tmpWord = In.readWord();
while (In.done()){
word_list.add(tmpWord);
word_list_len += 1;
// Out.println(tmpWord);
tmpWord = In.readWord();
}
In.close();
Random random = new Random();
int index = random.nextInt(word_list_len);
String word = (String) 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);
// Out.println(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();
// Out.println(pair.getKey() + " = " + pair.getValue());
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();
return result;
}
public String guess()
{
String Alphabet;
Random random = new Random();
String tmp = new String();
Alphabet = this.generate_alphabet();
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;
}
}
Out.println(Alphabet);
Out.println(tmp);
return tmp;
}
}