55241

Finding 5 values in a row diagonally in Java

Question:

I have this 10x10 array:

private static int[][] intersections = new int[10][10];

I'm using this code to find if there are 5 values in a row horizontally:

public static int horizontalCheck() { String horizontal = ""; for (int i = 0; i < intersections.length; i++) { for (int j = 0; j < intersections[i].length; j++) { horizontal += Integer.toString(intersections[i][j]); } if (horizontal.indexOf("11111") != -1) { // White wins. return 1; } else if (horizontal.indexOf("22222") != -1) { // Black wins. return 2; } horizontal = ""; } return 0; }

And a similar code to do it vertically. But my question is, how could I find if there are 5 values in a row diagonally? The board is sized 10x10 and the diagonals can be both ways anywhere on the board. If you have any questions or need some more information on the code, make sure to ask.

Answer1:

I suggest you write a helper function for this. The function should take these parameters:

<ul><li>r0 - The starting row from which the check needs to start</li> <li>c0 - The starting column from which the check needs to start</li> <li>dr - The vertical step from {-1, 0, 1}</li> <li>dc - The horizontal step from {-1, 0, 1}</li> <li>len - The number of items to be found</li> <li>num - The number to find.</li> </ul>

Here is how this function may look:

private static boolean checkRow(int r0, int c0, int dr, int dc, int len, int num) { for (int k = 0 ; k != len ; k++) { int r = r0 + k*dr; int c = c0 + k*dc; if (r < 0 || c < 0 || r >= intersections.length || c > intersections[r].length || intersections[r][c] != num) { return false; } } return true; }

With this function in hand, you can check for len items in a row in any direction that you wish:

// See if we've got five eights in any direction: for (int r = 0 ; r != intersections.length ; r++) { for (int c = 0 ; c != intersections[r].length ; c++) { if (checkRow(r, c, 0, 1, 5, 8)) { System.out.println("Horizontal, starting at "+r+" " +c); } if (checkRow(r, c, 1, 0, 5, 8)) { System.out.println("Vertical, starting at "+r+" " +c); } if (checkRow(r, c, 1, 1, 5, 8)) { System.out.println("Diagonal descending right, starting at "+r+" " +c); } if (checkRow(r, c, 1, -1, 5, 8)) { System.out.println("Diagonal descending left, starting at "+r+" " +c); } } }

Recommend

  • Getting JSON from Jersey with circular dependencies
  • Using .include? to prevent duplicates
  • use rollapply and zoo to calculate rolling average of a column of variables
  • Can order by work with properties from model?
  • How to check if a RxJS Observable contains a string in Angular2?
  • Select multiple items in JTextPane
  • Anybody have any idea how the divs are emitted in pinterest.com?
  • Textbox validation in jquery
  • use a single handler for multiple inputs onChange events
  • Creating a layer of gradient within an SVG path dynamically
  • Submission of new app with iAds
  • CSS how to fix an element to scroll horizontally with the page but not vertically?
  • Ionic Slide Up Slide Down Animation for only one view in sidebar
  • AlertDialog style when using setView()
  • HTML5 video only works in IE. The other browsers shows the black screen
  • How to split circle in to the sectors in google maps?
  • Django model inheritance, filtering models
  • AndEngine Applying Transparancy to AndEngine View
  • Sequential (transactional) API calls in angular 4 with state management
  • how to avoid repetitive constructor in children
  • Spark fat jar to run multiple versions on YARN
  • Android fill_parent issue
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • JFileChooser in front of fullscreen Swing application
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Javascript Callbacks with Object constructor
  • Javascript convert timezone issue
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • ActionScript 2 vs ActionScript 3 performance
  • Arrays break string types in Julia
  • Matrix multiplication with MKL
  • json Serialization in asp
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?