-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
129 lines (105 loc) · 4.84 KB
/
Main.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
package com.company;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_YELLOW = "\u001B[43m";
public static final String ANSI_GREEN = "\u001B[42m";
public static final String ANSI_RED = "\u001B[41m";
public static void main(String[] args) {
System.out.println(ANSI_GREEN + "Welcome to Java-Wordle!" + ANSI_RESET);
System.out.println("The purpose of the game is to guess a 5-letter word.");
System.out.println("If a letter in your guess is correct, it will appear green.");
System.out.println("If it is incorrect, it will appear red.");
System.out.println("If the letter is in the word but not in the right place, it will appear yellow.");
System.out.println("");
//Defining some variables for game play. "count" tracks number of attempts. "flag" marks whether it is a win or loss.
int count = 6;
int flag = 0;
String answer = answerRandomizer();
do {
char[] answerArray = answer.toCharArray();
System.out.println("You have " + count + " more tries. Enter your" +
" 5-letter guess: \r");
String guess = scanner.nextLine();
guess = guess.toUpperCase();
// Convert the guess into an array with each letter represented as a char variable.
char[] guessArray = guess.toCharArray();
//This is where I would like to add a dictionary to
// also check if words are valid.
if (!validateGuess(guessArray)){
do{
System.out.println("Invalid guess. Enter a 5 letter word.");
guess = scanner.nextLine();
guess = guess.toUpperCase();
guessArray = guess.toCharArray();
}while (!validateGuess(guessArray));
}
count-=1;
//Check whether the guess is equal to the answer.
if (answer.equalsIgnoreCase(guess)) {
count=0;
flag=1;
}else {
//Find correct/green letters. DecoyArray will allow us to
// cross letters off as they are
// found.
char[] decoyArray = guess.toCharArray();
for (int x = 0; x < 5; x++) {
if (answerArray[x] == guessArray[x]) {
decoyArray[x] = '#';
answerArray[x] = '$';
}
}
//Find misplaced/yellow letters.
for (int x = 0; x < 5; x++) {
for (int y = 4; y > -1; y--) {
if (decoyArray[y] == answerArray[x]) {
decoyArray[y] = '@';
answerArray[x] = '$';
}
}
}
//Print letters with correct color backgrounds.
for (int x = 0; x < 5; x++) {
if (decoyArray[x] == '#') {
System.out.print(ANSI_GREEN + " " + guessArray[x] + " " + ANSI_RESET + " ");
} else if (decoyArray[x] == '@') {
System.out.print(ANSI_YELLOW + " " + guessArray[x] + " " + ANSI_RESET + " ");
} else {
System.out.print(ANSI_RED + " " + guessArray[x] + " " + ANSI_RESET + " ");
}
}
}
}while (count>0);
if (flag == 0){
System.out.println("You are out of tries. The answer was " + answer + ".");
}
else if (flag>0) {
System.out.println(ANSI_GREEN + " " + answer + " " + ANSI_RESET + " ");
System.out.println("Congratulations! You win.");
}
}
private static boolean validateGuess(char[] guessArray){
if (guessArray.length!=5){
return false;
}
for (int i = 0; i < guessArray.length; i++) {
if (!(guessArray[i] >= 'A' && guessArray[i] <= 'Z')) {
return false;
}
}
return true;
}
private static String answerRandomizer(){
//create an array of possible answers:
String [] answerBlock = {"CYBER", "BYTES", "ARRAY", "SWITCH", "WHILE", "LOOPS", "SCOPE", "FILES", "CODES",
"ERROR", "MODEM", "MOUSE", "BASIC", "BLOCK", "CACHE", "CLICK", "CLOSE", "CRASH", "DEBUG",
"EMAIL", "ERASE", "TABLE", "VIRUS", "RESET", "WRITE", "LOOPS"};
//randomly assign an answer word
Random random = new Random();
return answerBlock[random.nextInt(25)];
}
}