diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/se/liu/gusso230/calendar/Appointment.java | 29 | ||||
| -rw-r--r-- | src/se/liu/gusso230/calendar/Cal.java | 63 | ||||
| -rw-r--r-- | src/se/liu/gusso230/calendar/Month.java | 86 | ||||
| -rw-r--r-- | src/se/liu/gusso230/calendar/SimpleDate.java | 29 | ||||
| -rw-r--r-- | src/se/liu/gusso230/calendar/TimePoint.java | 23 | ||||
| -rw-r--r-- | src/se/liu/gusso230/calendar/TimeSpan.java | 23 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/Board.java | 80 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/BoardTester.java | 20 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/BoardToTextConverter.java | 40 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/Point.java | 25 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/Poly.java | 42 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/SquareType.java | 14 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/TetrisViewer.java | 23 | ||||
| -rw-r--r-- | src/se/liu/gusso230/tetris/TetrominoMaker.java | 76 |
14 files changed, 573 insertions, 0 deletions
diff --git a/src/se/liu/gusso230/calendar/Appointment.java b/src/se/liu/gusso230/calendar/Appointment.java new file mode 100644 index 0000000..c0551ca --- /dev/null +++ b/src/se/liu/gusso230/calendar/Appointment.java @@ -0,0 +1,29 @@ +package se.liu.gusso230.calendar; + +public class Appointment { + private String subject; + private SimpleDate date; + private TimeSpan timeSpan; + + public Appointment(final String subject, final SimpleDate date, final TimeSpan timeSpan) { + this.subject = subject; + this.date = date; + this.timeSpan = timeSpan; + } + + public String getSubject() { + return subject; + } + + public SimpleDate getDate() { + return date; + } + + public TimeSpan getTimeSpan() { + return timeSpan; + } + + @Override public String toString() { + return getDate() + " " + getTimeSpan() + ": " + getSubject(); + } +} diff --git a/src/se/liu/gusso230/calendar/Cal.java b/src/se/liu/gusso230/calendar/Cal.java new file mode 100644 index 0000000..3f0856e --- /dev/null +++ b/src/se/liu/gusso230/calendar/Cal.java @@ -0,0 +1,63 @@ +package se.liu.gusso230.calendar; + +import java.util.ArrayList; +import java.util.List; + +public class Cal { + private List<Appointment> appointments; + + public static void main(String[] args) { + Cal cal = new Cal(); + cal.book(2021, "January", 26, 12, 15, 13, 00, "Lunch"); + cal.book(2021, "February", 26, 12, 10, 12, 50, "Lunch again"); + cal.book(2021, "March", 28, 12, 20, 12, 30, "Lunch"); + cal.book(2021, "March", 28, 12, 20, 12, 30, "Lunch one more time"); + cal.book(2020, "December", 31, 23, 50, 23, 59, "New Year's countdown"); + cal.show(); + } + + public Cal() { + this.appointments = new ArrayList<>(); + } + + public void show() { + for (Appointment appointment : appointments) { + System.out.println(appointment); + } + } + + public void book(int year, String monthString, int day, int startHour, int startMinute, + int endHour, int endMinute, String subject) { + if (year <= 1970) { + throw new IllegalArgumentException("Invalid year. Needs to be after 1970."); + } + if (startHour < 0 || startHour > 23) { + throw new IllegalArgumentException("Invalid startHour"); + } + if (endHour < 0 || endHour > 23) { + throw new IllegalArgumentException("Invalid endHour"); + } + if (startMinute < 0 || startMinute > 59) { + throw new IllegalArgumentException("Invalid startMinute"); + } + if (endMinute < 0 || endMinute > 59) { + throw new IllegalArgumentException("Invalid endMinute"); + } + + int monthNumber = Month.getMonthNumber(monthString); + int monthDays = Month.getMonthDays(monthString); + if (monthNumber == -1 || monthDays == -1) { + throw new IllegalArgumentException("Invalid month"); + } + if (day > monthDays) { + throw new IllegalArgumentException("Given day doesn't fit in given month"); + } + Month month = new Month(monthString, monthNumber, monthDays); + + SimpleDate simpleDate = new SimpleDate(year, month, day); + TimePoint start = new TimePoint(startHour, startMinute); + TimePoint end = new TimePoint(endHour, endMinute); + TimeSpan timeSpan = new TimeSpan(start, end); + appointments.add(new Appointment(subject, simpleDate, timeSpan)); + } +} diff --git a/src/se/liu/gusso230/calendar/Month.java b/src/se/liu/gusso230/calendar/Month.java new file mode 100644 index 0000000..a0224c1 --- /dev/null +++ b/src/se/liu/gusso230/calendar/Month.java @@ -0,0 +1,86 @@ +package se.liu.gusso230.calendar; + +public class Month { + private String name; + private int number; + private int days; + + public Month(String name, int number, int days) { + this.name = name; + this.number = number; + this.days = days; + } + + public String getName() { + return name; + } + + public int getNumber() { + return number; + } + + public int getDays() { + return days; + } + + static int getMonthNumber(String name) { + switch (name) { + case "January": + return 1; + case "February": + return 2; + case "March": + return 3; + case "April": + return 4; + case "May": + return 5; + case "June": + return 6; + case "July": + return 7; + case "August": + return 8; + case "September": + return 9; + case "October": + return 10; + case "November": + return 11; + case "December": + return 12; + default: + return -1; + } + } + static int getMonthDays(String name) { + switch (name) { + case "January": + return 31; + case "February": + return 28; + case "March": + return 31; + case "April": + return 30; + case "May": + return 31; + case "June": + return 30; + case "July": + return 31; + case "August": + return 31; + case "September": + return 30; + case "October": + return 31; + case "November": + return 30; + case "December": + return 31; + default: + return -1; + } + } +} diff --git a/src/se/liu/gusso230/calendar/SimpleDate.java b/src/se/liu/gusso230/calendar/SimpleDate.java new file mode 100644 index 0000000..f769096 --- /dev/null +++ b/src/se/liu/gusso230/calendar/SimpleDate.java @@ -0,0 +1,29 @@ +package se.liu.gusso230.calendar; + +public class SimpleDate { + private int year; + private Month month; + private int day; + + public SimpleDate(final int year, final Month month, final int day) { + this.year = year; + this.month = month; + this.day = day; + } + + public int getYear() { + return year; + } + + public Month getMonth() { + return month; + } + + public int getDay() { + return day; + } + + @Override public String toString() { + return getYear() + "-" + String.format("%02d", getMonth().getNumber()) + "-" + String.format("%02d", getDay()); + } +} diff --git a/src/se/liu/gusso230/calendar/TimePoint.java b/src/se/liu/gusso230/calendar/TimePoint.java new file mode 100644 index 0000000..76e24c2 --- /dev/null +++ b/src/se/liu/gusso230/calendar/TimePoint.java @@ -0,0 +1,23 @@ +package se.liu.gusso230.calendar; + +public class TimePoint { + private int hour; + private int minute; + + public TimePoint(int hour, int minute) { + this.hour = hour; + this.minute = minute; + } + + public int getHour() { + return hour; + } + + public int getMinute() { + return minute; + } + + @Override public String toString() { + return String.format("%02d:%02d", getHour(), getMinute()); + } +} diff --git a/src/se/liu/gusso230/calendar/TimeSpan.java b/src/se/liu/gusso230/calendar/TimeSpan.java new file mode 100644 index 0000000..1183924 --- /dev/null +++ b/src/se/liu/gusso230/calendar/TimeSpan.java @@ -0,0 +1,23 @@ +package se.liu.gusso230.calendar; + +public class TimeSpan { + private TimePoint start; + private TimePoint end; + + public TimeSpan(TimePoint start, TimePoint end) { + this.start = start; + this.end = end; + } + + public TimePoint getStart() { + return start; + } + + public TimePoint getEnd() { + return end; + } + + @Override public String toString() { + return getStart() + " - " + getEnd(); + } +} diff --git a/src/se/liu/gusso230/tetris/Board.java b/src/se/liu/gusso230/tetris/Board.java new file mode 100644 index 0000000..759df90 --- /dev/null +++ b/src/se/liu/gusso230/tetris/Board.java @@ -0,0 +1,80 @@ +package se.liu.gusso230.tetris; + +import java.util.Random; + +public class Board { + private SquareType[][] squares; + private int width; + private int height; + + private Poly falling; + private int fallingX; + private int fallingY; + + private final static Random RND = new Random(); + + public static void main(String[] args) { + Board board = new Board(5, 10); + } + + public Board(int width, int height) { + this.height = height; + this.width = width; + squares = new SquareType[height][width]; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + squares[y][x] = SquareType.EMPTY; + } + } + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public SquareType getSquareAt(int x, int y) { + if (falling != null && falling.covers(x, y, fallingX, fallingY)) { + return falling.getSquareType(); + } else { + return squares[y][x]; + } + } + + public Poly getFalling() { + return falling; + } + + public int getFallingX() { + return fallingX; + } + + public int getFallingY() { + return fallingY; + } + + public void setFalling(final Poly falling) { + this.falling = falling; + } + + public void setFallingX(final int fallingX) { + this.fallingX = fallingX; + } + + public void setFallingY(final int fallingY) { + this.fallingY = fallingY; + } + + public void randomize() { + SquareType[] values = SquareType.values(); + int numValues = values.length; + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + squares[y][x] = values[RND.nextInt(numValues)]; + } + } + } +} diff --git a/src/se/liu/gusso230/tetris/BoardTester.java b/src/se/liu/gusso230/tetris/BoardTester.java new file mode 100644 index 0000000..32c558f --- /dev/null +++ b/src/se/liu/gusso230/tetris/BoardTester.java @@ -0,0 +1,20 @@ +package se.liu.gusso230.tetris; + +public class BoardTester { + public static void main(String[] args) { + Board board = new Board(5, 5); + + board.setFalling(new TetrominoMaker().getPoly(0)); + board.setFallingX(0); + board.setFallingY(0); + + BoardToTextConverter converter = new BoardToTextConverter(); + + System.out.println(converter.toText(board)); + board.getFalling().rotateCW(); + System.out.println(converter.toText(board)); + + TetrisViewer viewer = new TetrisViewer(board); + viewer.show(); + } +} diff --git a/src/se/liu/gusso230/tetris/BoardToTextConverter.java b/src/se/liu/gusso230/tetris/BoardToTextConverter.java new file mode 100644 index 0000000..8096dd7 --- /dev/null +++ b/src/se/liu/gusso230/tetris/BoardToTextConverter.java @@ -0,0 +1,40 @@ +package se.liu.gusso230.tetris; + +public class BoardToTextConverter { + public BoardToTextConverter() {} + + private char fromSquareType(SquareType squareType) { + switch (squareType) { + case I: + return '#'; + case J: + return '%'; + case S: + return '@'; + case L: + return '~'; + case O: + return '*'; + case T: + return 'ยง'; + case Z: + return '&'; + } + return ' '; + } + + public String toText(Board board) { + StringBuilder s = new StringBuilder(); + for (int y = 0; y < board.getHeight(); y++) { + for (int x = 0; x < board.getWidth(); x++) { + s.append(fromSquareType(board.getSquareAt(x, y))); + } + s.append('\n'); + } + for (int i = 0; i < board.getWidth(); i++) { + s.append('-'); + } + s.append('\n'); + return s.toString(); + } +} diff --git a/src/se/liu/gusso230/tetris/Point.java b/src/se/liu/gusso230/tetris/Point.java new file mode 100644 index 0000000..32e92ab --- /dev/null +++ b/src/se/liu/gusso230/tetris/Point.java @@ -0,0 +1,25 @@ +package se.liu.gusso230.tetris; + +public class Point { + private int x; + private int y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public void rotateCW(int boundingBoxSize) { + int prevX = x; + x = boundingBoxSize - y; + y = prevX; + } +} diff --git a/src/se/liu/gusso230/tetris/Poly.java b/src/se/liu/gusso230/tetris/Poly.java new file mode 100644 index 0000000..01fed45 --- /dev/null +++ b/src/se/liu/gusso230/tetris/Poly.java @@ -0,0 +1,42 @@ +package se.liu.gusso230.tetris; + +public class Poly { + private SquareType squareType; + private Point[] points; + + private int boundingBoxSize; + + public Poly(SquareType squareType, Point[] points) { + this.squareType = squareType; + this.points = points; + + boundingBoxSize = 0; + for (Point point : this.points) { + boundingBoxSize = Math.max(boundingBoxSize, point.getX()); + boundingBoxSize = Math.max(boundingBoxSize, point.getY()); + } + } + + public SquareType getSquareType() { + return squareType; + } + + public boolean covers(int x, int y, int posX, int posY) { + for (Point point : points) { + if (posX + point.getX() == x && posY + point.getY() == y) { + return true; + } + } + return false; + } + + public void rotateCW() { + for (Point point : points) { + point.rotateCW(boundingBoxSize); + } + } + + public Point[] getPoints() { + return points; + } +} diff --git a/src/se/liu/gusso230/tetris/SquareType.java b/src/se/liu/gusso230/tetris/SquareType.java new file mode 100644 index 0000000..7f2e519 --- /dev/null +++ b/src/se/liu/gusso230/tetris/SquareType.java @@ -0,0 +1,14 @@ +package se.liu.gusso230.tetris; + +import java.util.Random; + +public enum SquareType { + EMPTY, I, O, T, S, Z, J, L; + + public static void main(String[] args) { + Random rnd = new Random(); + for (int i = 0; i < 25; i++) { + System.out.println(SquareType.values()[rnd.nextInt(SquareType.values().length)]); + } + } +} diff --git a/src/se/liu/gusso230/tetris/TetrisViewer.java b/src/se/liu/gusso230/tetris/TetrisViewer.java new file mode 100644 index 0000000..d98298f --- /dev/null +++ b/src/se/liu/gusso230/tetris/TetrisViewer.java @@ -0,0 +1,23 @@ +package se.liu.gusso230.tetris; + +import javax.swing.*; +import java.awt.*; + +public class TetrisViewer { + private Board board; + + public TetrisViewer(Board board) { + this.board = board; + } + + public void show() { + JFrame frame = new JFrame("Tetris"); + JTextArea gameTextArea = new JTextArea(board.getHeight(), board.getWidth()); + gameTextArea.setText(new BoardToTextConverter().toText(board)); + frame.setLayout(new BorderLayout()); + frame.add(gameTextArea, BorderLayout.CENTER); + gameTextArea.setFont(new Font("Monospaced", Font.PLAIN, 20)); + frame.pack(); + frame.setVisible(true); + } +} diff --git a/src/se/liu/gusso230/tetris/TetrominoMaker.java b/src/se/liu/gusso230/tetris/TetrominoMaker.java new file mode 100644 index 0000000..c9c8704 --- /dev/null +++ b/src/se/liu/gusso230/tetris/TetrominoMaker.java @@ -0,0 +1,76 @@ +package se.liu.gusso230.tetris; + +import java.util.ArrayList; +import java.util.List; + +public class TetrominoMaker { + List<Poly> polys; + + public TetrominoMaker() { + polys = new ArrayList<>(); + + // I + polys.add(new Poly(SquareType.I, new Point[] { + new Point(0, 1), + new Point(1, 1), + new Point(2, 1), + new Point(3, 1), + })); + + // J + polys.add(new Poly(SquareType.J, new Point[] { + new Point(0, 0), + new Point(0, 1), + new Point(1, 1), + new Point(2, 1), + })); + + // L + polys.add(new Poly(SquareType.L, new Point[] { + new Point(0, 1), + new Point(1, 1), + new Point(2, 1), + new Point(2, 0), + })); + + // O + polys.add(new Poly(SquareType.O, new Point[] { + new Point(0, 0), + new Point(0, 1), + new Point(1, 0), + new Point(1, 1), + })); + + // S + polys.add(new Poly(SquareType.S, new Point[] { + new Point(0, 0), + new Point(0, 1), + new Point(1, 0), + new Point(1, 1), + })); + + // T + polys.add(new Poly(SquareType.T, new Point[] { + new Point(0, 1), + new Point(1, 1), + new Point(2, 1), + new Point(1, 0), + })); + + // Z + polys.add(new Poly(SquareType.Z, new Point[] { + new Point(0, 0), + new Point(1, 0), + new Point(1, 1), + new Point(2, 1), + })); + } + + public int getNumberOfTypes() { + return polys.size(); + } + + public Poly getPoly(int n) { + return polys.get(n); + } +} |
