Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions DriftEvent.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions src/com/example/drift/AggressiveDriftStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.drift;

public class AggressiveDriftStrategy implements DriftStrategy {
@Override
public void drift() {
System.out.println("Performing aggressive drift!");
}
}
32 changes: 32 additions & 0 deletions src/com/example/drift/DriftCompetition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.drift;

import java.util.ArrayList;
import java.util.List;

public class DriftCompetition {
private List<DriftEventObserver> observers;

public DriftCompetition() {
observers = new ArrayList<>();
}

public void addObserver(DriftEventObserver observer) {
observers.add(observer);
}

public void removeObserver(DriftEventObserver observer) {
observers.remove(observer);
}

public void notifyObservers(DriftEvent event) {
for (DriftEventObserver observer : observers) {
observer.update(event);
}
}

public void startCompetition() {
System.out.println();
System.out.println("The drift competition has started!");
notifyObservers(new DriftEvent("Drift competition started! Prepare for the battle!"));
}
}
13 changes: 13 additions & 0 deletions src/com/example/drift/DriftEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.drift;

public class DriftEvent {
private String message;

public DriftEvent(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
5 changes: 5 additions & 0 deletions src/com/example/drift/DriftEventObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.drift;

public interface DriftEventObserver {
void update(DriftEvent event);
}
67 changes: 67 additions & 0 deletions src/com/example/drift/DriftParticipant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.example.drift;

import java.util.Random;
//Clasa de participanti la drift
public class DriftParticipant {
private String name;//Nume
private DriftStrategy driftStrategy;
private int score;//Scor

//Participant
public DriftParticipant(String name) {
this.name = name;
this.score = 0;
}

//Alegerea strategiei de drift
public void chooseRandomStrategy() {
Random random = new Random();
int strategyChoice = random.nextInt(2);

if (strategyChoice == 0) {
driftStrategy = new AggressiveDriftStrategy();
} else {
driftStrategy = new SmoothDriftStrategy();
}
}

//Drift start
public void performDrift() {
System.out.println(name + " is performing drift:");

// Verificare pentru strategia agresivă
if (driftStrategy instanceof AggressiveDriftStrategy) {
Random random = new Random();
int accidentChance = random.nextInt(10);

if (accidentChance == 0) {
System.out.println(name + " had an accident!");
score -= 20; // Scădere scor în caz de accident
return;
}
}

// Verificare pentru strategia lină
if (driftStrategy instanceof SmoothDriftStrategy) {
Random random = new Random();
int carStopChance = random.nextInt(10);

if (carStopChance == 0) {
System.out.println(name + "'s car stopped working!");
score -= 10; // Scădere scor în caz de oprire a mașinii
return;
}
}

driftStrategy.drift();
score += (driftStrategy instanceof AggressiveDriftStrategy) ? 15 : 10; // Incrementare scor în funcție de strategie
}

public String getName() {
return name;
}

public int getScore() {
return score;
}
}
5 changes: 5 additions & 0 deletions src/com/example/drift/DriftStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.drift;

public interface DriftStrategy {
void drift();
}
97 changes: 97 additions & 0 deletions src/com/example/drift/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.example.drift;

import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {
// Observer Pattern
DriftCompetition driftCompetition = new DriftCompetition();
Spectator spectator1 = new Spectator("Alex");
Spectator spectator2 = new Spectator("Andrei");
driftCompetition.addObserver(spectator1);
driftCompetition.addObserver(spectator2);
driftCompetition.startCompetition();

// Strategy Pattern
List<DriftParticipant> participants = new ArrayList<>();
participants.add(new DriftParticipant("Anton"));
participants.add(new DriftParticipant("Paul"));

// Alegeri random pentru strategiile de drift utilizate de participanti
for (DriftParticipant participant : participants) {
participant.chooseRandomStrategy();
}

// Incepe runda si se calculeaza scorul
for (int i = 0; i < 5; i++) {
System.out.println("\nRound " + (i + 1) + " begins:");

for (DriftParticipant participant : participants) {
participant.performDrift();
}
}

// Afisare scoreboard și selectare castigator
System.out.println("\nFinal Scores:");

int maxScore = Integer.MIN_VALUE;
List<DriftParticipant> winners = new ArrayList<>();

for (DriftParticipant participant : participants) {
int score = participant.getScore();
System.out.println(participant.getName() + ": " + score);

if (score > maxScore) {
maxScore = score;
winners.clear();
winners.add(participant);
} else if (score == maxScore) {
winners.add(participant);
}
}

// Verifică dacă există egalitate
if (winners.size() > 1) {
System.out.println("\nIt's a tie! The competition will be reorganized.");

// Reorganizează concursul până când se determină un câștigător unic
while (winners.size() > 1) {
System.out.println("\nReorganizing the competition...");

// Alegere random a strategiilor pentru participanții cu scorul maxim
for (DriftParticipant winner : winners) {
winner.chooseRandomStrategy();
}

// Incepe runda si se calculeaza scorul
for (int i = 0; i < 5; i++) {
System.out.println("\nRound " + (i + 1) + " begins:");

for (DriftParticipant winner : winners) {
winner.performDrift();
}
}

maxScore = Integer.MIN_VALUE;
winners.clear();

// Calculare scoruri actualizate și determinare câștigător
for (DriftParticipant participant : participants) {
int score = participant.getScore();
System.out.println(participant.getName() + ": " + score);

if (score > maxScore) {
maxScore = score;
winners.clear();
winners.add(participant);
} else if (score == maxScore) {
winners.add(participant);
}
}
}
}

System.out.println("\nThe winner is: " + winners.get(0).getName());
}
}
Loading