-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPVP.java
196 lines (161 loc) · 5.73 KB
/
PVP.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.util.*;
/**
* Die Klasse Game steuert ein Spiel mit mehreren Runden
* Die Methode round_method() wird immer wieder auf-
* gerufen,
*
* @author Götz und Dominik
* @version 9.11
*/
public class PVP
{
Human human_1;
Human human_2;
public PVP()
{
boolean running = true;
human_1 = new Human();
human_2 = new Human();
Human[] result = new Human[2];
result[0] = human_1;
result[1] = human_2;
while(running) {
result = round_word (result[0], result[1]);
Out.println();
Out.println(result[0].get_name() + " : " + result[0].get_score());
Out.println(result[1].get_name() + " : " + result[1].get_score());
Out.println();
Out.println("Beliebiges Zeichen zum fortfahren eingeben!");
Out.println("Q zum Beenden druecken");
String answer = In.readWord();
if((answer.equals("Q")) || (answer.equals("q"))){
Out.println("Beenden");
running = false;
break;
}
else{
In.done();
}
}
Out.println(human_1.get_name() + " : " + human_1.get_score());
Out.println(human_2.get_name() + " : " + human_2.get_score());
if(human_1.get_score()>human_2.get_score()){
Out.println(human_1.get_name() + " hat gewonnen!");
}
else {
if(human_1.get_score()==human_2.get_score()){
Out.println("Es steht Unentschieden ");
}
else{
Out.println(human_2.get_name() + " hat gewonnen!");
}
}
}
public Human[] round_word(Human human_1, Human human_2){
// Zwei Menschen werden uebergeben und zwei Menschen
// werden zurueckgegeben.
String name_1 = human_1.get_name();
String name_2 = human_2.get_name();
String word = human_1.word_input();
this.clear();
boolean running = true;
int fail_limit = 10;
int fail_counter = 0;
StringBuilder output_word = new StringBuilder();
List<String> used_chars = new ArrayList<String>();
int L = word.length();
for (int i = 0; i < L; i++){
output_word.append("-");
}
while (running = true){
this.clear();
Out.println(output_word + " " + word.length() + " Buchstaben");
Out.println(this.bar(fail_counter, fail_limit) + " Fehler: " + fail_counter + " / " + fail_limit);
Out.println(used_chars);
Out.println();
output_word.delete(0, output_word.length());
String Try = human_2.guess();
if (Try.length() == 1){
// It's a char!
used_chars.add(Try);
if (word.contains(Try) != true){
fail_counter += 1;
}
}
else{
if (Try.length() == word.length()){
if (Try.toUpperCase().equals(word.toUpperCase())){
human_2.add_score(1);
running = false;
Out.println("");
Out.println(word);
Out.println("Das Wort wurde erraten!");
Out.println("");
break;
}
else
{
Out.println("");
Out.println("Das war falsch!");
Out.println("");
fail_counter += 1;
}
}
else{
Out.println("");
Out.println("Entweder einen Buchstaben oder das ganze Wort ("+word.length()+" Buchstaben) eingeben!");
Out.println("");
fail_counter += 1;
}
}
for (int o = 0; o < word.length(); o++){
String tmp = word.substring(o, o+1);
if (used_chars.contains(tmp)){
output_word.append(tmp);
}
else{
output_word.append("-");
}
}
if (output_word.toString().toUpperCase().equals(word.toUpperCase())){
human_2.add_score(1);
running = false;
Out.println("");
Out.println(word);
Out.println("Das Wort wurde erraten!");
Out.println("");
break;
}
if (fail_counter == fail_limit){
human_1.add_score(1);
Out.println();
Out.println("Das Wort war: " + word);
Out.println();
running = false;
break;
}
}
Human[] opponents = new Human[2];
opponents[0] = human_2;
opponents[1] = human_1;
return opponents;
}
public void clear(){
Out.print('\u000C');
}
public String bar(int current, int maximum){
StringBuilder result = new StringBuilder();
result.append("[");
if(current >= 1){
for(int i = 1; i <= current;i++)
{
result.append("*");
}
}
for(int i=1; i <= (maximum-current);i++){
result.append(" ");
}
result.append("]");
return result.toString();
}
}