Skip to content

Instantly share code, notes, and snippets.

@gusdelact
Last active June 9, 2022 15:06
Show Gist options
  • Save gusdelact/eb60befa5db2ee3437d5e3aa69ac2b75 to your computer and use it in GitHub Desktop.
Save gusdelact/eb60befa5db2ee3437d5e3aa69ac2b75 to your computer and use it in GitHub Desktop.
import edu.duke.Point;
/**
* A simple example that will help you to use a FloorMap object.
*
* @author awsgus
* @version 0.5
*/
public class KivaSimpleWorld
{
public final static String DEFAULT_MAP =
"---------------\n"
+ "|K P |\n"
+ "| D |\n"
+ "| |\n"
+ "| |\n"
+ "| |\n"
+ "| |\n"
+ "| |\n"
+ "---------------";
private FloorMap theMap;
public KivaSimpleWorld()
{
theMap = new FloorMap(KivaSimpleWorld.DEFAULT_MAP );
}
public KivaSimpleWorld(String aStringMap)
{
theMap = new FloorMap(aStringMap );
}
public Point[] checkForThings()
{
Point initial= theMap.getInitialKivaLocation();
Point pod =theMap.getPodLocation();
Point drop = theMap.getDropZoneLocation();
System.out.println(initial);
System.out.println(pod);
System.out.println(drop);
Point [] locations = {initial, pod, drop};
return locations;
}
public FloorMapObject goTo(Point target)
{
FloorMapObject theObject = theMap.getObjectAtLocation(target);
return theObject;
}
public void putAt(Point target) throws Exception
{
FloorMapObject theObject = theMap.getObjectAtLocation(target);
switch (theObject)
{
case EMPTY:
System.out.println("You are in an empty space");
break;
case POD:
System.out.println("You found the Pod!");
break;
case DROP_ZONE:
System.out.println("You win!");
break;
case OBSTACLE:
throw new Exception("UUUGGGH :( :0 :O ");
// is not required because the program will not execute the break statement
//break;
default:
break;
}
}
}
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import edu.duke.Point;
/**
* The test class KivaSimpleWorldTest.
*
* @author awsgus
* @version 0.5
*/
public class KivaSimpleWorldTest
{
/**
* Default constructor for test class KivaSimpleWorldTest
*/
public KivaSimpleWorldTest()
{
}
private KivaSimpleWorld kivaSimp2;
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@BeforeEach
public void setUp()
{
kivaSimp2 = new KivaSimpleWorld();
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@AfterEach
public void tearDown()
{
kivaSimp2 = null;
}
@Test
public void testLocations()
{
assertNotNull(kivaSimp2.checkForThings());
Point [] thePoints=kivaSimp2.checkForThings();
Point init=thePoints[0];
assertEquals(init.getX(), 1);
assertEquals(init.getY(), 1);
Point pod=thePoints[1];
assertEquals(pod.getX(), 6);
assertEquals(pod.getY(), 1);
Point drop=thePoints[2];
assertEquals(drop.getX(), 6);
assertEquals(drop.getY(), 2);
}
@Test
public void testGoto()
{
//KivaSimpleWorld kivaSimp2 = new KivaSimpleWorld();
Point point2 = new Point(2,2);
FloorMapObject theObject = kivaSimp2.goTo(point2);
//first of all, test that theObject is not null
assertNotNull(theObject);
//check that the object in location 2,2 is an EMPTY space
assertEquals(theObject, FloorMapObject.EMPTY);
Point point3 = new Point(6,2);
FloorMapObject theObjectDrop = kivaSimp2.goTo(point3);
assertNotNull(theObjectDrop);
assertEquals(theObjectDrop, FloorMapObject.DROP_ZONE);
}
@Test
public void testError()
{
assert(true);
}
@Test
public void testBadDecision()
{
Point p3= new Point(0,0);
//KivaSimpleWorld kivaSimp2 = new KivaSimpleWorld();
try
{
kivaSimp2.putAt(p3);
//i don´t receive an exception but it was supposed to throw that
assert(false);
} catch (Exception ex) {
//i manage the exception
assert( true);
}
}
@Test
public void testPutAt()
{
// KivaSimpleWorld kivaSimp2 = new KivaSimpleWorld();
try
{
//check for an EMPTY
Point p3= new Point(2,3);
kivaSimp2.putAt(p3);
assert(true);
//check for a POD
Point p4= new Point(6,1);
kivaSimp2.putAt(p4);
assert(true);
//check for DROP zone
Point p5= new Point(6,2);
kivaSimp2.putAt(p5);
assert(true);
} catch(Exception ex)
{
assert(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment