danielbrthwt Posted January 7, 2012 Posted January 7, 2012 Some of you might know this program from a recent post of mine, i am writing a flood-it solver and it is nearly finished apart from that my function that thinks ahead dosent work, the problem is that somehow my main board ( globalBoard ) somehow picks up values from the temp board in the thinkAhead function called ( newBoard ) i have posted the code sorry its a bit long the thinkAhead function is close to the bottom public class Board { static int totalRemoved = 0; static int bestmove = 0; static int[] moveScores = new int[3]; static int[] finalMoves = new int[25]; static int elimColour = 0; static int highestGroup = 0; static int[][] blacklist = new int[14][14]; static int[][] tempblacklist = new int[14][14]; static int curGroup = 0; static int isGroup = 0; static int[][] group; static int numOfSquares = 0; static int totalPink = 0; static int totalPurple = 0; static int totalYellow = 0; static int totalRed = 0; static int totalBlue = 0; static int totalGreen = 0; int finished = 0; int removeColour = 0; //public static Square[][] globalBoard; static int[] pubAround; static int up; static int down; static int left; static int right; static int col; static int gid; static int pinkC = 0; static int pinkM = 0; static int[][] pinkLoc; static int purpleC; static int purpleM; static int[][] purpleLoc; static int yellowC = 0; static int yellowM = 0; static int[][] yellowLoc; static int redC = 0; static int redM = 0; static int[][] redLoc; static int blueC = 0; static int blueM = 0; static int[][] blueLoc; static int greenC = 0; static int greenM = 0; static int[][] greenLoc; static float pinkS; static float purpleS; static float yellowS; static float redS; static float blueS; static float greenS; public Board() { /*System.out.println("*TEST"); for ( int a = 0; a < 14; a++ ) { for ( int b = 0; b < 14; b++ ) { board[a][b] = new Square(); } }*/ } public void collectColours( Square[][] board ) { for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++ ) { if ( board[a][b].getGroupid() != 0 ) { if ( board[a][b].getNum() == 1 ) totalPink++; if ( board[a][b].getNum() == 2 ) totalPurple++; if ( board[a][b].getNum() == 3 ) totalYellow++; if ( board[a][b].getNum() == 4 ) totalRed++; if ( board[a][b].getNum() == 5 ) totalBlue++; if ( board[a][b].getNum() == 6 ) totalGreen++; } } } } public int checkStatus( Square[][] board ) { if ( board[0][0].getGroupid() != -1 ) { if ( board[0][0].getGroupArray().length == 14*14 ) return 1; } return 0; /* int startCol = board[0][0].getNum(); for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++ ) { if ( board[a][b].getNum() != startCol ) return 0; } } return 1; */ } public static int[][] extendArray( int[][] array ) { int[][] newArray = new int[array.length+1][2]; for ( int i = 0; i < array.length; i++ ) { newArray[i] = array[i]; } return newArray; } public static int[] extendArray( int[] array ) { int[] newArray = new int[array.length+1]; for ( int i = 0; i < array.length; i++ ) { newArray[i] = array[i]; } return newArray; } public static void printBoardNum( Square[][] board) { System.out.println("--------------------------------------------------------"); for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++ ) { System.out.printf("| %d ", board[a][b].getNum()); } System.out.println("|"); System.out.println("--------------------------------------------------------"); } } public static void clearVariables() { blacklist = new int[14][14]; numOfSquares = 0; curGroup = 0; elimColour = 0; up = 0; down = 0; left = 0; right = 0; col = 0; gid = 0; pinkS = 0; purpleS = 0; yellowS = 0; redS = 0; blueS = 0; greenS = 0; totalPink = 0; totalPurple = 0; totalYellow = 0; totalRed = 0; totalBlue = 0; totalGreen = 0; pinkC = 0; pinkM = 0; pinkLoc = new int[1][2]; purpleC = 0; purpleM = 0; purpleLoc = new int[1][2]; yellowC = 0; yellowM = 0; yellowLoc = new int[1][2]; redC = 0; redM = 0; redLoc = new int[1][2]; blueC = 0; blueM = 0; blueLoc = new int[1][2]; greenC = 0; greenM = 0; greenLoc = new int[1][2]; } public Square[][] processNewBoard( Square[][] board, String[][] colours ) { for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++) { board[a][b] = new Square(); board[a][b].setState(colours[a][b]); } } return board; } public static void printBoardGroupIds( Square[][] board ) { System.out.println("--------------------------------------------------------"); for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++) { System.out.printf("| %d ", board[a][b].getGroupid()); } System.out.println("|"); System.out.println("--------------------------------------------------------"); } } public static Square[][] resetBoard( Square[][] board ) { for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++ ) { board[a][b].addToGroup(-1); board[a][b].setScore(0); board[a][b].setGroupArray(new int[0][2]); } } return board; } public int[][] nextToClear( Square[][] board ) { int[][] startingGroup; int startingGroupId; int[][] nextToClear; int[][] blacklist; startingGroup = board[0][0].getGroupArray(); startingGroupId = board[0][0].getGroupid(); nextToClear = new int[0][2]; blacklist = new int[14][14]; if ( startingGroupId != -1 ) { for ( int i = 0; i < startingGroup.length; i++ ) { int a = startingGroup[i][0]; int b = startingGroup[i][1]; //nextToClear = extendArray(nextToClear); blacklist[a][b] = 1; if ( a > 0 && board[a-1][b].getGroupid() != startingGroupId && blacklist[a-1][b] != 1 ) { nextToClear = extendArray(nextToClear); nextToClear[nextToClear.length-1][0] = a-1; nextToClear[nextToClear.length-1][1] = b; blacklist[a-1][b] = 1; } if ( a < 13 && board[a+1][b].getGroupid() != startingGroupId && a > 0 && blacklist[a+1][b] != 1 ) { nextToClear = extendArray(nextToClear); nextToClear[nextToClear.length-1][0] = a+1; nextToClear[nextToClear.length-1][1] = b; blacklist[a-1][b] = 1; } if ( b > 0 && board[a][b-1].getGroupid() != startingGroupId && blacklist[a][b-1] != 1 ) { nextToClear = extendArray(nextToClear); nextToClear[nextToClear.length-1][0] = a; nextToClear[nextToClear.length-1][1] = b-1; blacklist[a][b-1] = 1; } if ( b < 13 && board[a][b+1].getGroupid() != startingGroupId && blacklist[a][b+1] != 1 ) { nextToClear = extendArray(nextToClear); nextToClear[nextToClear.length-1][0] = a; nextToClear[nextToClear.length-1][1] = b+1; blacklist[a][b+1] = 1; } } } else { nextToClear = extendArray(nextToClear); nextToClear[0][0] = 0; nextToClear[0][1] = 1; nextToClear = extendArray(nextToClear); nextToClear[1][0] = 1; nextToClear[0][1] = 0; } return nextToClear; } public Square[][] scoreSquare( Square[][] board, int[] touchingGroupsBlackList, int a, int b ) { int[][] squaresGroup = new int[1][2]; int ownid = board[a][b].getGroupid(); int multi = 0; if ( ownid == -1 ) { if ( a > 0 && board[a-1][b].getGroupid() != 0 && board[a-1][b].getGroupid() != -1 && touchingGroupsBlackList[board[a-1][b].getGroupid()] != -1 && board[a-1][b].getGroupid() != ownid ) multi += board[a-1][b].getGroupArray().length; if ( a < 13 && board[a+1][b].getGroupid() != 0 && board[a+1][b].getGroupid() != -1 && touchingGroupsBlackList[board[a+1][b].getGroupid()] != -1 && board[a+1][b].getGroupid() != ownid ) multi += board[a+1][b].getGroupArray().length; if ( b > 0 && board[a][b-1].getGroupid() != 0 && board[a][b-1].getGroupid() != -1 && touchingGroupsBlackList[board[a][b-1].getGroupid()] != -1 && board[a][b-1].getGroupid() != ownid ) multi += board[a][b-1].getGroupArray().length; if ( b < 13 && board[a][b+1].getGroupid() != 0 && board[a][b+1].getGroupid() != -1 && touchingGroupsBlackList[board[a][b+1].getGroupid()] != -1 && board[a][b+1].getGroupid() != ownid ) multi += board[a][b+1].getGroupArray().length; } else if ( ownid != -1 ) { squaresGroup = board[a][b].getGroupArray(); for ( int i = 0; i < squaresGroup.length; i++ ) { a = squaresGroup[i][0]; b = squaresGroup[i][1]; if ( a > 0 && board[a-1][b].getGroupid() != 0 && board[a-1][b].getGroupid() != -1 && touchingGroupsBlackList[board[a-1][b].getGroupid()] != -1 && board[a-1][b].getGroupid() != ownid ) multi += board[a-1][b].getGroupArray().length; if ( a < 13 && board[a+1][b].getGroupid() != 0 && board[a+1][b].getGroupid() != -1 && touchingGroupsBlackList[board[a+1][b].getGroupid()] != -1 && board[a+1][b].getGroupid() != ownid ) multi += board[a+1][b].getGroupArray().length; if ( b > 0 && board[a][b-1].getGroupid() != 0 && board[a][b-1].getGroupid() != -1 && touchingGroupsBlackList[board[a][b-1].getGroupid()] != -1 && board[a][b-1].getGroupid() != ownid ) multi += board[a][b-1].getGroupArray().length; if ( b < 13 && board[a][b+1].getGroupid() != 0 && board[a][b+1].getGroupid() != -1 && touchingGroupsBlackList[board[a][b+1].getGroupid()] != -1 && board[a][b+1].getGroupid() != ownid ) multi += board[a][b+1].getGroupArray().length; } } else multi = 1; board[a][b].setScore(multi); return board; } public Square[][] score( Square[][] board ) { int startingGroup; int[][] nextToClear; int[] touchingGroupsBlackList; int[] touchingGroups; int[] usedGroups; startingGroup = board[0][0].getGroupid(); nextToClear = nextToClear(board); touchingGroupsBlackList = new int[highestGroup+1]; touchingGroups = new int[0]; usedGroups = new int[highestGroup+1]; for ( int i = 0; i < nextToClear.length; i++ ) { int gId = board[nextToClear[i][0]][nextToClear[i][1]].getGroupid(); if ( gId != -1 && touchingGroupsBlackList[gId] != 1 && gId != startingGroup && gId != 0 ) { //touchingGroups = extendArray(touchingGroups); //touchingGroups[touchingGroups.length-1] = gId; touchingGroupsBlackList[gId] = 1; } } for ( int i = 0; i < nextToClear.length; i++ ) { int a; int b; int col; int totalSqu; a = nextToClear[i][0]; b = nextToClear[i][1]; col = board[a][b].getNum(); totalSqu = 1; if ( board[a][b].getGroupid() != -1 && col != board[0][0].getNum() && usedGroups[board[a][b].getGroupid()+1] != 1 ) { if ( board[a][b].getGroupid() != -1 ) totalSqu = board[a][b].getGroupArray().length; board = scoreSquare( board, touchingGroupsBlackList, a, B); usedGroups[board[a][b].getGroupid()+1] = 1; if ( col == 1 ) { pinkS += totalSqu * board[a][b].getScore(); pinkC += totalSqu; } if ( col == 2 ) { purpleS += totalSqu * board[a][b].getScore(); purpleC += totalSqu; } if ( col == 3 ) { yellowS += totalSqu * board[a][b].getScore(); yellowC += totalSqu; } if ( col == 4 ) { redS += totalSqu * board[a][b].getScore(); redC += totalSqu; } if ( col == 5 ) { blueS += totalSqu * board[a][b].getScore(); blueC += totalSqu; } if ( col == 6 ) { greenS += totalSqu * board[a][b].getScore(); greenC += totalSqu; } } } /*for ( int i = 0; i < nextToClear.length; i++ ) { int a = nextToClear[i][0]; int b = nextToClear[i][1]; int col = board[a][b].getNum(); if ( col == 1 ) pinkC += totalSqu; if ( col == 2 ) purpleC += totalSqu; if ( col == 3 ) yellowC += totalSqu; if ( col == 4 ) redC += totalSqu; if ( col == 5 ) blueC += totalSqu; if ( col == 6 ) greenC += totalSqu; int score = scoreSquare(board, a, B); board[a][b].setScore(score); }*/ return board; } public int[] chooesMoves( int choices ) { int[] moves = new int[0]; int[] options = new int[6]; float totalScore; int totalCount; int optionsLeft; totalScore = pinkS + purpleS + yellowS + redS + blueS + greenS; totalCount = pinkC + purpleC + yellowC + redC + blueC + greenC; optionsLeft = totalCount; for ( int i = 0; i < choices; i++ ) { if ( totalPink != 0 && pinkC == totalPink && options[0] != 1) { moves = extendArray(moves); moves[moves.length-1] = 1; options[0] = 1; removeColour = 1; } else if ( totalPurple != 0 && purpleC == totalPurple && options[1] != 1) { moves = extendArray(moves); moves[moves.length-1] = 2; options[1] = 1; removeColour = 1; } else if ( totalYellow != 0 && yellowC == totalYellow && options[2] != 1) { moves = extendArray(moves); moves[moves.length-1] = 3; options[2] = 1; removeColour = 1; } else if ( totalRed != 0 && redC == totalRed && options[3] != 1) { moves = extendArray(moves); moves[moves.length-1] = 4; options[3] = 1; removeColour = 1; } else if ( totalBlue != 0 && blueC == totalBlue && options[4] != 1) { moves = extendArray(moves); moves[moves.length-1] = 5; options[4] = 1; removeColour = 1; } else if ( totalGreen != 0 && greenC == totalGreen && options[5] != 1) { moves = extendArray(moves); moves[moves.length-1] = 6; options[5] = 1; removeColour = 1; } else if ( pinkS * 5 > totalScore-pinkS && options[0] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 1; options[0] = 1; optionsLeft -= pinkC; } else if ( purpleS * 5 > totalScore-purpleS && options[1] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 2; options[1] = 1; optionsLeft -= purpleC; } else if ( yellowS * 5 > totalScore-yellowS && options[2] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 3; options[2] = 1; optionsLeft -= yellowC; } else if ( redS * 5 > totalScore-redS && options[3] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 4; options[3] = 1; optionsLeft -= redC; } else if ( blueS * 5 > totalScore-blueS && options[4] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 5; options[4] = 1; optionsLeft -= blueC; } else if ( greenS * 5 > totalScore-greenS && options[5] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 6; options[5] = 1; optionsLeft -= greenC; } if ( pinkC * 5 > totalCount-pinkC && options[0] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 1; options[0] = 1; optionsLeft -= pinkC; } else if ( purpleC * 5 > totalCount-purpleC && options[1] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 2; options[1] = 1; optionsLeft -= purpleC; } else if ( yellowC * 5 > totalCount-yellowC && options[2] != 1) { moves = extendArray(moves); moves[moves.length-1] = 3; options[2] = 1; optionsLeft -= yellowC; } else if ( redC * 5 > totalCount-redC && options[3] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 4; options[3] = 1; optionsLeft -= redC; } else if ( blueC * 5 > totalCount-blueC && options[4] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 5; options[4] = 1; optionsLeft -= blueC; } else if ( greenC * 5 > totalCount-greenC && options[5] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 6; options[5] = 1; optionsLeft -= greenC; } /*else { moves = extendArray(moves); moves[i] = 0; }*/ else if ( options[0] != 1 && pinkC * optionsLeft > totalCount-pinkC ) { moves = extendArray(moves); moves[moves.length-1] = 1; options[0] = 1; optionsLeft--; totalCount -= pinkC; } else if ( options[1] != 1 && purpleC * optionsLeft > totalCount-purpleC) { moves = extendArray(moves); moves[moves.length-1] = 2; options[1] = 1; optionsLeft--; totalCount -= purpleC; } else if ( options[2] != 1 && yellowC * optionsLeft > totalCount-yellowC ) { moves = extendArray(moves); moves[moves.length-1] = 3; options[2] = 1; optionsLeft--; totalCount -= yellowC; } else if ( options[3] != 1 && redC * optionsLeft > totalCount-redC ) { moves = extendArray(moves); moves[moves.length-1] = 4; options[3] = 1; optionsLeft--; totalCount -= redC; } else if ( options[4] != 1 && blueC * optionsLeft > totalCount-blueC ) { moves = extendArray(moves); moves[moves.length-1] = 5; options[4] = 1; optionsLeft--; totalCount -= blueC; } else if ( options[5] != 1 && greenC * optionsLeft > totalCount-greenC ) { moves = extendArray(moves); moves[moves.length-1] = 6; options[5] = 1; optionsLeft--; totalCount -= greenC; } } /*int[] moves = new int[0]; int[] options = new int[6]; int totalSquares = pinkC + purpleC + yellowC + redC + blueC + greenC; for ( int i = 0; i < choices; i++ ) { if ( pinkC * 5 > totalSquares-pinkC && options[0] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 1; options[0] = 1; } else if ( purpleC * 5 > totalSquares-purpleC && options[1] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 2; options[1] = 1; } else if ( yellowC * 5 > totalSquares-yellowC && options[2] != 1) { moves = extendArray(moves); moves[moves.length-1] = 3; options[2] = 1; } else if ( redC * 5 > totalSquares-redC && options[3] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 4; options[3] = 1; } else if ( blueC * 5 > totalSquares-blueC && options[4] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 5; options[4] = 1; } else if ( greenC * 5 > totalSquares-greenC && options[5] != 1 ) { moves = extendArray(moves); moves[moves.length-1] = 6; options[5] = 1; } else { moves = extendArray(moves); moves[i] = 0; } }*/ return moves; } public static Square[][] findGroups( Square[][] board ) { highestGroup = 0; for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++ ) { board[a][b].setGroupArray(null); board[a][b].addToGroup(-1); } } group = null; int stop = 0; int stop2 = stop; for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++ ) { group = new int[1][2]; group[numOfSquares][0] = a; group[numOfSquares][1] = b; numOfSquares++; col = 0; col = board[a][b].getNum(); blacklist[a][b] = 1; isGroup = findAround(board, a, b, col); if ( isGroup == 1 && board[a][b].getGroupid() == -1 ) { board[a][b].addToGroup(curGroup); for ( int i = 0; i < group.length; i++ ) { board[group[i][0]][group[i][1]].setGroupArray(group); blacklist[(group[i][0])][group[i][1]] = 1; } curGroup++; highestGroup++; numOfSquares = 0; } numOfSquares = 0; tempblacklist = new int[14][14]; } } blacklist = new int[14][14]; tempblacklist = new int[14][14]; curGroup = 0; isGroup = 0; group = new int[1][2]; numOfSquares = 0; return board; } private static int findAround( Square[][] board, int a, int b, int col) { int[] around = new int[4]; int inGroup = 0; if ( a > 0 ) around[0] = board[a-1][b].getNum(); else around[0] = 0; if ( a < 13 ) around[1] = board[a+1][b].getNum(); else around[1] = 0; if ( b > 0 ) around[2] = board[a][b-1].getNum(); else around[2] = 0; if ( b < 13 ) around[3] = board[a][b+1].getNum(); else around[3] = 0; if ( /*a > 0 &&*/ around[0] == col && tempblacklist[a-1][b] != 1 && blacklist[a-1][b] != 1 ) { group = extendArray(group); group[numOfSquares][0] = a-1; group[numOfSquares][1] = b; numOfSquares++; tempblacklist[a-1][b] = 1; board[a-1][b].addToGroup(curGroup); inGroup = 1; findAround( board, a-1, b, col ); } if ( /*a < 13 &&*/ around[1] == col && tempblacklist[a+1][b] != 1 && blacklist[a+1][b] != 1) { group = extendArray(group); group[numOfSquares][0] = a+1; group[numOfSquares][1] = b; numOfSquares++; tempblacklist[a+1][b] = 1; board[a+1][b].addToGroup(curGroup); inGroup = 1; findAround( board, a+1, b, col ); } if ( /*b > 0 &&*/ around[2] == col && tempblacklist[a][b-1] != 1 && blacklist[a][b-1] != 1) { group = extendArray(group); group[numOfSquares][0] = a; group[numOfSquares][1] = b-1; numOfSquares++; tempblacklist[a][b-1] = 1; board[a][b-1].addToGroup(curGroup); inGroup = 1; findAround( board, a, b-1, col ); } if ( /*b < 13 &&*/ around[3] == col && tempblacklist[a][b+1] != 1 && blacklist[a][b+1] != 1) { group = extendArray(group); group[numOfSquares][0] = a; group[numOfSquares][1] = b+1; numOfSquares++; tempblacklist[a][b+1] = 1; board[a][b+1].addToGroup(curGroup); inGroup = 1; findAround( board, a, b+1, col ); } pubAround = around; return inGroup; } public int thinkFoward( Square[][] board, int[] moves, int amMoves, int stepsAhead, int depth ) { int move = 0; int oldLen = 0; int tmpMove; int status; int[] newMoves; int[] moveScores = new int[moves.length]; Square[][] newBoard = new Square[14][14]; if ( stepsAhead != 0 ) { for ( int i = 0; i < moves.length; i++ ) { newBoard = board; status = checkStatus(newBoard); if ( status == 0 && moves[i] != 0 ) { //oldLen = newBoard[0][0].getGroupArray().length; tmpMove = moves[i]; newBoard = makeMove( newBoard, tmpMove ); //printBoardGroupIds(newBoard); //newBoard = resetBoard(newBoard); newBoard = findGroups(newBoard); //printBoardGroupIds(newBoard); finished = checkStatus(newBoard); collectColours(newBoard); //System.out.println("*"); //printBoardNum(newBoard); totalRemoved += (14*14)-((14*14)-newBoard[0][0].getGroupArray().length); //totalRemoved += newBoard[0][0].getGroupArray().length-oldLen; //int tempLen = newBoard[0][0].getGroupArray().length; //int grpCheck = newBoard[2][1].getGroupid(); //totalRemoved = newBoard[0][0].getGroupArray().length; if ( finished == 1 ) { finished = 0; if ( depth != 0 ) return 0; else return moves[i]; } if ( removeColour == 1 ) { removeColour = 0; if ( depth != 0 ) return 0; else return moves[i]; } clearVariables(); newBoard = score(newBoard); newMoves = chooesMoves( amMoves ); thinkFoward( newBoard, newMoves, amMoves, stepsAhead-1, depth+1 ); /*newBoard = resetBoard(newBoard); status = checkStatus(newBoard); newBoard = findGroups(newBoard); score(newBoard); int[] newMoves = chooesMoves( 3 ); moves[i] = thinkFoward( board, newMoves, stepsAhead-1, depth+1 ); totalRemoved += (14*14)-((14*14)-newBoard[0][0].getGroupArray().length);*/ } /*else { return 0; }*/ if ( depth == 0) { //test = move; moveScores[i] = totalRemoved; //test = move; totalRemoved = 0; } } if ( depth == 0 ) { int total = 0; for ( int i = 0; i < moves.length; i++ ) { //test = move; total += moveScores[i]; } if ( moveScores[0] == (int) total/3 ) { for ( int i = 0; i < moves.length; i++ ) { //test = move; if ( moveScores[i] * moveScores.length-1 > total - moveScores[i] ) { move = moves[i]; } //test = move; } } else move = moves[0]; /* int a = moveScores[0]; int b = moveScores[1]; int c = moveScores[2]; if ( a * 2 > b+c ) move = moves[0]; if ( b * 2 > a+c ) move = moves[1]; if ( c * 2 > a+b ) move = moves[2]; */ } } return move; } public Square[][] makeMove( Square[][] board, int move ) { if ( board[0][0].getGroupid() != -1 ) { int[][] toChange = board[0][0].getGroupArray(); for ( int i = 0; i < toChange.length; i++ ) { board[toChange[i][0]][toChange[i][1]].tempChange(move); } } else { board[0][0].tempChange(move); } return board; } public int[] runBot( String[][] rawData, int test ) { Square[][] globalBoard = new Square[14][14]; int[] allMoves = new int[0]; int [] moves = {1,2,3,4,5,6}; int move = 0; int i = 0; int status = 0; globalBoard = processNewBoard( globalBoard, rawData ); while ( status == 0 /*&& < 25*/ ) { printBoardNum(globalBoard); globalBoard = findGroups( globalBoard ); int[][] stGroup = globalBoard[0][0].getGroupArray(); //printBoardGroupIds(globalBoard); collectColours(globalBoard); globalBoard = score( globalBoard ); moves = chooesMoves( 3 ); move = thinkFoward( globalBoard, moves, 3, 1, 0); if ( move != 0 ) globalBoard = makeMove( globalBoard, move ); allMoves = extendArray(allMoves); allMoves[allMoves.length-1] = move; i++; status = checkStatus(globalBoard); globalBoard = resetBoard(globalBoard); clearVariables(); } return allMoves; } } Quote
Sparda Posted January 7, 2012 Posted January 7, 2012 I suspect the issue is the same as before, except this time harder to find where it's happening. Basically in Java when you go a = new A(); b = a;, a and b are now the same object, so b.setVal(2). will result in a.getVal() been 2. This applies to arrays and objects. This does not apply to primitives. Quote
danielbrthwt Posted January 7, 2012 Author Posted January 7, 2012 ah okay so how do i duplicate the array object without getting these issues Quote
danielbrthwt Posted January 7, 2012 Author Posted January 7, 2012 haha dident know that thanks, ill rember this no doubt ill make a simple mistake like this again Quote
danielbrthwt Posted January 7, 2012 Author Posted January 7, 2012 (edited) I changed it to Square[][] newBoard = board.clone(); but it didn't change anything it still picks up values it shouldn't, i was researching and i found something that said that the clone() method doesn't work for multi dimensional arrays Edited January 8, 2012 by danielbrthwt Quote
Sparda Posted January 8, 2012 Posted January 8, 2012 Perhaps the issue is that individual elements in the arrays are coming from some where else and are been put into the array by reference. This would have the same behavior has previously described. Could this be the case? Quote
danielbrthwt Posted January 8, 2012 Author Posted January 8, 2012 (edited) Perhaps the issue is that individual elements in the arrays are coming from some where else and are been put into the array by reference. This would have the same behavior has previously described. Could this be the case? It could be i will have a look can you give me an example of what to look for, also i debugged my program and saw that even tho i was cloneing the array Square board = new Square[14][14]; Square newBoard; newBoard = board.clone(); newBoard2[0][0].tempChange(4); when i changed a value in newBoard it still changed in board Edited January 8, 2012 by danielbrthwt Quote
int0x80 Posted January 9, 2012 Posted January 9, 2012 Glad to see you're moving forward with this. As a side note, when you have big chunks of code to post, consider using pastebin or pastie :] Quote
danielbrthwt Posted January 10, 2012 Author Posted January 10, 2012 The problem was what you said but the clone() method didn't make a new/unconnected copy of my array of objects it just made a reference which is what it was doing in the first place so i made my own copy method which seemed to fix things public Square[][] copy( Square[][] board ) { Square[][] newBoard = new Square[14][14]; for ( int a = 0; a < board.length; a++ ) { for ( int b = 0; b < board[a].length; b++ ) { int col = board[a][b].getNum(); int score = board[a][b].getScore(); int gId = board[a][b].getGroupid(); int[][] groupArray = board[a][b].getGroupArray(); newBoard[a][b] = new Square(); newBoard[a][b].tempChange(col); newBoard[a][b].setScore(score); newBoard[a][b].addToGroup(gId); newBoard[a][b].setGroupArray(groupArray); } } return newBoard; } P.S int0x80 thanks for the tip that will be helpful when posting thing in the future :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.