Did commenting

This commit is contained in:
Malte Tammena 2017-10-20 13:51:52 +02:00 committed by Maurizio Bruetsch
parent dc1e45fe0c
commit a4110b96a2

View file

@ -3,10 +3,27 @@ package player.malte;
import java.util.Set; import java.util.Set;
import java.util.HashSet; import java.util.HashSet;
/**
* A generator for patterns for the game of Connect Four.
*/
public class PatternGenerator { public class PatternGenerator {
/**
* Do not instanciate.
*/
private PatternGenerator() {} private PatternGenerator() {}
/**
* Returns the winning patterns.
* X | X | XXXX | X
* X | X | | X
* X | X | | X
* X | X | | X
* Any of the above with an empty space is considered a winning pattern.
* Except the first one, in which only the topmost space can be empty.
*
* @param id The player's id.
*/
public static Set<Pattern> winningPatterns(int id) { public static Set<Pattern> winningPatterns(int id) {
// Four in a row with one hole // Four in a row with one hole
Set<Pattern> pats = PatternGenerator.emptySpaceGenerator(new Item(0, 0, id), Set<Pattern> pats = PatternGenerator.emptySpaceGenerator(new Item(0, 0, id),
@ -23,6 +40,7 @@ public class PatternGenerator {
new Item(1, 1, id), new Item(1, 1, id),
new Item(2, 2, id), new Item(2, 2, id),
new Item(3, 3, id))); new Item(3, 3, id)));
// Three on top of each other.
pats.add(new Pattern(new Item(0, 0, 0), pats.add(new Pattern(new Item(0, 0, 0),
new Item(0, 1, id), new Item(0, 1, id),
new Item(0, 2, id), new Item(0, 2, id),
@ -30,19 +48,36 @@ public class PatternGenerator {
return pats; return pats;
} }
/**
* Creates patterns with one empty space.
* Returns a set of patterns, each one containing an empty space.
* Each element of model is replaced once with an empty space Item.
* Always adds one support Item beneath the hole.
*
* @param model The basis of the pattern.
* @return A set of patterns created by the above rule.
*/
public static Set<Pattern> emptySpaceGenerator(Item... model) { public static Set<Pattern> emptySpaceGenerator(Item... model) {
Set<Pattern> ret = new HashSet<>(); Set<Pattern> ret = new HashSet<>();
// Iterate over all models.
for (int i = 0; i < model.length; i++) { for (int i = 0; i < model.length; i++) {
// Create a new pattern.
Pattern newP = new Pattern(model); Pattern newP = new Pattern(model);
Item x = model[i]; Item x = model[i];
Item newI = new Item(x.getPosX(), x.getPosY(), 0); Item newI = new Item(x.getPosX(), x.getPosY(), 0);
// Replace one item with the new 0-one.
newP.replaceItem(x, newI); newP.replaceItem(x, newI);
// Add the support.
newP.addItem(new Item(x.getPosX(), x.getPosY() + 1, new int[]{1, 2})); newP.addItem(new Item(x.getPosX(), x.getPosY() + 1, new int[]{1, 2}));
// Add the pattern to the set.
ret.add(newP); ret.add(newP);
} }
return ret; return ret;
} }
/**
* TODO: THIS
*/
public static Set<Pattern> empty2SpaceGenerator(Item... model) { public static Set<Pattern> empty2SpaceGenerator(Item... model) {
Set<Pattern> ret = new HashSet<>(); Set<Pattern> ret = new HashSet<>();
for (int i = 0; i < model.length; i++) { for (int i = 0; i < model.length; i++) {
@ -56,8 +91,10 @@ public class PatternGenerator {
return ret; return ret;
} }
/**
* TODO: THIS
*/
public static Set<Pattern> twoOfFourRowPatterns(int id) { public static Set<Pattern> twoOfFourRowPatterns(int id) {
// TODO: THIS
return null; return null;
} }
} }