Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.5 kB
10
Indexable
Never
import java.util.*;

class Main {
  public static void main(String[] args) {
    print2D(determineWinner(5,5));
  }

  public static Boolean[][] determineWinner(int p, int q) {
    Boolean[][] matrix = new Boolean[p+1][q+1];
    // starting cases
    matrix[0][0] = false;
    matrix[1][0] = true;
    matrix[0][1] = true;

    for (int i = 0; i < p+1; i++) {
      for (int j = 0; j < q+1; j++) {
        if (matrix[i][j] == null) {
          //if pile 1 has no more stones
          if (i == 0) {
            matrix[i][j] = matrix[i][j-1] ? false : true;
          }
          // if pile 2 has no more stones
          else if (j == 0) {
            matrix[i][j] = matrix[i-1][j] ? false : true;
          }
            // if pile 1 and 2 have at least 1 stone
          else {
            // can either take 1 stone from i, 1 stone from j, or 1 stone from both i and j
            Boolean[] neighboring_moves = {matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1]};
            boolean found = false;
            for(boolean b : neighboring_moves) {
              if (!b) {
                found = true;
              }
              
            } matrix[i][j] = (found) ? true : false;
          }
        }
      }
    }
    
    return matrix;
  }

  
 

  public static void print2D(Boolean mat[][])
    {
        for (Boolean[] row : mat)
 
            // converting each row as string
            // and then printing in a separate line
            System.out.println(Arrays.toString(row));
    }
  
}