diff --git a/.gitignore b/.gitignore index d16bbbe..6df27a3 100644 --- a/.gitignore +++ b/.gitignore @@ -74,4 +74,52 @@ fabric.properties .idea/httpRequests # Android studio 3.1+ serialized cache file -.idea/caches/build_file_checksums.ser \ No newline at end of file +.idea/caches/build_file_checksums.ser + +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +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 + +oolab/.idea/ +oolab/gradlew.bat +oolab/gradlew +oolab/.gitignore \ No newline at end of file diff --git a/README.md b/README.md index 586c886..f84bf95 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ Kamil Rudny Piątek 11:20 Gerard -https://gitexercises.fracz.com/committer/4a757cf05e8a3f76ed8599ee38a79085391ed03d?email=karudny@student.agh.edu.pl \ No newline at end of file +https://gitexercises.fracz.com/committer/4a757cf05e8a3f76ed8599ee38a79085391ed03d?email=karudny@student.agh.edu.pl + +Za zadania z gita z srebrnej skrzynki chce marchewkę lab, a z brązowej weterynarza. \ No newline at end of file diff --git a/oolab/.gitignore b/oolab/.gitignore deleted file mode 100644 index b63da45..0000000 --- a/oolab/.gitignore +++ /dev/null @@ -1,42 +0,0 @@ -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/**/build/ -!**/src/test/**/build/ - -### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ -*.iws -*.iml -*.ipr -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 \ No newline at end of file diff --git a/oolab/.idea/.gitignore b/oolab/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/oolab/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/oolab/.idea/gradle.xml b/oolab/.idea/gradle.xml deleted file mode 100644 index 2a65317..0000000 --- a/oolab/.idea/gradle.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/oolab/.idea/misc.xml b/oolab/.idea/misc.xml deleted file mode 100644 index 10f381e..0000000 --- a/oolab/.idea/misc.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/oolab/.idea/uiDesigner.xml b/oolab/.idea/uiDesigner.xml deleted file mode 100644 index 2b63946..0000000 --- a/oolab/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/oolab/.idea/vcs.xml b/oolab/.idea/vcs.xml deleted file mode 100644 index 6c0b863..0000000 --- a/oolab/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/oolab/gradlew b/oolab/gradlew deleted file mode 100644 index 1b6c787..0000000 --- a/oolab/gradlew +++ /dev/null @@ -1,234 +0,0 @@ -#!/bin/sh - -# -# Copyright © 2015-2021 the original authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -############################################################################## -# -# Gradle start up script for POSIX generated by Gradle. -# -# Important for running: -# -# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is -# noncompliant, but you have some other compliant shell such as ksh or -# bash, then to run this script, type that shell name before the whole -# command line, like: -# -# ksh Gradle -# -# Busybox and similar reduced shells will NOT work, because this script -# requires all of these POSIX shell features: -# * functions; -# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», -# «${var#prefix}», «${var%suffix}», and «$( cmd )»; -# * compound commands having a testable exit status, especially «case»; -# * various built-in commands including «command», «set», and «ulimit». -# -# Important for patching: -# -# (2) This script targets any POSIX shell, so it avoids extensions provided -# by Bash, Ksh, etc; in particular arrays are avoided. -# -# The "traditional" practice of packing multiple parameters into a -# space-separated string is a well documented source of bugs and security -# problems, so this is (mostly) avoided, by progressively accumulating -# options in "$@", and eventually passing that to Java. -# -# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, -# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; -# see the in-line comments for details. -# -# There are tweaks for specific operating systems such as AIX, CygWin, -# Darwin, MinGW, and NonStop. -# -# (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt -# within the Gradle project. -# -# You can find Gradle at https://github.com/gradle/gradle/. -# -############################################################################## - -# Attempt to set APP_HOME - -# Resolve links: $0 may be a link -app_path=$0 - -# Need this for daisy-chained symlinks. -while - APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path - [ -h "$app_path" ] -do - ls=$( ls -ld "$app_path" ) - link=${ls#*' -> '} - case $link in #( - /*) app_path=$link ;; #( - *) app_path=$APP_HOME$link ;; - esac -done - -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" -APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD=maximum - -warn () { - echo "$*" -} >&2 - -die () { - echo - echo "$*" - echo - exit 1 -} >&2 - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -nonstop=false -case "$( uname )" in #( - CYGWIN* ) cygwin=true ;; #( - Darwin* ) darwin=true ;; #( - MSYS* | MINGW* ) msys=true ;; #( - NONSTOP* ) nonstop=true ;; -esac - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD=$JAVA_HOME/jre/sh/java - else - JAVACMD=$JAVA_HOME/bin/java - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then - case $MAX_FD in #( - max*) - MAX_FD=$( ulimit -H -n ) || - warn "Could not query maximum file descriptor limit" - esac - case $MAX_FD in #( - '' | soft) :;; #( - *) - ulimit -n "$MAX_FD" || - warn "Could not set maximum file descriptor limit to $MAX_FD" - esac -fi - -# Collect all arguments for the java command, stacking in reverse order: -# * args from the command line -# * the main class name -# * -classpath -# * -D...appname settings -# * --module-path (only if needed) -# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. - -# For Cygwin or MSYS, switch paths to Windows format before running java -if "$cygwin" || "$msys" ; then - APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) - CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) - - JAVACMD=$( cygpath --unix "$JAVACMD" ) - - # Now convert the arguments - kludge to limit ourselves to /bin/sh - for arg do - if - case $arg in #( - -*) false ;; # don't mess with options #( - /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath - [ -e "$t" ] ;; #( - *) false ;; - esac - then - arg=$( cygpath --path --ignore --mixed "$arg" ) - fi - # Roll the args list around exactly as many times as the number of - # args, so each arg winds up back in the position where it started, but - # possibly modified. - # - # NB: a `for` loop captures its iteration list before it begins, so - # changing the positional parameters here affects neither the number of - # iterations, nor the values presented in `arg`. - shift # remove old arg - set -- "$@" "$arg" # push replacement arg - done -fi - -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. - -set -- \ - "-Dorg.gradle.appname=$APP_BASE_NAME" \ - -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ - "$@" - -# Use "xargs" to parse quoted args. -# -# With -n1 it outputs one arg per line, with the quotes and backslashes removed. -# -# In Bash we could simply go: -# -# readarray ARGS < <( xargs -n1 <<<"$var" ) && -# set -- "${ARGS[@]}" "$@" -# -# but POSIX shell has neither arrays nor command substitution, so instead we -# post-process each arg (as a line of input to sed) to backslash-escape any -# character that might be a shell metacharacter, then use eval to reverse -# that process (while maintaining the separation between arguments), and wrap -# the whole thing up as a single "set" statement. -# -# This will of course break if any of these variables contains a newline or -# an unmatched quote. -# - -eval "set -- $( - printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | - xargs -n1 | - sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | - tr '\n' ' ' - )" '"$@"' - -exec "$JAVACMD" "$@" diff --git a/oolab/gradlew.bat b/oolab/gradlew.bat deleted file mode 100644 index 107acd3..0000000 --- a/oolab/gradlew.bat +++ /dev/null @@ -1,89 +0,0 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/oolab/src/main/java/agh/ics/oop/OptionsParser.java b/oolab/src/main/java/agh/ics/oop/OptionsParser.java index 027c488..b4e6d4e 100644 --- a/oolab/src/main/java/agh/ics/oop/OptionsParser.java +++ b/oolab/src/main/java/agh/ics/oop/OptionsParser.java @@ -1,23 +1,26 @@ package agh.ics.oop; import agh.ics.oop.model.MoveDirection; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; public class OptionsParser { - public static MoveDirection[] Parser(String[] args) { + public static List Parser(String[] args) { MoveDirection[] moveDirection = new MoveDirection[args.length]; + List MoveDirectionList = new ArrayList<>(); for(int i = 0; i < args.length; i++){ if (Objects.equals(args[i], "f")) { - moveDirection[i] = MoveDirection.FORWARD; + MoveDirectionList.add(MoveDirection.FORWARD); } else if (Objects.equals(args[i], "b")) { - moveDirection[i] = MoveDirection.BACKWARDS; + MoveDirectionList.add(MoveDirection.BACKWARD); } else if (Objects.equals(args[i], "l")) { - moveDirection[i] = MoveDirection.LEFT; + MoveDirectionList.add(MoveDirection.LEFT); } else if (Objects.equals(args[i], "r")) { - moveDirection[i] = MoveDirection.RIGHT; + MoveDirectionList.add(MoveDirection.RIGHT); } } - return moveDirection; + return MoveDirectionList; } } diff --git a/oolab/src/main/java/agh/ics/oop/Simulation.java b/oolab/src/main/java/agh/ics/oop/Simulation.java new file mode 100644 index 0000000..80176ec --- /dev/null +++ b/oolab/src/main/java/agh/ics/oop/Simulation.java @@ -0,0 +1,44 @@ +package agh.ics.oop; + +import agh.ics.oop.model.Animal; +import agh.ics.oop.model.MoveDirection; +import agh.ics.oop.model.Vector2d; + +import java.util.ArrayList; +import java.util.List; + +public class Simulation { + List Animals = new ArrayList<>(); + List Directions = new ArrayList<>(); + public Simulation(List positions, List directions) { + for(Vector2d position : positions) { + if (position.getX() >= 0 && position.getX() < 5 && position.getY() >= 0 && position.getY() < 5) + { + Animal new_animal = new Animal(position.getX(), position.getY()); + Animals.add(new_animal); + } + } + + this.Directions = directions; + } + + public void run() { + int animal_count = Animals.size(); + int cnt = 0; + + for(MoveDirection currentMove : Directions) { + Animal current_animal = Animals.get(0); + Animals.remove(0); + current_animal.move(currentMove); + System.out.println("Zwierze " + ((cnt % animal_count) + 1) + " " + current_animal.toString()); + cnt += 1; + Animals.add(current_animal); + } + } + + public List getAnimals() { + return Animals; + } + + public List getDirections() { return Directions; } +} diff --git a/oolab/src/main/java/agh/ics/oop/World.java b/oolab/src/main/java/agh/ics/oop/World.java index 95a8f9a..6ee6905 100644 --- a/oolab/src/main/java/agh/ics/oop/World.java +++ b/oolab/src/main/java/agh/ics/oop/World.java @@ -1,20 +1,23 @@ package agh.ics.oop; +import agh.ics.oop.model.Animal; import agh.ics.oop.model.MapDirection; import agh.ics.oop.model.MoveDirection; import agh.ics.oop.model.Vector2d; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; public class World { - public static void run(MoveDirection[] directions) { + public static void run(List directions) { for (MoveDirection direction : directions) { switch (direction) { case FORWARD: System.out.println("Zwierzak idzie prosto"); break; - case BACKWARDS: + case BACKWARD: System.out.println("Zwierzak idzie do tyłu"); break; case LEFT: @@ -27,22 +30,10 @@ public static void run(MoveDirection[] directions) { } } public static void main(String[] args) { - /*System.out.println("System zaczął działanie"); - run(OptionsParser.Parser(args)); - System.out.println("System skończył działanie"); - - Vector2d position1 = new Vector2d(1,2); - System.out.println(position1); - Vector2d position2 = new Vector2d(-2,1); - System.out.println(position2); - System.out.println(position1.add(position2));*/ - - MapDirection direction = MapDirection.EAST; - System.out.println("Aktualny kierunek: " + direction); - System.out.println("Następny kierunek: " + direction.next()); - System.out.println("Poprzedni kierunek: " + direction.previous()); - System.out.println("Jednostkowy wektor: " + direction.toUnitVector()); - + List directions = OptionsParser.Parser(args); + List positions = List.of(new Vector2d(2,2), new Vector2d(3,4)); + Simulation simulation = new Simulation(positions, directions); + simulation.run(); } } diff --git a/oolab/src/main/java/agh/ics/oop/model/Animal.java b/oolab/src/main/java/agh/ics/oop/model/Animal.java new file mode 100644 index 0000000..6f37d59 --- /dev/null +++ b/oolab/src/main/java/agh/ics/oop/model/Animal.java @@ -0,0 +1,53 @@ +package agh.ics.oop.model; + +public class Animal { + private MapDirection direction; + private Vector2d position; + public Animal() { + this.direction = MapDirection.NORTH; + this.position = new Vector2d(2,2); + } + public Animal(int x, int y){ + this.direction = MapDirection.NORTH; + this.position = new Vector2d(x, y); + } + public MapDirection getDirection() { + return this.direction; + } + public Vector2d getPosition() { + return this.position; + } + @Override + public String toString() { + return "Pozycja: " + position.toString() + " Orientacja: " + direction.toString(); + } + public boolean isAt(Vector2d position) { + return position.equals(this.position); + } + public void move(MoveDirection direction) { + Vector2d MAP_RIGHT_TOP = new Vector2d(5,5); + Vector2d MAP_LEFT_BOTTOM = new Vector2d(0,0); + + switch(direction) { + case RIGHT: + this.direction = this.direction.next(); + break; + case LEFT: + this.direction = this.direction.previous(); + break; + case FORWARD: + Vector2d newPosition1 = this.position.add(this.direction.toUnitVector()); + if (newPosition1.precedes(MAP_RIGHT_TOP) && newPosition1.follows(MAP_LEFT_BOTTOM)) { + this.position = newPosition1; + } + break; + case BACKWARD: + Vector2d newPosition2 = this.position.subtract(this.direction.toUnitVector()); + if (newPosition2.precedes(MAP_RIGHT_TOP) && newPosition2.follows(MAP_LEFT_BOTTOM)) { + this.position = newPosition2; + } + break; + } + } + +} diff --git a/oolab/src/main/java/agh/ics/oop/model/MapDirection.java b/oolab/src/main/java/agh/ics/oop/model/MapDirection.java index 5bf5d12..e013ba1 100644 --- a/oolab/src/main/java/agh/ics/oop/model/MapDirection.java +++ b/oolab/src/main/java/agh/ics/oop/model/MapDirection.java @@ -6,10 +6,10 @@ public enum MapDirection { @Override public String toString() { return switch (this) { - case NORTH -> "Północ"; - case EAST -> "Wschód"; - case SOUTH -> "Południe"; - case WEST -> "Zachód"; + case NORTH -> "Polnoc"; + case EAST -> "Wschod"; + case SOUTH -> "Poludnie"; + case WEST -> "Zachod"; }; } public MapDirection next() { diff --git a/oolab/src/main/java/agh/ics/oop/model/MoveDirection.java b/oolab/src/main/java/agh/ics/oop/model/MoveDirection.java index 7c32ccc..9a7aaa7 100644 --- a/oolab/src/main/java/agh/ics/oop/model/MoveDirection.java +++ b/oolab/src/main/java/agh/ics/oop/model/MoveDirection.java @@ -1,5 +1,5 @@ package agh.ics.oop.model; public enum MoveDirection { - FORWARD, BACKWARDS, LEFT, RIGHT; + FORWARD, BACKWARD, LEFT, RIGHT; } diff --git a/oolab/src/main/java/agh/ics/oop/model/Vector2d.java b/oolab/src/main/java/agh/ics/oop/model/Vector2d.java index 1e880e9..fd80a10 100644 --- a/oolab/src/main/java/agh/ics/oop/model/Vector2d.java +++ b/oolab/src/main/java/agh/ics/oop/model/Vector2d.java @@ -84,7 +84,4 @@ public int hashCode() { return Objects.hash(x, y); } - - - } diff --git a/oolab/src/test/java/agh/ics/oop/model/AnimalTest.java b/oolab/src/test/java/agh/ics/oop/model/AnimalTest.java new file mode 100644 index 0000000..9a2dce8 --- /dev/null +++ b/oolab/src/test/java/agh/ics/oop/model/AnimalTest.java @@ -0,0 +1,53 @@ +package agh.ics.oop.model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class AnimalTest { + @Test + public void getDirectionTest(){ + Animal animal1 = new Animal(); + Animal animal2 = new Animal(2,2); + assertEquals(animal1.getDirection(), MapDirection.NORTH); + assertEquals(animal2.getDirection(), MapDirection.NORTH); + } + + @Test + public void getPositionTest(){ + Animal animal1 = new Animal(); + Animal animal2 = new Animal(2,2); + Animal animal3 = new Animal(1,3); + + assertEquals(animal1.getPosition(), new Vector2d(2,2)); + assertEquals(animal2.getPosition(), new Vector2d(2,2)); + assertEquals(animal3.getPosition(), new Vector2d(1,3)); + } + + @Test + public void toStringTest(){ + Animal animal1 = new Animal(); + Animal animal2 = new Animal(2,2); + Animal animal3 = new Animal(1,3); + + assertEquals(animal1.toString(), "Pozycja: (2,2) Orientacja: Północ"); + assertEquals(animal2.toString(), "Pozycja: (2,2) Orientacja: Północ"); + assertEquals(animal3.toString(), "Pozycja: (1,3) Orientacja: Północ"); + } + + @Test + public void isAt(){ + Animal animal1 = new Animal(); + Animal animal2 = new Animal(2,2); + Animal animal3 = new Animal(1,3); + + assertTrue(animal1.isAt(new Vector2d(2,2))); + assertTrue(animal2.isAt(new Vector2d(2,2))); + assertTrue(animal3.isAt(new Vector2d(1,3))); + assertFalse(animal1.isAt(new Vector2d(1,2))); + assertFalse(animal2.isAt(new Vector2d(1,2))); + assertFalse(animal3.isAt(new Vector2d(2,3))); + } + + +} diff --git a/oolab/src/test/java/agh/ics/oop/model/IntegrationMoveTest.java b/oolab/src/test/java/agh/ics/oop/model/IntegrationMoveTest.java new file mode 100644 index 0000000..f981df0 --- /dev/null +++ b/oolab/src/test/java/agh/ics/oop/model/IntegrationMoveTest.java @@ -0,0 +1,155 @@ +package agh.ics.oop.model; + +import agh.ics.oop.OptionsParser; + +import agh.ics.oop.Simulation; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class IntegrationMoveTest { + @Test + public void testValidMoves() { + String[] args = {"f", "b", "l", "r", "r", "f", "l", "b"}; + List directions = OptionsParser.Parser(args); + List positions = List.of(new Vector2d(2,2), new Vector2d(3,4)); + Simulation simulation = new Simulation(positions, directions); + simulation.run(); + + List finalAnimals = simulation.getAnimals(); + List actualPositions = new ArrayList<>(); + + for (Animal animal : finalAnimals) { + actualPositions.add(animal.getPosition()); + } + List correctPositions = new ArrayList<>() {{ + add(new Vector2d(2,3)); + add(new Vector2d(3,3)); + }}; + + assertEquals(correctPositions, actualPositions); + } + @Test + public void testInvalidMoves() { + String[] args = {"f", "w", "h", "b", "l", "r", "r", "f", "l", "b"}; + List directions = OptionsParser.Parser(args); + List positions = List.of(new Vector2d(2,2), new Vector2d(3,4)); + Simulation simulation = new Simulation(positions, directions); + simulation.run(); + + List finalAnimals = simulation.getAnimals(); + List actualPositions = new ArrayList<>(); + + for (Animal animal : finalAnimals) { + actualPositions.add(animal.getPosition()); + } + List correctPositions = new ArrayList<>() {{ + add(new Vector2d(2,3)); + add(new Vector2d(3,3)); + }}; + + assertEquals(correctPositions, actualPositions); + } + @Test + public void testOutOfBorderMoves1() { + String[] args = {"f", "l", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f"}; + List directions = OptionsParser.Parser(args); + List positions = List.of(new Vector2d(2,2), new Vector2d(3,4)); + Simulation simulation = new Simulation(positions, directions); + simulation.run(); + + List finalAnimals = simulation.getAnimals(); + List actualPositions = new ArrayList<>(); + + for (Animal animal : finalAnimals) { + actualPositions.add(animal.getPosition()); + } + List correctPositions = new ArrayList<>() {{ + add(new Vector2d(2,4)); + add(new Vector2d(0,4)); + }}; + + assertEquals(correctPositions, actualPositions); + } + @Test + public void testOutOfBorderMoves2() { + String[] args = {"f", "l", "l", "b", "b", "b", "b", "b", "b", "b", "b", "b"}; + List directions = OptionsParser.Parser(args); + List positions = List.of(new Vector2d(2,2), new Vector2d(3,4)); + Simulation simulation = new Simulation(positions, directions); + simulation.run(); + + List finalAnimals = simulation.getAnimals(); + List actualPositions = new ArrayList<>(); + + for (Animal animal : finalAnimals) { + actualPositions.add(animal.getPosition()); + } + List correctPositions = new ArrayList<>() {{ + add(new Vector2d(4,3)); + add(new Vector2d(4,4)); + }}; + + assertEquals(correctPositions, actualPositions); + } + @Test + public void testInputValues() { + String[] args = {"f", "l", "r", "b", "r", "f", "j", "k"}; + List directions = OptionsParser.Parser(args); + List positions = List.of(new Vector2d(7,11), new Vector2d(3,4), new Vector2d(1,1), new Vector2d(0,0)); + Simulation simulation = new Simulation(positions, directions); + + List finalAnimals = simulation.getAnimals(); + List finalDirections = simulation.getDirections(); + List finalPositions = new ArrayList<>(); + + + for (Animal animal : finalAnimals) { + finalPositions.add(animal.getPosition()); + } + + List correctPositions = new ArrayList<>() {{ + add(new Vector2d(3,4)); + add(new Vector2d(1,1)); + add(new Vector2d(0,0)); + }}; + + List correctDirections = new ArrayList<>() {{ + add(MoveDirection.FORWARD); + add(MoveDirection.LEFT); + add(MoveDirection.RIGHT); + add(MoveDirection.BACKWARD); + add(MoveDirection.RIGHT); + add(MoveDirection.FORWARD); + }}; + + assertEquals(correctPositions, finalPositions); + assertEquals(correctDirections, finalDirections); + } + @Test + public void testOrientation() { + String[] args = {"f", "b", "l", "r", "r", "f", "l", "b"}; + List directions = OptionsParser.Parser(args); + List positions = List.of(new Vector2d(2,2), new Vector2d(3,4)); + Simulation simulation = new Simulation(positions, directions); + simulation.run(); + + List finalAnimals = simulation.getAnimals(); + List actualDirections = new ArrayList<>(); + + for (Animal animal : finalAnimals) { + actualDirections.add(animal.getDirection()); + } + List correctDirections = new ArrayList<>() {{ + add(MapDirection.WEST); + add(MapDirection.EAST); + }}; + + assertEquals(correctDirections, actualDirections); + } + +} diff --git a/oolab/src/test/java/agh/ics/oop/model/OptionsParserTest.java b/oolab/src/test/java/agh/ics/oop/model/OptionsParserTest.java index 7506c54..2a27580 100644 --- a/oolab/src/test/java/agh/ics/oop/model/OptionsParserTest.java +++ b/oolab/src/test/java/agh/ics/oop/model/OptionsParserTest.java @@ -1,20 +1,38 @@ package agh.ics.oop.model; import agh.ics.oop.OptionsParser; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class OptionsParserTest { @Test public void testParseValidDirections() { - String[] args = {"f", "b", "l", "r"}; - MoveDirection[] result = OptionsParser.Parser(args); + String[] args1 = {"f", "b", "l", "r"}; + List result1 = OptionsParser.Parser(args1); + List correct1 = new ArrayList<>() {{ + add(MoveDirection.FORWARD); + add(MoveDirection.BACKWARD); + add(MoveDirection.LEFT); + add(MoveDirection.RIGHT); + }}; - assertArrayEquals(new MoveDirection[]{MoveDirection.FORWARD, MoveDirection.BACKWARDS, MoveDirection.LEFT, MoveDirection.RIGHT}, result); + Assertions.assertEquals(result1, correct1); - String[] args1 = {"f", "r", "k", "r"}; - MoveDirection[] result1 = OptionsParser.Parser(args1); + String[] args2 = {"b", "k", "r", "l", "f", "b"}; + List result2 = OptionsParser.Parser(args2); + List correct2 = new ArrayList<>() {{ + add(MoveDirection.BACKWARD); + add(MoveDirection.RIGHT); + add(MoveDirection.LEFT); + add(MoveDirection.FORWARD); + add(MoveDirection.BACKWARD); + }}; - assertArrayEquals(new MoveDirection[]{MoveDirection.FORWARD, MoveDirection.RIGHT, null, MoveDirection.RIGHT}, result1); + Assertions.assertEquals(result2, correct2); } }