package kakuro.server.tablefactory;
import java.util.Collections;
import java.util.Random;
import java.util.Vector;
import kakuro.server.ISkeletonTable;
import kakuro.server.ITableFactory;
import kakuro.table.ITable;
import kakuro.table.Position;
/** Teszt implementáció, nem használt! */
public class GeneralTableFactory implements ITableFactory {
private static final GeneralTableFactory _instance = new GeneralTableFactory();
private GeneralTableFactory() {
}
public static GeneralTableFactory getInstance() {
return _instance;
}
public ISkeletonTable generateSkeleton(int skillLevel) {
return null;
}
public ITable generateTable(int skillLevel) {
if (skillLevel == ITable.DIFFICULTY_EASY) {
return populateSkeleton(new SimpleSkeletonTable());
}
return populateSkeleton(new FixedSkeletonTable());
}
//ezzel kezdj, ha easyt valasztunk elejen, akkor SimpleSkeletont kapsz
//ha medium vagy hard, akkor a grafikus szar a FixedSkeletonnal hiv meg itt
public ITable populateSkeleton(ISkeletonTable skeleton) {
int sorokszama = skeleton.getRowCount();
int oszlopokszama = skeleton.getColumnCount();
int positionPriority[][] = new int[oszlopokszama+1][sorokszama+1];
int positionValues[][] = new int[oszlopokszama+1][sorokszama+1];
System.out.println("sorok.sz: "+sorokszama+" # oszlopok.sz: "+oszlopokszama);
for (int y=0; y<sorokszama; y++) {
for (int x=0; x<oszlopokszama; x++) {
positionPriority[x][y]=0;
positionValues[x][y]=0;
Position actual = new Position(x, y);
int celltipus = skeleton.getType(actual);
String tipus = (celltipus==ISkeletonTable.CELL_BLACK?"Fekete":"Feher");
System.out.println("at ("+x+","+y+"): "+tipus);
// ### poziciok prioritasanak szamolasa ###
if ( celltipus == ISkeletonTable.CELL_WHITE){
int x_pos = x;
boolean whitePos=true;
int celltipusNext=ISkeletonTable.CELL_BLACK;
//int celltipusNextBlack=ISkeletonTable.CELL_BLACK;
positionPriority[x][y]++;
do{
x_pos--;
Position nextPosition = new Position(x_pos, y);
celltipusNext = skeleton.getType(nextPosition);
if( celltipusNext == ISkeletonTable.CELL_WHITE )
positionPriority[x][y]++;
else
whitePos = false;
}while( whitePos == true );
// ### vizszintes + ###
x_pos = x;
do{
while ( x_pos < (oszlopokszama-1) ){
x_pos++;
Position nextPosition = new Position(x_pos, y);
celltipusNext = skeleton.getType(nextPosition);
if( celltipusNext == ISkeletonTable.CELL_WHITE )
positionPriority[x][y]++;
else
whitePos = false;
if( x_pos < (oszlopokszama-1) ){
Position nextBlackPosition = new Position(x_pos+1, y);
int celltipusNextBlack = skeleton.getType(nextBlackPosition);
if( celltipusNextBlack == ISkeletonTable.CELL_BLACK)
break;
}
if( x_pos == (oszlopokszama-1))
break;
}
}while( whitePos == true );
// ### vizszintes - ###
x_pos=x;
do{
x_pos--;
Position nextPosition = new Position(x_pos, y);
celltipusNext = skeleton.getType(nextPosition);
if( celltipusNext == ISkeletonTable.CELL_WHITE )
positionPriority[x][y]++;
else
whitePos = false;
}while( whitePos == true );
// ### fuggoleges + ###
int y_pos = y;
do{
while ( y_pos < (sorokszama-1) ){
y_pos++;
Position nextPosition = new Position(x, y_pos);
celltipusNext = skeleton.getType(nextPosition);
if( celltipusNext == ISkeletonTable.CELL_WHITE )
positionPriority[x][y]++;
else
whitePos = false;
if( y_pos < (sorokszama-1) ){
Position nextBlackPosition = new Position(x, y_pos);
int celltipusNextBlack = skeleton.getType(nextBlackPosition);
if( celltipusNextBlack == ISkeletonTable.CELL_BLACK)
break;
}
if( y_pos == (sorokszama-1))
break;
}
}while( whitePos == true );
// ### fuggoleges
y_pos=y;
do{
y_pos--;
Position nextPosition = new Position(x, y_pos);
celltipusNext = skeleton.getType(nextPosition);
if( celltipusNext == ISkeletonTable.CELL_WHITE )
positionPriority[x][y]++;
else
whitePos = false;
}while( whitePos == true );
}
System.out.println(" vizszintes prio szam: "+ positionPriority[x][y]);
// a poziciok ertekkel valo feltoltese
int i_pos, j_pos;
for( int prioNumIdx = 16; prioNumIdx >1; prioNumIdx-- ){
for (int j=0; j<sorokszama; j++) {
for (int i=0; i<oszlopokszama; i++) {
if ( positionPriority[i][j] == prioNumIdx ){
i_pos=i;
boolean[] values = new boolean[10]; // a lehetseges ertekek
for ( int v=1; v<10; v++) values[v]= true;
while ( positionPriority[i_pos][j] !=0 ){
i_pos++;
for (int check=0; check!=10; check++){
if( positionValues[i_pos][j]==check) values[check] = false;
}
}
i_pos=i;
while ( positionPriority[i_pos][j] !=0 ){
i_pos--;
for (int check=0; check!=10; check++){
if( positionValues[i_pos][j]==check) values[check] = false;
}
}
j_pos=j;
while ( positionPriority[i][j_pos] !=0 ){
j_pos++;
for (int check=0; check!=10; check++){
if( positionValues[i][j_pos]==check) values[check] = false;
}
}
j_pos=j;
while ( positionPriority[i][j_pos] !=0 ){
j_pos--;
for (int check=0; check!=10; check++){
if( positionValues[i][j_pos]==check) values[check] = false;
}
}
int valuesCount=0;
for (int v=0; v!=10; v++)
if ( values[v]==true ) valuesCount++;
Random rand = new Random();
//int n = 10;
//int r = rand.nextInt(n+1);
int r = rand.nextInt(valuesCount);
int trueIdx=0;
int w=1;
while( w!=10 ){
if( values[w]==true ){
trueIdx++;
if( trueIdx==r ) positionValues[i][j]=w;
}
w++;
}
}
//System.out.println(i+". sor, "+j+". oszlop: "+ positionValues[i][j]);
}
//System.out.println(" ###### ");
}
}
}
}
for (int jdx=0; jdx< sorokszama; jdx++){
int str[]=new int[oszlopokszama+1];
for (int idx=0; idx < oszlopokszama; idx++){
str[idx]=positionValues[idx][jdx];
//System.out.println(positionValues[idx][jdx]);
}
//System.out.println(jdx+". sor: "+str[0]+str[1]+str[2]+str[3]+str[4]+str[5]+str[6]+str[7]+str[8] );
}
// ###########################################
System.out.println("#########################");
Vector<String> wordsInRows = new Vector<String>();
Vector<String> wordsInColumns = new Vector<String>();
/*for (int x=0; x!=oszlopokszama+1; x++)
for (int y=0; y!=sorokszama+1; y++)
positionValues[x][y]=0;*/
for (int y=0; y!=sorokszama; y++) {
for (int x=0; x!=oszlopokszama; x++) {
Position actual = new Position(x, y);
Position prePosition_X = new Position( x-1 , y );
Position prePosition_Y = new Position( x , y-1 );
int celltipus = skeleton.getType(actual);
int celltipus_pre_X = skeleton.getType(prePosition_X);
int celltipus_pre_Y = skeleton.getType(prePosition_Y);
//String tipus = (celltipus==ISkeletonTable.CELL_BLACK?"Fekete":"Feher");
//System.out.println("at ("+x+","+y+"): "+tipus);
if ( celltipus_pre_X == ISkeletonTable.CELL_BLACK && celltipus == ISkeletonTable.CELL_WHITE ){
boolean nextPositionBlack=false;
int celltipusNext=ISkeletonTable.CELL_BLACK;
int x_pos = x; // ### vizszintes ###
do{
x_pos++;
if ( x_pos == oszlopokszama){
String nextWord= x + "~" + (x_pos-1) + "," + y ;
wordsInRows.add(nextWord);
nextPositionBlack = true;
}else{
Position nextPosition = new Position(x_pos, y);
celltipusNext = skeleton.getType(nextPosition);
if( celltipusNext == ISkeletonTable.CELL_BLACK ) {
nextPositionBlack = true;
String nextWord= x + "~" + (x_pos-1) + "," + y ;
wordsInRows.add(nextWord);
}
}
}while( nextPositionBlack != true );
}
if ( celltipus_pre_Y == ISkeletonTable.CELL_BLACK && celltipus == ISkeletonTable.CELL_WHITE ){
boolean nextPositionBlack=false;
int celltipusNext=ISkeletonTable.CELL_BLACK;
int y_pos = y; // ### f�gg�leges ###
do{
y_pos++;
if ( y_pos == sorokszama){
String nextWord= x + "," + y + "~" + (y_pos-1) ;
wordsInColumns.add(nextWord);
nextPositionBlack = true;
}else{
Position nextPosition = new Position(x, y_pos);
celltipusNext = skeleton.getType(nextPosition);
if( celltipusNext == ISkeletonTable.CELL_BLACK ) {
nextPositionBlack = true;
String nextWord= x + "," + y + "~" + (y_pos-1) ;
wordsInColumns.add(nextWord);
}
}
}while( nextPositionBlack != true );
}
}
}
/*for (int vr=0; vr!= wordsInRows.size();vr++ ){
System.out.println("wordsInRows["+vr+"]: " + wordsInRows.elementAt(vr));
}
for (int vc=0; vc!= wordsInColumns.size();vc++ ){
System.out.println("wordsInColumns["+vc+"]: " + wordsInColumns.elementAt(vc));
}*/
int edgeMatrix[][] = new int[ wordsInColumns.size()+3 ][ wordsInRows.size()+3 ];
int xPos, xPos_Start, xPos_End;
int yPos, yPos_Start, yPos_End;
for (int vc=0; vc!= wordsInColumns.size();vc++ ){
String wordCol= wordsInColumns.elementAt(vc);
String[] pairWordCol = wordCol.split(",");
xPos = Integer.parseInt(pairWordCol[0]);
String[] pairWordCol_Y = pairWordCol[1].split("~");
yPos_Start = Integer.parseInt(pairWordCol_Y[0]);
yPos_End = Integer.parseInt(pairWordCol_Y[1]);
for (int vr=0; vr!= wordsInRows.size();vr++ ){
String wordRow= wordsInRows.elementAt(vr);
String[] pairWordRow = wordRow.split(",");
yPos = Integer.parseInt(pairWordRow[1]);
String[] pairWordRow_X = pairWordRow[0].split("~");
xPos_Start = Integer.parseInt(pairWordRow_X[0]);
xPos_End = Integer.parseInt(pairWordRow_X[1]);
if( ( xPos_Start <= xPos && xPos<= xPos_End && yPos_Start <= yPos && yPos<= yPos_End ) )
edgeMatrix[vc][vr]=1;
else
edgeMatrix[vc][vr]=0;
}
}
int sumRowPrio;
for (int sumc=0; sumc!= wordsInColumns.size();sumc++ ){
sumRowPrio=0;
for (int sumr=0; sumr!= wordsInRows.size();sumr++ ){
sumRowPrio = edgeMatrix[sumc][sumr] +sumRowPrio;
edgeMatrix[wordsInColumns.size()+2][sumr] =
edgeMatrix[wordsInColumns.size()+2][sumr] + edgeMatrix[sumc][sumr];
}
edgeMatrix[sumc][wordsInRows.size()+2]=sumRowPrio;
}
Vector<String> check_Col = new Vector<String>();
for( int check_C=0; check_C!=wordsInColumns.size();check_C++ ){
check_Col.add( edgeMatrix[check_C][wordsInRows.size()+2]+"@"+check_C );
}
Collections.sort(check_Col);
Vector<String> check_Row=new Vector<String>();
for( int check_R=0; check_R!=wordsInRows.size();check_R++ ){
check_Row.add( edgeMatrix[wordsInColumns.size()+2][check_R]+"@"+check_R);
}
Collections.sort(check_Row);
int countDown=0;
int index_c = check_Col.size();
int index_r = check_Row.size();
//String chColumn;
//String chRow;
while ( (0 != index_c) && ( 0 !=index_r) ) {
//String chColumn = check_Col.elementAt(index_c);
//String chRow= ""+check_Row.elementAt(index_r);
//String[] pair_chColumn = chColumn.split("");
countDown++;
index_c=index_c-countDown;
index_r=index_r-countDown;
}
// ITT akad el a stringekn�l....
// ennyi kell m�g:
// CIKLUS 1-t�l max 9-ig RND sz�m gener�l�s
//- gener�lni egy sz�mot 1-9-ig (ezt kiz�rjuk a k�ss�bbiekb�l)
// -a sorberendezetteket a legnagyobb �rt�kek vizsz/f�gg sz� kereszetz�d�se szerint p�ros�tva
// -egy ciklusban minden lehehets�ges sor/oszlop p�ros�t�st megtesz�nk, am�g van
// - a m�r l�terej�tt p�ros�t�s cs�m�pontj�hoz rendelej�k a fentebb gener�lt sz�mot
// - �s megjel�lj�k a p�ros�tott sorokat/oszlopokat, hogy azokat egy cikluson bel�l ne akarjuk m�g egyszer p�ros�tani
// - az �rt�kkel ell�tott kereszetz�d�set kivessz�k a k�vetkez� k�r p�ros�t�si lehet�s�geib�l
// ( 1-0 �lm�trixn�l 1: a kereszetez�d�s, 0: a nem l�tez� vagy m�r �rt�kkel ell�tott kereszetez�d�s,
// teh�t egyszer�en null�zom)
// az �lm�trix kereszetz�d�s�hez tartoz� szavakb�l j�n a t�bla koordin�ta,
// oda be�rom a poz�ci�nak adott �rt�ket -> ez mehet a positionValues[][]-ba, �s �gy �t is adhat�
//return SimpleTableFactory.getInstance().populateSkeleton(skeleton);
int[][] _matrix = new int[oszlopokszama][sorokszama];
System.out.println("sorok: "+sorokszama+", oszlopok: "+oszlopokszama);
for (int j=0; j<sorokszama; j++)
for (int i=0; i<oszlopokszama; i++)
_matrix[i][j] = positionValues[i][j];
return new GeneralTable(skeleton, positionValues);
}
}