Skip to content

Commit

Permalink
Added jetBrains annotations to Sudoku
Browse files Browse the repository at this point in the history
  • Loading branch information
TreeSnowFence committed Aug 9, 2024
1 parent b50f77d commit 14dcf9c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/main/java/edu/rpi/legup/puzzle/sudoku/Sudoku.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.rules.ContradictionRule;
import edu.rpi.legup.ui.boardview.BoardView;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class Sudoku extends Puzzle {
private SudokuView boardView;
Expand Down Expand Up @@ -43,6 +45,7 @@ public Board generatePuzzle(int difficulty) {
}

@Override
@Contract(pure = true)
/**
* Determines if the given dimensions are valid for Sudoku
*
Expand Down Expand Up @@ -80,6 +83,7 @@ public boolean isValidDimensions(int rows, int columns) {
* @return true if board is valid, false otherwise
*/
@Override
@Contract(pure = true)
public boolean isBoardComplete(Board board) {
SudokuBoard sudokuBoard = (SudokuBoard) board;

Expand All @@ -104,5 +108,5 @@ public boolean isBoardComplete(Board board) {
* @param board the board that has changed
*/
@Override
public void onBoardChange(Board board) {}
public void onBoardChange(@NotNull Board board) {}
}
3 changes: 3 additions & 0 deletions src/main/java/edu/rpi/legup/puzzle/sudoku/SudokuCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.awt.*;
import java.util.HashSet;
import java.util.Set;
import org.jetbrains.annotations.Contract;


public class SudokuCell extends GridCell<Integer> {
private int groupIndex;
Expand Down Expand Up @@ -52,6 +54,7 @@ public void setAnnotations(Set<Integer> annotations) {
* @return a new copy of the SudokuCell that is independent of this one
*/
@Override
@Contract (pure = true)
public SudokuCell copy() {
SudokuCell copy = new SudokuCell(data, (Point) location.clone(), groupIndex, max);
copy.setIndex(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import edu.rpi.legup.controller.ElementController;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import java.awt.event.MouseEvent;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class SudokuCellController extends ElementController {
@Override
public void changeCell(MouseEvent e, PuzzleElement data) {
@Contract(pure = false)
public void changeCell(@NotNull MouseEvent e,@NotNull PuzzleElement data) {
SudokuCell cell = (SudokuCell) data;
System.out.print(111);
if (e.getButton() == MouseEvent.BUTTON1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.jetbrains.annotations.Contract;

public class SudokuCellFactory extends ElementFactory {
/**
Expand All @@ -19,6 +20,7 @@ public class SudokuCellFactory extends ElementFactory {
* @throws InvalidFileFormatException if file is invalid
*/
@Override
@Contract (pure = false)
public SudokuCell importCell(Node node, Board board) throws InvalidFileFormatException {
try {
if (!node.getNodeName().equalsIgnoreCase("cell")) {
Expand Down Expand Up @@ -59,6 +61,7 @@ public SudokuCell importCell(Node node, Board board) throws InvalidFileFormatExc
* @param puzzleElement PuzzleElement cell
* @return xml PuzzleElement
*/
@Contract (pure = false)
public org.w3c.dom.Element exportCell(Document document, PuzzleElement puzzleElement) {
org.w3c.dom.Element cellElement = document.createElement("cell");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import edu.rpi.legup.model.gameboard.GridCell;
import edu.rpi.legup.ui.boardview.GridElementView;
import java.awt.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class SudokuElementView extends GridElementView {
private static final Font FONT = new Font("TimesRoman", Font.BOLD, 16);
Expand All @@ -13,7 +15,7 @@ public class SudokuElementView extends GridElementView {
private static final Color GIVEN_COLOR = new Color(0x75, 0x75, 0x75, 0x80);
private static final Color BACKGROUND_COLOR = new Color(0xEEEEEE);

public SudokuElementView(GridCell cell) {
public SudokuElementView(@NotNull GridCell cell) {
super(cell);
}

Expand All @@ -23,17 +25,19 @@ public SudokuElementView(GridCell cell) {
* @return PuzzleElement associated with this view
*/
@Override
public SudokuCell getPuzzleElement() {
public @NotNull SudokuCell getPuzzleElement() {
return (SudokuCell) super.getPuzzleElement();
}

@Override
@Contract (pure = true)
public void drawGiven(Graphics2D graphics2D) {
graphics2D.setColor(GIVEN_COLOR);
graphics2D.fillRect(location.x, location.y, size.width, size.height);
}

@Override
@Contract (pure = true)
public void drawElement(Graphics2D graphics2D) {
graphics2D.setStroke(new BasicStroke(1));
graphics2D.setColor(BACKGROUND_COLOR);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/edu/rpi/legup/puzzle/sudoku/SudokuExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import edu.rpi.legup.model.PuzzleExporter;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import org.w3c.dom.Document;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class SudokuExporter extends PuzzleExporter {

public SudokuExporter(Sudoku sudoku) {
public SudokuExporter(@NotNull Sudoku sudoku) {
super(sudoku);
}

@Override
protected org.w3c.dom.Element createBoardElement(Document newDocument) {
@Contract (pure = true)
protected @NotNull org.w3c.dom.Element createBoardElement(@NotNull Document newDocument) {
SudokuBoard board;
if (puzzle.getTree() != null) {
board = (SudokuBoard) puzzle.getTree().getRootNode().getBoard();
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/edu/rpi/legup/puzzle/sudoku/SudokuImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class SudokuImporter extends PuzzleImporter {
public SudokuImporter(Sudoku sudoku) {
public SudokuImporter(@NotNull Sudoku sudoku) {
super(sudoku);
}

@Override
@Contract(pure = true, value = "-> true")
public boolean acceptsRowsAndColumnsInput() {
return true;
}

@Override
@Contract(pure = true, value = "-> false")
public boolean acceptsTextInput() {
return false;
}
Expand All @@ -30,6 +34,7 @@ public boolean acceptsTextInput() {
* @throws RuntimeException if board can not be created
*/
@Override
@Contract (pure = false)
public void initializeBoard(int rows, int columns) {
SudokuBoard sudokuBoard;
int minorSize = (int) Math.sqrt(rows);
Expand Down Expand Up @@ -57,7 +62,8 @@ public void initializeBoard(int rows, int columns) {
* @throws InvalidFileFormatException if file is invalid
*/
@Override
public void initializeBoard(Node node) throws InvalidFileFormatException {
@Contract(pure = false)
public void initializeBoard(@NotNull Node node) throws InvalidFileFormatException {
try {
if (!node.getNodeName().equalsIgnoreCase("board")) {
throw new InvalidFileFormatException(
Expand Down Expand Up @@ -129,7 +135,8 @@ public void initializeBoard(Node node) throws InvalidFileFormatException {
}

@Override
public void initializeBoard(String[] statements) throws UnsupportedOperationException {
@Contract(value = "_ -> fail", pure = false)
public void initializeBoard(@NotNull String[] statements) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Sudoku cannot accept text input");
}
}
3 changes: 2 additions & 1 deletion src/main/java/edu/rpi/legup/puzzle/sudoku/SudokuView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import java.awt.*;
import java.util.Set;
import javax.swing.*;
import org.jetbrains.annotations.NotNull;

public class SudokuView extends GridBoardView {
private static final Color STROKE_COLOR = new Color(0, 0, 0);
private static final Stroke MINOR_STOKE = new BasicStroke(1);
private static final Stroke MAJOR_STOKE = new BasicStroke(4);

public SudokuView(SudokuBoard board) {
public SudokuView(@NotNull SudokuBoard board) {
super(new BoardController(), new SudokuCellController(), board.getDimension());

int minorSize = (int) Math.sqrt(gridSize.width);
Expand Down

0 comments on commit 14dcf9c

Please sign in to comment.