add files

This commit is contained in:
2026-04-14 20:07:23 +03:30
commit 83df1ab515
6 changed files with 728 additions and 0 deletions
+144
View File
@@ -0,0 +1,144 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
class AuthService {
private Map<String, User> userList = new HashMap<>();
private List<User> lastLogin = new ArrayList<>();
static AuthService obj = new AuthService();
private AuthService() {
}
public static AuthService getObject() {
return obj;
}
public String check(String password) {
if (password.length() < 8) {
return "Password Not Valid!";
}
char[] b = password.toCharArray();
for (int i = 0; i < b.length; i++) {
if (Character.isUpperCase(b[i]) == true) {
break;
} else if (i == b.length - 1) {
return "Password Not Valid!";
}
}
for (int j = 0; j < b.length; j++) {
if (Character.isLowerCase(b[j]) == true) {
break;
} else if (j == b.length - 1) {
return "Password Not Valid!";
}
}
for (int k = 0; k < b.length; k++) {
if (Character.isDigit(b[k]) == true) {
break;
} else if (k == b.length - 1) {
return "Password Not Valid!";
}
}
for (int l = 0; l < b.length; l++) {
if (b[l] == '@' || b[l] == '#' || b[l] == '$' || b[l] == '%' || b[l] == '^' || b[l] == '&' || b[l] == '+'
|| b[l] == '=') {
break;
} else if (l == b.length - 1) {
return "Password Not Valid!";
}
}
return "";
}
public void signup(String username, String password) {
if (userList.size() == 0) {
if (check(password).equals("Password Not Valid!")) {
System.out.println("Password Not Valid!");
return;
} else {
System.out.println("SignUp Successful!");
User u = new User(username, password, 0);
userList.put(u.getUsername(), u);
Leaderboard.getObject().addUser(u);
return;
}
} else {
if (userList.keySet().contains(username)) {
System.out.println("Username Exists!");
return;
} else {
if (check(password).equals("Password Not Valid!")) {
System.out.println("Password Not Valid!");
return;
} else {
System.out.println("SignUp Successful!");
User u = new User(username, password, 0);
Leaderboard.getObject().addUser(u);
userList.put(u.getUsername(), u);
return;
}
}
}
}
public String login(String username, String password) {
if (userList.size() == 0) {
System.out.println("Invalid Username or Password!");
return ("Invalid Username or Password!");
} else {
if (userList.keySet().contains(username)) {
if ((userList.get(username).getEncryptedPassword().equals(password))) {
System.out.println("Login Successful!");
lastLogin.add(userList.get(username));
return ("Login Successful!");
} else {
System.out.println("Invalid Username or Password!");
return ("Invalid Username or Password!");
}
}
System.out.println("Invalid Username or Password!");
return ("Invalid Username or Password!");
}
}
public Set<String> getUsers() {
return (userList.keySet());
}
public User getlastlogin() {
return (lastLogin.get(lastLogin.size() - 1));
}
}
+286
View File
@@ -0,0 +1,286 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
class Game {
private String secretWord;
private double remainingAttempts;
private User currentUser;
private Set<Character> guess = new HashSet<>();
private Scanner input = new Scanner(System.in);
private char[] secretwordarray;
public Game() {
setSecretWord();
setRemainingAttempts();
setCurrentUser();
}
public void setSecretWord() {
WordBank a = WordBank.getObject();
secretWord = a.getRandomWord();
}
public void getSecretWord() {
System.out.println(secretWord);
}
public void setRemainingAttempts() {
if (secretWord.equals("there is no word in Word Bank ")) {
return;
}
remainingAttempts = 5.0;
return;
}
public void getRemainingAttempts() {
System.out.println(remainingAttempts);
}
public void setCurrentUser() {
if (secretWord.equals("there is no word in Word Bank ")) {
return;
}
AuthService a = AuthService.getObject();
currentUser = a.getlastlogin();
}
public User getCurrentUser() {
return (currentUser);
}
public void startGame(User user) {
if (secretWord.equals("there is no word in Word Bank ")) {
return;
}
secretwordarray = secretWord.toCharArray();
char[] word = secretWord.toCharArray();
int j = 0;
for (int i = 0; i < secretWord.length(); i++) {
j += 1;
secretwordarray[i] = '_';
}
displayWordState();
System.out.println("\n This word has " + j + " letters.");
int z = 0;
while (true) {
System.out.println("\nGuess a letter: ");
if (z < 1) {
System.out.println("\nPRESS 0 AND GET A HINT (-3 POINTS)");
}
String letter = input.nextLine();
if (letter.equals("0") && z < 1) {
z += 1;
if (currentUser.useHint()) {
Leaderboard.getObject().updateScore(currentUser, -3);
int t = 0;
for (int i = 0; i < word.length; i++) {
if (secretwordarray[i] != word[i]) {
t += 1;
}
}
char[] hint = new char[t];
for (int i = 0; i < word.length; i++) {
if (secretwordarray[i] != word[i]) {
hint[i] = word[i];
}
}
Random r = new Random();
int random = r.nextInt(hint.length);
for (int k = 0; k < word.length; k++) {
if (word[k] == hint[random]) {
secretwordarray[k] = hint[random];
continue;
}
}
displayWordState();
int h = 0;
for (char w : secretwordarray) {
if (w == '_') {
h += 1;
}
}
if (h == 0) {
System.out.println("game over");
return;
}
continue;
}
continue;
}
String a = guessLetter(letter);
if (a.equals("Correct guess!")) {
displayWordState();
if (Arrays.equals(secretwordarray, word)) {
System.out.println("You Won");
Leaderboard.getObject().updateScore(currentUser, secretWord.length());
return;
}
continue;
} else if (a.equals("wrong choice!")) {
if (secretWord.length() <= 8.0) {
remainingAttempts -= 1.0;
if (remainingAttempts == 4.0) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ");
System.out.println(" ______\\ \\ | | ");
System.out.println("|______/ / | |__");
System.out.println(" /_/ |_____");
}
else if (remainingAttempts == 3.0) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___");
System.out.println(" ______\\ \\ | | / _ \\");
System.out.println("|______/ / | |__| (_) \\ ");
System.out.println(" /_/ |_____\\___/");
}
else if (remainingAttempts == 2.0) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___ ____ ");
System.out.println(" ______\\ \\ | | / _ \\/ __|");
System.out.println("|______/ / | |__| (_) \\__ \\ ");
System.out.println(" /_/ |_____\\___/|____/");
}
else if (remainingAttempts == 1.0) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___ ____ ___ ");
System.out.println(" ______\\ \\ | | / _ \\/ __|/ _ \\");
System.out.println("|______/ / | |__| (_) \\__ \\ __/");
System.out.println(" /_/ |_____\\___/|____/\\___|");
}
else if (remainingAttempts == 0.0) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___ ____ ___ _ __ ");
System.out.println(" ______\\ \\ | | / _ \\/ __|/ _ \\ |__|");
System.out.println("|______/ / | |__| (_) \\__ \\ __/ | ");
System.out.println(" /_/ |_____" + "\\___/|____/" + "\\___|_| ");
return;
}
} else if (secretWord.length() > 8.0) {
remainingAttempts -= 0.5;
if (remainingAttempts == 4.0 || remainingAttempts == 4.5) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ");
System.out.println(" ______\\ \\ | | ");
System.out.println("|______/ / | |__");
System.out.println(" /_/ |_____");
}
else if (remainingAttempts == 3.0 || remainingAttempts == 3.5) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___");
System.out.println(" ______\\ \\ | | / _ \\");
System.out.println("|______/ / | |__| (_) \\ ");
System.out.println(" /_/ |_____\\___/");
}
else if (remainingAttempts == 2.0 || remainingAttempts == 2.5) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___ ____ ");
System.out.println(" ______\\ \\ | | / _ \\/ __|");
System.out.println("|______/ / | |__| (_) \\__ \\ ");
System.out.println(" /_/ |_____\\___/|____/");
}
else if (remainingAttempts == 1.0 || remainingAttempts == 1.5) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___ ____ ___ ");
System.out.println(" ______\\ \\ | | / _ \\/ __|/ _ \\");
System.out.println("|______/ / | |__| (_) \\__ \\ __/");
System.out.println(" /_/ |_____\\___/|____/\\___|");
}
else if (remainingAttempts == 0.0 || remainingAttempts == 0.5) {
System.out.println(" __ _ ");
System.out.println(" \\ \\ | | ___ ____ ___ _ __ ");
System.out.println(" ______\\ \\ | | / _ \\/ __|/ _ \\ |__|");
System.out.println("|______/ / | |__| (_) \\__ \\ __/ | ");
System.out.println(" /_/ |_____\\___/|____/\\___|_| ");
return;
} else {
continue;
}
}
}
}
}
public String guessLetter(String letter) {
char[] word = secretWord.toCharArray();
char lett = letter.charAt(0);
if (guess.contains(lett)) {
System.out.println("You have already chosen this letter. ");
System.out.println("\n " + guess);
return ("");
}
guess.add(lett);
int o = 0;
for (int k = 0; k < word.length; k++) {
if (word[k] == lett) {
secretwordarray[k] = lett;
o += 1;
continue;
}
}
if (o > 0) {
System.out.println("Correct guess!");
return ("Correct guess!");
} else {
System.out.println("wrong choice!");
return ("wrong choice!");
}
}
public void displayWordState() {
String wordState = "";
for (int i = 0; i < secretwordarray.length; i++) {
wordState += secretwordarray[i] + " ";
}
System.out.println(wordState);
}
}
+45
View File
@@ -0,0 +1,45 @@
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
class Leaderboard {
private Map<User, Integer> scores = new HashMap<>();
private PriorityQueue<User> displayList = new PriorityQueue<>(
(u1, u2) -> u2.getScore() - u1.getScore());
public void addUser(User a) {
scores.put(a, a.getScore());
}
static Leaderboard obj = new Leaderboard();
private Leaderboard() {
}
public static Leaderboard getObject() {
return obj;
}
public void updateScore(User user, int points) {
int p = user.getScore();
p += points;
user.setScore(p);
scores.put(user, p);
}
public void display() {
displayList.clear();
System.out.println();
System.out.println("Leaderboard");
System.out.println("--------------------------------");
for (User u : scores.keySet()) {
displayList.add(u);
}
while (!displayList.isEmpty()) {
User u = displayList.poll();
System.out.println(u.getUsername() + " --- " + scores.get(u));
}
}
}
+88
View File
@@ -0,0 +1,88 @@
class User {
private String username;
private String encryptedPassword;
private int score;
private boolean hint = false;
public User() {
}
public User(String username, String encryptedPassword, int score) {
setUsername(username);
setEncryptedPassword(encryptedPassword);
setScore(score);
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return (username);
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
public String getEncryptedPassword() {
return (encryptedPassword);
}
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return (score);
}
public void changePassword(String oldPass, String newPass) {
if (!oldPass.equals(encryptedPassword)) {
System.out.println("Old Password Not Valid!! ");
return;
} else {
String check = AuthService.getObject().check(newPass);
System.out.println(check);
if (!check.equals("Password Not Valid!")) {
encryptedPassword = newPass;
System.out.println("Password changed successfully.");
return;
} else {
System.out.println("New Password Not Valid!");
}
}
}
public void addScore(int wordLength) {
score += wordLength;
}
public boolean useHint() {
if (hint) {
System.out.println(" you used your hint!");
return (false);
} else if (score <= 0) {
System.out.println("you dont have enough score ");
return (false);
} else {
hint = true;
return (true);
}
}
}
+47
View File
@@ -0,0 +1,47 @@
import java.util.*;
class WordBank {
private Set<String> wordBank = new HashSet<>();
private Random r = new Random();
static WordBank obj = new WordBank();
public int setSize() {
return (wordBank.size());
}
private WordBank() {
}
public static WordBank getObject() {
return obj;
}
public void addWord(String word) {
for (String i : wordBank) {
if (word.equals(i)) {
System.out.println("This word already exists! ");
return;
}
}
if (word == null || word.equals("")) {
System.out.println("Word is not valid!");
return;
}
wordBank.add(word);
}
public String getRandomWord() {
if (wordBank.size() == 0) {
System.out.println("there is no word in Word Bank \n");
return ("there is no word in Word Bank ");
}
Object[] wordBankArray = wordBank.toArray();
int a = r.nextInt(wordBankArray.length);
return ((String) wordBankArray[a]);
}
}
+118
View File
@@ -0,0 +1,118 @@
import java.util.*;
public class finalproject {
public static void main(String[] args) {
while (true) {
asli();
}
}
public static void asli() {
Scanner input = new Scanner(System.in);
boolean exit = true;
System.out.println(
"\nlooser game\n- - - - - - - - - - - - \nEnter your choice : \n1. Login\n2. Signup\n3. Exit ");
int choice = input.nextInt();
input.nextLine();
if (choice == 1) {
while (true) {
System.out.println("please enter your name : ");
String name = input.nextLine();
System.out.println("pleade enter your password : ");
String password = input.nextLine();
String a = AuthService.getObject().login(name, password);
if (a.equals("Invalid Username or Password!")) {
System.out.println("1. try again ");
System.out.println("2.back");
int choice2 = input.nextInt();
input.nextLine();
if (choice2 == 1) {
continue;
} else if (choice2 == 2) {
return;
} else {
System.out.println("Unvalid input!");
return;
}
} else {
while (exit) {
exit = startmenue();
}
}
return;
}
} else if (choice == 2) {
System.out.println("pleade enter your name : ");
String name = input.nextLine();
System.out.println("pleade enter your password : ");
String password = input.nextLine();
AuthService.getObject().signup(name, password);
} else if (choice == 3) {
System.exit(0);
} else {
System.out.println("Unvalid input!");
}
}
public static boolean startmenue() {
Scanner input = new Scanner(System.in);
System.out.println(
"\nEnter your choice : \n1. Start game\n2. Show leaderboard\n3. Add new word\n4. Exit\n5. change password");
int choice3 = input.nextInt();
input.nextLine();
if (choice3 == 1) {
Game game = new Game();
game.startGame(AuthService.getObject().getlastlogin());
return (true);
} else if (choice3 == 2) {
Leaderboard.getObject().display();
return (true);
} else if (choice3 == 3) {
System.out.println("please enter your word : ");
String newword = input.nextLine();
WordBank.getObject().addWord(newword);
System.out.println();
System.out.println("Word added successfully!");
return (true);
} else if (choice3 == 4) {
return (false);
} else if (choice3 == 5) {
System.out.println("plaese enter your old password: ");
String oldpass = input.nextLine();
System.out.println("plaese enter new password: ");
String newpass = input.nextLine();
User a = AuthService.getObject().getlastlogin();
a.changePassword(oldpass, newpass);
return (true);
} else {
System.out.println("Unvalid input!");
return (true);
}
}
}