<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_3</artifactId>
<version>2.8.5</version>
</dependency>
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
public class PlayerActor extends AbstractActor {
private String playerName;
private int score;
private ActorRef opponent;
public PlayerActor(String playerName) {
this.playerName = playerName;
this.score = 0;
}
public static Props props(String playerName) {
return Props.create(PlayerActor.class, playerName);
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Attack.class, this::handleAttack)
.match(Defend.class, this::handleDefend)
.match(ReportScore.class, this::handleReportScore)
.match(InitOpponent.class, this::handleInitOpponent)
.build();
}
private void handleAttack(Attack attack) {
opponent.tell(new Defend(attack.getDamage()), getSelf());
}
private void handleDefend(Defend defend) {
int damage = defend.getDamage();
score += damage;
System.out.println(playerName + " defends against an attack and scores " + damage + " points.");
}
private void handleReportScore(ReportScore reportScore) {
getSender().tell(new ScoreResponse(playerName, score), getSelf());
}
private void handleInitOpponent(InitOpponent initOpponent) {
opponent = initOpponent.getOpponent();
}
public static class Attack {
private final int damage;
public Attack(int damage) {
this.damage = damage;
}
public int getDamage() {
return damage;
}
}
public static class Defend {
private final int damage;
public Defend(int damage) {
this.damage = damage;
}
public int getDamage() {
return damage;
}
}
public static class ReportScore {
}
public static class InitOpponent {
private final ActorRef opponent;
public InitOpponent(ActorRef opponent) {
this.opponent = opponent;
}
public ActorRef getOpponent() {
return opponent;
}
}
public static class ScoreResponse {
private final String playerName;
private final int score;
public ScoreResponse(String playerName, int score) {
this.playerName = playerName;
this.score = score;
}
public String getPlayerName() {
return playerName;
}
public int getScore() {
return score;
}
}
}
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
public class GameController extends AbstractActor {
private final ActorRef player1;
private final ActorRef player2;
public GameController(ActorRef player1, ActorRef player2) {
this.player1 = player1;
this.player2 = player2;
// 初始化玩家的对手
player1.tell(new PlayerActor.InitOpponent(player2), getSelf());
player2.tell(new PlayerActor.InitOpponent(player1), getSelf());
}
public static Props props(ActorRef player1, ActorRef player2) {
return Props.create(GameController.class, player1, player2);
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(PlayerActor.ScoreResponse.class, this::handleScoreResponse)
.match(PlayerActor.Attack.class, this::handleAttack)
.build();
}
private void handleScoreResponse(PlayerActor.ScoreResponse scoreResponse) {
System.out.println(scoreResponse.getPlayerName() + " has a score of " + scoreResponse.getScore() + " points.");
}
private void handleAttack(PlayerActor.Attack attack) {
// 随机选择一个玩家发起攻击
if (Math.random() < 0.5) {
player1.tell(attack, getSelf());
} else {
player2.tell(attack, getSelf());
}
}
}
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
public class GameLauncher {
public static void main(String[] args) {
ActorSystem system = ActorSystem.create("GameSystem");
// 创建两个玩家
ActorRef player1 = system.actorOf(PlayerActor.props("Player 1"));
ActorRef player2 = system.actorOf(PlayerActor.props("Player 2"));
// 创建游戏控制器
ActorRef gameController = system.actorOf(GameController.props(player1, player2));
// 发起攻击
gameController.tell(new PlayerActor.Attack(10), ActorRef.noSender());
}
}
// 运行结果
Player 2 defends against an attack and scores 5 points.
👋 你好,我是 Lorin 洛林,一位 Java 后端技术开发者!座右铭:Technology has the power to make the world a better place.
🚀 我对技术的热情是我不断学习和分享的动力。我的博客是一个关于Java生态系统、后端开发和最新技术趋势的地方。
🧠 作为一个 Java 后端技术爱好者,我不仅热衷于探索语言的新特性和技术的深度,还热衷于分享我的见解和最佳实践。我相信知识的分享和社区合作可以帮助我们共同成长。
💡 在我的博客上,你将找到关于Java核心概念、JVM 底层技术、常用框架如Spring和Mybatis 、MySQL等数据库管理、RabbitMQ、Rocketmq等消息中间件、性能优化等内容的深入文章。我也将分享一些编程技巧和解决问题的方法,以帮助你更好地掌握Java编程。
🌐 我鼓励互动和建立社区,因此请留下你的问题、建议或主题请求,让我知道你感兴趣的内容。此外,我将分享最新的互联网和技术资讯,以确保你与技术世界的最新发展保持联系。我期待与你一起在技术之路上前进,一起探讨技术世界的无限可能性。
📖 保持关注我的博客,让我们共同追求技术卓越。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。