<commit_before>package com.wvu.ai.csp.project.SudokuSolver;

import aima.core.search.csp.CSP;
import aima.core.search.csp.Domain;
import aima.core.search.csp.Variable;
import java.util.LinkedList;
import java.util.List;

public class SudokuCSP extends CSP {

	private static final Object[] DOMAIN_VALUES = new Object[]{1, 2, 3, 4, 5, 6, 7, 8, 9};

	public SudokuCSP(Board sudokuBoard) {
		loadVariables(sudokuBoard);
		generateRowConstraints(sudokuBoard);
		generateColumnConstraints(sudokuBoard);
		generateRegionConstraints(sudokuBoard);
	}

	private void loadVariables(Board sudokuBoard) {
		for (CellCollection row : sudokuBoard.getRows()) {
			for (Cell cell : row.getCollection()) {
				Variable newVariable = new Variable(cell.getName());
				addVariable(newVariable);
				if (cell.isAssigned()) {
					setDomain(newVariable, new Domain(new Object[]{}));
				} else {
					setDomain(newVariable, new Domain(DOMAIN_VALUES));
				}
			}
		}
	}

	private void generateRowConstraints(Board sudokuBoard) {
		List<Variable> cellVariables = getVariables();
		int sudokuBoardSideSize = sudokuBoard.getSize();
		for (int rowNum = 0; rowNum < cellVariables.size(); rowNum += sudokuBoardSideSize) {
			List<Variable> rowVariables = new LinkedList<>();
			for (int colNum = 0; colNum < sudokuBoardSideSize; colNum++) {
				rowVariables.add(cellVariables.get(rowNum + colNum));
			}
			addConstraint(new CellCollectionConstraint(rowVariables));
		}
	}

	private void generateColumnConstraints(Board sudokuBoard) {
		List<Variable> cellVariables = getVariables();
		int sudokuBoardSideSize = sudokuBoard.getSize();
		for (int colNum = 0; colNum < sudokuBoardSideSize; colNum++) {
			List<Variable> colVariables = new LinkedList<>();
			for (int rowNum = 0; rowNum < cellVariables.size(); rowNum += sudokuBoardSideSize) {
				colVariables.add(cellVariables.get(rowNum + colNum));
			}
			addConstraint(new CellCollectionConstraint(colVariables));<commit_msg>Setup_CSP: Fix domain value generation to be variable based on board size<commit_after>package com.wvu.ai.csp.project.SudokuSolver;

import aima.core.search.csp.CSP;
import aima.core.search.csp.Domain;
import aima.core.search.csp.Variable;
import java.util.LinkedList;
import java.util.List;

public class SudokuCSP extends CSP {

	private final Object[] domainValues;

	public SudokuCSP(Board sudokuBoard) {
		LinkedList<Object> possibleValues = new LinkedList<>();
		for (int i = 1; i <= sudokuBoard.getSize(); i++) {
			possibleValues.add(i);
		}
		domainValues = possibleValues.toArray(new Object[possibleValues.size()]);
		loadVariables(sudokuBoard);
		generateRowConstraints(sudokuBoard);
		generateColumnConstraints(sudokuBoard);
		generateRegionConstraints(sudokuBoard);
	}

	private void loadVariables(Board sudokuBoard) {
		for (CellCollection row : sudokuBoard.getRows()) {
			for (Cell cell : row.getCollection()) {
				Variable newVariable = new Variable(cell.getName());
				addVariable(newVariable);
				if (cell.isAssigned()) {
					setDomain(newVariable, new Domain(new Object[]{}));
				} else {
					setDomain(newVariable, new Domain(domainValues));
				}
			}
		}
	}

	private void generateRowConstraints(Board sudokuBoard) {
		List<Variable> cellVariables = getVariables();
		int sudokuBoardSideSize = sudokuBoard.getSize();
		for (int rowNum = 0; rowNum < cellVariables.size(); rowNum += sudokuBoardSideSize) {
			List<Variable> rowVariables = new LinkedList<>();
			for (int colNum = 0; colNum < sudokuBoardSideSize; colNum++) {
				rowVariables.add(cellVariables.get(rowNum + colNum));
			}
			addConstraint(new CellCollectionConstraint(rowVariables));
		}
	}

	private void generateColumnConstraints(Board sudokuBoard) {
		List<Variable> cellVariables = getVariables();
		int sudokuBoardSideSize = sudokuBoard.getSize();
		for (int colNum = 0; colNum < sudokuBoardSideSize; colNum++) {
			List<Variable> colVariables = new LinkedList<>();
			for (int rowNum = 0; rowNum < cellVariables.size(); rowNum += sudokuBoardSideSize) {
				colVariables.add(cellVariables.get(rowNum + colNum));
			}
			addConstraint(new CellCollectionConstraint(colVariables));
{'fraction_non_alphanumeric': 0.11924381968007755, 'fraction_numerical': 0.004362578768783325, 'mean_word_length': 5.1413690476190474, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7150312', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1287, 'n_words': 278}