generated from BudDavis/TicTacToe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b19783
commit 014e3e4
Showing
3 changed files
with
252 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> Words_found = new ArrayList<String>(); | ||
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<String> validWords; | ||
private List<String> 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<String> 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(); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Player_Data> 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<Player_Data> 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); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|