-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
12 changed files
with
613 additions
and
1 deletion.
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
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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>ru.mifi.practice</groupId> | ||
<artifactId>AaDS</artifactId> | ||
<version>2024.2</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>vol_</artifactId> | ||
<name>vol_</name> | ||
<description>Модуль №_.</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
169 changes: 169 additions & 0 deletions
169
vol_/src/main/java/ru/mifi/practice/voln/AdventureGame.java
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 |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package ru.mifi.practice.voln; | ||
|
||
import lombok.Getter; | ||
import ru.mifi.practice.voln.logic.Item; | ||
import ru.mifi.practice.voln.logic.Person; | ||
import ru.mifi.practice.voln.logic.Updatable; | ||
import ru.mifi.practice.voln.transmit.Output; | ||
|
||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Random; | ||
|
||
public final class AdventureGame implements Updatable, Updatable.Context { | ||
private static final int GAME_LINE_LENGTH = 20; | ||
private final Random random = new Random(new Date().getTime()); | ||
private final Object[] gameLine = new Object[GAME_LINE_LENGTH + 1]; | ||
private final Output output; | ||
private final Person.Player player; | ||
private int index; | ||
@Getter | ||
private boolean running; | ||
|
||
public AdventureGame(Output output, Person.Player player) { | ||
this.output = output; | ||
this.player = player; | ||
running = true; | ||
nextGameLine(); | ||
} | ||
|
||
private void nextGameLine() { | ||
Arrays.fill(gameLine, null); | ||
index = 0; | ||
gameLine[0] = player; | ||
int items = random.nextInt(3) + 5; | ||
int enemies = random.nextInt(7) + 2; | ||
for (int i = 0; i < items; i++) { | ||
generateElement(Type.ITEM); | ||
} | ||
for (int i = 0; i < enemies; i++) { | ||
generateElement(Type.ENEMY); | ||
} | ||
} | ||
|
||
private void generateElement(Type type) { | ||
int index = 2; | ||
while (gameLine[index] != null) { | ||
index = random.nextInt(gameLine.length - 3) + 2; | ||
} | ||
switch (type) { | ||
case ENEMY -> { | ||
gameLine[index] = new Person.Mob("Paul", 80, index, | ||
new Item.DamageItem(random.nextInt(10) + 5), false); | ||
} | ||
case EMPTY -> { | ||
} | ||
case ITEM -> { | ||
if (random.nextBoolean()) { | ||
gameLine[index] = new Item.Health(random.nextInt(30) + 15); | ||
} else { | ||
gameLine[index] = new Item.Hummer(); | ||
} | ||
} | ||
default -> throw new IllegalStateException("Unexpected value: " + type); | ||
} | ||
} | ||
|
||
void update() { | ||
update(this); | ||
} | ||
|
||
@Override | ||
public void update(Context context) { | ||
for (Object o : gameLine) { | ||
if (o instanceof Updatable update) { | ||
update.update(context); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void died(Person person) { | ||
if (person instanceof Person.Player) { | ||
output.println("Game over"); | ||
running = false; | ||
} else if (person instanceof Person.Mob mob) { | ||
gameLine[index] = null; | ||
index = mob.getIndex(); | ||
gameLine[index] = player; | ||
player.addInventory(new Item.Health(100 - player.health())); | ||
} | ||
} | ||
|
||
private View viewIndex(int index) { | ||
Objects.checkIndex(index, gameLine.length); | ||
Object object = gameLine[index]; | ||
if (object instanceof Item item) { | ||
return new View(Type.ITEM, item); | ||
} else if (object instanceof Person.Mob mob) { | ||
return new View(Type.ENEMY, mob); | ||
} else if (object instanceof Person.Player player) { | ||
return new View(Type.PLAYER, player); | ||
} | ||
return new View(Type.EMPTY, null); | ||
} | ||
|
||
@Override | ||
public View view(Person.Player player) { | ||
return viewIndex(index + 1); | ||
} | ||
|
||
@Override | ||
public View view(Person.Mob mob) { | ||
int i = mob.getIndex(); | ||
Objects.checkIndex(i - 1, gameLine.length); | ||
return viewIndex(i - 1); | ||
} | ||
|
||
@Override | ||
public void hit(Person person, Item item) { | ||
output.println("Hit " + person + " on " + item); | ||
} | ||
|
||
public View playerView() { | ||
return view(player); | ||
} | ||
|
||
public void forward() { | ||
if (index + 1 < gameLine.length && gameLine[index + 1] instanceof Person.Mob) { | ||
output.println("We cant move forward"); | ||
return; | ||
} | ||
index++; | ||
if (index >= gameLine.length) { | ||
nextGameLine(); | ||
} else { | ||
gameLine[index - 1] = null; | ||
gameLine[index] = player; | ||
} | ||
} | ||
|
||
public void attack() { | ||
View view = playerView(); | ||
if (view.type() == Type.ENEMY) { | ||
Person.Mob mob = (Person.Mob) view.element(); | ||
mob.hit(player.getSelectedItem(), this); | ||
} else { | ||
output.println("We cant kick empty space"); | ||
} | ||
} | ||
|
||
public List<Item> listItems() { | ||
return player.items(); | ||
} | ||
|
||
public void selectItem(int itemIndex) { | ||
player.selectItem(itemIndex, this); | ||
} | ||
|
||
public void catchItem() { | ||
Updatable.View view = playerView(); | ||
if (view.type() == Updatable.Type.ITEM) { | ||
player.addInventory((Item)view.element()); | ||
gameLine[index + 1] = null; | ||
forward(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package ru.mifi.practice.voln; | ||
|
||
import ru.mifi.practice.voln.logic.Item; | ||
import ru.mifi.practice.voln.logic.Person; | ||
import ru.mifi.practice.voln.logic.Updatable; | ||
import ru.mifi.practice.voln.transmit.Transmit; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
@SuppressWarnings("PMD.LooseCoupling") | ||
public abstract class Main { | ||
public static void main(String[] args) { | ||
Transmit transmit = new Transmit.Standard(); | ||
transmit.println("Input f - move front"); | ||
transmit.println("Input v - view front"); | ||
transmit.println("Input s - select item"); | ||
transmit.println("Input c - catch item"); | ||
transmit.println("Input a - attack"); | ||
transmit.println("Input w - waiting"); | ||
transmit.println("Input q - quit"); | ||
Person.Player player = new Person.Player("Unknown"); | ||
AdventureGame game = new AdventureGame(transmit, player); | ||
LinkedList<Character> commands = new LinkedList<>(); | ||
boolean isRunning = true; | ||
while (isRunning && game.isRunning()) { | ||
if (commands.isEmpty()) { | ||
transmit.print("Input player command: "); | ||
String input = transmit.readText(); | ||
input.chars().forEach(c -> commands.add((char) c)); | ||
} | ||
Character c = commands.removeFirst(); | ||
switch (c) { | ||
case 'q': { | ||
isRunning = false; | ||
break; | ||
} | ||
case 'w': { | ||
transmit.println("waiting..."); | ||
break; | ||
} | ||
case 'a': { | ||
game.attack(); | ||
break; | ||
} | ||
case 'v': { | ||
Updatable.View view = game.playerView(); | ||
transmit.println(view.toString()); | ||
break; | ||
} | ||
case 'c': { | ||
game.catchItem(); | ||
break; | ||
} | ||
case 's': { | ||
List<Item> items = game.listItems(); | ||
if (!items.isEmpty()) { | ||
for (int i = 0; i < items.size(); i++) { | ||
Item item = items.get(i); | ||
transmit.println("[" + i + "] " + item.toString()); | ||
} | ||
transmit.print("Select item or -1 skip: "); | ||
var input = transmit.readInt().orElse(-1); | ||
game.selectItem(input); | ||
} | ||
break; | ||
} | ||
case 'f': { | ||
game.forward(); | ||
break; | ||
} | ||
default: { | ||
transmit.println("Invalid command"); | ||
} | ||
} | ||
game.update(); | ||
} | ||
transmit.println("game completed!"); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package ru.mifi.practice.voln.logic; | ||
|
||
import ru.mifi.practice.voln.logic.Updatable.Context; | ||
|
||
public interface Item { | ||
|
||
int damage(); | ||
|
||
interface Once extends Item { | ||
void apply(Person person, Context context); | ||
} | ||
|
||
final class Health implements Once { | ||
private final int health; | ||
|
||
public Health(int health) { | ||
this.health = health; | ||
} | ||
|
||
@Override | ||
public int damage() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public void apply(Person person, Context context) { | ||
person.healthUp(health); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "H" + health; | ||
} | ||
} | ||
|
||
class DamageItem implements Item { | ||
private final int damage; | ||
|
||
public DamageItem(int damage) { | ||
this.damage = damage; | ||
} | ||
|
||
@Override | ||
public int damage() { | ||
return damage; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "D" + damage; | ||
} | ||
} | ||
|
||
final class Hummer extends DamageItem { | ||
|
||
public Hummer() { | ||
super(10); | ||
} | ||
} | ||
} |
Oops, something went wrong.