diff --git a/src/main/java/uta/cse3310/Board_Game.java b/src/main/java/uta/cse3310/Board_Game.java index 7bbf984..449d2b5 100644 --- a/src/main/java/uta/cse3310/Board_Game.java +++ b/src/main/java/uta/cse3310/Board_Game.java @@ -1,27 +1,147 @@ package uta.cse3310; + + import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.Timer; +import java.util.TimerTask; + + public class Board_Game { - public int Game_Id; - public int Total_valid_words; - public ArrayList Words_found = new ArrayList(); - public int Player_count; - - public boolean check_selection(){ - return true; - } - public int display_board(){ - return 1; - } - public int display_words(){ - return 1; - } - public boolean leave_game(){ - return true; - } - public boolean handle_outstanding(){ - return true; - } - public long display_time(){ - return 1; - } + private int gameId; + private List validWords; + private List wordsFound = new ArrayList<>(); + private int playerCount; + private String currentWord = ""; + private int currentPlayer = 0; + private int level; // 1 for easy, 2 for medium, 3 for hard + private Timer timer; + private long startTime; + private Board_Create board ; + + + public Board_Game(int gameId, List validWords, int playerCount, int level, Board_Create board) { + this.gameId = gameId; + this.validWords = validWords; + this.playerCount = playerCount; + this.level = level; + this.timer = new Timer(); + this.board = board; + } + + + public boolean checkSelection(String letter) { + cancelTimer(); + currentWord += letter; + if (validWords.contains(currentWord)) { + wordsFound.add(currentWord); + currentWord = ""; + startTimer(); + return true; + } else if (validWords.stream().anyMatch(word -> word.startsWith(currentWord))) { + // make sure that the entered current word could be the start of a valid word + startTimer(); + return true; + } else { + currentWord = ""; + nextPlayer(); + return false; + } + } + + + private void nextPlayer() { + currentPlayer = (currentPlayer + 1) % playerCount; + // Display error message and switch to the next player + startTimer(); + } + + + public void displayBoard() { + board.updateBoard(currentWord); + board.displayBoard(); + // this method to display the game board + } + + + public boolean leaveGame() { + Scanner scanner = new Scanner(System.in); + + + System.out.println("Are you sure you want to leave the game? (yes/no)"); + String response = scanner.nextLine(); + if (response.equalsIgnoreCase("yes")) { + System.out.println("It was so hard for us to build this game. Please play one more round. Still want to exit? (yes/no)"); + response = scanner.nextLine(); + if (response.equalsIgnoreCase("yes")) { + // Exit the game and go to the lobby + System.out.println("Exiting the game..."); + return true; + } + } + + + // Return to the game + System.out.println("Returning to the game..."); + return false; + } + + + + public boolean handleOutstanding() { + // this method to handle outstanding actions in the game + return true; + } + + + public long displayTime() { + long currentTime = System.currentTimeMillis(); + long timeElapsed = currentTime - startTime; + long timeRemaining = getTimeLimit() * 1000 - timeElapsed; + return timeRemaining / 1000; + // Return time remaining in seconds + // this method to display the time remaining for the current player + + } +private long getTimeLimit() { + switch (level) { + case 1: return 30; + case 2: return 15; + case 3: return 8; + default: return 0; + } +} + + + private void startTimer() { + timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run() { + System.out.println("Time out!"); + nextPlayer(); + } + }, getTimeLimit() * 1000); // convert time from millisecond to seconds + } + + + private void cancelTimer() { + if (timer != null) { + timer.cancel(); + } + } + + } + + + + + + + + + + + diff --git a/src/main/java/uta/cse3310/Leaderboard.java b/src/main/java/uta/cse3310/Leaderboard.java index e27cd91..2df3b8d 100644 --- a/src/main/java/uta/cse3310/Leaderboard.java +++ b/src/main/java/uta/cse3310/Leaderboard.java @@ -1,17 +1,46 @@ package uta.cse3310; + +import java.util.*; + + public class Leaderboard { - public int Score; - public int Wins; - public String Handle; - - public int update_leaderbaord(){ - return 1; - } - public int add_player(){ - return 1; - } - public int remove_player(){ - return 1; - } + private Map players = new HashMap<>(); + + + public boolean addPlayer(String handle) { + if (players.containsKey(handle)) { + return false; // player already exists + } + + + players.put(handle, new Player_Data(handle)); + return true; + } + + + public Player_Data getPlayer(String handle) { + return players.get(handle); + } + + + public int getPlayerWins(String handle) { + Player_Data playerData = players.get(handle); + return playerData != null ? playerData.getWins() : 0; + } + + + public void displayLeaderboard() { + List playerDataList = new ArrayList<>(players.values()); + playerDataList.sort(Comparator.comparingInt(Player_Data::getScore).reversed()); + + + for (Player_Data playerData : playerDataList) { + int totalGames = playerData.getWins() + playerData.getLosses() + playerData.getDraws(); + System.out.println("Handle: " + playerData.getHandle() + ", Score: " + playerData.getScore() + + ", Wins: " + playerData.getWins() + ", Losses: " + playerData.getLosses() + + ", Draws: " + playerData.getDraws() + ", Total games: " + totalGames); + } + } } + diff --git a/src/main/java/uta/cse3310/Player_Data.java b/src/main/java/uta/cse3310/Player_Data.java index 09d2ee2..c25d790 100644 --- a/src/main/java/uta/cse3310/Player_Data.java +++ b/src/main/java/uta/cse3310/Player_Data.java @@ -1,14 +1,71 @@ package uta.cse3310; + +import java.util.Random; + + public class Player_Data { - public String Handle; - public int Game_id; - public int Score; - - public int update_score(){ - return 1; - } - public int update_stats(){ - return 1; - } + private String handle; + private int gameId; + private int score; + private int wins; + private int losses; + private int draws; + + + public Player_Data(String handle) { + this.handle = handle; + this.gameId = new Random().nextInt(10000); // Generate a unique game ID + this.score = 0; + this.wins = 0; + this.losses = 0; + this.draws = 0; + } + + + public void updateStats(boolean win, boolean loss, boolean draw){ + if (win) this.wins++; + if (loss) this.losses++; + if (draw) this.draws++; + updateScore(); + } + + + private void updateScore(){ + int totalGames = wins + losses + draws; + if (totalGames > 0) { + this.score = (wins * 100) / totalGames; + } + } + + + public String getHandle() { + return handle; + } + + + public int getGameId() { + return gameId; + } + + + public int getScore() { + return score; + } + + + public int getWins() { + return wins; + } + + + public int getLosses() { + return losses; + } + + + public int getDraws() { + return draws; + } } +