diff --git a/Assignment_1/Assignment_resources/Junit/tut_ex1/Book.java b/Assignment_1/Assignment_resources/Junit/tut_ex1/Book.java new file mode 100644 index 0000000000000000000000000000000000000000..cd20a059ffb2443f5207d9615e3902175f0aa00e --- /dev/null +++ b/Assignment_1/Assignment_resources/Junit/tut_ex1/Book.java @@ -0,0 +1,46 @@ +package LibraryManagement; + +public class Book { + private String title; + private String author; + private boolean loaned; + + public Book(String title, String author) { + this.title = title; + this.author = author; + this.loaned = false; + } + + public boolean isLoaned() { + return loaned; + } + + public void setLoaned(boolean loaned) { + this.loaned = loaned; + } + + // Getters and setters + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Book book = (Book) obj; + return title.equals(book.title) && author.equals(book.author); + } +} \ No newline at end of file diff --git a/Assignment_1/Assignment_resources/Junit/tut_ex1/Library.java b/Assignment_1/Assignment_resources/Junit/tut_ex1/Library.java new file mode 100644 index 0000000000000000000000000000000000000000..27e94c42141012278d6f2f1428ca8f8317bb751e --- /dev/null +++ b/Assignment_1/Assignment_resources/Junit/tut_ex1/Library.java @@ -0,0 +1,64 @@ +package LibraryManagement; + +import java.util.ArrayList; +import java.util.List; + + +public class Library { + private List<Book> books; + private List<Member> members; + private List<Loan> loans; + + public Library() { + this.books = new ArrayList<>(); + this.members = new ArrayList<>(); + this.loans = new ArrayList<>(); + } + + public void addBook(Book book) { + books.add(book); + } + + public void addMember(Member member) { + members.add(member); + } + + public void loanBook(Book book, Member member) throws Exception { + if (!books.contains(book)) { + throw new Exception("Book not in library"); + } + if (!members.contains(member)) { + throw new Exception("Member not registered"); + } + if (book.isLoaned()) { + throw new Exception("Book already loaned"); + } + Loan loan = new Loan(book, member); + loans.add(loan); + book.setLoaned(true); + } + + public void returnBook(Book book) throws Exception { + if (!books.contains(book)) { + throw new Exception("Book not in library"); + } + Loan loan = loans.stream().filter(l -> l.getBook().equals(book)).findFirst().orElse(null); + if (loan == null) { + throw new Exception("Book not loaned"); + } + loans.remove(loan); + book.setLoaned(false); + } + + public List<Book> getBooks() { + return books; + } + + public List<Member> getMembers() { + return members; + } + + public List<Loan> getLoans() { + return loans; + } +} \ No newline at end of file diff --git a/Assignment_1/Assignment_resources/Junit/tut_ex1/LibraryManagementSystemTestComplete.java b/Assignment_1/Assignment_resources/Junit/tut_ex1/LibraryManagementSystemTestComplete.java new file mode 100644 index 0000000000000000000000000000000000000000..d61dee63d555dfe2a3e1ed31c11d063df85b5b0d --- /dev/null +++ b/Assignment_1/Assignment_resources/Junit/tut_ex1/LibraryManagementSystemTestComplete.java @@ -0,0 +1,68 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + + +class LibraryManagementSystemTestComplete { + + private static Library library; + private static Book book1; + private static Book book2; + private static Member member1; + private static Member member2; + + @BeforeEach + void setUp() { + // Create library and add books and members + library = new Library(); + book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald"); + book2 = new Book("To Kill a Mockingbird", "Harper Lee"); + member1 = new Member("John Doe", "12345"); + member2 = new Member("Jane Smith", "67890"); + + library.addBook(book1); + library.addBook(book2); + library.addMember(member1); + library.addMember(member2); + } + + @Test + void testLoanBook() throws Exception { + library.loanBook(book1, member1); + assertTrue(book1.isLoaned()); + assertEquals(1, library.getLoans().size()); + } + + @Test + void testLoanBookNotRegistered() { + Member newMember = new Member("New Member", "99999"); + assertThrows(Exception.class, () -> library.loanBook(book2, newMember)); + } + + @Test + void testReturnBook() throws Exception { + library.loanBook(book1, member1); + library.returnBook(book1); + assertFalse(book1.isLoaned()); + assertEquals(0, library.getLoans().size()); + } + + @Test + void testReturnBookNotLoaned() { + assertThrows(Exception.class, () -> library.returnBook(book2)); + } + + @Test + void testAddBook() { + Book newBook = new Book("1984", "George Orwell"); + library.addBook(newBook); + assertTrue(library.getBooks().contains(newBook)); + } + + @Test + void testAddMember() { + Member newMember = new Member("New Member", "99999"); + library.addMember(newMember); + assertTrue(library.getMembers().contains(newMember)); + } +} diff --git a/Assignment_1/Assignment_resources/Junit/tut_ex1/Loan.java b/Assignment_1/Assignment_resources/Junit/tut_ex1/Loan.java new file mode 100644 index 0000000000000000000000000000000000000000..07edb8850cd3c9b553955261493325cf41cb9a4c --- /dev/null +++ b/Assignment_1/Assignment_resources/Junit/tut_ex1/Loan.java @@ -0,0 +1,38 @@ +package LibraryManagement; + +public class Loan { + private Book book; + private Member member; + private java.time.LocalDate loanDate; + + public Loan(Book book, Member member) { + this.book = book; + this.member = member; + this.loanDate = java.time.LocalDate.now(); + } + + // Getters and setters + public Book getBook() { + return book; + } + + public void setBook(Book book) { + this.book = book; + } + + public Member getMember() { + return member; + } + + public void setMember(Member member) { + this.member = member; + } + + public java.time.LocalDate getLoanDate() { + return loanDate; + } + + public void setLoanDate(java.time.LocalDate loanDate) { + this.loanDate = loanDate; + } +} \ No newline at end of file diff --git a/Assignment_1/Assignment_resources/Junit/tut_ex1/Member.java b/Assignment_1/Assignment_resources/Junit/tut_ex1/Member.java new file mode 100644 index 0000000000000000000000000000000000000000..ef2f678ba2c1b276e19687941d134f8657ede2d5 --- /dev/null +++ b/Assignment_1/Assignment_resources/Junit/tut_ex1/Member.java @@ -0,0 +1,36 @@ +package LibraryManagement; + +public class Member { + private String name; + private String id; + + public Member(String name, String id) { + this.name = name; + this.id = id; + } + + // Getters and setters + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Member member = (Member) obj; + return id.equals(member.id); + } +} \ No newline at end of file diff --git a/Assignment_1/Notes.txt b/Assignment_1/Notes.txt deleted file mode 100644 index ab020473a792529619acd0cd71da9f35c39204e5..0000000000000000000000000000000000000000 --- a/Assignment_1/Notes.txt +++ /dev/null @@ -1,5 +0,0 @@ -Going to use either these 3 codes: - -classmate's, -Liam's, -Or Guis' \ No newline at end of file diff --git a/Assignment_1/README.md b/Assignment_1/README.md deleted file mode 100644 index b11b1f23fb2cef950663967732c8585e54df478b..0000000000000000000000000000000000000000 --- a/Assignment_1/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# CPSC 501, W25 -### Devon Harstrom, 30132397 - ---- - -Used code from __ and refactored diff --git a/Assignment_1/Scrap and Notes/Notes.txt b/Assignment_1/Scrap and Notes/Notes.txt new file mode 100644 index 0000000000000000000000000000000000000000..1b7313c8e7283ab38d674b2914fb9a11e2ce7381 --- /dev/null +++ b/Assignment_1/Scrap and Notes/Notes.txt @@ -0,0 +1,48 @@ +Going to use either these 3 codes: + +classmate's, + + + +------------- + + +public void move(int index) {//changes the value of aWorld[][] must set old position to null then update it to the new position. + if (checkPerim(index) == true){ //checks the perim for things not null if it finds something it returns true and the move code below doesn't run + return; + } + if (loc[index] == null) { + return; + } + if (loc[index].getCanMove()==false){ + return; + } + if((loc[index].getRow()-1 != -1)&&(loc[index].getColumn()-1 != -1)) { + if ((loc[index].getType() == 'E')&&(aWorld[loc[index].getRow()-1][loc[index].getColumn()-1]== null)) { + oldRow = loc[index].getRow(); + oldCol = loc[index].getColumn(); + loc[index].setRow(oldRow-1); + loc[index].setColumn(oldCol-1); + aWorld[oldRow-1][oldCol-1] = aWorld[oldRow][oldCol]; + aWorld[oldRow][oldCol] = null; + } + } + if((loc[index].getRow()+1 != 10)&&(loc[index].getColumn()+1 != 10)) { + if ((loc[index].getType() == 'O')&&(aWorld[loc[index].getRow()+1][loc[index].getColumn()+1]== null)) { + oldRow = loc[index].getRow(); + oldCol = loc[index].getColumn(); + loc[index].setRow(oldRow+1); + loc[index].setColumn(oldCol+1); + aWorld[oldRow+1][oldCol+1] = aWorld[oldRow][oldCol]; + aWorld[oldRow][oldCol] = null; + } + } + if ((GameStatus.debugModeOn == false)||(loc[index] == null)) { + return; + } + if (loc[index].getType() == 'E') {//elf move data + status.elfMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); + } else if (loc[index].getType() == 'O') {//orc move data + status.orcMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); + } + } \ No newline at end of file diff --git a/Assignment_1/Scrap and Notes/reworks.java b/Assignment_1/Scrap and Notes/reworks.java new file mode 100644 index 0000000000000000000000000000000000000000..3d5218a843f823299617dcbe183e5c1c66b3f922 --- /dev/null +++ b/Assignment_1/Scrap and Notes/reworks.java @@ -0,0 +1,40 @@ +//public void move(int index) {//changes the value of aWorld[][] must set old position to null then update it to the new position. +// if (checkPerimeter(index) == true){ //checks the perim for things not null if it finds something it returns true and the move code below doesn't run +// return; +// } +// if (loc[index] == null) { +// return; +// } +// if (loc[index].getCanMove()==false){ +// return; +// } +// +// if((loc[index].getRow()-1 != -1)&&(loc[index].getColumn()-1 != -1)) { +// moveLogic('E', index, -1); +// } +// if((loc[index].getRow()+1 != 10)&&(loc[index].getColumn()+1 != 10)) { +// moveLogic('O', index, 1); +// } +// if ((GameStatus.debugModeOn == true)&&(loc[index] != null)) { +// debugMovement(index); +// } +//} +// +//public void debugMovement(int index){ +// if (loc[index].getType() == 'E') {//elf move data +// status.elfMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); +// } else if (loc[index].getType() == 'O') {//orc move data +// status.orcMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); +// } +//} +// +//public void moveLogic(String entityType, int index, int increment){ +// if ((loc[index].getType() == entityType)&&(aWorld[loc[index].getRow()+ increment][loc[index].getColumn()+increment]== null)) { +// oldRow = loc[index].getRow(); +// oldCol = loc[index].getColumn(); +// loc[index].setRow(oldRow+increment); +// loc[index].setColumn(oldCol+increment); +// aWorld[oldRow+increment][oldCol+increment] = aWorld[oldRow][oldCol]; +// aWorld[oldRow][oldCol] = null; +// } +//} \ No newline at end of file diff --git a/Assignment_1/Scrap and Notes/reworks2.java b/Assignment_1/Scrap and Notes/reworks2.java new file mode 100644 index 0000000000000000000000000000000000000000..ca4d02105e9e47b806f1c61919dbc990d0fc5830 --- /dev/null +++ b/Assignment_1/Scrap and Notes/reworks2.java @@ -0,0 +1,62 @@ +//public class Entity +//{ +// public static final char DEFAULT_APPEARANCE = 'X'; +// public static final char EMPTY = ' '; +// public static final int DEFAULT_HP = 1; +// +// private char appearance; +// private int hitPoints; +// private int damage; +// +// public Entity(char appearance, int hitPoints, int damage) { +// this.appearance = appearance; +// this.hitPoints = hitPoints; +// this.damage = damage; +// } +// +// public Entity(char newAppearance) { +// appearance = newAppearance; +// hitPoints = DEFAULT_HP; +// damage = ORC_DAMAGE; +// } +// +// public char getAppearance() +// { +// return(appearance); +// } +// +// public int getHitPoints() +// { +// return(hitPoints); +// } +// +// public void setAppearance(char newAppearance) +// { +// appearance = newAppearance; +// } +// +// public void setHitPoints(int newHitPoints) +// { +// hitPoints = newHitPoints; +// } +//} +// +//public class Orc extends Entity { +// public static final char ORC = 'O'; +// public static final int ORC_DAMAGE = 3; +// public static final int ORC_HP = 10; +// +// public Orc() { +// super(ORC_APPEARANCE, ORC_HP, ORC_DAMAGE); +// } +//} +// +//public class Elf extends Entity { +// public static final char ELF = 'E'; +// public static final int ELF_DAMAGE = 7; +// public static final int ELF_HP = 15; +// +// public Elf() { +// super(ELF_APPEARANCE, ELF_HP, ELF_DAMAGE); +// } +//} \ No newline at end of file diff --git a/Assignment_1/Scrap and Notes/scrap2.txt b/Assignment_1/Scrap and Notes/scrap2.txt new file mode 100644 index 0000000000000000000000000000000000000000..8c98b2b7a47cf4fb96f28eb5e349d9220f1f9981 --- /dev/null +++ b/Assignment_1/Scrap and Notes/scrap2.txt @@ -0,0 +1,114 @@ +import java.io.FileReader; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.util.Scanner; + +/* + Usage: + * Call the static read method to prompt the user for the file containing the + positions. + * The method will prompt for a filename and try to open that the file in the + location where the Driver for your Java program is located. + * The prompt will be repeated if there are problems accessing the file (e.g. + the file is not at the specified location, it is corrupted etc.). + * The method returns a reference to a 2D array of references to objects of type + Entity. + * The appearance attribute of the entity will correspond to the appearance of + characters in the file. + * Starting files must be exactly 10x10 characters in size and consist only of the + 'O' character, an 'E' character or a space. + + Version Feb 16B, 2021 + * Program repeats prompt for filename if file not found at location. + + Version Feb 16A, 2021 + * Program allows the user to specify the input file at runtime. + + Version: Feb 11A: + * Changed references of dwarves to elves. Constants related to the + * Entity attributes deleted. +*/ +public class FileInitialization +{ + public static Entity[][] read() { + Entity [][] temp = new Entity[World.SIZE][World.SIZE]; + String line; // changed to this as it's going to be null regardless + int row, column; // changed from r and c so its more readable + char letter; +// BufferedReader bufferReader = null; +// FileReader fileReader = null; + FileContainer aContainer; // changed to this as it's going to be null regardless + try { + aContainer = checkingFile(); + BufferedReader bufferReader = aContainer.getBufferedReader(); //changed name from br + FileReader fileReader = aContainer.getFileReader(); // changed name from br so its more readable + line = bufferReader.readLine(); + if (line == null) + System.out.println("Empty file, nothing to read"); + row = 0; + while (line != null) { + column = 0; + while(column < World.SIZE) { + letter = line.charAt(column); + temp[row][column] = createEntity(letter); + column = column + 1; + } + line = bufferReader.readLine(); + if (line != null) + row = row + 1; + } + fileReader.close(); + } + catch (FileNotFoundException e) { + String location = System.getProperty("user.dir"); + System.out.println("Input file not in " + location); + } + catch (IOException e) { + System.out.println("General file input problem " + + "reading starting positions"); + } + return(temp); + } + + private static FileContainer checkingFile() + { + FileContainer aContainer; // changed to this as it's going to be null regardless + Scanner in = new Scanner(System.in); + String filename; // changed to this as it's going to be null regardless +// boolean fileFound = false; + while (true) // changed to this as it's still if false + { + try + { + System.out.print("Name of file containing starting positions: "); + filename = in.nextLine(); + FileReader fileReader = new FileReader(filename); + BufferedReader bufferReader = new BufferedReader(fileReader); +// fileFound = true; + aContainer = new FileContainer(bufferReader, fileReader); + return aContainer; + } + catch (FileNotFoundException ex) + { + String location = System.getProperty("user.dir"); + System.out.println("Input file not in " + location); + } + + } +// aContainer = new FileContainer(); +// return(aContainer); + } + + private static Entity createEntity(char letter) { + return switch (letter) { + case 'O' -> new Orc(); + case 'E' -> new Elf(); + case Entity.EMPTY -> null; + default -> { + System.out.println("Error: Invalid character [" + letter + "] in file"); + yield null; // 'yield' is used to return a value from the block + } + }; + } +} \ No newline at end of file diff --git a/Assignment_1/Scrap and Notes/scrap3.txt b/Assignment_1/Scrap and Notes/scrap3.txt new file mode 100644 index 0000000000000000000000000000000000000000..a3a6f9141f52ce719c8b2f4b2c2197460667ac4a --- /dev/null +++ b/Assignment_1/Scrap and Notes/scrap3.txt @@ -0,0 +1,173 @@ +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import javax.swing.JPanel; +import javax.swing.Timer; +// removed unused import + +//Author Ethan McCorquodale + +/* + Version 3/4/2021: I got the graphical portion setup and functional still need to attach it logically + to the World class and have sprites to represent elves/dwarves + Version 3/5/2021: I got the sprites setup correctly, I'm having issues with having them behave correctly though the attack() method + in the World class needs some tweaking I'm pretty sure as it's a logical error that's occurring (orc isn't attacking + elf after it's told to stop moving when the space it's moving to is occupied) + Version 3/9/2021: Finally fixed that weird bug that would cause issues when one entity dies, I did so by adding a sortDead method + at this point the program runs without any bugs and meets the design specifications I just need to add Debug mode now + */ +public class GamePanel extends JPanel implements ActionListener{ + // removed unused variables like system.in + static final int Height = 300; + static final int Width = 300; + static final int gridSize = 30; + static final int elfSize = 15; + final int Delay = 1; + int moveCount = 0; + World world = new World(); + Timer timer; + GamePanel() + { + this.setPreferredSize(new Dimension(Width,Height)); + this.setBackground(Color.blue); + this.setFocusable(true); + this.addKeyListener(new MyKeyAdapter()); + startGame(); + } + + public void startGame() { + timer = new Timer(Delay,this); + } + + public void paintComponent(Graphics graphics) { // changed g to graphics since I don't like single letter variables + super.paintComponent(graphics); + drawWorld(graphics); + } + + public void drawWorld(Graphics graphics) { + for (int row = 0; row<World.SIZE;row++) { //fixed static warning + for (int column = 0; column<World.SIZE; column++) { + if(world.aWorld[row][column] == null) { + graphics.setColor(Color.white); + graphics.fillRect(column*gridSize, row*gridSize, gridSize, gridSize); // removed unnecessary 0+ + graphics.setColor(Color.black); + graphics.drawRect(column*gridSize, row*gridSize, gridSize, gridSize); // removed unnecessary 0+ + } + else if(world.aWorld[row][column].getAppearance() == 'E') { + drawElf(graphics, column*gridSize, row*gridSize); + } else if (world.aWorld[row][column].getAppearance() == 'O') { + drawOrc(graphics,column*gridSize,row*gridSize); + } + } + } + } + public void drawOrc(Graphics graphics, int x, int y) { + + int[] xCoord = {x+6,x+19,x+elfSize/2+5}; + int[] yCoord = {y+10,y+10,y}; + Color orcGreen = new Color(21,158,26); + graphics.setColor(Color.white); + graphics.fillRect(x,y, gridSize, gridSize);//sets the background of that grid square white + graphics.setColor(orcGreen); + graphics.fillOval(x+5, y+7, elfSize, elfSize+3); + + graphics.setColor(Color.black); + graphics.drawOval(x+5, y+7, elfSize, elfSize+3); + graphics.setColor(orcGreen); + graphics.fillOval(x+13,y+9,5,7);//draws right eye + graphics.fillPolygon(xCoord, yCoord, 3);//fills in hat + graphics.setColor(Color.black); + graphics.drawPolygon(xCoord, yCoord, 3);//draws outline for hat + graphics.fillOval(x+7,y+9,5,7);//draws left eye + graphics.fillOval(x+13,y+9,5,7);//draws right eye + + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+10, (x+5)+(elfSize/3), y+elfSize+13);//left foot line 1 + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+13, x+6, y+elfSize+13);//left foot line 2 + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+10, (x+5)+elfSize-(elfSize/3), y+elfSize+13); + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+13, (x+5)+elfSize-(elfSize/3)+5, y+elfSize+13); + graphics.drawRect(x,y, gridSize, gridSize); + graphics.drawString("HP: "+world.aWorld[y/gridSize][x/gridSize].getHitPoints(), x, y); + + } + public void drawElf(Graphics graphics, int x, int y) { + + int[] xCoord = {x+5,x+20,x+elfSize/2+5}; + int[] yCoord = {y+7,y+7,y}; + graphics.setColor(Color.white); + graphics.fillRect(x,y, gridSize, gridSize); + graphics.setColor(Color.green); + graphics.fillRect(x+5, y+7, elfSize, elfSize); + + graphics.fillPolygon(xCoord, yCoord, 3); + graphics.setColor(Color.black); + graphics.drawPolygon(xCoord, yCoord, 3); + graphics.drawRect(x+5, y+7, elfSize, elfSize); + graphics.fillOval(x+7,y+9,5,7); + graphics.fillOval(x+13,y+9,5,7); + + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+7, (x+5)+(elfSize/3), y+elfSize+13); + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+13, x+6, y+elfSize+13); + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+7, (x+5)+elfSize-(elfSize/3), y+elfSize+13); + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+13, (x+5)+elfSize-(elfSize/3)+5, y+elfSize+13); + graphics.drawRect(x,y, gridSize, gridSize); + graphics.drawString("HP: "+world.aWorld[y/gridSize][x/gridSize].getHitPoints(), x, y); + } + @Override + public void actionPerformed(ActionEvent e) { + world.turnSinceAtk++; + for (int i = 0; i<Entity.getElfCounter()+Entity.getOrcCounter()+5; i++) {//the +5 stops the case where when multiple entities die the sortDead() breaks by running this more times than it should it finds all elves/orcs separated by a null + + if (!GameStatus.debugModeOn) { //switched, still checking if false + world.move(i); + } + } + if (GameStatus.debugModeOn) { + world.move(moveCount); + repaint(); + timer.stop(); + } + if(GameStatus.debugModeOn) { + moveCount++; + world.turnSinceAtk = 0;//if this doesn't happen cease fire will be met with each entity only moving one at a time + if(world.location[moveCount]== null) { + System.out.println("Cannot move null");//gives output for when the "buffer" +3 is being called in the debug + } + } + + if (moveCount >= Entity.getOrcCounter()+Entity.getElfCounter()+3) {//the +3 is there for the same reason the +5 is added to the for loop sortDead() sometimes leaves a null between entities and I cant figure out how to fix that so, I use this buffer + moveCount = 0; + } + repaint(); + timer.stop(); + if (world.turnSinceAtk >= 10 ) { + world.programRunning = false; + System.out.println("Cease Fire, Game over."); + } + } + // de-nest + public class MyKeyAdapter extends KeyAdapter{ + public void keyPressed(KeyEvent e) { + switch(e.getKeyCode()) { + case KeyEvent.VK_SPACE: + if(!world.programRunning) { // switched it, but it's still false + timer.start(); + } + break; // else was unnecessary + + case KeyEvent.VK_D: + if(GameStatus.debugModeOn) { + System.out.println("Debug mode left"); + GameStatus.debugModeOn = false; + break; + } + System.out.println("Debug mode entered"); + GameStatus.debugModeOn = true; + break; + } + } + } +} \ No newline at end of file diff --git a/Assignment_1/original_code/assignment3GUI - Classmate's/src/World.java b/Assignment_1/original_code/assignment3GUI - Classmate's/src/World.java index 73af68f4aface3757dda92859c3312950ba0e1ac..d2e0b9701d971deb364aa1516f06eb152c9ef12a 100644 --- a/Assignment_1/original_code/assignment3GUI - Classmate's/src/World.java +++ b/Assignment_1/original_code/assignment3GUI - Classmate's/src/World.java @@ -182,7 +182,7 @@ public class World return false; } public void move(int index) {//changes the value of aWorld[][] must set old position to null then update it to the new position. - if (checkPerim(index) == false) {//checks the perim for things not null if it finds something it returns true and the move code below doesn't run + if (checkPerimiter(index) == false) {//checks the perim for things not null if it finds something it returns true and the move code below doesn't run if (loc[index] != null) { if (loc[index].getCanMove()) { if((loc[index].getRow()-1 != -1)&&(loc[index].getColumn()-1 != -1)) { diff --git a/Assignment_1/original_code/assignment3GUI - Classmate's/src/refactor_1_testOG.java b/Assignment_1/original_code/assignment3GUI - Classmate's/src/refactor_1_testOG.java new file mode 100644 index 0000000000000000000000000000000000000000..49d19d48df56177e6bfa1a08668aaa7b83bd0e68 --- /dev/null +++ b/Assignment_1/original_code/assignment3GUI - Classmate's/src/refactor_1_testOG.java @@ -0,0 +1,116 @@ +import org.junit.jupiter.api.Test; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.*; + +import java.awt.*; +import javax.swing.*; + +class refactor_1_test { + private static World world; + +// @BeforeEach +// void setUp() { +// String simulatedInput = "Assignment_1/original_code/assignment3GUI - Classmate's/src/data.txt"; + //// String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; +// InputStream originalSystemInStream = System.in; // Save the original System.in +// ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); +// System.setIn(simulatedInputStream); // Set System.in to the new input stream +// world = new World(); // This will call FileInitialization.read() and use the simulated input +// System.setIn(originalSystemInStream); // Restore the original System.in +// } + + @Test + void testBoardInitialization() { + String simulatedInput = "Assignment_1/original_code/assignment3GUI - Classmate's/src/data.txt"; +// String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + System.out.println("Test 1 running."); + assertNotNull(world.aWorld, "Board should be initialized"); + assertEquals(World.SIZE, world.aWorld.length, "Board should have correct rows"); + assertEquals(World.SIZE, world.aWorld[0].length, "Board should have correct columns"); + assertEquals(4, world.elfCounter); // check if right amount of elves and orcs + assertEquals(4, world.orcCounter); + } + + @Test + void sortDeadTest(){ + // not sure how to test because the comment just says doesn't work as intended?? + //but works in gui + } + + @Test + void testSort(){ + String simulatedInput = "Assignment_1/original_code/assignment3GUI - Classmate's/src/data.txt"; +// String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + //in data.txt the sorted array should be OOEEOOEE + StringBuilder afterSort = new StringBuilder(); + for (Location location : world.loc) { + afterSort.append(location != null ? location.getType() : ""); + } + assertEquals("OOEEOOEE", afterSort.toString(), "Locations should be sorted like this"); + } + + @Test + void testAttack(){ + // create world and tell them to attack + String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + world.tempRow = 0; + world.tempColumn = 0; + world.attack(0); //elf will attack + int Hp = world.aWorld[world.loc[1].getRow()][world.loc[1].getColumn()].getHitPoints(); + System.out.println("health after attack: " + Hp); + assertTrue(world.aWorld[world.loc[1].getRow()][world.loc[1].getColumn()].getHitPoints() < 10, "Orc should lose HP after attack"); + } + + @Test + void checkPerimTest(){ + String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/perimTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + assertTrue(world.checkPerim(0), "should be true, something next to the elf"); + assertTrue(world.checkPerim(1), "should be true, something next to the orc"); + assertFalse(world.checkPerim(2), "should be false, nothing next to the elf"); + assertFalse(world.checkPerim(3), "should be false, spot is null"); + } + + @Test + void testMove(){ + String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/moveTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + assertEquals('O', world.aWorld[0][0].getAppearance(), "should be an Orc at 0,0"); + System.out.println(world.aWorld[0][0].getAppearance()+ " at: 0:0"); + world.move(1); + assertEquals('O', world.aWorld[1][1].getAppearance(), "should be an Orc"); + System.out.println(world.aWorld[1][1].getAppearance() + " at: 1:1, from move function" ); + + } + + +} \ No newline at end of file diff --git a/Assignment_1/refactored_code/CPSC501A1W25README.md b/Assignment_1/refactored_code/CPSC501A1W25README.md new file mode 100644 index 0000000000000000000000000000000000000000..29cbcb1a429f2613e3c3710f0d16679499e8f553 --- /dev/null +++ b/Assignment_1/refactored_code/CPSC501A1W25README.md @@ -0,0 +1,394 @@ +# CPSC 501, W25, T02 +### Devon Harstrom, 30132397 + +--- + +### Info: +> Used code from Ethan McCorquodale a classmate who gave permission to use this old code +I then refactored it </br> +> The original code is by Ethan McCorquodale. I was given permission to use it: +> “... I give you guys (and anyone else who is in the same situation) permission to use this project.†</br> +All refactoring was tested on: </br> +Assignment_1/refactored_code/assignment3GUI - Classmate's/src/refactorTests.java +which tests the world functions to see if the game runs properly +> other testing was done in the GUI outside of code as it is a GUI based coed + +--- + +#how to run: +> run the java file Simulator.Java in the refactored folder +> enter a txt world file for a basic one enter: +> Assignment_1/refactored_code/assignment3GUI - Classmate's/src/data.txt </br> +> This is the map that the Gui was tested on during the refactoring + +--- + +### Refactors performed: +> Refactor branch 1: De-nesting code in world (major) </br> +Refactor branch 2: Pulling out methods (major) </br> +Refactor branch 3: Rearrange classes? (major) </br> +Refactor branch 4: Name changes, removing unused code, etc. (small) </br> +Refactor branch 5: Comments (small) </br> + +--- + +### Explanation + +##### Refactor 1: +> Refactor 1 was de-nesting code blocks in the world.java file and class +> The original file had lots of nested code which made the readability and understanding +> of the code harder. So as a solution I de-nested this by taking the opposite if statements and returning +> if it was true so for example: +``` +if(((loc[index].getType() == 'O')&&(aWorld[tempRow][tempColumn].getAppearance() != 'O'))||((loc[index].getType() == 'E')&&(aWorld[tempRow][tempColumn].getAppearance() != 'E'))) {//makes sure it's not attacking it's own kind + turnSinceAtk = 0; + if((loc[index].getType() == 'E')&&(aWorld[loc[index].getRow()][loc[index].getColumn()] != null)) { + //... a bunch more if statements + } +------------------------------------AFTER------------------------------------------ +if(aWorld[tempRow][tempColumn] == null){ + return; +} +if((loc[index].getType() == 'O') && (aWorld[tempRow][tempColumn].getAppearance() == 'O')) { + return; +} +if((loc[index].getType() == 'E') && (aWorld[tempRow][tempColumn].getAppearance() == 'E')) { + return; +} +``` +> This makes it much easier to read and understand +> I did this for every function in World which includes +> Constructor, sortDead, sortLocations, attack, checkPerim, move + + +--- + +##### Refactor 2: +> Refactor 2 built off refactor 1 as now that the functions were properly de-nested +> I could separate functions into smaller functions in the World.java file. Adding again to the readability +> split up functions stops some functions from being too long which happened alot in the original code +> For example the move function originally looked like this: +``` +public void move(int index) {//changes the value of aWorld[][] must set old position to null then update it to the new position. + if (checkPerimiter(index) == false) {//checks the perim for things not null if it finds something it returns true and the move code below doesn't run + if (loc[index] != null) { + if (loc[index].getCanMove()) { + if((loc[index].getRow()-1 != -1)&&(loc[index].getColumn()-1 != -1)) { + if ((loc[index].getType() == 'E')&&(aWorld[loc[index].getRow()-1][loc[index].getColumn()-1]== null)) { + oldRow = loc[index].getRow(); + oldCol = loc[index].getColumn(); + loc[index].setRow(oldRow-1); + loc[index].setColumn(oldCol-1); + aWorld[oldRow-1][oldCol-1] = aWorld[oldRow][oldCol]; + aWorld[oldRow][oldCol] = null; + } + } + if((loc[index].getRow()+1 != 10)&&(loc[index].getColumn()+1 != 10)) { + if ((loc[index].getType() == 'O')&&(aWorld[loc[index].getRow()+1][loc[index].getColumn()+1]== null)) { + oldRow = loc[index].getRow(); + oldCol = loc[index].getColumn(); + loc[index].setRow(oldRow+1); + loc[index].setColumn(oldCol+1); + aWorld[oldRow+1][oldCol+1] = aWorld[oldRow][oldCol]; + aWorld[oldRow][oldCol] = null; + } + } + if ((GameStatus.debugModeOn)&&(loc[index]!= null)) { + if (loc[index].getType() == 'E') {//elf move data + status.elfMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); + } else if (loc[index].getType() == 'O') {//orc move data + status.orcMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); + } + } + } + } + } + } +``` +> which is very long and also has some potential repeate code with: +``` +if ((loc[index].getType() == 'E')&&(aWorld[loc[index].getRow()-1][loc[index].getColumn()-1]== null)) { + oldRow = loc[index].getRow(); + oldCol = loc[index].getColumn(); + loc[index].setRow(oldRow-1); + loc[index].setColumn(oldCol-1); + aWorld[oldRow-1][oldCol-1] = aWorld[oldRow][oldCol]; + aWorld[oldRow][oldCol] = null; +} +``` +> so separating functions also allows me to reduce repetitive code, and i can use polymorphism to add to this +> this is what it now look like in refactor 2: +``` +public void moveLogic(char entityType, int index, int increment){ + if ((loc[index].getType() == entityType)&&(aWorld[loc[index].getRow()+ increment][loc[index].getColumn()+increment]== null)) { + oldRow = loc[index].getRow(); + oldCol = loc[index].getColumn(); + loc[index].setRow(oldRow+increment); + loc[index].setColumn(oldCol+increment); + aWorld[oldRow+increment][oldCol+increment] = aWorld[oldRow][oldCol]; + aWorld[oldRow][oldCol] = null; + } +} +``` +> where I can just pass in parameters and have one function. + +--- + +##### Refactor 3: +> Refactor 3 separated the entity class into two subclasses Orcs and Elves +> This allows a better structure in the code and the potential for other entities +> I can also move debug statements to the new subclasses and keep track of entity count +> in the entity class which makes it easier to understand. +> here is an example of the old and new entity class with the elf subclass: +``` +---------------OLD------------------ +//Author Ethan McCorquodale + +/* + Version Feb 17A, 2021 + * Added method so an Entity can damage another Entity. + + Version: Feb 12A, 2021 + * Added attribute to track if Entity has been moved or not. + + Version: Feb 11B, 2021 + * Changed references from dwarves to elves. + * Added a new damage attribute. + +*/ + +public class Entity +{ + public static final char DEFAULT_APPEARANCE = 'X'; + public static final char ELF = 'E'; + public static final char EMPTY = ' '; + public static final char ORC = 'O'; + public static final int DEFAULT_HP = 1; + public static final int ORC_DAMAGE = 3; + public static final int ELF_DAMAGE = 7; + public static final int ORC_HP = 10; + public static final int ELF_HP = 15; + + private char appearance; + private int hitPoints; + private int damage; + + boolean attacking= false; + public Entity() + { + setAppearance(DEFAULT_APPEARANCE); + setHitPoints(DEFAULT_HP); + } + + public Entity(char newAppearance) + { + appearance = newAppearance; + hitPoints = DEFAULT_HP; + damage = ORC_DAMAGE; + } + + public Entity(char newAppearance, int newHitPoints, int newDamage) + { + setAppearance(newAppearance); + setDamage(newDamage); + setHitPoints(newHitPoints); + } + /* public void setAtt(boolean isAttacking) { + attacking = isAttacking; + } + public boolean getAtt() { + return attacking; + }*/ + public char getAppearance() + { + return(appearance); + } + + public int getDamage() + { + return(damage); + } + + public int getHitPoints() + { + return(hitPoints); + } + + public void setAppearance(char newAppearance)//changed this from private to public to change the entity appearance to ' ' + { + appearance = newAppearance; + } + + private void setDamage(int newDamage) + { + if (newDamage < 1) + { + System.out.println("Damage must be 1 or greater"); + } + else + { + damage = newDamage; + } + } + + public void setHitPoints(int newHitPoints) + { + hitPoints = newHitPoints; + } + + +} +---------------NEW------------------ +public abstract class Entity +{ +// public static final char DEFAULT_APPEARANCE = 'X'; + public static final char EMPTY = ' '; +// public static final int DEFAULT_HP = 1; + + private char appearance; + private int hitPoints; + private int damage; + + private static int orcCounter = 0; + private static int elfCounter = 0; + + public Entity(char appearance, int hitPoints, int damage) { + this.appearance = appearance; + this.hitPoints = hitPoints; + this.damage = damage; + } + + public abstract void debugAttack(GameStatus status, int tempRow, int tempColumn, int targetRow, int targetColumn, int HP, int HPAfter); + public abstract void debugLossConditions(GameStatus status, int entityCounter); + + public char getAppearance() + { + return(appearance); + } + + public int getHitPoints() + { + return(hitPoints); + } + + public void setAppearance(char newAppearance) + { + appearance = newAppearance; + } + + public void setHitPoints(int newHitPoints) + { + hitPoints = newHitPoints; + } + + public int getDamage() + { + return(damage); + } + + public static int getElfCounter() { + return elfCounter; + } + + public static int getOrcCounter() { + return orcCounter; + } + + public static void incrementElfCounter(int value) { + elfCounter = elfCounter + value; + } + + public static void incrementOrcCounter(int value) { + orcCounter = orcCounter + value; + } + + public static void setElfCounter(int newElfCounter){ + elfCounter = newElfCounter; + } + + public static void setOrcCounter(int newOrcCounter){ + orcCounter = newOrcCounter; + } +} + +public class Elf extends Entity { + public static final char ELF = 'E'; + public static final int ELF_DAMAGE = 7; + public static final int ELF_HP = 15; + + public Elf() { + super(ELF, ELF_HP, ELF_DAMAGE); + } + + @Override + public void debugAttack(GameStatus status, int tempRow, int tempColumn, int targetRow, int targetColumn, int HP, int HPAfter) { + status.elfAttackDebug(tempRow, tempColumn, targetRow, targetColumn, HP, HPAfter); + } + + @Override + public void debugLossConditions(GameStatus status, int entityCounter){ + status.orcLoseDebug(entityCounter); + } +} +``` +> The same is done what Orc subclass + +--- + +##### Refactor 4: +> refactor 4 was a number of small refactors such as changing names, removing unsless code +> general warning fixing and writing code more consistently +> This was done across all files. Here are some examples: +``` +Name changing: + r,c to rows and columns + i to location index + g to graphics + +Removing unused code: + public static final int ORCS_WIN = 0; + public static final int ELVES_WIN = 1; + public static final int DRAW = 2; + public static final int CEASE_FIRE = 3; + public static final int NOT_OVER = 4; + +Consistent code: + if(GameStatus.debugModeOn == false) to + if(GameStatus.debugModeOn) + + if(world.programRunning != false) { + timer.start(); + break; + }else { + break; + } + to + if(world.programRunning != false) { + timer.start(); + } + break; +``` +> And much more + +--- + +##### Refactor 5 +> Refactor 5 is simply commenting to explain code +> this was mainly done in the world.java file +> This just helps with readability and understanding +> for example: + +``` +// logic for attacking gets health and subtracts the attacking entities damage and sets the new health +and +deathLogic(attackingEntity, index); // check if attacker killed enemy +victoryLogic(attackingEntity); // check if no enemy remains +and much more +``` + +--- + +That is all the Refactoring done on the original code + +--- + diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Elf.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Elf.java new file mode 100644 index 0000000000000000000000000000000000000000..7c2ea671131382214747c6eb07f5217b49faf675 --- /dev/null +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Elf.java @@ -0,0 +1,26 @@ +public class Elf extends Entity { + public static final char ELF = 'E'; // Elves are represented as an E on the grid + public static final int ELF_DAMAGE = 7; // damage + public static final int ELF_HP = 15; // health of each elf + + // elf is a subclass of entity passing in appearance, health and damage + public Elf() { + super(ELF, ELF_HP, ELF_DAMAGE); + } + + // both are the debug statements for attack and loss conditions + @Override + public void debugAttack(GameStatus status, int tempRow, int tempColumn, int targetRow, int targetColumn, int HP, int HPAfter) { + status.elfAttackDebug(tempRow, tempColumn, targetRow, targetColumn, HP, HPAfter); + } + + @Override + public void debugLossConditions(GameStatus status, int entityCounter){ + status.elfLoseDebug(entityCounter); + } + + @Override + public void debugMovement(GameStatus status, int oldRow, int oldCol, int newRow, int newCol){ + status.elfMoveDebug(oldRow,oldCol,newRow,newCol); + } +} \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Entity.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Entity.java index 9e06be4a26a6d177d8e0623c55885d0c96a33990..4859c3400199f41dd7b05a106e30cb43729c07a6 100644 --- a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Entity.java +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Entity.java @@ -1,96 +1,65 @@ -//Author Ethan McCorquodale +public abstract class Entity +{ + //removed unused + public static final char EMPTY = ' '; // this is for null + private char appearance; // appearance is O or E + private int hitPoints; //health + private final int damage; // damage -/* - Version Feb 17A, 2021 - * Added method so an Entity can damage another Entity. + private static int orcCounter = 0; // start at 0 + private static int elfCounter = 0; - Version: Feb 12A, 2021 - * Added attribute to track if Entity has been moved or not. + // subclasses are appearance, health, damage + public Entity(char appearance, int hitPoints, int damage) { + this.appearance = appearance; + this.hitPoints = hitPoints; + this.damage = damage; + } - Version: Feb 11B, 2021 - * Changed references from dwarves to elves. - * Added a new damage attribute. + //debug for each subclass + public abstract void debugAttack(GameStatus status, int tempRow, int tempColumn, int targetRow, int targetColumn, int HP, int HPAfter); + public abstract void debugLossConditions(GameStatus status, int entityCounter); + public abstract void debugMovement(GameStatus status, int oldRow, int oldCol, int newRow, int newCol); -*/ -public class Entity -{ - public static final char DEFAULT_APPEARANCE = 'X'; - public static final char ELF = 'E'; - public static final char EMPTY = ' '; - public static final char ORC = 'O'; - public static final int DEFAULT_HP = 1; - public static final int ORC_DAMAGE = 3; - public static final int ELF_DAMAGE = 7; - public static final int ORC_HP = 10; - public static final int ELF_HP = 15; - - private char appearance; - private int hitPoints; - private int damage; - - boolean attacking= false; - public Entity() + // getter, setter methods, add some more + public char getAppearance() { - setAppearance(DEFAULT_APPEARANCE); - setHitPoints(DEFAULT_HP); + return(appearance); } - - public Entity(char newAppearance) + public int getHitPoints() { - appearance = newAppearance; - hitPoints = DEFAULT_HP; - damage = ORC_DAMAGE; + return(hitPoints); } - - public Entity(char newAppearance, int newHitPoints, int newDamage) + public void setAppearance(char newAppearance) { - setAppearance(newAppearance); - setDamage(newDamage); - setHitPoints(newHitPoints); - } - /* public void setAtt(boolean isAttacking) { - attacking = isAttacking; + appearance = newAppearance; } - public boolean getAtt() { - return attacking; - }*/ - public char getAppearance() + public void setHitPoints(int newHitPoints) { - return(appearance); + hitPoints = newHitPoints; } - public int getDamage() { return(damage); } - - public int getHitPoints() - { - return(hitPoints); + public static int getElfCounter() { + return elfCounter; } - - public void setAppearance(char newAppearance)//changed this from private to public to change the entity appearance to ' ' - { - appearance = newAppearance; + public static int getOrcCounter() { + return orcCounter; } - - private void setDamage(int newDamage) - { - if (newDamage < 1) - { - System.out.println("Damage must be 1 or greater"); - } - else - { - damage = newDamage; - } + public static void incrementElfCounter(int value) { + elfCounter = elfCounter + value; } - - public void setHitPoints(int newHitPoints) - { - hitPoints = newHitPoints; + public static void incrementOrcCounter(int value) { + orcCounter = orcCounter + value; } + public static void setElfCounter(int newElfCounter){ + elfCounter = newElfCounter; + } + public static void setOrcCounter(int newOrcCounter){ + orcCounter = newOrcCounter; + } +} - -} \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileContainer.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileContainer.java index 1b80fc0bc94519170225eb3166c8fe9637c74eb8..df046222d2f69429ad85070d62347cdb44601a20 100644 --- a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileContainer.java +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileContainer.java @@ -3,23 +3,23 @@ import java.io.BufferedReader; public class FileContainer { - private BufferedReader br; - private FileReader fr; + private final BufferedReader bufferReader; //these don't change so made them final + private final FileReader fileReader; public FileContainer(BufferedReader aBufferedReader, FileReader aFileReader) { - br = aBufferedReader; - fr = aFileReader; + bufferReader = aBufferedReader; + fileReader = aFileReader; } public BufferedReader getBufferedReader() { - return(br); + return(bufferReader); } public FileReader getFileReader() { - return(fr); + return(fileReader); } } \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileInitialization.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileInitialization.java index 4ce778ca4c3bee18ba09c554b24ca8034ac433d5..b43ef71aff884d86c5489572d4718ea7584285dc 100644 --- a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileInitialization.java +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/FileInitialization.java @@ -6,7 +6,7 @@ import java.util.Scanner; /* Usage: - * Call the static read method to prompt the user for the file containing the + * Call the static read method to prompt the user for the file containing the positions. * The method will prompt for a filename and try to open that the file in the location where the Driver for your Java program is located. @@ -14,9 +14,9 @@ import java.util.Scanner; the file is not at the specified location, it is corrupted etc.). * The method returns a reference to a 2D array of references to objects of type Entity. - * The appearance attribute of the entity will correspond to the appearance of + * The appearance attribute of the entity will correspond to the appearance of characters in the file. - * Starting files must be exactly 10x10 characters in size and consist only of the + * Starting files must be exactly 10x10 characters in size and consist only of the 'O' character, an 'E' character or a space. Version Feb 16B, 2021 @@ -29,63 +29,64 @@ import java.util.Scanner; * Changed references of dwarves to elves. Constants related to the * Entity attributes deleted. */ + +// this file takes in the txt file and initializes the world which is called in the world class +// this is where we get the values public class FileInitialization { public static Entity[][] read() { Entity [][] temp = new Entity[World.SIZE][World.SIZE]; - String line = null; - int r, c; + String line; // changed to this as it's going to be null regardless + int row, column; // changed from r and c so its more readable char letter; - BufferedReader br = null; - FileReader fr = null; - FileContainer aContainer = null; + FileContainer aContainer; // changed to this as it's going to be null regardless try { - aContainer = checkingFile(br,fr); - br = aContainer.getBufferedReader(); - fr = aContainer.getFileReader(); - line = br.readLine(); + aContainer = checkingFile(); + BufferedReader bufferReader = aContainer.getBufferedReader(); //changed name from br + FileReader fileReader = aContainer.getFileReader(); // changed name from br so its more readable + line = bufferReader.readLine(); if (line == null) System.out.println("Empty file, nothing to read"); - r = 0; - while (line != null) { - c = 0; - while(c < World.SIZE) { - letter = line.charAt(c); - temp[r][c] = createEntity(letter); - c = c + 1; + row = 0; + while (line != null) { + column = 0; + while(column < World.SIZE) { + letter = line.charAt(column); + temp[row][column] = createEntity(letter); + column = column + 1; } - line = br.readLine(); + line = bufferReader.readLine(); if (line != null) - r = r + 1; + row = row + 1; } - fr.close(); + fileReader.close(); } catch (FileNotFoundException e) { String location = System.getProperty("user.dir"); System.out.println("Input file not in " + location); } catch (IOException e) { - System.out.println("General file input problem " + - "reading starting positions"); + System.out.println("General file input problem " + + "reading starting positions"); } return(temp); } - - private static FileContainer checkingFile(BufferedReader br, FileReader fr) + + private static FileContainer checkingFile() { - FileContainer aContainer = null; + FileContainer aContainer; // changed to this as it's going to be null regardless Scanner in = new Scanner(System.in); - String filename = null; - boolean fileFound = false; - while (fileFound == false) + String filename; // changed to this as it's going to be null regardless + while (true) // changed to this as it's still if false { try { System.out.print("Name of file containing starting positions: "); - filename = in.nextLine(); - fr = new FileReader(filename); - br = new BufferedReader(fr); - fileFound = true; + filename = in.nextLine(); + FileReader fileReader = new FileReader(filename); + BufferedReader bufferReader = new BufferedReader(fileReader); + aContainer = new FileContainer(bufferReader, fileReader); + return aContainer; } catch (FileNotFoundException ex) { @@ -93,32 +94,18 @@ public class FileInitialization System.out.println("Input file not in " + location); } - } - aContainer = new FileContainer(br,fr); - return(aContainer); + } } - - - private static Entity createEntity(char letter) - { - Entity e = null; - switch(letter) - { - case Entity.ORC: - e = new Entity(Entity.ORC,Entity.ORC_HP,Entity.ORC_DAMAGE); - break; - - case Entity.ELF: - e = new Entity(Entity.ELF,Entity.ELF_HP,Entity.ELF_DAMAGE); - break; - case Entity.EMPTY: - e = null; - break; - default: - System.out.println("Error: Invalid character [" + letter + - "] in file"); - } - return(e); + private static Entity createEntity(char letter) { + return switch (letter) { + case 'O' -> new Orc(); + case 'E' -> new Elf(); + case Entity.EMPTY -> null; + default -> { + System.out.println("Error: Invalid character [" + letter + "] in file"); + yield null; // 'yield' is used to return a value from the block + } + }; } } \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GamePanel.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GamePanel.java index 5ab0e625b4d52d5deb40697f239b542130ff8129..c1b141c4eaa4c4fc2c1982dc8b2f5062d5016b7e 100644 --- a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GamePanel.java +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GamePanel.java @@ -7,21 +7,21 @@ import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JPanel; import javax.swing.Timer; +// removed unused import - - -import java.util.Scanner; //Author Ethan McCorquodale /* Version 3/4/2021: I got the graphical portion setup and functional still need to attach it logically to the World class and have sprites to represent elves/dwarves - Version 3/5/2021: I got the sprites setuap correctly, I'm having issues with having them behave correctly though the attack() method - in the World class needs some tweaking I'm pretty sure as it's a logical error that's occurring (orc isn't attacking + Version 3/5/2021: I got the sprites setup correctly, I'm having issues with having them behave correctly though the attack() method + in the World class needs some tweaking I'm pretty sure as it's a logical error that's occurring (orc isn't attacking elf after it's told to stop moving when the space it's moving to is occupied) Version 3/9/2021: Finally fixed that weird bug that would cause issues when one entity dies, I did so by adding a sortDead method at this point the program runs without any bugs and meets the design specifications I just need to add Debug mode now */ + +// this file draws the game in the GUI and handles the "space key interaction" public class GamePanel extends JPanel implements ActionListener{ static final int Height = 300; static final int Width = 300; @@ -30,9 +30,8 @@ public class GamePanel extends JPanel implements ActionListener{ final int Delay = 1; int moveCount = 0; World world = new World(); - Scanner in = new Scanner(System.in); Timer timer; - GamePanel() + GamePanel() { this.setPreferredSize(new Dimension(Width,Height)); this.setBackground(Color.blue); @@ -40,91 +39,90 @@ public class GamePanel extends JPanel implements ActionListener{ this.addKeyListener(new MyKeyAdapter()); startGame(); } - + public void startGame() { timer = new Timer(Delay,this); } - - public void paintComponent(Graphics g) { - super.paintComponent(g); - drawWorld(g); + + public void paintComponent(Graphics graphics) { // changed g to graphics since I don't like single letter variables + super.paintComponent(graphics); + drawWorld(graphics); } - - public void drawWorld(Graphics g) { - for (int r = 0; r<world.SIZE;r++) { - - for (int c = 0; c<world.SIZE; c++) { - if(world.aWorld[r][c] == null) { - g.setColor(Color.white); - g.fillRect(0+c*gridSize, 0+r*gridSize, gridSize, gridSize); - g.setColor(Color.black); - g.drawRect(0+c*gridSize, 0+r*gridSize, gridSize, gridSize); - } - else if(world.aWorld[r][c].getAppearance() == 'E') { - drawElf(g, c*gridSize, r*gridSize); - } else if (world.aWorld[r][c].getAppearance() == 'O') { - drawOrc(g,c*gridSize,r*gridSize); + + public void drawWorld(Graphics graphics) { + for (int row = 0; row<World.SIZE;row++) { //fixed static warning + for (int column = 0; column<World.SIZE; column++) { + if(world.aWorld[row][column] == null) { + graphics.setColor(Color.white); + graphics.fillRect(column*gridSize, row*gridSize, gridSize, gridSize); // removed unnecessary 0+ + graphics.setColor(Color.black); + graphics.drawRect(column*gridSize, row*gridSize, gridSize, gridSize); // removed unnecessary 0+ + } + else if(world.aWorld[row][column].getAppearance() == 'E') { + drawElf(graphics, column*gridSize, row*gridSize); + } else if (world.aWorld[row][column].getAppearance() == 'O') { + drawOrc(graphics,column*gridSize,row*gridSize); } } } - } - public void drawOrc(Graphics g, int x, int y) { - + } + public void drawOrc(Graphics graphics, int x, int y) { + int[] xCoord = {x+6,x+19,x+elfSize/2+5}; int[] yCoord = {y+10,y+10,y}; Color orcGreen = new Color(21,158,26); - g.setColor(Color.white); - g.fillRect(x,y, gridSize, gridSize);//sets the background of that grid square white - g.setColor(orcGreen); - g.fillOval(x+5, y+7, elfSize, elfSize+3); - - g.setColor(Color.black); - g.drawOval(x+5, y+7, elfSize, elfSize+3); - g.setColor(orcGreen); - g.fillOval(x+13,y+9,5,7);//draws right eye - g.fillPolygon(xCoord, yCoord, 3);//fills in hat - g.setColor(Color.black); - g.drawPolygon(xCoord, yCoord, 3);//draws outline for hat - g.fillOval(x+7,y+9,5,7);//draws left eye - g.fillOval(x+13,y+9,5,7);//draws right eye - - g.drawLine((x+5)+(elfSize/3), y+elfSize+10, (x+5)+(elfSize/3), y+elfSize+13);//left foot line 1 - g.drawLine((x+5)+(elfSize/3), y+elfSize+13, x+6, y+elfSize+13);//left foot line 2 - g.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+10, (x+5)+elfSize-(elfSize/3), y+elfSize+13); - g.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+13, (x+5)+elfSize-(elfSize/3)+5, y+elfSize+13); - g.drawRect(x,y, gridSize, gridSize); - g.drawString("HP: "+world.aWorld[y/gridSize][x/gridSize].getHitPoints(), x, y); - + graphics.setColor(Color.white); + graphics.fillRect(x,y, gridSize, gridSize);//sets the background of that grid square white + graphics.setColor(orcGreen); + graphics.fillOval(x+5, y+7, elfSize, elfSize+3); + + graphics.setColor(Color.black); + graphics.drawOval(x+5, y+7, elfSize, elfSize+3); + graphics.setColor(orcGreen); + graphics.fillOval(x+13,y+9,5,7);//draws right eye + graphics.fillPolygon(xCoord, yCoord, 3);//fills in hat + graphics.setColor(Color.black); + graphics.drawPolygon(xCoord, yCoord, 3);//draws outline for hat + graphics.fillOval(x+7,y+9,5,7);//draws left eye + graphics.fillOval(x+13,y+9,5,7);//draws right eye + + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+10, (x+5)+(elfSize/3), y+elfSize+13);//left foot line 1 + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+13, x+6, y+elfSize+13);//left foot line 2 + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+10, (x+5)+elfSize-(elfSize/3), y+elfSize+13); + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+13, (x+5)+elfSize-(elfSize/3)+5, y+elfSize+13); + graphics.drawRect(x,y, gridSize, gridSize); + graphics.drawString("HP: "+world.aWorld[y/gridSize][x/gridSize].getHitPoints(), x, y); + } - public void drawElf(Graphics g, int x, int y) { - + public void drawElf(Graphics graphics, int x, int y) { + int[] xCoord = {x+5,x+20,x+elfSize/2+5}; int[] yCoord = {y+7,y+7,y}; - g.setColor(Color.white); - g.fillRect(x,y, gridSize, gridSize); - g.setColor(Color.green); - g.fillRect(x+5, y+7, elfSize, elfSize); - - g.fillPolygon(xCoord, yCoord, 3); - g.setColor(Color.black); - g.drawPolygon(xCoord, yCoord, 3); - g.drawRect(x+5, y+7, elfSize, elfSize); - g.fillOval(x+7,y+9,5,7); - g.fillOval(x+13,y+9,5,7); - - g.drawLine((x+5)+(elfSize/3), y+elfSize+7, (x+5)+(elfSize/3), y+elfSize+13); - g.drawLine((x+5)+(elfSize/3), y+elfSize+13, x+6, y+elfSize+13); - g.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+7, (x+5)+elfSize-(elfSize/3), y+elfSize+13); - g.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+13, (x+5)+elfSize-(elfSize/3)+5, y+elfSize+13); - g.drawRect(x,y, gridSize, gridSize); - g.drawString("HP: "+world.aWorld[y/gridSize][x/gridSize].getHitPoints(), x, y); + graphics.setColor(Color.white); + graphics.fillRect(x,y, gridSize, gridSize); + graphics.setColor(Color.green); + graphics.fillRect(x+5, y+7, elfSize, elfSize); + + graphics.fillPolygon(xCoord, yCoord, 3); + graphics.setColor(Color.black); + graphics.drawPolygon(xCoord, yCoord, 3); + graphics.drawRect(x+5, y+7, elfSize, elfSize); + graphics.fillOval(x+7,y+9,5,7); + graphics.fillOval(x+13,y+9,5,7); + + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+7, (x+5)+(elfSize/3), y+elfSize+13); + graphics.drawLine((x+5)+(elfSize/3), y+elfSize+13, x+6, y+elfSize+13); + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+7, (x+5)+elfSize-(elfSize/3), y+elfSize+13); + graphics.drawLine((x+5)+elfSize-(elfSize/3), y+elfSize+13, (x+5)+elfSize-(elfSize/3)+5, y+elfSize+13); + graphics.drawRect(x,y, gridSize, gridSize); + graphics.drawString("HP: "+world.aWorld[y/gridSize][x/gridSize].getHitPoints(), x, y); } @Override public void actionPerformed(ActionEvent e) { world.turnSinceAtk++; - for (int i = 0; i<world.elfCounter+world.orcCounter+5; i++) {//the +5 stops the case where when multiple entities die the sortDead() breaks by running this more times than it should it finds all elves/orcs separated by a null - - if (GameStatus.debugModeOn == false) { + for (int i = 0; i<Entity.getElfCounter()+Entity.getOrcCounter()+5; i++) {//the +5 stops the case where when multiple entities die the sortDead() breaks by running this more times than it should it finds all elves/orcs separated by a null + + if (!GameStatus.debugModeOn) { //switched, still checking if false world.move(i); } } @@ -135,13 +133,13 @@ public class GamePanel extends JPanel implements ActionListener{ } if(GameStatus.debugModeOn) { moveCount++; - world.turnSinceAtk = 0;//if this doesnt happen cease fire will be met with each entity only moving one at a time - if(world.loc[moveCount]== null) { + world.turnSinceAtk = 0;//if this doesn't happen cease fire will be met with each entity only moving one at a time + if(world.location[moveCount]== null) { System.out.println("Cannot move null");//gives output for when the "buffer" +3 is being called in the debug } } - - if (moveCount >= world.orcCounter+world.elfCounter+3) {//the +3 is there for the same reason the +5 is added to the for loop sortDead() sometimes leaves a null between entities and I cant figure out how to fix that so I use this buffer + + if (moveCount >= Entity.getOrcCounter()+Entity.getElfCounter()+3) {//the +3 is there for the same reason the +5 is added to the for loop sortDead() sometimes leaves a null between entities and I cant figure out how to fix that so, I use this buffer moveCount = 0; } repaint(); @@ -151,16 +149,15 @@ public class GamePanel extends JPanel implements ActionListener{ System.out.println("Cease Fire, Game over."); } } + public class MyKeyAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_SPACE: - if(world.programRunning != false) { + if(world.programRunning) { // switched it, but it's still true // fixed accidentally set to false timer.start(); - break; - }else { - break; } + break; // else was unnecessary case KeyEvent.VK_D: if(GameStatus.debugModeOn) { diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GameStatus.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GameStatus.java index 08e55fee036b502c648be011265b19341114c9af..47159a10c04cff1e01ef8992eaaaba42204f5a82 100644 --- a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GameStatus.java +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/GameStatus.java @@ -4,30 +4,24 @@ public class GameStatus { public static boolean debugModeOn = false; - public static void exampleStatic() - { - System.out.println("Hiss! Pop!"); - } - public void orcAttackDebug(int tempRow, int tempCol, int attRow, int attCol, int hitpoints,int hitpointsAfter) { + public void orcAttackDebug(int tempRow, int tempCol, int attRow, int attCol, int hitPoints,int hitPointsAfter) { System.out.println("<<attack method called orc>>"); System.out.println("Elf Defender r/c: " +tempRow+"/"+tempCol); System.out.println("Orc attackers r/c: "+attRow+"/"+attCol); - System.out.println("Elf defenders hitpoints: "+hitpoints); - System.out.println("Elf defenders hitpoints after attack"+ hitpointsAfter); + System.out.println("Elf defenders hit points: "+hitPoints); + System.out.println("Elf defenders hit points after attack"+ hitPointsAfter); } - public void elfAttackDebug(int tempRow, int tempCol, int attRow, int attCol, int hitpoints, int hitpointsAfter) { + public void elfAttackDebug(int tempRow, int tempCol, int attRow, int attCol, int hitPoints, int hitPointsAfter) { System.out.println("<<attack method called for elf>>"); System.out.println("Orc defender r/c: " +tempRow+"/"+tempCol); System.out.println("Elf attackers r/c: "+attRow+"/"+attCol); - System.out.println("Orc defenders hitpoints: "+hitpoints); - System.out.println("Orc defenders hitpoints after attack"+ hitpointsAfter); + System.out.println("Orc defenders hit points: "+hitPoints); + System.out.println("Orc defenders hit points after attack"+ hitPointsAfter); } public void orcMoveDebug(int oldRow, int oldCol, int currentRow, int currentCol) { System.out.println("<<Orc move called>>"); System.out.println("Orc old r/c: "+oldRow+"/"+oldCol); System.out.println("Moved to r/c: "+currentRow+"/"+ currentCol); - - } public void elfMoveDebug(int oldRow, int oldCol, int currentRow, int currentCol) { System.out.println("<<Elf move called>>"); diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Location.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Location.java index affd51e59d21482cf60723520ca768a476416996..44371f3da2303997bd074f935319c38c0bf9b965 100644 --- a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Location.java +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Location.java @@ -1,4 +1,6 @@ //Author Ethan McCorquodale + +// all information for location such as row column and entity type public class Location { private int row; @@ -12,11 +14,11 @@ public class Location entityType = type; } + // getter setter methods for all the variables public int getColumn() { return(column); } - public int getRow() { return(row); @@ -37,7 +39,6 @@ public class Location { column = newColumn; } - public void setRow(int newRow) { row = newRow; diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Orc.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Orc.java new file mode 100644 index 0000000000000000000000000000000000000000..4fd0c336321541876ec9946b31aa6bb009008813 --- /dev/null +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/Orc.java @@ -0,0 +1,26 @@ +public class Orc extends Entity { + public static final char ORC = 'O'; // orc are represented as an O on the grid + public static final int ORC_DAMAGE = 3; // damage + public static final int ORC_HP = 10; // health for each + + // is a subclass of entity passing in appearance, health, and damage + public Orc() { + super(ORC, ORC_HP, ORC_DAMAGE); + } + + // both are the debug statements for attack and loss conditions + @Override + public void debugAttack(GameStatus status, int tempRow, int tempColumn, int targetRow, int targetColumn, int HP, int HPAfter) { + status.orcAttackDebug(tempRow, tempColumn, targetRow, targetColumn, HP, HPAfter); + } + + @Override + public void debugLossConditions(GameStatus status, int entityCounter){ + status.orcLoseDebug(entityCounter); + } + + @Override + public void debugMovement(GameStatus status, int oldRow, int oldCol, int newRow, int newCol){ + status.orcMoveDebug(oldRow,oldCol,newRow,newCol); + } +} \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/World.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/World.java index 73af68f4aface3757dda92859c3312950ba0e1ac..1057a1c4e4cd943e92e892988fa8e5e9716cb25d 100644 --- a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/World.java +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/World.java @@ -1,28 +1,17 @@ -import java.util.Scanner; //Author Ethan McCorquodale public class World { - public static final int SIZE = 10; - public static final int ORCS_WIN = 0; - public static final int ELVES_WIN = 1; - public static final int DRAW = 2; - public static final int CEASE_FIRE = 3; - public static final int NOT_OVER = 4; - private int i = 0; //creates a local index for the location array in the Display method. - Location[] loc = new Location[100];//an array that'll track each entities type,location coordinates and if it can move. - int elfCounter = 0; - int orcCounter = 0; - int tempRow = 0; + public static final int SIZE = 10; //size of world + private int locationIndex = 0; //creates a local index for the location array in the Display method. // changed from i since that's not a great name + Location[] location = new Location[100];//an array that'll track each entities type,location coordinates and if it can move. + int tempRow = 0; // temp row and colum for switching and swapping things in the world int tempColumn = 0; - int oldRow; + int oldRow; // old row and colum for switching and swapping things in the world int oldCol; - int HP; - int turnSinceAtk; - int HPAfter; - char tempType = ' '; - public Entity [][] aWorld; - public boolean programRunning = true; + int turnSinceAtk; // to help with deciding if a cease fire occurred resets every attack + public Entity [][] aWorld; // world of entities Elf, Orcs or null + public boolean programRunning = true; //programming running GameStatus status = new GameStatus(); // Post-condition: the position of the characters in the @@ -30,190 +19,228 @@ public class World // objects in the array. The type of character in the file // determines the appearance of the Entity(O or D) or if the // element is empty (spaces become null elements) - - public World() - { - aWorld = new Entity[SIZE][SIZE]; - int r; - int c; - for (r = 0; r < SIZE; r++)//creates each object to null - { - for (c = 0; c < SIZE; c++) - { - aWorld[r][c] = null; - } - } - aWorld = FileInitialization.read(); - for (r = 0; r < SIZE; r++) - { - System.out.println(""); - System.out.println(" - "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+ "- "); - for (c = 0; c < SIZE; c++) - { - if (aWorld[r][c] == null) { - System.out.print("| "); - } else { - System.out.print("|"+aWorld[r][c].getAppearance()); - if ((aWorld[r][c].getAppearance() == 'O')||(aWorld[r][c].getAppearance() == 'E')) { - loc[i] = new Location(r,c,aWorld[r][c].getAppearance());//Initializes each Entities location/type - if (loc[i].getType() == 'O') {//this checks the type and counts +1 if it's an orc - orcCounter++; - } else if (loc[i].getType() == 'E') { - elfCounter++; - } - i++;//Cause each initialization of the loc[i] to go to the next element - } - } - } - System.out.print("|"); - - } - System.out.println(); - System.out.println(" - "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+ "- "); - sortLocations(); - } - public void sortDead() {//supposed to try and sort null elements and bring them to the end doesn't always work as intended - for (int i = 0; i<=elfCounter+orcCounter;i++) { - if (loc[i] != null) { - if((aWorld[loc[i].getRow()][loc[i].getColumn()] == null)&&(loc[i+1] != null)) { - loc[i] = loc[i+1]; - loc[i+1] = null; - if(i-2 >= 0) { - if(loc[i-2]== null) { - sortDead(); - } - } - } - } - } - } - public void sortLocations() {//puts the elfs in the beginning of the array, checks if there is an orc before and elf and if there is it swaps positions in the array - for (int j = 0; j<elfCounter+orcCounter; j++) { - if (loc[j+orcCounter] != null) { - if(((loc[j].getType() == 'O')&&(loc[j+orcCounter].getType() == 'E'))) { - tempRow = loc[j+orcCounter].getRow(); - tempColumn = loc[j+orcCounter].getColumn(); - tempType = loc[j+orcCounter].getType(); - loc[j+orcCounter].setType(loc[j].getType()); - loc[j+orcCounter].setColumn(loc[j].getColumn()); - loc[j+orcCounter].setRow(loc[j].getRow()); - loc[j].setRow(tempRow); - loc[j].setColumn(tempColumn); - loc[j].setType(tempType); - } - } - } - } - public void attack(int index) { - if(((loc[index].getType() == 'O')&&(aWorld[tempRow][tempColumn].getAppearance() != 'O'))||((loc[index].getType() == 'E')&&(aWorld[tempRow][tempColumn].getAppearance() != 'E'))) {//makes sure it's not attacking it's own kind - turnSinceAtk = 0; - if((loc[index].getType() == 'E')&&(aWorld[loc[index].getRow()][loc[index].getColumn()] != null)) { - HP = aWorld[tempRow][tempColumn].getHitPoints(); - aWorld[tempRow][tempColumn].setHitPoints(aWorld[tempRow][tempColumn].getHitPoints()-Entity.ELF_DAMAGE); - HPAfter = aWorld[tempRow][tempColumn].getHitPoints(); - if (GameStatus.debugModeOn) {//attack data for elf attackers - status.elfAttackDebug(tempRow, tempColumn, loc[index].getRow(), loc[index].getColumn(),HP,HPAfter); - } - if (aWorld[tempRow][tempColumn].getHitPoints() <=0) { - aWorld[tempRow][tempColumn] = null; - orcCounter--; - sortDead(); - if(loc[index] != null) { - loc[index].setCanMove(true); - } - - if (orcCounter<=0) {//gives the game an end condition if there's no orcs left - System.out.println("Elves win no more orcs left."); - programRunning = false; - if(GameStatus.debugModeOn) {//lose condition data - status.orcLoseDebug(orcCounter); - } - } - } - } else if((loc[index].getType() == 'O')&&(aWorld[loc[index].getRow()][loc[index].getColumn()] != null)) { - HP = aWorld[tempRow][tempColumn].getHitPoints(); - aWorld[tempRow][tempColumn].setHitPoints(aWorld[tempRow][tempColumn].getHitPoints()-Entity.ORC_DAMAGE); - HPAfter = aWorld[tempRow][tempColumn].getHitPoints(); - if (GameStatus.debugModeOn) { - //attack data for Orc attackers - status.orcAttackDebug(tempRow, tempColumn, loc[index].getRow(), loc[index].getColumn(),HP,HPAfter); - } - if (aWorld[tempRow][tempColumn].getHitPoints() <=0) { - aWorld[tempRow][tempColumn] = null; - elfCounter--; - sortDead(); - if(loc[index] != null) { - loc[index].setCanMove(true); - } - if (elfCounter<=0) {//gives the game an end condition if there's no Elves left - System.out.println("Orcs win no more elves left."); - programRunning = false; - if(GameStatus.debugModeOn) {//gives data for lose condition when no elves left standing - status.elfLoseDebug(elfCounter); - } - } - } - } else { - loc[index].setCanMove(true); - } - } else { - loc[index].setCanMove(true); - } - } - public boolean checkPerim(int index) {//looks at all the spaces around the current index of loc[index] looking for things not null - if (loc[index] != null) { - for(int r = loc[index].getRow()-1; r<loc[index].getRow()+2;r++) { - for(int c = loc[index].getColumn()-1; c<loc[index].getColumn()+2;c++ ) { - if((r <= 9)&&(c <= 9)&&(r > - 1)&&(c > -1)) {//ensuring r/c is in aWorld - if((aWorld[r][c] != null) && (loc[index].getType() != aWorld[r][c].getAppearance())) { - - loc[index].setCanMove(false); - tempRow = r; - tempColumn = c; - attack(index); - return true; - } - } - } - } - loc[index].setCanMove(true); - return false; - } - return false; - } - public void move(int index) {//changes the value of aWorld[][] must set old position to null then update it to the new position. - if (checkPerim(index) == false) {//checks the perim for things not null if it finds something it returns true and the move code below doesn't run - if (loc[index] != null) { - if (loc[index].getCanMove()) { - if((loc[index].getRow()-1 != -1)&&(loc[index].getColumn()-1 != -1)) { - if ((loc[index].getType() == 'E')&&(aWorld[loc[index].getRow()-1][loc[index].getColumn()-1]== null)) { - oldRow = loc[index].getRow(); - oldCol = loc[index].getColumn(); - loc[index].setRow(oldRow-1); - loc[index].setColumn(oldCol-1); - aWorld[oldRow-1][oldCol-1] = aWorld[oldRow][oldCol]; - aWorld[oldRow][oldCol] = null; - } - } - if((loc[index].getRow()+1 != 10)&&(loc[index].getColumn()+1 != 10)) { - if ((loc[index].getType() == 'O')&&(aWorld[loc[index].getRow()+1][loc[index].getColumn()+1]== null)) { - oldRow = loc[index].getRow(); - oldCol = loc[index].getColumn(); - loc[index].setRow(oldRow+1); - loc[index].setColumn(oldCol+1); - aWorld[oldRow+1][oldCol+1] = aWorld[oldRow][oldCol]; - aWorld[oldRow][oldCol] = null; - } - } - if ((GameStatus.debugModeOn)&&(loc[index]!= null)) { - if (loc[index].getType() == 'E') {//elf move data - status.elfMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); - } else if (loc[index].getType() == 'O') {//orc move data - status.orcMoveDebug(oldRow,oldCol,loc[index].getRow(),loc[index].getRow()); - } - } - } - } - } - } + + // constructor initialize the grid and the entities location + public World() + { + aWorld = new Entity[SIZE][SIZE]; // world of the size 10 + aWorld = FileInitialization.read(); // get info from the provided txt file + initializeGrid(); + } + + // print borders and initialize locations + public void initializeGrid(){ + for (int row = 0; row < SIZE; row++){ // changed all instances of r and c so its more readable + System.out.println("\n - "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+ "- "); // removed unnecessary print + for (int column = 0; column < SIZE; column++){ + initializeLocations(row, column); + } + System.out.print("|"); + } + System.out.println("\n - "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+"- "+ "- "); + sortLocations(); + } + + // place the Orcs, Elves or nulls entities in the world + public void initializeLocations(int row, int column){ + if (aWorld[row][column] == null) { + System.out.print("| "); + return; // no entity so move on + } + char currentCellAppearance = aWorld[row][column].getAppearance(); + System.out.print("|"+currentCellAppearance); + location[locationIndex] = new Location(row,column,currentCellAppearance);//Initializes each Entities location/type + if (location[locationIndex].getType() == 'O') {//this checks the type and counts +1 if it's an orc + Entity.incrementOrcCounter(1); // increase the count of orcs + } + if (location[locationIndex].getType() == 'E') { + Entity.incrementElfCounter(1); // same with elves + } + locationIndex++;//Cause each initialization of the location[locationIndex] to go to the next element + } + + //supposed to try and sort null elements and bring them to the end doesn't always work as intended + public void sortDead() { + for (int locationIndex = 0; locationIndex<=Entity.getElfCounter()+Entity.getOrcCounter();locationIndex++) { + if (location[locationIndex] == null) { + continue; // its null then move on + } + if((aWorld[location[locationIndex].getRow()][location[locationIndex].getColumn()] != null)||(location[locationIndex+1] == null)) { + continue; // if the element in the world isn't null then move on or next index in location is null + } + // swap the values in the array + location[locationIndex] = location[locationIndex+1]; + location[locationIndex+1] = null; + if(locationIndex-2 >= 0 && location[locationIndex-2]== null) { + sortDead(); + } + } + } + + //puts the elfs in the beginning of the array, + // checks if there is an orc before and elf and if there is it swaps positions in the array + // basically moves all the entities (orc or elves) to the front of the array since we don't care about null entities + public void sortLocations() { + int orcCounter = Entity.getOrcCounter(); + for (int j = 0; j<Entity.getElfCounter()+orcCounter; j++) { + if (location[j+orcCounter] == null){ + continue; // if its null then move on + } + if(((location[j].getType() != 'O')||(location[j+orcCounter].getType() != 'E'))) { + continue; // if it's not an elf or orc move on + } + // get row, columns and entity type (char of O or E) + // and set the new values + tempRow = location[j+orcCounter].getRow(); + tempColumn = location[j+orcCounter].getColumn(); + char tempType = location[j+orcCounter].getType(); + location[j+orcCounter].setType(location[j].getType()); + location[j+orcCounter].setColumn(location[j].getColumn()); + location[j+orcCounter].setRow(location[j].getRow()); + location[j].setRow(tempRow); + location[j].setColumn(tempColumn); + location[j].setType(tempType); + } + } + // handles attacking + public void attack(int index) {// new + if(aWorld[tempRow][tempColumn] == null){ + return; // if null move on + } + if((location[index].getType() == 'O') && (aWorld[tempRow][tempColumn].getAppearance() == 'O')) { + return; // if attacking their own kind move on (won't attack) + } + if((location[index].getType() == 'E') && (aWorld[tempRow][tempColumn].getAppearance() == 'E')) { + return; // if attacking their own kind move on (won't attack) + } + turnSinceAtk = 0; // reset since we are now attacking + if((location[index].getType() == 'E')&&(aWorld[location[index].getRow()][location[index].getColumn()] != null)) { + Entity attackingEntity = new Elf(); //initialize elf which is a subclass of entity and call attack logic + attackLogic(attackingEntity, index); // with elf as the attacking entity + } else if((location[index].getType() == 'O')&&(aWorld[location[index].getRow()][location[index].getColumn()] != null)) { + Entity attackingEntity = new Orc(); // exact same idea just with orc + attackLogic(attackingEntity, index); + } else { + location[index].setCanMove(true); // they can move if they aren't attacking + } + } + + // logic for attacking gets health and subtracts the attacking entities damage and sets the new health + public void attackLogic(Entity attackingEntity, int index){ + int entityDmg = attackingEntity.getDamage(); + int HP = aWorld[tempRow][tempColumn].getHitPoints(); + aWorld[tempRow][tempColumn].setHitPoints(aWorld[tempRow][tempColumn].getHitPoints()-entityDmg); + int HPAfter = aWorld[tempRow][tempColumn].getHitPoints(); + + if (GameStatus.debugModeOn) {//debug attack data for elf attackers, apart of elf subclass + attackingEntity.debugAttack(status, tempRow, tempColumn, location[index].getRow(), location[index].getColumn(),HP,HPAfter); + } + deathLogic(attackingEntity, index); // check if attacker killed enemy + victoryLogic(attackingEntity); // check if no enemy remains + if(GameStatus.debugModeOn) {//debug lose condition data, part of subclasses + if (attackingEntity instanceof Elf) { + attackingEntity.debugLossConditions(status, Entity.getOrcCounter()); + } else if (attackingEntity instanceof Orc) { + attackingEntity.debugLossConditions(status, Entity.getElfCounter()); + } + } + } + + // if the enemy lost all its health it is killed and the count of that entity goes down + public void deathLogic(Entity attackingEntity, int index){ + if (aWorld[tempRow][tempColumn].getHitPoints() >0){ + return; // if they are still alive then move on this doesn't apply + } + aWorld[tempRow][tempColumn] = null; + + if (attackingEntity instanceof Elf) { + Entity.incrementOrcCounter(-1); // increment by a negative value + } else if (attackingEntity instanceof Orc) { + Entity.incrementElfCounter(-1); + } + sortDead(); + if(location[index] != null) { + location[index].setCanMove(true); + } + } + + // if there is no more enemies than the attackers have won so print out who won and end the game + public void victoryLogic(Entity attackingEntity){ + if (attackingEntity instanceof Elf && Entity.getOrcCounter() > 0) { + return; // if there still exists orcs and elves are attacking then no victory + } + else if (attackingEntity instanceof Orc && Entity.getElfCounter() > 0) { + return; // same exact thing but opposite + } + // get the name of the attacking entity since they won + System.out.println(attackingEntity.getClass().getSimpleName() + " wins, no more opponents left."); + programRunning = false; //end game + } + + // check perimeter of the index + public boolean checkPerimeter(int index) {//looks at all the spaces around the current index of location[index] looking for things not null + if (location[index] == null){ + return false; // if null move on + } + for(int row = location[index].getRow()-1; row<location[index].getRow()+2;row++) { + for(int column = location[index].getColumn()-1; column<location[index].getColumn()+2;column++ ) { + if((row > 9)||(column > 9)||(row <= - 1)||(column <= -1)){ //ensuring row/column is in aWorld + continue; // if outside world then move on + } + if((aWorld[row][column] == null) || (location[index].getType() == aWorld[row][column].getAppearance())) { + continue; // if null move on or if they are next to their kind then move on + } + // if we are here then their is an enemy so stop and prepare to attack + location[index].setCanMove(false); + tempRow = row; // set temp row and column + tempColumn = column; + attack(index); // attack with the current index as the attacking entity + return true; // return true there is an enemy + } + } + location[index].setCanMove(true); // no enemy so can move + return false; // no enemy + } + // move the current entity + //changes the value of aWorld[][] must set old position to null then update it to the new position. + public void move(int index) { + if (checkPerimeter(index)){ + return; // check the around current index if there is an enemy dont move + } + if (location[index] == null) { + return; // move on if it's not an entity at index + } + if (!location[index].getCanMove()){ + return; // if it already can't move, move on + } + + if((location[index].getRow()-1 != -1)&&(location[index].getColumn()-1 != -1)) { + moveLogic('E', index, -1); // logic behind moving + } + if((location[index].getRow()+1 != 10)&&(location[index].getColumn()+1 != 10)) { + moveLogic('O', index, 1); + } + if ((GameStatus.debugModeOn)&&(location[index] != null)) { + if (location[index].getType() == 'E') {//elf move data + Entity movingEntity = new Elf(); + movingEntity.debugMovement(status, oldRow,oldCol,location[index].getRow(),location[index].getRow()); + } else if (location[index].getType() == 'O') {//orc move data + Entity movingEntity = new Orc(); + movingEntity.debugMovement(status, oldRow,oldCol,location[index].getRow(),location[index].getRow()); + } + } + } + + // increment the rows and columns setting the entity at the new location and removing the old one + // orcs move down and elves move up, so -1 for elves and +1 for orcs (for increment) + public void moveLogic(char entityType, int index, int increment){ + if ((location[index].getType() == entityType)&&(aWorld[location[index].getRow()+ increment][location[index].getColumn()+increment]== null)) { + oldRow = location[index].getRow(); + oldCol = location[index].getColumn(); + location[index].setRow(oldRow+increment); + location[index].setColumn(oldCol+increment); + aWorld[oldRow+increment][oldCol+increment] = aWorld[oldRow][oldCol]; + aWorld[oldRow][oldCol] = null; + } + } } \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt new file mode 100644 index 0000000000000000000000000000000000000000..d96c03d14207b3325bd553b1bbffed81755c188a --- /dev/null +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt @@ -0,0 +1,10 @@ +O +E + + + + + + + + \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/data2.txt b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/data2.txt new file mode 100644 index 0000000000000000000000000000000000000000..925cdc3deb9c37419592b3e6f2d533332e537d08 --- /dev/null +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/data2.txt @@ -0,0 +1,10 @@ + + O O + E + E + O + OE E + E + E + + \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/moveTest.txt b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/moveTest.txt new file mode 100644 index 0000000000000000000000000000000000000000..786c2c2df2ab8d3684da1ed68c650da86cae9ad2 --- /dev/null +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/moveTest.txt @@ -0,0 +1,10 @@ +O + + + + + + + + +E \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/perimTest.txt b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/perimTest.txt new file mode 100644 index 0000000000000000000000000000000000000000..87316a98e36a52f81e694759f4ade63e23dbd6f2 --- /dev/null +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/perimTest.txt @@ -0,0 +1,10 @@ +O +E + + + + + O + + + \ No newline at end of file diff --git a/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/refactorTests.java b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/refactorTests.java new file mode 100644 index 0000000000000000000000000000000000000000..2df6806ccd776f0070e27892356433394b887468 --- /dev/null +++ b/Assignment_1/refactored_code/assignment3GUI - Classmate's/src/refactorTests.java @@ -0,0 +1,163 @@ +import org.junit.jupiter.api.Test; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.*; + +class refactorTests { + private static World world; + + @BeforeEach + void setUp() { + Entity.setOrcCounter(0); + Entity.setElfCounter(0); + } +// String simulatedInput = "Assignment_1/original_code/assignment3GUI - Classmate's/src/data.txt"; +//// String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; +// InputStream originalSystemInStream = System.in; // Save the original System.in +// ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); +// System.setIn(simulatedInputStream); // Set System.in to the new input stream +// world = new World(); // This will call FileInitialization.read() and use the simulated input +// System.setIn(originalSystemInStream); // Restore the original System.in + + + @Test + void testBoardInitialization() { + String simulatedInput = "Assignment_1/original_code/assignment3GUI - Classmate's/src/data.txt"; +// String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; + // note chatgpt helped with the system.in portion as i wasn't sure how to enter into terminal though code + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + System.out.println("Test 1 running."); + assertNotNull(world.aWorld, "Board should be initialized"); + assertEquals(World.SIZE, world.aWorld.length, "Board should have correct rows"); + assertEquals(World.SIZE, world.aWorld[0].length, "Board should have correct columns"); + assertEquals(4, Entity.getElfCounter()); // check if right amount of elves and orcs + assertEquals(4, Entity.getOrcCounter()); + } + + @Test + void sortDeadTest(){ + String simulatedInput = "Assignment_1/original_code/assignment3GUI - Classmate's/src/data.txt"; +// String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; + // note chatgpt helped with the system.in portion as i wasn't sure how to enter into terminal though code + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + world.sortDead(); + assertNull(world.location[world.location.length - 1]); + assertNull(world.location[world.location.length - 2]); + assertNull(world.location[world.location.length - 20]); + System.out.println(world.location[world.location.length - 20]); + } + + @Test + void testSort(){ + String simulatedInput = "Assignment_1/original_code/assignment3GUI - Classmate's/src/data.txt"; +// String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + //in data.txt the sorted array should be OOEEOOEE + StringBuilder afterSort = new StringBuilder(); + for (Location location : world.location) { + afterSort.append(location != null ? location.getType() : ""); + } + assertEquals("OOEEOOEE", afterSort.toString(), "Locations should be sorted like this"); + } + + @Test + void testAttack(){ + // create world and tell them to attack + String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/attackTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + world.tempRow = 0; + world.tempColumn = 0; + world.attack(0); //elf will attack + int Hp = world.aWorld[world.location[1].getRow()][world.location[1].getColumn()].getHitPoints(); + System.out.println("health after attack: " + Hp); + assertTrue(world.aWorld[world.location[1].getRow()][world.location[1].getColumn()].getHitPoints() < 10, "Orc should lose HP after attack"); + } + + @Test + void checkPerimeterTest(){ + String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/perimTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + assertTrue(world.checkPerimeter(0), "should be true, something next to the elf"); + assertTrue(world.checkPerimeter(1), "should be true, something next to the orc"); + assertFalse(world.checkPerimeter(2), "should be false, nothing next to the elf"); + assertFalse(world.checkPerimeter(3), "should be false, spot is null"); + } + + @Test + void testMove(){ + String simulatedInput = "Assignment_1/refactored_code/assignment3GUI - Classmate's/src/moveTest.txt\n"; + InputStream originalSystemInStream = System.in; // Save the original System.in + ByteArrayInputStream simulatedInputStream = new ByteArrayInputStream(simulatedInput.getBytes()); + System.setIn(simulatedInputStream); // Set System.in to the new input stream + world = new World(); // This will call FileInitialization.read() and use the simulated input + System.setIn(originalSystemInStream); // Restore the original System.in + + assertEquals('O', world.aWorld[0][0].getAppearance(), "should be an Orc at 0,0"); + System.out.println(world.aWorld[0][0].getAppearance()+ " at: 0:0"); + world.move(1); + assertEquals('O', world.aWorld[1][1].getAppearance(), "should be an Orc"); + System.out.println(world.aWorld[1][1].getAppearance() + " at: 1:1, from move function" ); + + } + + @Test + void testEntities(){ + Entity testElfEntity = new Elf(); + Entity testOrcEntity = new Orc(); + + assertNotNull(testElfEntity, "should not be null"); + assertNotNull(testOrcEntity, "should not be null"); + + assertEquals('E', testElfEntity.getAppearance()); + assertEquals('O', testOrcEntity.getAppearance()); + + assertEquals(7, testElfEntity.getDamage()); + assertEquals(3, testOrcEntity.getDamage()); + + assertEquals(15, testElfEntity.getHitPoints()); + assertEquals(10, testOrcEntity.getHitPoints()); + } + + @Test + void testEntityCounters() { + assertEquals(0, Entity.getElfCounter()); + assertEquals(0, Entity.getOrcCounter()); + + Entity.incrementElfCounter(5); + Entity.incrementOrcCounter(-1); + + assertEquals(5, Entity.getElfCounter()); + assertEquals(-1, Entity.getOrcCounter()); + + Entity.setElfCounter(0); + Entity.setOrcCounter(0); + + assertEquals(0, Entity.getElfCounter()); + assertEquals(0, Entity.getOrcCounter()); + } +} \ No newline at end of file diff --git a/out/production/501-gitlab/Elf.class b/out/production/501-gitlab/Elf.class new file mode 100644 index 0000000000000000000000000000000000000000..3655c02272a71c5542574dbe175e7519266afd86 Binary files /dev/null and b/out/production/501-gitlab/Elf.class differ diff --git a/out/production/501-gitlab/Entity.class b/out/production/501-gitlab/Entity.class new file mode 100644 index 0000000000000000000000000000000000000000..8864418cb1a2e33629521eace40214c29ee345ae Binary files /dev/null and b/out/production/501-gitlab/Entity.class differ diff --git a/out/production/501-gitlab/FileContainer.class b/out/production/501-gitlab/FileContainer.class new file mode 100644 index 0000000000000000000000000000000000000000..f096f1d884e892b9940c712a078c0cb2ea8bbb60 Binary files /dev/null and b/out/production/501-gitlab/FileContainer.class differ diff --git a/out/production/501-gitlab/FileInitialization.class b/out/production/501-gitlab/FileInitialization.class new file mode 100644 index 0000000000000000000000000000000000000000..da5d0ff30627fe6a372cb4e1ba2556772f4690f0 Binary files /dev/null and b/out/production/501-gitlab/FileInitialization.class differ diff --git a/out/production/501-gitlab/GameFrame.class b/out/production/501-gitlab/GameFrame.class new file mode 100644 index 0000000000000000000000000000000000000000..3144fff124a737ed4b425d223ca68a638f4aac0a Binary files /dev/null and b/out/production/501-gitlab/GameFrame.class differ diff --git a/out/production/501-gitlab/GamePanel$MyKeyAdapter.class b/out/production/501-gitlab/GamePanel$MyKeyAdapter.class new file mode 100644 index 0000000000000000000000000000000000000000..5352d307ceb607c8ff71bf7632daa0768037370a Binary files /dev/null and b/out/production/501-gitlab/GamePanel$MyKeyAdapter.class differ diff --git a/out/production/501-gitlab/GamePanel.class b/out/production/501-gitlab/GamePanel.class new file mode 100644 index 0000000000000000000000000000000000000000..d5ae57f40a5565f407a063935c7b3ee1ed96f7a9 Binary files /dev/null and b/out/production/501-gitlab/GamePanel.class differ diff --git a/out/production/501-gitlab/GameStatus.class b/out/production/501-gitlab/GameStatus.class new file mode 100644 index 0000000000000000000000000000000000000000..6c8bb193a70f1fe646252dce86ab10c622fdceaf Binary files /dev/null and b/out/production/501-gitlab/GameStatus.class differ diff --git a/out/production/501-gitlab/Location.class b/out/production/501-gitlab/Location.class new file mode 100644 index 0000000000000000000000000000000000000000..4b1b5b63ea49b304e85f6edcd9e3f00f3276f3e9 Binary files /dev/null and b/out/production/501-gitlab/Location.class differ diff --git a/out/production/501-gitlab/Orc.class b/out/production/501-gitlab/Orc.class new file mode 100644 index 0000000000000000000000000000000000000000..5a83a7671771031d497e23f09b37619b30690307 Binary files /dev/null and b/out/production/501-gitlab/Orc.class differ diff --git a/out/production/501-gitlab/Simulator.class b/out/production/501-gitlab/Simulator.class new file mode 100644 index 0000000000000000000000000000000000000000..04efb73c6b631a816f41e51649085a1e51f9e344 Binary files /dev/null and b/out/production/501-gitlab/Simulator.class differ diff --git a/out/production/501-gitlab/World.class b/out/production/501-gitlab/World.class new file mode 100644 index 0000000000000000000000000000000000000000..5fb1ed8a7e620a133b7497b4b1d38f1ef15457ab Binary files /dev/null and b/out/production/501-gitlab/World.class differ diff --git a/out/production/501-gitlab/attackTest.txt b/out/production/501-gitlab/attackTest.txt new file mode 100644 index 0000000000000000000000000000000000000000..d96c03d14207b3325bd553b1bbffed81755c188a --- /dev/null +++ b/out/production/501-gitlab/attackTest.txt @@ -0,0 +1,10 @@ +O +E + + + + + + + + \ No newline at end of file diff --git a/out/production/501-gitlab/data.txt b/out/production/501-gitlab/data.txt new file mode 100644 index 0000000000000000000000000000000000000000..58bedd626f2ca6881a97d6def66eb967735c88b0 --- /dev/null +++ b/out/production/501-gitlab/data.txt @@ -0,0 +1,10 @@ + + O O + E + E + O + O + E + E + + \ No newline at end of file diff --git a/out/production/501-gitlab/data2.txt b/out/production/501-gitlab/data2.txt new file mode 100644 index 0000000000000000000000000000000000000000..925cdc3deb9c37419592b3e6f2d533332e537d08 --- /dev/null +++ b/out/production/501-gitlab/data2.txt @@ -0,0 +1,10 @@ + + O O + E + E + O + OE E + E + E + + \ No newline at end of file diff --git a/out/production/501-gitlab/moveTest.txt b/out/production/501-gitlab/moveTest.txt new file mode 100644 index 0000000000000000000000000000000000000000..786c2c2df2ab8d3684da1ed68c650da86cae9ad2 --- /dev/null +++ b/out/production/501-gitlab/moveTest.txt @@ -0,0 +1,10 @@ +O + + + + + + + + +E \ No newline at end of file diff --git a/out/production/501-gitlab/perimTest.txt b/out/production/501-gitlab/perimTest.txt new file mode 100644 index 0000000000000000000000000000000000000000..87316a98e36a52f81e694759f4ade63e23dbd6f2 --- /dev/null +++ b/out/production/501-gitlab/perimTest.txt @@ -0,0 +1,10 @@ +O +E + + + + + O + + + \ No newline at end of file diff --git a/out/production/501-gitlab/refactor_1_test.class b/out/production/501-gitlab/refactor_1_test.class new file mode 100644 index 0000000000000000000000000000000000000000..729327417910f95ce79c3d0e9f9ab5e8066d5785 Binary files /dev/null and b/out/production/501-gitlab/refactor_1_test.class differ