-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessonThree.java
More file actions
106 lines (91 loc) · 3.9 KB
/
Copy pathLessonThree.java
File metadata and controls
106 lines (91 loc) · 3.9 KB
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
import java.util.Random;
import java.util.Scanner;
public class LessonThree {
public static void main(String[] args) {
Random rnd = new Random();
Scanner scan = new Scanner(System.in);
boolean goOn = true;
do {
int tryCount = 3;
int playerAnswer = 777;
boolean isPlayerWinner = false;
int numberToGuess = rnd.nextInt(10);
System.out.println("Угадай число от 0 до 9. Осталось попыток: " + tryCount);
do {
playerAnswer = PlayerInput();
isPlayerWinner = CompareAnswers(playerAnswer, numberToGuess);
if (isPlayerWinner == false) {
tryCount--;
System.out.println("Попробуй еще раз. Попыток осталось: " +tryCount);
}
} while (isPlayerWinner == false && tryCount > 0);
if (tryCount <= 0) System.out.println("Ты проиграл! Правильный ответ: " + numberToGuess);
if (isPlayerWinner == true) System.out.println("Поздравляю! Ты угадал правильно!");
goOn=AnotherGame();
}while (goOn==true);
System.out.println("Теперь сыграем в угадайку. Я загадал животное, а ты попробуй отгадать.");
String[] animalList=new String[] {"корова","лошадь","кошка","собака","слон","медведь","мамонт","копибара","антилопа","суслик"};
boolean isWordGuessed=false;
int wordIndex=rnd.nextInt(animalList.length);
char[] guessedWord = WordToChar(animalList[wordIndex]);
char[] playerWord;
do {
playerWord = WordToChar(scan.nextLine());
isWordGuessed=CompareWords(guessedWord, playerWord);
if (isWordGuessed==true) System.out.println("Поздравляю! Ты угадал!");
else System.out.println("Попробуй еще раз!");
}while (isWordGuessed==false);
}
static int PlayerInput(){
Scanner scan = new Scanner(System.in);
boolean isNum=false;
int playerNum=0;
do {
System.out.println("Введи целое число.");
if (isNum = scan.hasNextInt()){
playerNum=scan.nextInt();
}else {
String scanResult=scan.nextLine();
}
}while (isNum==false);
return playerNum;
}
static boolean CompareAnswers(int playerNum, int realNum){
if (playerNum<realNum){
System.out.println("Загаданное число больше твоего!");
return false;
}else if (playerNum>realNum){
System.out.println("Загаданное число меньше твоего!");
return false;
}else return true;
}
static boolean AnotherGame(){
System.out.println("Хочешь сыграть еще раз? 1-Да / 2-Нет");
int isYes=PlayerInput();
if (isYes==1) return true;
else return false;
}
static char[] WordToChar(String inputWord){
char[] wordInChars=new char[15];
for (int i=0; i<wordInChars.length;i++){
if (i<inputWord.length()) {
wordInChars[i] = inputWord.charAt(i);
}else wordInChars[i]='#';
}
return wordInChars;
}
static boolean CompareWords (char guessedWord[], char playerWord[]){
String wordToPrint="";
boolean isItSame=true;
for (int i=0; i<guessedWord.length;i++){
if (guessedWord[i]==playerWord[i]){
wordToPrint+=guessedWord[i];
}else {
wordToPrint+="#";
isItSame=false;
}
}
System.out.println(wordToPrint);
return isItSame;
}
}