Compare commits
	
		
			4 Commits
		
	
	
		
			c585c54
			...
			a1e36ccbc7
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						a1e36ccbc7
	
				 | 
					
					
						|||
| 
						
						
							
						
						e983c5aa13
	
				 | 
					
					
						|||
| 
						
						
							
						
						498529f29a
	
				 | 
					
					
						|||
| 
						
						
							
						
						e06abe60de
	
				 | 
					
					
						
							
								
								
									
										20
									
								
								app/src/main/java/school_project/Map.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								app/src/main/java/school_project/Map.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
public class Map extends Shape{
 | 
			
		||||
    private ArrayList<Piece> pieces;
 | 
			
		||||
 | 
			
		||||
    public Map(boolean[][] matrix) {
 | 
			
		||||
        super(matrix);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Map() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: 2/27/23 Tests for Map 
 | 
			
		||||
    public void AddShape(Piece piece){
 | 
			
		||||
        pieces.add(piece);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								app/src/main/java/school_project/MapParser.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								app/src/main/java/school_project/MapParser.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,44 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.lang.reflect.Array;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
 | 
			
		||||
public class MapParser {
 | 
			
		||||
    public static Map ParseMapFile(File file) throws IllegalArgumentException, IllegalAccessException, IOException {
 | 
			
		||||
        System.out.println(file.getAbsolutePath());
 | 
			
		||||
        FileInputStream fileStream = new FileInputStream(file);
 | 
			
		||||
        if(!file.isFile()) throw new IllegalArgumentException("The argument should be a file");
 | 
			
		||||
        if(!file.canRead()) throw new IllegalAccessException("This file can't be read");
 | 
			
		||||
 | 
			
		||||
        byte[] bytes = fileStream.readAllBytes();
 | 
			
		||||
        int start_position = 0, end_position = 0;
 | 
			
		||||
        for (int i = 0; i < bytes.length; i++) {
 | 
			
		||||
            if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 83){ // SMS
 | 
			
		||||
                start_position = i+3;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (int i = start_position; i < bytes.length; i++) {
 | 
			
		||||
            if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 69){ // SME
 | 
			
		||||
                end_position = i;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        byte[] map_data = Arrays.copyOfRange(bytes, start_position, end_position); //TODO tonitch cursor
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        fileStream.close();
 | 
			
		||||
        return new Map(); //TODO: Send the parsed map
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    public static void SaveMapFile(File file){
 | 
			
		||||
//    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) throws IOException, IllegalAccessException {
 | 
			
		||||
        ParseMapFile(new File("test.smap"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								app/src/main/java/school_project/Piece.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								app/src/main/java/school_project/Piece.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
public class Piece extends Shape{
 | 
			
		||||
 | 
			
		||||
    private int x,y; // Position in the Map Object
 | 
			
		||||
    public Piece() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Piece(boolean[][] matrix) {
 | 
			
		||||
        super(matrix);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Rotate the matrix of the piece. Used when the player right click
 | 
			
		||||
     *
 | 
			
		||||
     * @param times Set the amount of time the rotation should be executed. Should be set between 1 and 3.
 | 
			
		||||
     */
 | 
			
		||||
    public void RotateRight(int times){
 | 
			
		||||
        while(times > 0) {
 | 
			
		||||
            boolean[][] temp_matrix = new boolean[width][height];
 | 
			
		||||
            for (int i = 0; i < width; i++) {
 | 
			
		||||
                for (int j = 0; j < height; j++) {
 | 
			
		||||
                    temp_matrix[i][j] = matrix[-j+height-1][i];
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            times--;
 | 
			
		||||
            matrix = temp_matrix;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										40
									
								
								app/src/main/java/school_project/Shape.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								app/src/main/java/school_project/Shape.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,40 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public class Shape {
 | 
			
		||||
 | 
			
		||||
    protected boolean[][] matrix;
 | 
			
		||||
    protected int height, width;
 | 
			
		||||
 | 
			
		||||
    public Shape() {
 | 
			
		||||
        matrix = new boolean[0][0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Shape(boolean[][] matrix){
 | 
			
		||||
        this.setShape(matrix);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setShape(boolean[][] matrix) throws IllegalArgumentException{
 | 
			
		||||
        height = matrix.length;
 | 
			
		||||
        width = matrix[0].length;
 | 
			
		||||
 | 
			
		||||
        for (boolean[] row: matrix){
 | 
			
		||||
            if(row.length != width){
 | 
			
		||||
                throw new IllegalArgumentException("The argument should be a square matrix");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        this.matrix = matrix;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getHeight() {
 | 
			
		||||
        return height;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getWidth() {
 | 
			
		||||
        return width;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean[][] getShape() {
 | 
			
		||||
        return matrix;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										60
									
								
								app/src/test/java/school_project/PieceTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								app/src/test/java/school_project/PieceTest.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
 | 
			
		||||
class PieceTest {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void rotateRight() {
 | 
			
		||||
        boolean[][] piece1_matrix = {
 | 
			
		||||
            {true, false, false},
 | 
			
		||||
            {false, true, false},
 | 
			
		||||
            {true, true, false},
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        boolean[][] piece1_matrix_result = {
 | 
			
		||||
                {true, false, true},
 | 
			
		||||
                {true, true, false},
 | 
			
		||||
                {false, false, false},
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        boolean[][] piece2_matrix = {
 | 
			
		||||
                {true},
 | 
			
		||||
                {false},
 | 
			
		||||
                {true},
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        boolean[][] piece2_matrix_result = {
 | 
			
		||||
                {true, false, true},
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        boolean[][] piece3_matrix_result = {
 | 
			
		||||
                {false, false, false},
 | 
			
		||||
                {false, true, true},
 | 
			
		||||
                {true, false, true},
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        Piece piece1 = new Piece();
 | 
			
		||||
        piece1.setShape(piece1_matrix);
 | 
			
		||||
 | 
			
		||||
        Piece piece2 = new Piece(piece2_matrix);
 | 
			
		||||
 | 
			
		||||
        Piece piece3 = new Piece(piece1_matrix);
 | 
			
		||||
 | 
			
		||||
        Piece piece4 = new Piece(piece1_matrix);
 | 
			
		||||
 | 
			
		||||
        piece1.RotateRight(1);
 | 
			
		||||
        assertArrayEquals(piece1_matrix_result, piece1.getShape());
 | 
			
		||||
 | 
			
		||||
        piece2.RotateRight(1);
 | 
			
		||||
        assertArrayEquals(piece2_matrix_result, piece2.getShape());
 | 
			
		||||
 | 
			
		||||
        piece3.RotateRight(3);
 | 
			
		||||
        assertArrayEquals(piece3_matrix_result, piece3.getShape());
 | 
			
		||||
 | 
			
		||||
        piece4.RotateRight(3);
 | 
			
		||||
        assertNotEquals(piece1_matrix_result, piece4.getShape());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								app/src/test/java/school_project/ShapeTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								app/src/test/java/school_project/ShapeTest.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,48 @@
 | 
			
		||||
package school_project;
 | 
			
		||||
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
 | 
			
		||||
class ShapeTest {
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void setShape() {
 | 
			
		||||
        boolean[][] matrix_shape1 = {
 | 
			
		||||
                {true, false},
 | 
			
		||||
                {true, true},
 | 
			
		||||
                {true, false}
 | 
			
		||||
        };
 | 
			
		||||
        boolean[][] matrix_shape2 = {
 | 
			
		||||
                {true, false, true},
 | 
			
		||||
                {true, true, true},
 | 
			
		||||
                {true, false, true},
 | 
			
		||||
        };
 | 
			
		||||
        boolean[][] matrix_shape3 = {
 | 
			
		||||
                {true, false, true},
 | 
			
		||||
                {true, true}
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        boolean[][] matrix_shape4 = {
 | 
			
		||||
                {true},
 | 
			
		||||
                {false},
 | 
			
		||||
                {true}
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        Shape shape1 = new Shape();
 | 
			
		||||
        shape1.setShape(matrix_shape1);
 | 
			
		||||
        assertEquals(3, shape1.getHeight());
 | 
			
		||||
        assertEquals(2, shape1.getWidth());
 | 
			
		||||
 | 
			
		||||
        Shape shape2 = new Shape(matrix_shape2);
 | 
			
		||||
        assertEquals(3, shape2.getHeight());
 | 
			
		||||
        assertEquals(3, shape2.getWidth());
 | 
			
		||||
 | 
			
		||||
        assertThrows(IllegalArgumentException.class, () -> new Shape(matrix_shape3));
 | 
			
		||||
 | 
			
		||||
        Shape shape4 = new Shape(matrix_shape4);
 | 
			
		||||
        assertEquals(3, shape4.getHeight());
 | 
			
		||||
        assertEquals(1, shape4.getWidth());
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user