Skip to content

Commit

Permalink
#0000 parse from string
Browse files Browse the repository at this point in the history
  • Loading branch information
HDouss committed Dec 25, 2023
1 parent 4e650dd commit cc6780e
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,21 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
16 changes: 16 additions & 0 deletions src/main/java/learnmind/state/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ public Code(final int one, final int two, final int three, final int four) {
this.num = 1000 * one + 100 * two + 10 * three + four;
}

/**
* Code constructor from the numerical representation.
* @param num Numerical representation
*/
public Code(final int num) {
this.num = num;
}

/**
* Code constructor from a string representation.
* @param str String representation of a code
*/
public Code(final String str) {
this(Integer.parseInt(str));
}

/**
* Accessor for the numerical representation of the code.
* @return A numerical representation of the code.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/learnmind/state/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public Result(final int black, final int white) {
this.num = Result.representation(black, white);
}

/**
* Result constructor from a string representation.
* @param str String representation of a result
*/
public Result(final String str) {
this(
Integer.parseInt(str.split(",\\s")[0].split(":")[1]),
Integer.parseInt(str.split(",\\s")[1].split(":")[1])
);
}

/**
* Result constructor. Arguments represents code to be broken
* and the guess made by the player.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/learnmind/state/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public Row(final Code guess, final Result result) {
this.result = result;
}

/**
* Row constructor from a string representation.
* @param str String representation of a result
*/
public Row(final String str) {
this(
new Code(str.split("\\s--\\s")[0].split(":\\s")[1]),
new Result(str.split("\\s--\\s")[1].split(":\\s")[1])
);
}

/**
* Acessor for the result.
* @return Row's result
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/learnmind/state/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* A state is a list of {@link Row}s.
Expand All @@ -24,6 +25,15 @@ public State(Row... row) {
this(Arrays.asList(row));
}

/**
* State constructor from a string representation.
* @param str String representation of a result
*/
public State(final String str) {
this(Arrays.stream(str.split(System.lineSeparator())).map(Row::new).collect(Collectors.toList()));
}


/**
* State constructor.
* @param rows Rows list
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/learnmind/state/CodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package learnmind.state;


import java.util.Random;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Tests for {@link Code}.
* @since 0.1
*/
public final class CodeTest {

/**
* {@link Code} could be built using a string representation.
*/
@Test
public void buildsAsString() {
final Random rnd = new Random();
final Code code = new RandomCode(rnd.nextInt(9) + 1);
MatcherAssert.assertThat(new Code(code.toString()), Matchers.equalTo(new Code(code.num())));
}

}
25 changes: 25 additions & 0 deletions src/test/java/learnmind/state/ResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package learnmind.state;


import java.util.Random;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Tests for {@link Result}.
* @since 0.1
*/
public final class ResultTest {

/**
* {@link Result} could be built using a string representation.
*/
@Test
public void buildsAsString() {
final Random rnd = new Random();
final Result result = new Result(rnd.nextInt(5), rnd.nextInt(5));
MatcherAssert.assertThat(new Result(result.toString()), Matchers.equalTo(result));
}

}
27 changes: 27 additions & 0 deletions src/test/java/learnmind/state/RowTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package learnmind.state;


import java.util.Random;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Tests for {@link Row}.
* @since 0.1
*/
public final class RowTest {

/**
* {@link Row} could be built using a string representation.
*/
@Test
public void buildsAsString() {
final Random rnd = new Random();
final Result result = new Result(rnd.nextInt(5), rnd.nextInt(5));
final Code code = new Code(new RandomCode(rnd.nextInt(9) + 1).num());
final Row row = new Row(code, result);
MatcherAssert.assertThat(new Row(row.toString()), Matchers.equalTo(row));
}

}
27 changes: 27 additions & 0 deletions src/test/java/learnmind/state/StateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package learnmind.state;


import java.util.Random;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Tests for {@link State}.
* @since 0.1
*/
public final class StateTest {

/**
* {@link State} could be built using a string representation.
*/
@Test
public void buildsAsString() {
final Random rnd = new Random();
final Result result = new Result(rnd.nextInt(5), rnd.nextInt(5));
final Code code = new Code(new RandomCode(rnd.nextInt(9) + 1).num());
final Row row = new Row(code, result);
MatcherAssert.assertThat(new Row(row.toString()), Matchers.equalTo(row));
}

}

0 comments on commit cc6780e

Please sign in to comment.