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
38 changes: 37 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
/.metadata
/robots/.settings
/robots/bin
eclipse.bat
eclipse.bat
out/
/robots/.classpath
/robots/.project
# Translations
*.mo

# 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*

# IDEA
.idea/
*.iml

# Maven
target/
34 changes: 34 additions & 0 deletions resources/Messages_en.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Main menu
menu.file=File
menu.exit=Exit
menu.lookandfeel=Look & Feel
menu.lookandfeel.system=System
menu.lookandfeel.cross=Cross-platform
menu.test=Tests
menu.test.log=Log message
menu.windows=Windows
menu.windows.showLog=Show log
menu.windows.showGame=Show game field
menu.windows.showCoord=Show coordinates
menu.language=Language
menu.language.russian=Russian
menu.language.english=English

# Window titles
window.log.title=Log window
window.game.title=Game field
window.coord.title=Robot coordinates

# Dialogs
exit.confirm.title=Exit confirmation
exit.confirm.message=Do you really want to exit the application?
option.yes=Yes
option.no=No

# Log
log.start=Log works
log.newline=New line

# Coordinates format
coord.position=Position: ({0,number,#.#}, {1,number,#.#})
coord.direction=Direction: {0,number,#.#}\u00B0
34 changes: 34 additions & 0 deletions resources/Messages_ru.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Main menu
menu.file=\u0424\u0430\u0439\u043b
menu.exit=\u0412\u044b\u0445\u043e\u0434
menu.lookandfeel=\u0420\u0435\u0436\u0438\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f
menu.lookandfeel.system=\u0421\u0438\u0441\u0442\u0435\u043c\u043d\u0430\u044f \u0441\u0445\u0435\u043c\u0430
menu.lookandfeel.cross=\u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0445\u0435\u043c\u0430
menu.test=\u0422\u0435\u0441\u0442\u044b
menu.test.log=\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u043b\u043e\u0433
menu.windows=\u041e\u043a\u043d\u0430
menu.windows.showLog=\u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b
menu.windows.showGame=\u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435
menu.windows.showCoord=\u041f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b
menu.language=\u042f\u0437\u044b\u043a
menu.language.russian=\u0420\u0443\u0441\u0441\u043a\u0438\u0439
menu.language.english=\u0410\u043D\u0433\u043B\u0438\u0439\u0441\u043A\u0438\u0439

# Window titles
window.log.title=\u041f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0440\u0430\u0431\u043e\u0442\u044b
window.game.title=\u0418\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435
window.coord.title=\u041a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \u0440\u043e\u0431\u043e\u0442\u0430

# Dialogs
exit.confirm.title=\u041f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u0432\u044b\u0445\u043e\u0434\u0430
exit.confirm.message=\u0412\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f?
option.yes=\u0414\u0430
option.no=\u041D\u0435\u0442

# Log
log.start=\u041f\u0440\u043e\u0442\u043e\u043a\u043e\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442
log.newline=\u041d\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430

# Coordinates format
coord.position=\u041f\u043e\u0437\u0438\u0446\u0438\u044f: ({0,number,#.#}, {1,number,#.#})
coord.direction=\u041d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435: {0,number,#.#}\u00b0
115 changes: 115 additions & 0 deletions robots/src/collections/BoundedCircularQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package collections;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class BoundedCircularQueue<E> extends AbstractQueue<E> {
private final int capacity;
private final Object[] buffer;
private final AtomicInteger head = new AtomicInteger(0);
private final AtomicInteger size = new AtomicInteger(0);

public BoundedCircularQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException("Capacity must be positive");
this.capacity = capacity;
this.buffer = new Object[capacity];
}

@Override
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
while (true) {
int currentSize = size.get();
if (currentSize < capacity) {
if (size.compareAndSet(currentSize, currentSize + 1)) {
int pos = (head.get() + currentSize) % capacity;
buffer[pos] = e;
return true;
}
} else {
int oldHead = head.get();
int newHead = (oldHead + 1) % capacity;
if (head.compareAndSet(oldHead, newHead)) {
buffer[oldHead] = e;
return true;
}
}
}
}

@Override
public E poll() {
while (true) {
int currentSize = size.get();
if (currentSize == 0) return null;
if (size.compareAndSet(currentSize, currentSize - 1)) {
int idx = head.getAndUpdate(h -> (h + 1) % capacity);
@SuppressWarnings("unchecked")
E result = (E) buffer[idx];
buffer[idx] = null;
return result;
}
}
}

@Override
public E peek() {
int currentSize = size.get();
if (currentSize == 0) return null;
@SuppressWarnings("unchecked")
E result = (E) buffer[head.get()];
return result;
}

@Override
public int size() {
return size.get();
}

@Override
public Iterator<E> iterator() {
return snapshot().iterator();
}

public List<E> getAll() {
return snapshot();
}

public List<E> getRange(int start, int end) {
int currentSize = size.get();
if (start < 0 || end > currentSize || start > end)
throw new IndexOutOfBoundsException("Invalid range: " + start + ".." + end);
int currentHead = head.get();
List<E> result = new ArrayList<>(end - start);
for (int i = start; i < end; i++) {
@SuppressWarnings("unchecked")
E element = (E) buffer[(currentHead + i) % capacity];
result.add(element);
}
return Collections.unmodifiableList(result);
}

private List<E> snapshot() {
int currentHead = head.get();
int currentSize = size.get();
List<E> result = new ArrayList<>(currentSize);
for (int i = 0; i < currentSize; i++) {
@SuppressWarnings("unchecked")
E element = (E) buffer[(currentHead + i) % capacity];
result.add(element);
}
return Collections.unmodifiableList(result);
}

public void clear() {
while (true) {
int oldSize = size.get();
if (oldSize == 0) return;
if (size.compareAndSet(oldSize, 0)) {
head.set(0);
Arrays.fill(buffer, null);
return;
}
}
}
}
64 changes: 64 additions & 0 deletions robots/src/collections/BoundedCircularQueueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package collections;

import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;

class BoundedCircularQueueTest {

@Test
void testOfferAndPoll() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(3);
assertTrue(q.offer("A"));
assertTrue(q.offer("B"));
assertTrue(q.offer("C"));
assertEquals(3, q.size());
assertEquals("A", q.poll());
assertEquals("B", q.peek());
assertEquals(2, q.size());
}

@Test
void testOverflow() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(3);
q.offer("A");
q.offer("B");
q.offer("C");
q.offer("D"); // перезаписывает A
assertEquals(3, q.size());
assertEquals("B", q.poll());
assertEquals("C", q.poll());
assertEquals("D", q.poll());
assertNull(q.poll());
}

@Test
void testGetAll() {
BoundedCircularQueue<Integer> q = new BoundedCircularQueue<>(5);
for (int i = 0; i < 5; i++) q.offer(i);
assertEquals(List.of(0, 1, 2, 3, 4), q.getAll());
}

@Test
void testGetRange() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(5);
q.offer("a");
q.offer("b");
q.offer("c");
q.offer("d");
assertEquals(List.of("b", "c"), q.getRange(1, 3));
}

@Test
void testIteratorSnapshot() {
BoundedCircularQueue<String> q = new BoundedCircularQueue<>(3);
q.offer("X");
q.offer("Y");
Iterator<String> it = q.iterator();
q.offer("Z"); // добавление не ломает итератор
assertTrue(it.hasNext());
assertEquals("X", it.next());
assertEquals("Y", it.next());
assertFalse(it.hasNext());
}
}
89 changes: 89 additions & 0 deletions robots/src/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import log.Logger;

public class ConfigManager {
private static final String CONFIG_FILE = System.getProperty("user.home") + File.separator + ".robot-config.properties";
private final Properties props = new Properties();

public void load() {
File file = new File(CONFIG_FILE);
if (!file.exists()) return;
try (FileInputStream fis = new FileInputStream(file)) {
props.load(fis);
} catch (IOException e) {
Logger.debug("Не удалось загрузить конфигурацию: " + e.getMessage());
}
}

public void save() {
try (FileOutputStream fos = new FileOutputStream(CONFIG_FILE)) {
props.store(fos, "Robot program configuration");
} catch (IOException e) {
Logger.debug("Не удалось сохранить конфигурацию: " + e.getMessage());
}
}

public void setMainWindowBounds(int x, int y, int width, int height, int state) {
setProperty("main.x", x);
setProperty("main.y", y);
setProperty("main.width", width);
setProperty("main.height", height);
setProperty("main.state", state);
}

public int getMainWindowX(int defaultValue) { return getIntProperty("main.x", defaultValue); }
public int getMainWindowY(int defaultValue) { return getIntProperty("main.y", defaultValue); }
public int getMainWindowWidth(int defaultValue) { return getIntProperty("main.width", defaultValue); }
public int getMainWindowHeight(int defaultValue) { return getIntProperty("main.height", defaultValue); }
public int getMainWindowState(int defaultValue) { return getIntProperty("main.state", defaultValue); }

public void setWindowBounds(String windowName, int x, int y, int width, int height, boolean icon, boolean maximized) {
setProperty(key(windowName, "x"), x);
setProperty(key(windowName, "y"), y);
setProperty(key(windowName, "width"), width);
setProperty(key(windowName, "height"), height);
setProperty(key(windowName, "icon"), icon);
setProperty(key(windowName, "maximized"), maximized);
}

public boolean hasWindow(String windowName) {
return props.containsKey(key(windowName, "x"));
}

public int getWindowX(String windowName, int defaultValue) { return getIntProperty(key(windowName, "x"), defaultValue); }
public int getWindowY(String windowName, int defaultValue) { return getIntProperty(key(windowName, "y"), defaultValue); }
public int getWindowWidth(String windowName, int defaultValue) { return getIntProperty(key(windowName, "width"), defaultValue); }
public int getWindowHeight(String windowName, int defaultValue) { return getIntProperty(key(windowName, "height"), defaultValue); }
public boolean getWindowIcon(String windowName, boolean defaultValue) { return getBooleanProperty(key(windowName, "icon"), defaultValue); }
public boolean getWindowMaximized(String windowName, boolean defaultValue) { return getBooleanProperty(key(windowName, "maximized"), defaultValue); }

public void setWindowVisible(String windowName, boolean visible) {
setProperty(key(windowName, "visible"), visible);
}

public boolean getWindowVisible(String windowName, boolean defaultValue) {
return getBooleanProperty(key(windowName, "visible"), defaultValue);
}


private String key(String windowName, String suffix) { return windowName + "." + suffix; }
private void setProperty(String key, int value) { props.setProperty(key, Integer.toString(value)); }
private void setProperty(String key, boolean value) { props.setProperty(key, Boolean.toString(value)); }

private int getIntProperty(String key, int defaultValue) {
String val = props.getProperty(key);
if (val == null) return defaultValue;
try { return Integer.parseInt(val); } catch (NumberFormatException e) { return defaultValue; }
}

private boolean getBooleanProperty(String key, boolean defaultValue) {
String val = props.getProperty(key);
return val == null ? defaultValue : Boolean.parseBoolean(val);
}
}
Loading