This commit is contained in:
Sobottasgithub
2026-01-12 16:17:14 +01:00
commit f335297065
9 changed files with 288 additions and 0 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake .

24
.gitignore vendored Normal file
View File

@@ -0,0 +1,24 @@
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*

12
GNUmakefile Normal file
View File

@@ -0,0 +1,12 @@
JAVA_FMT = google-java-format
JAVA_FILES = $(shell find . -name '*.java')
XML_FMT = xmlindent
XML_FILES = $(shell find . -name '*.xml')
.PHONY: fmt
fmt:
@echo "Formatting all Java files..."
@for f in $(JAVA_FILES); do \
echo " $$f"; \
$(JAVA_FMT) -i $$f; \
done

29
Hero.java Normal file
View File

@@ -0,0 +1,29 @@
public class Hero {
private String name;
private int strength;
private LinkedList weaponInventory;
private Weapon currentWeapon;
public Hero(String name, int strength) {
this.name = name;
this.strength = strength;
}
public LinkedList getWeaponInventory() {
return weaponInventory;
}
public void addWeapon(Weapon weapon) {
weaponInventory.append(weapon);
}
public void chooseWeapon(weapon) {
weaponInventory.toFirst();
while(weaponInventory.hasAccess() && weaponInventory.getCurrentContent() != weapon) {
weaponInventory.toNext();
}
if (weaponInventory.hasAccess()) {
currentWeapon = weaponInventory.getCurrentContent();
}
}
}

108
LinkedList.java Normal file
View File

@@ -0,0 +1,108 @@
public class LinkedList {
private Node first;
private Node last;
private Node current;
private int size;
public LinkedList() {}
public void toFirst() {
current = first;
}
public void toLast() {
current = last;
}
public Node getFirst() {
return first;
}
public Node getLast() {
return last;
}
public void toNext() {
if (hasAccess()) {
current = current.nextNode();
}
}
public boolean isEmpty() {
if (first == null && last == null) {
return true;
}
return false;
}
public boolean hasAccess() {
if (current == null) {
return false;
}
return true;
}
public void append(Object content) {
Node newNode = new Node(content);
if (isEmpty()) {
first = newNode;
last = newNode;
} else {
last.setNext(newNode);
last = newNode;
}
size++;
}
public int getSize() {
return size;
}
public void insert(Object content) {
if (hasAccess()) {
Node newNode = new Node(content);
Node nextNode = current.getNextNode();
current.setNextNode(newNode);
newNode.setNextNode(newNode);
}
size++;
}
public void remove() {
if (hasAccess()) {
if (head == tail) {
head = null;
tail = null;
current = null;
return;
}
Node newCurrent = current.getNextNode();
Node pointer = head;
while(pointer.getNextNode() != current) {
pointer = pointer.getNextNode();
}
current = newCurrent;
pointer.setNextNode(current);
}
size--;
}
public void concat(LinkedList weaponList) {
if (!isEmpty()) {
last.setNextNode(weaponList.getFirst());
last = weaponList.getLast();
size = weaponList.getSize();
return;
}
head = weaponList.getFirst();
last = weaponList.getFirst();
current = head;
size += weaponList.getSize();
}
public Object getCurrentContent() {
return current.getContent();
}
}

5
Main.java Normal file
View File

@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

25
Node.java Normal file
View File

@@ -0,0 +1,25 @@
public class Node<T> {
private T content = null;
private Node nextNode = null;
public Node(T content) {
setContent(content);
}
private void setContent(T content) {
this.content = content;
}
private T getContent() {
return this.content;
}
private void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
private Node getNextNode() {
return this.nextNode;
}
}

27
Weapon.java Normal file
View File

@@ -0,0 +1,27 @@
public class Weapon {
public String name;
public int damage;
public Weapon() {}
public Weapon(String name, int damage) {
setName(name);
setDamage(damage);
}
public void setName(String name) {
this.name = name;
}
public void setDamage(int damage) {
this.damage = damage;
}
public int getDamage() {
return damage;
}
public String getName() {
return name;
}
}

57
flake.nix Normal file
View File

@@ -0,0 +1,57 @@
{
description = "Linked-List";
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
];
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
perSystem = { config, self', inputs', pkgs, system, ... }: let
jdk = pkgs.jdk21.override {
};
buildInputs = [
jdk
];
devTools = [
pkgs.google-java-format
];
in {
devShells.default = pkgs.mkShell {
buildInputs = buildInputs ++ devTools;
};
packages = {
default = pkgs.stdenv.mkDerivation {
pname = "linked-list";
version = "1.0.0";
src = ./.;
nativeBuildInputs = buildInputs ++ [ pkgs.makeWrapper ];
buildPhase = ''
javac -Werror -g:none -deprecation -verbose Main.java
'';
installPhase = ''
mkdir -p $out/{bin,lib}
cp *.class $out/lib
makeWrapper ${pkgs.lib.getExe jdk} $out/bin/linked-list --add-flags "-cp $out/lib/ Main"
'';
};
};
};
flake = {
};
};
}