Search code examples
javamathtic-tac-toe

TicTacToe - generating solutions


Given a grid of nxn squares, where each square has an id, the first(top left) square has id 0 (so a 5x5 grid will have ids 0-24) like below:

00 01 02 03 04

05 06 07 08 09

10 11 12 13 14

15 16 17 18 19

20 21 22 23 24

I need to generate all diagonal solutions of length Y. So if Y is 3, then some of the solutions will be:

0,6,12

and

11,17,23

but obviously NOT

3,9,15 (because the 15 does not follow on diagonally)

Any ideas how these solutions can be generated?

This is what I've got so far (dimension = 5, inARow = 3):

public ArrayList<int[]> getSolutions(int dimension, int inARow) {

    ArrayList<int[]> solutions = new ArrayList<int[]>();

    //create row solutions
    for(int i=0; i<dimension*dimension; i = i+dimension) {
        for(int j=i; j<=i+dimension - inARow; j++){            
            int[] row = new int[inARow];
            int counter = 0;
            for(int k=j; k<j+inARow; k++){
                row[counter++] = k;
            }
            solutions.add(row);
        }
    }

    //create column solutions        
    for(int i=0;i<dimension;i++){
        for(int j=i; j<(dimension*dimension)-(dimension*inARow)+dimension;j=j+dimension){
            int[] col = new int[inARow];
            int counter = 0;
            for(int k=j;k<j+(dimension*inARow);k=k+dimension){
                col[counter++] = k;
            }
            solutions.add(col);
        }
    }

    //create diagonals        
    for(int i=0; i<dimension*dimension; i++){            
        for(int j=i; j<i+(dimension * inARow); j = j+dimension+1){
            System.out.println(j);
        }            
    }                

    return solutions;

This gives me all the diagonal solutions but also gives me the bad ones like 3,9,15. Im having trouble eliminating those.

Anti-diagonals are also solutions so 2,6,10 would also be a solution but if I get normal diagonals working I can probably do the same for anti-diagonals.


Solution

  • It is your lucky day... I updated the answer to work as a general solution for all grid sizes and lengths

    In coffee script, which makes nice pseudo code

    w = 5 # width of the grid
    h = 5 # height of the grid
    l = 3 # length to capture
    
    m = [] # matches
    
    for i in [0..(w*h)-1] # loop through each square
        col = i % w # calculate what column we are in
        row = Math.floor i / w # calculate what row we are in
        nums = [] # re-set matched lines array
        if col < w - (l-1) and row < h - (l-1) # if there is space for diagonal right
            for j in [0..l-1] # loop until length is reached
                nums.push i+(j*w)+j # push it onto the array of squares
            m.push nums # push into the array of matched lines
        nums = []
        if col > l-2 and row < h-l+1 # if there is space for diagonal left
            for j in [0..l-1]
                nums.push i+(j*w)-j
            m.push nums
    
    console.dir m
    # or
    console.log i.join "," for i in m
    

    Which compiles to javascript (so you can test it)

    var col, h, i, j, l, m, nums, row, w, _i, _len, _ref, _ref2, _ref3;
    w = 5;
    h = 5;
    l = 3;
    m = [];
    for (i = 0, _ref = (w * h) - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
      col = i % w;
      row = Math.floor(i / w);
      nums = [];
      if (col < w - (l - 1) && row < h - (l - 1)) {
        for (j = 0, _ref2 = l - 1; 0 <= _ref2 ? j <= _ref2 : j >= _ref2; 0 <= _ref2 ? j++ : j--) {
          nums.push(i + (j * w) + j);
        }
        m.push(nums);
      }
      nums = [];
      if (col > l - 2 && row < h - l + 1) {
        for (j = 0, _ref3 = l - 1; 0 <= _ref3 ? j <= _ref3 : j >= _ref3; 0 <= _ref3 ? j++ : j--) {
          nums.push(i + (j * w) - j);
        }
        m.push(nums);
      }
    }
    console.dir(m);
    for (_i = 0, _len = m.length; _i < _len; _i++) {
      i = m[_i];
      console.log(i.join(","));
    }