Word search algorithm segmentation fault - c++
i've been working on word search algorithm for quite long I think i made it good and decided to test limits. I've created program which makes file as big as I want to. So i made a matrix 10000 * 10000 (10000000 letters) and really long word from top left corner to bottom right corner. Thing is that it works with 4000 * 4000 matrix but then it gets bigger it just crashes. I tried to comment all other checkings for possible location and left the right one and it works perfectly even with 10000 * 10000 matrix but as soon as I add other checks it stops and I have no idea why. Any suggestions?
My code:
#include <iostream> //Might Be:
#include <string> // <----->
#include <fstream> // /-\ (1)/\ /\(3)
#include <new> // | \ /
#include <cstdlib> // | \ /
// | \ /
// | \ /
// | \ /
// \_/ (2)\/ \/(4)
//
using namespace std;
//Loop[4] //Loop[5]
int * Possibles(int Widht, int Height, int Poz, int Poz1, int Leng, int * Possible)
{
if(Poz1 < Widht - Leng + 1) // To right
{
Possible[0] = 1;
}
if(Poz1 >= Leng - 1) // To left
{
Possible[1] = 1;
}
if(Poz <= Height - Leng) // From top to bottom
{
Possible[2] = 1;
}
if(Poz >= Leng) // From bottom to top
{
Possible[3] = 1;
}
if(Poz + Leng <= Height && Poz1 + Leng <= Widht) //(2)
{
Possible[4] = 1;
}
if(Poz + Leng <= Height && Poz1 - Leng + 1 >= 0) //(4)
{
Possible[5] = 1;
}
if(Poz - Leng + 1 >= 0 && Poz1 - Leng + 1 >= 0) //(1)
{
Possible[6] = 1;
}
if(Poz - Leng + 1 >= 0 && Poz1 + Leng <= Widht) //(3)
{
Possible[7] = 1;
}
return Possible;
}
int * Zero(int * Possible)
{
Possible[0] = 0;
Possible[1] = 0;
Possible[2] = 0;
Possible[3] = 0;
Possible[4] = 0;
Possible[5] = 0;
Possible[6] = 0;
Possible[7] = 0;
return Possible;
}
string Next(string * NewMatrix, int Height, int Widht)
{
return NewMatrix[Height].substr(Widht, 1);
}
bool Find(string Word, int Poz, int Poz1, int Look, string Have, string * Matrix, int * Possible, int Backup, int Backup1)
{
if(Have == Word)
{
return true;
return Possible;
}
string NewLet = Word.substr(Look, 1);
if(Possible[0] == 1)
{
if(NewLet == Next(Matrix, Poz, Poz1 + 1))
{
Have += NewLet;
return Find(Word, Poz, Poz1 + 1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[0] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
if(Possible[1] == 1)
{
if(NewLet == Next(Matrix, Poz, Poz1 - 1))
{
Have += NewLet;
return Find(Word, Poz, Poz1 - 1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[1] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
if(Possible[2] == 1)
{
if(NewLet == Next(Matrix, Poz + 1, Poz1))
{
Have += NewLet;
return Find(Word, Poz + 1, Poz1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[2] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
if(Possible[3] == 1)
{
if(NewLet == Next(Matrix, Poz - 1, Poz1))
{
Have += NewLet;
return Find(Word, Poz - 1, Poz1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[3] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
if(Possible[4] == 1)
{
if(NewLet == Next(Matrix, Poz + 1, Poz1 + 1))
{
Have += NewLet;
return Find(Word, Poz + 1, Poz1 + 1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[4] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
if(Possible[5] == 1)
{
if(NewLet == Next(Matrix, Poz + 1, Poz1 - 1))
{
Have += NewLet;
return Find(Word, Poz + 1, Poz1 - 1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[5] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
if(Possible[6] == 1)
{
if(NewLet == Next(Matrix, Poz - 1, Poz1 - 1))
{
Have += NewLet;
return Find(Word, Poz - 1, Poz1 - 1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[6] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
if(Possible[7] == 1)
{
if(NewLet == Next(Matrix, Poz - 1, Poz1 + 1))
{
Have += NewLet;
return Find(Word, Poz - 1, Poz1 + 1, Look + 1, Have, Matrix, Possible, Backup, Backup1);
}
else
{
Possible[7] = 0;
Have = Word.substr(0, 1);
return Find(Word, Backup, Backup1, 1, Have, Matrix, Possible, Backup, Backup1);
}
}
return false;
}
string Diro(int * Possible)
{
string Dir;
bool Next = true;
if(Possible[0] == 1 && Next == true)
{
Dir = " From right to left";
Next = false;
}
if(Possible[1] == 1 && Next == true)
{
Dir = " From left to right";
Next = false;
}
if(Possible[2] == 1 && Next == true)
{
Dir = " From top to bottom";
Next = false;
}
if(Possible[3] == 1 && Next == true)
{
Dir = " From bottom to top";
Next = false;
}
if(Possible[4] == 1 && Next == true)
{
Dir = " ";
Next = false;
}
if(Possible[5] == 1 && Next == true)
{
Dir = " ";
Next = false;
}
if(Possible[6] == 1 && Next == true)
{
Dir = " ";
Next = false;
}
if(Possible[7] == 1 && Next == true)
{
Dir = " ";
Next = false;
}
return Dir;
}
int main()
{
int Height = 0, Widht = 0, Numb = 0;
int Loop[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int * Possible = new int[8];
string Dir, Search, Tempo, Temp;
ifstream Data("C:/Users/Magician/AppData/Local/VirtualStore/Program Files (x86)/CodeBlocks/MakeMaze/Files/Maze.txt");
Data >> Widht >> Height;
string * NewMatrix = new string[Height];
while(Loop[7] < Height)
{
Tempo = "";
Loop[8] = 0;
while(Loop[8] < Widht)
{
Data >> Temp;
Tempo += Temp;
Loop[8]++;
}
NewMatrix[Loop[7]] = Tempo;
Loop[7]++;
}
Data >> Numb;
string * Words = new string[Numb];
while(Loop[2] < Numb)
{
Data >> Words[Loop[2]];
Loop[2]++;
}
Data.close();
while(Loop[3] < Numb)
{
Search = Words[Loop[3]].substr(0, 1);
Loop[4] = 0;
while(Loop[4] < Height)
{
Loop[5] = 0;
while(Loop[5] < Widht)
{
if(NewMatrix[Loop[4]].substr(Loop[5], 1) == Search)
{
Zero(Possible);
Possibles(Widht, Height, Loop[4], Loop[5], Words[Loop[3]].size(), Possible);
if(Find(Words[Loop[3]], Loop[4], Loop[5], 1, Search, NewMatrix, Possible, Loop[4], Loop[5]))
{
cout << Words[Loop[3]] << " At: " << Loop[4] + 1 << " collumn, symbol " << Loop[5] + 1 << " " << Diro(Possible) << endl;
Loop[5] = Widht;
Loop[4] = Height;
}
}
Loop[5]++;
}
Loop[4]++;
}
Loop[3]++;
}
delete [] Possible;
delete [] Words;
delete [] NewMatrix;
return 0;
}
If you didn't understood what I wrote before: when I comment every if(Possible[] == )
except for if(Possible[5] == 1) in function Find() algorithm works then all allowed it doesn't. I've tried with 100 * 100 matrix with a lot of words to find and everything's ok.
One condition in Possibles is incorrect:
/* INCORRECT: Should be [ Poz >= Leng - 1 ] */
if(Poz >= Leng) // From bottom to top
{
Possible[3] = 1;
}
But this one is only a logical error and should not cause segmentation faults.
It looks like you have encountered a stack overflow.
Let's do simple calculation. For 10000 * 10000 matrix and word length of 10000, if you start calling Find() at the top left of the matrix, then three directions are possible. In worst case, Find() will traverse about 10000*3 elements. Note in Func() there are 3 string instances (sizeof(string) == 24 in 32bit VC2013), plus various integers. The size of a single frame could easily exceed 100 bytes. Since you are using recursive calls, this could lead to a stack usage of at least 10000 * 3 * 100 = 3000000bytes = approx. 3M.
This number is not very large, but enough for a stack overflow since Windows has a default stack size of 1M. (http://msdn.microsoft.com/en-us/library/8cxs58a6.aspx)
Advice for improvements
This is my used pattern to solve this kinds of matrix traversal problems.
First, define an constant array to hold offsets for movements (Moore neighborhood):
const int delta[8][2] = {
{ 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 },
{ -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 }
};
Second, use a single for to check for all directions:
int initial_x = .., initial_y = ..;
for (int dir = 0; dir < 8; dir++) {
for (int count = 0; count < WORD_LENGTH; count++) {
int current_x = initial_x + delta[dir][0] * count;
int current_y = initial_y + delta[dir][1] * count;
if (IS_INVALID(current_x, current_y)) {
break;
}
}
}
Last, insert various code and flags to complete the program.
Another hint: you can use char type to get and compare a single character in a string (Use word[idx] to get idxth character of word). This could be substantially faster than using substr.
Related
Tic tac toe Minimax Algorithm Having Weird Behavior (C++)
The other day, I wrote a console game of Tic-Tac-Toe in c++ for my son. He wanted me to add a computer, and I ended us using the minimax algorithm for the first time. I did some quick testing, but really just gave my laptop to my son as soon as it was printing stuff, who played with it for a couple minuets. I looked over his sholder once or twice, and noticed that it wasn't playing optimally, iv'e been trying to debug it, but I can't see where it goes wrong. I tried getting rid of alpha beta prunning, but that did not change anything. For context, on the board the computer is -1, blank is 0, and the player is 1. Here is the minimax function: int minimax(int board[9], int depth, int alpha, int beta, bool isMaxizimaizingPlayer) { bool found = false; for (int i = 0; i < 9; i++) { if (board[i] == 0) { found = true; } } if (!found) { return eval(board); } if (depth == 0 || eval(board) != 0) { return eval(board); } if (isMaxizimaizingPlayer) { int maxEval = -2; for (int spot = 0; spot < 9; spot++) { if (board[spot] == 0) { board[spot] = 1; int e = minimax(board, depth - 1, alpha, beta, false); if (e > maxEval) { maxEval = e; } //if (beta < alpha) //{ // break; //} board[spot] = 0; } } return maxEval; } else { int minEval = 2; for (int spot = 0; spot < 9; spot++) { if (board[spot] == 0) { board[spot] = -1; int e = minimax(board, depth - 1, alpha, beta, true); if (e < minEval) { minEval = e; } //if (beta < alpha) //{ // break; //} board[spot] = 0; } } return minEval; } } To be compleate, here is my eval function: int eval(int board[9]) { /*horizontial*/ for (int i = 0; i < 3; i++) { if (board[i * 3] == board[i * 3 + 1] && board[i * 3 + 2] == board[i * 3] && board[i * 3] != 0) { return board[i * 3]; } } /*vertical*/ for (int i = 0; i < 3; i++) { if (board[i] == board[i + 3] && board[i] == board[i + 6] && board[i] != 0) { return board[i]; } } /*Both diags*/ if (board[4] != 0) { if (board[0] == board[4] && board[0] == board[8]) { return board[4]; } if (board[2] == board[4] && board[4] == board[6]) { return board[4]; } } return 0; } And here is the inital call: int spot = 0; int minEval = 2; for (int i = 0; i < 9; i++) { if (board[i] == 0) { board[i] = -1; int score = minimax(board, 3, -2, 2, false); if (score < minEval) { minEval = score; spot = i; } board[i] = 0; } } std::cout << "The computer went in spot " << spot + 1 << std::endl; board[spot] = -1; printBoard(board);
It looks like you only call minimax with a depth of three, so the algorithm will only look up to three moves ahead, if you want optimal play you need to set the depth to > 9, so that the agent is always looking ahead to the end of the game.
Decremeting one from a number represent as a vector?
I am trying to decrement a number, which can be infinitely long and is represented in a vector, by 1. As a small example: vector<int> v1 = {5, 0, 0, 0}; After subtracting one from the end, the result should be: vector<int> v1 = {4, 9, 9, 9}; This is my current code: int size = v1.size(); bool carry = false; for (int i = size - 1; i > 0; i--) { if (v1.at(i) == 0) { v1.at(i) = 9; if (v1.at(0) == 1) { v1.at(0) = 0; } carry = true; } else { v1.at(i) -= 1; carry = false; } } if (carry == true && v1.at(0) == 0) { v1.erase(v1.begin()); } else if (carry == true) { v1.at(0) -= 1; } return v1; When I test it, everything works fine, except numbers like 11119. They turn out to be 00019. Is there anything I could tweak?
It seems to me that you didn't think through the logic clearly. Here's what needs to happen. If the last number is 0, it needs to be changed to 9 and a carry has to be maintained. Repeat until no carry needs to be maintained. That logic is best implemented using a do - while loop. Here's what I came up with. int size = v.size(); bool carry = true; int i = size - 1; do { if (v.at(i) == 0) { v.at(i) = 9; } else { v.at(i)--; carry = false; } --i; } while ( carry == true && i >= 0); Here's a complete program #include <iostream> #include <vector> void test(std::vector<int> v) { int size = v.size(); bool carry = true; int i = size - 1; do { if (v.at(i) == 0) { v.at(i) = 9; } else { v.at(i)--; carry = false; } --i; } while ( carry == true && i >= 0); for ( auto item : v ) { std::cout << item << " "; } std::cout << std::endl; } int main() { test({1, 1, 1, 1, 9}); test({5, 0, 0, 0, 0}); } and its output 1 1 1 1 8 4 9 9 9 9 See it working at https://ideone.com/lxs1vz.
A subtraction as you tried to do, could be done like this: #include <iterator> std::vector<int> v1 = {5, 0, 0, 0}; for (std::vector<int>::reverse_iterator i = v1.rbegin(); i != v1.rend(); i++) { *i -= 1; if (*i < 0) { *i = 9; } else { break; } }
Rat in maze problems, how to display coordinates where rat stepped in random generated matrix?
I started coding and searched a lot codes on internet but couldn't find the solution for my problem. I'm gonna paste there my current code. I already coded that if there is perfect path for rat or no (rat can only step on number one, "0" and "2" are walls.) But I cant display the coordinates of each "number one". For an example: 1,0,1, 1,2,2, 1,1,1, Then the path is, (0,0) -> (1,0) -> (2,0) -> (1,2) -> (2,2) if the matrix is 1,0,2, 0,2,1, 1,0,1, Then no path for the rat. Please edit my code and paste it there please or if you have already solved program for my problem please share it with me... I'm really new in c++. // CPP program to solve Rat in a maze // problem with backtracking using stack #include <cstring> #include <iostream> #include <stack> using namespace std; #define N 4 #define M 5 void mtxkeres(int (&mat)[N][M]); class node { public: int x, y; int dir; node(int i, int j) { x = i; y = j; // Initially direction // set to 0 dir = 0; } }; // maze of n*m matrix int n = N, m = M; // Coordinates of food int fx, fy; bool visited[N][M]; bool isReachable(int maze[N][M]) { // Initially starting at (0, 0). int i = 0, j = 0; stack<node> s; node temp(i, j); s.push(temp); while (!s.empty()) { // Pop the top node and move to the // left, right, top, down or retract // back according the value of node's // dir variable. temp = s.top(); int d = temp.dir; i = temp.x, j = temp.y; // Increment the direction and // push the node in the stack again. temp.dir++; s.pop(); s.push(temp); // If we reach the Food coordinates // return true if (i == fx and j == fy) { return true; } // Checking the Up direction. if (d == 0) { if (i - 1 >= 0 and maze[i - 1][j] and visited[i - 1][j]) { node temp1(i - 1, j); visited[i - 1][j] = false; s.push(temp1); } } // Checking the left direction else if (d == 1) { if (j - 1 >= 0 and maze[i][j - 1] and visited[i][j - 1]) { node temp1(i, j - 1); visited[i][j - 1] = false; s.push(temp1); } } // Checking the down direction else if (d == 2) { if (i + 1 < n and maze[i + 1][j] and visited[i + 1][j]) { node temp1(i + 1, j); visited[i + 1][j] = false; s.push(temp1); } } // Checking the right direction else if (d == 3) { if (j + 1 < m and maze[i][j + 1] and visited[i][j + 1]) { node temp1(i, j + 1); visited[i][j + 1] = false; s.push(temp1); } } // If none of the direction can take // the rat to the Food, retract back // to the path where the rat came from. else { visited[temp.x][temp.y] = true; s.pop(); } } // If the stack is empty and // no path is found return false. return false; } // Driver code int main() { // Initially setting the visited // array to true (unvisited) memset(visited, true, sizeof(visited)); // Maze matrix int maze[N][M] = { { 1, 0, 1, 1, 0 }, { 1, 1, 1, 0, 1 }, { 0, 1, 0, 1, 1 }, { 1, 1, 1, 1, 1 } }; // Food coordinates fx = 2; fy = 3; if (isReachable(maze)) { cout << "Path Found!" << '\n'; } else cout << "No Path Found!" << '\n'; return 0; }
Coin Change Bottom Up Dynamic Programming
http://uva.onlinejudge.org/external/6/674.html I'm trying to solve that problem. Note, though, that it's not the minimum coin change problem, it asks me for the different number of ways to make N cents using 50, 25, 15, 10, 5 and 1 cent coins. It's fairly straightforward, so I made this function: int count(int n, int m) // n is the N of the problem, m is the number of coin types and s[] is {1, 5, 10, 25, 50} { if (n == 0) { return 1; } if (n < 0) { return 0; } if (m < 0 && n >= 1) { return 0; } return DP[n][m - 1] + DP[n - s[m]][m]; } Fairly straightforward too is adding Dynamic Programming with memoization: int count(int n, int m) { if (n == 0) { return 1; } if (n < 0) { return 0; } if (m < 0 && n >= 1) { return 0; } if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1) { return count(n, m - 1) + count(n - s[m], m); } else { return DP[n][m - 1] + DP[n - s[m]][m]; } } However, none of these is fast enough - I need bottom up Dynamic Programming, but I am having difficulties coding it, even with some help from Algorithmist - http://www.algorithmist.com/index.php/Coin_Change. void generate() { for (i = 0; i < MAX; i++) { for (u = 0; u < m; u++) { if (i == 0) { DP[i][u] = 1; } else if (u == 0) { DP[i][u] = 0; } else if (s[u] > i) { DP[i][u] = DP[i][u - 1]; } else { DP[i][u] = DP[i][u - 1] + DP[i - s[u]][u]; } } } } I get 0 for every result for some reason, here's my full code: #include <stdio.h> #include <string.h> using namespace std; #define MAX 7490 int s[] = {1, 5, 10, 25, 50}, m = 5, input, DP[MAX][5], i, u; int count(int n, int m) { if (n == 0) { return 1; } if (n < 0) { return 0; } if (m < 0 && n >= 1) { return 0; } if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1) { return count(n, m - 1) + count(n - s[m], m); } else { return DP[n][m - 1] + DP[n - s[m]][m]; } } void generate() { for (i = 0; i < MAX; i++) { for (u = 0; u < m; u++) { if (i == 0) { DP[i][u] = 1; } else if (u == 0) { DP[i][u] = 0; } else if (s[u] > i) { DP[i][u] = DP[i][u - 1]; } else { DP[i][u] = DP[i][u - 1] + DP[i - s[u]][u]; } } } } int main() { memset(DP, -1, sizeof DP); generate(); while (scanf("%d", &input) != EOF) { //printf("%d\n", count(input, 4)); printf("%d\n", DP[input][4]); } return 0; }
You did the mistake here: else if (u == 0) { DP[i][u] = 0; } It should be DP[i][u]=1 because you can produce any value i using 1 cent coin in 1 possible way. i.e. to take 5 cent you will take 5 one cent coins which is one way to make 5-cent in total. ----- Btw, in you 1st approach in count method did you have this: if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1) { return count(n, m - 1) + count(n - s[m], m); } Or this: if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1) { return DP[n][m] = count(n, m - 1) + count(n - s[m], m); } If you did not memoize an already calculated result then this memoization check if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1) will never work, which might be the cause of your 1st approach to be too slow :-?
C++ Not Counting white beands
I need some help. I'm writing a code in C++ that will ultimately take a random string passed in, and it will do a break at every point in the string, and it will count the number of colors to the right and left of the break (r, b, and w). Here's the catch, the w can be either r or b when it breaks or when the strong passes it ultimately making it a hybrid. My problem is when the break is implemented and there is a w immediately to the left or right I can't get the program to go find the fist b or r. Can anyone help me? #include <stdio.h> #include "P2Library.h" void doubleNecklace(char neck[], char doubleNeck[], int size); int findMaxBeads(char neck2[], int size); #define SIZE 7 void main(void) { char necklace[SIZE]; char necklace2[2 * SIZE]; int brk; int maxBeads; int leftI, rightI, leftCount = 0, rightCount=0, totalCount, maxCount = 0; char leftColor, rightColor; initNecklace(necklace, SIZE); doubleNecklace(necklace, necklace2, SIZE); maxBeads = findMaxBeads(necklace2, SIZE * 2); checkAnswer(necklace, SIZE, maxBeads); printf("The max number of beads is %d\n", maxBeads); } int findMaxBeads(char neck2[], int size) { int brk; int maxBeads; int leftI, rightI, leftCount = 0, rightCount=0, totalCount, maxCount = 0; char leftColor, rightColor; for(brk = 0; brk < 2 * SIZE - 1; brk++) { leftCount = rightCount = 0; rightI = brk; rightColor = neck2[rightI]; if(rightI == 'w') { while(rightI == 'w') { rightI++; } rightColor = neck2[rightI]; } rightI = brk; while(neck2[rightI] == rightColor || neck2[rightI] == 'w') { rightCount++; rightI++; } if(brk > 0) { leftI = brk - 1; leftColor = neck2[leftI]; if(leftI == 'w') { while(leftI == 'w') { leftI--; } leftColor = neck2[leftI]; } leftI = brk - 1; while(leftI >= 0 && neck2[leftI] == leftColor || neck2[leftI] == 'w') { leftCount++; leftI--; } } totalCount = leftCount + rightCount; if(totalCount > maxCount) { maxCount = totalCount; } } return maxCount; } void doubleNecklace(char neck[], char doubleNeck[], int size) { int i; for(i = 0; i < size; i++) { doubleNeck[i] = neck[i]; doubleNeck[i+size] = neck[i]; } }
I didn't study the code in detail, but something is not symmetric: in the for loop, the "left" code has an if but the "right" code doesn't. Maybe you should remove that -1 in the for condition and add it as an if for the "right" code: for(brk = 0; brk < 2 * SIZE; brk++) { leftCount = rightCount = 0; if (brk < 2 * SIZE - 1) { rightI = brk; rightColor = neck2[rightI]; //... } if(brk > 0) { leftI = brk - 1; leftColor = neck2[leftI]; //... } //... Just guessing, though... :-/ Maybe you should even change those < for <=.