Skip to content

Commit

Permalink
fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RamonTorresUta committed Jul 25, 2024
1 parent ebfbe3d commit 5a3285b
Show file tree
Hide file tree
Showing 16 changed files with 459 additions and 438 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,121 +1,121 @@
package uta.cse3310;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.java_websocket.WebSocket;

public class Game {

private List<Player> players;
private List<Round> rounds;
private int currentRoundIndex;
private boolean isGameActive;
private Statistics stats;
private int gameId;

public Game(List<Player> players, String wordFilePath, String stakeFilePath, Statistics stats) throws IOException {
this.players = players;
this.rounds = new ArrayList<>();
this.currentRoundIndex = 0;
this.isGameActive = true;
this.stats = stats;
this.gameId = 0;
stats = new Statistics();

// Initialize rounds
for (int i = 0; i < 3; i++) { // Assuming a game consists of 3 rounds
rounds.add(new Round(players, wordFilePath, stakeFilePath));
}
}

public List<Player> getPlayers() {
return players;
}

public void setGameId(int gameId) {
this.gameId = gameId;
}

public void removePlayer(WebSocket conn) {
players.removeIf(player -> player.getId().equals(conn.getAttachment()));
}

public void update(UserEvent event) {
// Implementation of update method
}

public void startGame() {
System.out.println("Game started with " + players.size() + " players.");
startNextRound();
}

public void startNextRound() {
if (currentRoundIndex >= rounds.size()) {
System.out.println("All rounds completed.");
isGameActive = false;
determineWinner();
return;
}

Round currentRound = rounds.get(currentRoundIndex);
currentRound.startRound();
currentRoundIndex++;
}

public void playRound() {
if (!isGameActive) {
System.out.println("Game is not active.");
return;
}

Round currentRound = rounds.get(currentRoundIndex - 1); // -1 because we incremented in startNextRound
while (currentRound.isRoundActive()) {
currentRound.nextTurn();
}

// After the round ends, start the next round
startNextRound();
}
public void endGame(Player player) {
stats.updateWinner(player);
}

public Statistics getStatistics() {
return stats;
}

private void determineWinner() {
Player winner = null;
int highestScore = 0;
for (Player player : players) {
if (player.getScore() > highestScore) {
highestScore = player.getScore();
winner = player;
}
}

if (winner != null) {
System.out.println("The winner is " + winner.getName() + " with a score of " + highestScore);
stats.updateWinner(winner);
} else {
System.out.println("No winner determined.");
}
}

public void resetGame() {
currentRoundIndex = 0;
isGameActive = true;
for (Round round : rounds) {
try {
round.resetRound();
} catch (IOException e) {
System.err.println("Error resetting round: " + e.getMessage());
}
}
}
}



package uta.cse3310;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.java_websocket.WebSocket;

public class Game {

private List<Player> players;
private List<Round> rounds;
private int currentRoundIndex;
private boolean isGameActive;
private Statistics stats;
private int gameId;

public Game(List<Player> players, String wordFilePath, String stakeFilePath, Statistics stats) throws IOException {
this.players = players;
this.rounds = new ArrayList<>();
this.currentRoundIndex = 0;
this.isGameActive = true;
this.stats = stats;
this.gameId = 0;
stats = new Statistics();

// Initialize rounds
for (int i = 0; i < 3; i++) { // Assuming a game consists of 3 rounds
rounds.add(new Round(players, wordFilePath, stakeFilePath));
}
}

public List<Player> getPlayers() {
return players;
}

public void setGameId(int gameId) {
this.gameId = gameId;
}

public void removePlayer(WebSocket conn) {
players.removeIf(player -> player.getId().equals(conn.getAttachment()));
}

public void update(UserEvent event) {
// Implementation of update method
}

public void startGame() {
System.out.println("Game started with " + players.size() + " players.");
startNextRound();
}

public void startNextRound() {
if (currentRoundIndex >= rounds.size()) {
System.out.println("All rounds completed.");
isGameActive = false;
determineWinner();
return;
}

Round currentRound = rounds.get(currentRoundIndex);
currentRound.startRound();
currentRoundIndex++;
}

public void playRound() {
if (!isGameActive) {
System.out.println("Game is not active.");
return;
}

Round currentRound = rounds.get(currentRoundIndex - 1); // -1 because we incremented in startNextRound
while (currentRound.isRoundActive()) {
currentRound.nextTurn();
}

// After the round ends, start the next round
startNextRound();
}
public void endGame(Player player) {
stats.updateWinner(player);
}

public Statistics getStatistics() {
return stats;
}

private void determineWinner() {
Player winner = null;
int highestScore = 0;
for (Player player : players) {
if (player.getScore() > highestScore) {
highestScore = player.getScore();
winner = player;
}
}

if (winner != null) {
System.out.println("The winner is " + winner.getName() + " with a score of " + highestScore);
stats.updateWinner(winner);
} else {
System.out.println("No winner determined.");
}
}

public void resetGame() {
currentRoundIndex = 0;
isGameActive = true;
for (Round round : rounds) {
try {
round.resetRound();
} catch (IOException e) {
System.err.println("Error resetting round: " + e.getMessage());
}
}
}
}




Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
package uta.cse3310;

import java.util.Timer;
import java.util.TimerTask;

public class GameTimer {

private Timer timer;
private long startTime;
private long elapsedTime;
private boolean isRunning;

public GameTimer() {
this.timer = new Timer();
this.elapsedTime = 0;
this.isRunning = false;
}

public void start() {
if (isRunning) {
return; // Timer is already running
}
this.startTime = System.currentTimeMillis();
this.isRunning = true;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
}
}, 0, 1000); // Update elapsed time every second
}

public void stop() {
if (!isRunning) {
return; // Timer is already stopped
}
this.timer.cancel();
this.elapsedTime = System.currentTimeMillis() - startTime;
this.isRunning = false;
}

public void reset() {
this.timer.cancel();
this.timer = new Timer();
this.elapsedTime = 0;
this.isRunning = false;
}

public long getElapsedTime() {
return elapsedTime / 1000; // Return elapsed time in seconds
}

public boolean isRunning() {
return isRunning;
}
}

package uta.cse3310;

import java.util.Timer;
import java.util.TimerTask;

public class GameTimer {

private Timer timer;
private long startTime;
private long elapsedTime;
private boolean isRunning;

public GameTimer() {
this.timer = new Timer();
this.elapsedTime = 0;
this.isRunning = false;
}

public void start() {
if (isRunning) {
return; // Timer is already running
}
this.startTime = System.currentTimeMillis();
this.isRunning = true;
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
}
}, 0, 1000); // Update elapsed time every second
}

public void stop() {
if (!isRunning) {
return; // Timer is already stopped
}
this.timer.cancel();
this.elapsedTime = System.currentTimeMillis() - startTime;
this.isRunning = false;
}

public void reset() {
this.timer.cancel();
this.timer = new Timer();
this.elapsedTime = 0;
this.isRunning = false;
}

public long getElapsedTime() {
return elapsedTime / 1000; // Return elapsed time in seconds
}

public boolean isRunning() {
return isRunning;
}
}


File renamed without changes.
20 changes: 20 additions & 0 deletions src/main/java/uta/cse3310/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package uta.cse 3310;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;



public class Main {
public static void main(String[] args)
{
Word word = new Word();
word.getWords();
WordList wordist = new WordList();
wordist.checkLetter();

}
}
Loading

0 comments on commit 5a3285b

Please sign in to comment.