Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/Rule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api;

import game.Board;
import game.GameState;

import java.util.function.Function;

public class Rule <T extends Board> {
Function<T , GameState> condition;
public Rule(Function<T, GameState> condition){
this.condition = condition;
}
}
67 changes: 4 additions & 63 deletions api/RuleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,16 @@
import java.util.function.Function;

public class RuleEngine {
Map<String, List<Rule<TicTacToeBoard>>> ruleMap = new HashMap<>();
Map<String, RuleSet<? extends Board>> ruleMap = new HashMap<>();

public RuleEngine(){
String name = TicTacToeBoard.class.getName();
ruleMap.put(name,new ArrayList<>());
// Rules
ruleMap.get(name).add(new Rule<> ((board)->outerTraversal(board::getSymbol)));
ruleMap.get(name).add(new Rule<> ((board)->outerTraversal((i,j)-> board.getSymbol(j,i))));
ruleMap.get(name).add(new Rule<> ((board)->traverse((i)-> board.getSymbol(i,i))));
ruleMap.get(name).add(new Rule<> ((board)->traverse((i)-> board.getSymbol(i,2-i))));
ruleMap.get(name).add(new Rule<> (this::countMoves));

ruleMap.put(TicTacToeBoard.class.getName(),TicTacToeBoard.getRules());
}

public GameState getState(Board board){
if(board instanceof TicTacToeBoard board1) {
List<Rule<TicTacToeBoard>>rules = ruleMap.get(TicTacToeBoard.class.getName());
for(Rule <TicTacToeBoard> rule:rules){
RuleSet<TicTacToeBoard> rules = (RuleSet<TicTacToeBoard>) ruleMap.get(TicTacToeBoard.class.getName());
for(Rule <TicTacToeBoard> rule: rules){
GameState gameState = rule.condition.apply(board1);
if(gameState.isOver()){
return gameState;
Expand All @@ -40,57 +32,6 @@ public GameState getState(Board board){
return new GameState(true, "-");
}
}

public GameState countMoves (TicTacToeBoard board1){
int count=0;
GameState gameState = new GameState(false, "-");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board1.getSymbol(i,j)!=null){
count ++;
}
}
}
if(count == 9){
gameState = new GameState(true,"-");
}
return gameState;
}

public GameState outerTraversal(BiFunction<Integer, Integer,String> next){
GameState result = new GameState(false, "-");
for (int i = 0; i < 3; i++) {
int finalI = i;
Function<Integer,String> nextValue = (j) -> next.apply(finalI,j);
GameState traversal = traverse(nextValue);
if(traversal.isOver()) {
result = traversal;
break;
}
}
return result;
}

public GameState traverse(Function<Integer,String> next){
GameState result = new GameState(false, "-");
boolean possibleStreak = true;
for (int i = 0; i < 3; i++) {
if (next.apply(i)== null || !next.apply(0).equals(next.apply(i))) {
possibleStreak = false;
break;
}
}
if (possibleStreak) {
result = new GameState(true, next.apply(0));
}
return result;
}
}


class Rule <T extends Board> {
Function<T , GameState> condition;
Rule(Function<T, GameState> condition){
this.condition = condition;
}
}
24 changes: 24 additions & 0 deletions api/RuleSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package api;

import game.Board;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RuleSet <T extends Board> implements Iterable<Rule<T>>{
List<Rule<T>> rules = new ArrayList<>();
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<Rule<T>> iterator() {
return rules.iterator();
}

public void add (Rule<T> rule){
rules.add(rule);
}
}
61 changes: 61 additions & 0 deletions boards/TicTacToeBoard.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package boards;

import api.Rule;
import api.RuleSet;
import game.Board;
import game.Cell;
import game.GameState;
import game.Move;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;

public class TicTacToeBoard implements Board, Cloneable {
String[][] cells = new String[3][3];
Expand All @@ -29,6 +34,15 @@ public void setCell(Cell cell, String symbol) {
}
}

public static RuleSet<TicTacToeBoard> getRules(){
RuleSet<TicTacToeBoard> rules = new RuleSet<>();
rules.add(new Rule<>((board)->outerTraversal(board::getSymbol)));
rules.add(new Rule<> ((board)->outerTraversal((i,j)-> board.getSymbol(j,i))));
rules.add(new Rule<> ((board)->traverse((i)-> board.getSymbol(i,i))));
rules.add(new Rule<> ((board)->traverse((i)-> board.getSymbol(i,2-i))));
rules.add(new Rule<> (TicTacToeBoard::countMoves));
return rules;
}
@Override
public void move(Move move){
setCell(move.getCell(),move.getPlayer().symbol());
Expand All @@ -51,4 +65,51 @@ public String toString(){
public TicTacToeBoard clone() {
return new TicTacToeBoard(this);
}


public static GameState countMoves(TicTacToeBoard board1){
int count=0;
GameState gameState = new GameState(false, "-");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board1.getSymbol(i,j)!=null){
count ++;
}
}
}
if(count == 9){
gameState = new GameState(true,"-");
}
return gameState;
}

public static GameState outerTraversal(BiFunction<Integer, Integer, String> next){
GameState result = new GameState(false, "-");
for (int i = 0; i < 3; i++) {
int finalI = i;
Function<Integer,String> nextValue = (j) -> next.apply(finalI,j);
GameState traversal = traverse(nextValue);
if(traversal.isOver()) {
result = traversal;
break;
}
}
return result;
}

public static GameState traverse(Function<Integer, String> next){
GameState result = new GameState(false, "-");
boolean possibleStreak = true;
for (int i = 0; i < 3; i++) {
if (next.apply(i)== null || !next.apply(0).equals(next.apply(i))) {
possibleStreak = false;
break;
}
}
if (possibleStreak) {
result = new GameState(true, next.apply(0));
}
return result;
}

}