Reputation: 715
Ok so let me preface by saying that my professor has thrown a project at us in which I dont think we were quite ready to take on. Nevertheless I have to build a 5-in a row game that is "smart". so I started off by having having the program ask the user the dimensions of the board using this:
https://gist.github.com/2307257
Now since the board in this game is quite large, I want to have an algorithm that will rank the space on the board, so that the computer tries to control the middle of the board. For instance, on a 5x5 board the values would be:
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
in which case the computer would pick the middle of the board (value three). me being a total noob am having some difficulty doing this. I have been thinking that I can do this with some loops, as I believe the value for any spot on the board should be: array [ (min width + i) through (max width - i) ] [ (min height + i) through (max height - i)] = 1 + i Right? well i have been trying to use something like this:
https://gist.github.com/2307327
but of course it doesn't work, or else I wouldn't be here. so please help me figure this out. Thanks
Upvotes: 0
Views: 2174
Reputation: 41
That code will cause a buffer overflow because an array of length/size 5 will have indices from 0 to 4.
Line 27 will cause a crash when y is equal height because you will have caused a write outside of array bounds. When x is equal to width and y is less than height, you will be writing to the array[0][y+1] cell.
Your loops should be ... for (y = 0; y < height; y++) for (x = 0; x < width; x++) ... because height and width (say they) equal 5. y and x will iterate from 0 to 4. Which will be valid array indices.
You are ignoring your y direction if you want to populate the array as you have shown above. You can simplify.
#define MIN(A, B) (((A) < (B)) ? A : B)
// x or y is cell 0 to 4 then the cell value should x or y + 1
// x or y is close to the other edge then the cell value should be width - x, or height - y
// I'm assuming that if the playing field is larger than 9x9 then a cell value of 5 is
// good enough.
array[x][y] =
MIN(
5,
MIN(
MIN(x + 1, width - x),
MIN(y + 1, height - y)));
I see you are trying to be clever by realizing that the results are symetric with line 27. However if you do that then you should change the for loops to iterate over 'half' the width (and eventually height.) Otherwise you will be overwriting cells that already have been computed. In short this is an optimization. Something you should do last and after you get something working first.
Often, doing something simply and easy to understand will be 'good enough' in terms of speed that optimizing may not be necessary. In this case, you would need a super slow machine from the 1970's for the brain dead simple algorithm to appear slow.
Some nit picking...
- Validate your input. If some yahoo types in negative numbers you are going to crash when you attempt to create the array. In this case it would be useless to accept values that are less than 5 because you need to connect 5. A board of 4x4 would result in the impossible.
Upvotes: 1