From f0e225bfdf329ef566e7eb61af0c8757197e6a48 Mon Sep 17 00:00:00 2001
From: Maxime <maxime.tissot@student.reutlingen-university.de>
Date: Mon, 11 Jul 2022 00:00:58 +0200
Subject: [PATCH] Comments and readme

---
 .../hsrt/VSundSOA/backendapi/GameAPI.java     |   3 +
 .../fieldCreation/FieldCreation.java          | 158 ++++++++++--------
 .../backendapi/gameInstance/Field.java        |   3 -
 .../gameInstance/GameController.java          |  61 ++++++-
 .../backendapi/login/LoginController.java     |  27 +++
 .../java/hsrt/VSundSOA/backendapi/Tests.java  |   4 +-
 README.md                                     |  92 ++--------
 7 files changed, 192 insertions(+), 156 deletions(-)

diff --git a/Backend/src/main/java/hsrt/VSundSOA/backendapi/GameAPI.java b/Backend/src/main/java/hsrt/VSundSOA/backendapi/GameAPI.java
index f06e6b8..3e11fa3 100644
--- a/Backend/src/main/java/hsrt/VSundSOA/backendapi/GameAPI.java
+++ b/Backend/src/main/java/hsrt/VSundSOA/backendapi/GameAPI.java
@@ -3,6 +3,9 @@ package hsrt.VSundSOA.backendapi;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
+/**
+ * Main API to start the whole thing
+ */
 @SpringBootApplication
 public class GameAPI {
     public static void main(String[] args) {
diff --git a/Backend/src/main/java/hsrt/VSundSOA/backendapi/fieldCreation/FieldCreation.java b/Backend/src/main/java/hsrt/VSundSOA/backendapi/fieldCreation/FieldCreation.java
index 4d43771..e93cf17 100644
--- a/Backend/src/main/java/hsrt/VSundSOA/backendapi/fieldCreation/FieldCreation.java
+++ b/Backend/src/main/java/hsrt/VSundSOA/backendapi/fieldCreation/FieldCreation.java
@@ -18,15 +18,20 @@ public class FieldCreation {
     private Random random = new Random();
     ArrayList<Integer> possibleFill = new ArrayList<>();
 
+//Ich habe erst eine eigene brutforce Lösung ausprobieren wollen.
+//Es ist mir aber nicht gelungen es rechtzeitig zum laufen zu bringen.
+//Somit habe ich einfach einen Code aus dem Netz geholt und angepasst, damit es funktioniert.
+//Die Überreste meines Versuches sind in holeField() und den auskommentierten Funktionen am Ende zu finden.
+
+    /**
+     * Creates a new Sudoku grid as well as the solution
+     *
+     * @return a list of two arrays of int arrays (int[][]), the first is the sudoku grid, the second its solution
+     */
     @ResponseStatus(HttpStatus.OK)
     @GetMapping("/field/create")
     public ArrayList<int[][]> getNewField() {
         ArrayList<int[][]> fields = new ArrayList<>(Arrays.asList(holedField, solutionField));
-        createField();
-        return fields;
-    }
-
-    private void createField() {
         for (Integer x = 0; x < 9; x++) {
             for (Integer y = 0; y < 9; y++) {
                 solutionField[x][y] = -1;
@@ -38,45 +43,12 @@ public class FieldCreation {
         cornerInit();
 
         holeField();
+        return fields;
     }
 
-    private boolean initialiseField(Integer row, Integer column) {
-        ArrayList<Integer> possibleFill = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
-
-        while (possibleFill.size() > 0) {
-            solutionField[row][column] = possibleFill.get(random.nextInt(possibleFill.size()));
-            if (correctPlacement(row, column)) {
-                if (column.equals(8) && row.equals(8)) {
-                    return true;
-                }
-                if (column.equals(8)) {
-                    if (initialiseField(++row, 0)) {
-                        return true;
-                    }
-                } else {
-                    if (initialiseField(row, ++column)) {
-                        return true;
-                    }
-                }
-            } else {
-                possibleFill.remove(possibleFill.indexOf(solutionField[row][column]));
-            }
-        }
-/*
-
-        if (possibleFill.size() == 0) {
-            System.out.println("possible fill == 0 for row " + row);
-            for (Integer y = 0; y < 9; y++) {
-                solutionField[row][y] = -1;
-            }
-            initialiseField(row, 0);
-        }
-*/
-
-        solutionField[row][column] = -1;
-        return false;
-    }
-
+    /**
+     * Randomly generates the middle block of the grid
+     */
     void centerInit() {
         for (int i = 0; i < 9; ++i) {
             Integer n = random.nextInt(9) + 1;
@@ -92,6 +64,9 @@ public class FieldCreation {
                 solutionField[i][j] = possibleFill.get(k++);
     }
 
+    /**
+     * Fills the upper, lower, left and right blocks of the grid
+     */
     void crossInit() {
         for (int i = 3; i < 6; ++i) {
             int l = 0;
@@ -131,6 +106,9 @@ public class FieldCreation {
         }
     }
 
+    /**
+     * Fills the corner blocks of the grid
+     */
     void cornerInit() {
         for (int i = 0; i < 3; ++i) {
             int l = 0;
@@ -170,6 +148,71 @@ public class FieldCreation {
         }
     }
 
+    /**
+     * Randomly removes some numbers in the solution to create a sudoku grid
+     */
+    private void holeField() {
+        Boolean removed;
+//        Integer stackX, stackY;
+        for (Integer holes = 50; holes >= 0; holes--) {
+//            removed = false;
+            do {
+                Integer x = random.nextInt(9);
+                Integer y = random.nextInt(9);
+                if (holedField[x][y] > 0) {
+                    removed = true;
+                } else {
+                    holedField[x][y] = solutionField[x][y];
+                    removed = false;
+                }
+//                stackX = holedField[x][y];
+//                stackY = holedField[8 - x][8 - y];
+//                holedField[x][y] = -1;
+//                holedField[8 - x][8 - y] = -1;
+//                if (!correctRemove(0).equals(1)) {
+//                    removed = true;
+//                } else {
+//                    holedField[x][y] = stackX;
+//                    holedField[8 - x][8 - y] = stackY;
+//                }
+            } while (removed);
+        }
+    }
+
+    /*
+    private boolean initialiseField(Integer row, Integer column) {
+        ArrayList<Integer> possibleFill = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
+
+        while (possibleFill.size() > 0) {
+            solutionField[row][column] = possibleFill.get(random.nextInt(possibleFill.size()));
+            if (correctPlacement(row, column)) {
+                if (column.equals(8) && row.equals(8)) {
+                    return true;
+                }
+                if (column.equals(8)) {
+                    if (initialiseField(++row, 0)) {
+                        return true;
+                    }
+                } else {
+                    if (initialiseField(row, ++column)) {
+                        return true;
+                    }
+                }
+            } else {
+                possibleFill.remove(possibleFill.indexOf(solutionField[row][column]));
+            }
+        }
+        if (possibleFill.size() == 0) {
+            System.out.println("possible fill == 0 for row " + row);
+            for (Integer y = 0; y < 9; y++) {
+                solutionField[row][y] = -1;
+            }
+            initialiseField(row, 0);
+        }
+        solutionField[row][column] = -1;
+        return false;
+    }
+
     private boolean correctPlacement(Integer row, Integer column) {
         Integer toTest = solutionField[row][column];
         for (Integer x = 0; x < 9; x++) {
@@ -200,36 +243,6 @@ public class FieldCreation {
         return true;
     }
 
-    private void holeField() {
-
-        Boolean removed;
-        Integer stackX, stackY;
-        for (Integer holes = 50; holes >= 0; holes--) {
-            removed = false;
-            do {
-                Integer x = random.nextInt(9);
-                Integer y = random.nextInt(9);
-                if (holedField[x][y] > 0) {
-                    removed = true;
-                } else {
-                    holedField[x][y] = solutionField[x][y];
-                    removed = false;
-                }
-
-//                stackX = holedField[x][y];
-//                stackY = holedField[8 - x][8 - y];
-//                holedField[x][y] = -1;
-//                holedField[8 - x][8 - y] = -1;
-//                if (!correctRemove(0).equals(1)) {
-//                    removed = true;
-//                } else {
-//                    holedField[x][y] = stackX;
-//                    holedField[8 - x][8 - y] = stackY;
-//                }
-            } while (removed);
-        }
-    }
-
     private Integer correctRemove(Integer foundSolutions) {
         Integer missingCells = 0;
         for (Integer x = 0; x < 9; x++) {
@@ -257,4 +270,5 @@ public class FieldCreation {
         }
         return foundSolutions;
     }
+    */
 }
\ No newline at end of file
diff --git a/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/Field.java b/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/Field.java
index 4d8e84c..588571b 100644
--- a/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/Field.java
+++ b/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/Field.java
@@ -9,7 +9,6 @@ public class Field {
     private int[][] startField = new int[9][9];
     private int[][] solutionField = new int[9][9];
 
-
     public int getCellsToFill() {
         int  cellsToFill = 0;
         for (int row = 0; row < 9; row++){
@@ -43,7 +42,5 @@ public class Field {
                   7, 5, 3, 6, 2, 8, 4, 9, 1,
                   6, 8, 9, 4, 1, 3, 7, 2, 5,
                   1, 2, 4, 9, 5, 7, 6, 3, 8};
-
-
     }
 }
diff --git a/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/GameController.java b/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/GameController.java
index 6624923..a064c1e 100644
--- a/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/GameController.java
+++ b/Backend/src/main/java/hsrt/VSundSOA/backendapi/gameInstance/GameController.java
@@ -9,22 +9,59 @@ import javax.annotation.PostConstruct;
 import javax.validation.Valid;
 import java.util.*;
 
+/* unbenutzt da die Zeit nicht gereicht hat
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+*/
+
+/**
+ * The controller that will transmit and receive information concerning the development of the game
+ */
 @CrossOrigin(origins = "*")
 @RestController
 public class GameController {
     private ArrayList<Placement> history = new ArrayList<>();
     private Field field = new Field();
 
+    /**
+     * creates a new sudoku grid through the field creation module
+     *
+     * @return an array of int arrays (int[][]) that are used to save the sudoku grid
+     */
     @PostConstruct
     @ResponseStatus(HttpStatus.OK)
     @GetMapping("/game/newField")
-    private int[][] createField(){
+    public int[][] createField(){
         try {
             Thread.sleep(2000);
         }catch (Exception e){
             e.printStackTrace();
         }
         history.clear();
+
+        //Leider wird mir die Zeit knapp und ich kann es nicht mehr über einem richtigem request machen. Aber so war mein Ansatz.
+        /*
+        StringBuffer content = new StringBuffer();
+        try {
+            URL url = new URL("localhost:8080/field/create");
+            HttpURLConnection con = (HttpURLConnection) url.openConnection();
+            con.setRequestMethod("GET");
+
+            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
+            String inputLine;
+            while ((inputLine = in.readLine()) != null) {
+                content.append(inputLine);
+            }
+            in.close();
+            con.disconnect();
+        }catch (Exception e){
+
+        }
+        System.out.println(content);
+        */
+
         FieldCreation fieldCreation = new FieldCreation();
         ArrayList<int[][]> list = fieldCreation.getNewField();
         field.setStartField(list.get(0));
@@ -32,12 +69,23 @@ public class GameController {
         return field.getStartField();
     }
 
+    /**
+     * Returns the existing sudoku grid
+     *
+     * @return an array of int arrays (int[][]) that are used to save the sudoku grid
+     */
     @ResponseStatus(HttpStatus.OK)
     @GetMapping("/game/field")
     public int[][] getField(){
         return field.getStartField();
     }
 
+    /**
+     * Asserts wether a placed number in the sudoku grid is correct
+     *
+     * @param placement Class containing: username of player, position X of placement, position Y of placement and placed number
+     * @return true if the placed number is correct
+     */
     @ResponseStatus(HttpStatus.OK)
     @PutMapping("/game/placement")
     public Boolean ValidatePlacement(@Valid @RequestBody @NotNull Placement placement){
@@ -48,6 +96,11 @@ public class GameController {
         return false;
     }
 
+    /**
+     * Asserts wether the game is over or not
+     *
+     * @return true if the game is won
+     */
     @ResponseStatus(HttpStatus.OK)
     @GetMapping("/game/win")
     public Boolean verifyWin(){
@@ -57,6 +110,12 @@ public class GameController {
         return false;
     }
 
+
+    /**
+     * returns the log of every play made in this game
+     *
+     * @return An arraylist of placements containing every play made
+     */
     @ResponseStatus(HttpStatus.OK)
     @GetMapping("/game/history")
     private ArrayList<Placement> getHistory(){
diff --git a/Backend/src/main/java/hsrt/VSundSOA/backendapi/login/LoginController.java b/Backend/src/main/java/hsrt/VSundSOA/backendapi/login/LoginController.java
index d420bb2..c85231e 100644
--- a/Backend/src/main/java/hsrt/VSundSOA/backendapi/login/LoginController.java
+++ b/Backend/src/main/java/hsrt/VSundSOA/backendapi/login/LoginController.java
@@ -5,17 +5,31 @@ import org.springframework.web.bind.annotation.*;
 
 import java.util.*;
 
+/**
+ * The controller that will receive and manage the player information
+ */
 @CrossOrigin(origins = "*")
 @RestController
 public class LoginController {
     private ArrayList<User> userList = new ArrayList<>();
 
+    /**
+     * Function to get the whole list of players
+     *
+     * @return A list of users
+     */
     @ResponseStatus(HttpStatus.OK)
     @GetMapping("/login/userList")
     public ArrayList<User> getUserList(){
         return userList;
     }
 
+    /**
+     * Function to get the information of one user
+     *
+     * @param username String in the request path that is used as key to find the user
+     * @return Requested user containing: username, color and number of played games
+     */
     @ResponseStatus(HttpStatus.OK)
     @GetMapping("/login/{username}")
     public User getUser(@PathVariable String username){
@@ -27,6 +41,12 @@ public class LoginController {
         return null;
     }
 
+    /**
+     * Add a new user to the list
+     *
+     * @param user information about new user as: username, color and played games
+     * @return either the new user or if it already existed, the existing one
+     */
     @ResponseStatus(HttpStatus.OK)
     @PostMapping("/login/registry")
     public User postUser(@RequestBody User user){
@@ -39,6 +59,13 @@ public class LoginController {
         return user;
     }
 
+    /**
+     * Change the saved data of a user (except the username)
+     *
+     * @param username String in the request path
+     * @param user information to describe the changes to the user
+     * @return the updated user
+     */
     @ResponseStatus(HttpStatus.OK)
     @PatchMapping("/login/update/{username}")
     public User patchUser(@PathVariable String username, @RequestBody User user){
diff --git a/Backend/src/test/java/hsrt/VSundSOA/backendapi/Tests.java b/Backend/src/test/java/hsrt/VSundSOA/backendapi/Tests.java
index 5a4575e..2b7e2e1 100644
--- a/Backend/src/test/java/hsrt/VSundSOA/backendapi/Tests.java
+++ b/Backend/src/test/java/hsrt/VSundSOA/backendapi/Tests.java
@@ -1,14 +1,16 @@
 package hsrt.VSundSOA.backendapi;
 
 import hsrt.VSundSOA.backendapi.fieldCreation.FieldCreation;
+import hsrt.VSundSOA.backendapi.gameInstance.GameController;
 
 import java.util.ArrayList;
 
 public class Tests {
     private FieldCreation fieldCreator = new FieldCreation();
+    private GameController gameController = new GameController();
 
     public void testFieldCreator(){
-        ArrayList<int[][]> newField = fieldCreator.getNewField();
+        gameController.createField();
         System.out.println();
     }
 }
diff --git a/README.md b/README.md
index 576aba6..1a41b7e 100644
--- a/README.md
+++ b/README.md
@@ -1,92 +1,26 @@
 # Verteilte_Systeme_und_SOA_Gruppe_1
 
-
-
-## Getting started
-
-To make it easy for you to get started with GitLab, here's a list of recommended next steps.
-
-Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
-
-## Add your files
-
-- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
-- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
-
-```
-cd existing_repo
-git remote add origin https://gitlab.reutlingen-university.de/tissot/verteilte_systeme_und_soa_gruppe_1.git
-git branch -M main
-git push -uf origin main
-```
-
-## Integrate with your tools
-
-- [ ] [Set up project integrations](https://gitlab.reutlingen-university.de/tissot/verteilte_systeme_und_soa_gruppe_1/-/settings/integrations)
-
-## Collaborate with your team
-
-- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
-- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
-- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
-- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
-- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
-
-## Test and Deploy
-
-Use the built-in continuous integration in GitLab.
-
-- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
-- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
-- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
-- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
-- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
-
-***
-
-# Editing this README
-
-When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
-
-## Suggestions for a good README
-Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
-
 ## Name
-Choose a self-explaining name for your project.
+- Sudoku with Friends
 
 ## Description
-Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
-
-## Badges
-On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
-
-## Visuals
-Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
+- A web application that lets you generate sudoku grids and fill them. However, others can play on the same grid simultaneously. 
 
 ## Installation
-Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
+- Open the Backend folder as project in IntelliJ and start GameAPI.
+- Open Sudoku.html in your browser.
+- Open Sudoku.html in second browser to simulate a second player.
 
 ## Usage
-Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
+- Choose a name and login.
+- Choose a number from the list on the left, then click on the cell you want to place it into.
+- If it's correct the number will appear.
+- Other players will be listed on the right in there colour.
+- Correct numbers from other players will be displayed in there colour.
 
 ## Support
-Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
-
-## Roadmap
-If you have ideas for releases in the future, it is a good idea to list them in the README.
-
-## Contributing
-State if you are open to contributions and what your requirements are for accepting them.
-
-For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
-
-You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
-
-## Authors and acknowledgment
-Show your appreciation to those who have contributed to the project.
-
-## License
-For open source projects, say how it is licensed.
+- maxime.tissot@student.reutlingen-university.de
 
 ## Project status
-If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
+- Known bugs:
+- A win won't update correctly to all players
\ No newline at end of file
-- 
GitLab