Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void printM(int[][] x) {
- for (int i = 0; i < x.length; i++) {
- for (int j = 0; j < x[i].length; j++) {
- System.out.print(x[i][j] + " ");
- }
- System.out.println();
- }
- }
- boolean riga(int[][] x) {
- int i = 0;
- int j = 0;
- int variabile = x[j][0];
- int contatore = 1;
- while (i < x[0].length && j < x[0].length) {
- i++;
- if (x[j][i] == variabile && x[j][i] != 0) {
- contatore++;
- if (contatore == x[0].length) {
- return true;
- }
- } else {
- j++;
- if (i < x[0].length && j < x[0].length) {
- i = 0;
- contatore = 1;
- variabile = x[j][0];
- } else {
- return false;
- }
- }
- }
- return false;
- }
- boolean colonna(int[][] x) {
- int i = 0;
- int j = 0;
- int variabile = x[0][j];
- int contatore = 1;
- while (i < x[0].length && j < x[0].length) {
- i++;
- if (x[i][j] == variabile && x[i][j] != 0) {
- contatore++;
- if (contatore == x[0].length) {
- return true;
- }
- } else {
- j++;
- if (i < x[0].length && j < x[0].length) {
- i = 0;
- contatore = 1;
- variabile = x[0][j];
- } else {
- return false;
- }
- }
- }
- return false;
- }
- boolean diagonalePrima(int[][] x) {
- int i = 0;
- int j = 0;
- int variabile = x[i][j];
- int contatore = 1;
- while (i < x[0].length && j < x[0].length) {
- if (variabile != 0) {
- j++;
- i++;
- }
- if (x[i][j] == variabile && x[i][j] != 0) {
- contatore++;
- if (contatore == x[0].length) {
- return true;
- }
- } else {
- return false;
- }
- }
- return false;
- }
- boolean diagonaleSeconda(int[][] x) {
- int i = 0;
- int j = x[0].length -1 ;
- int variabile = x[i][j];
- int contatore = 1;
- while (i < x.length && j >= 0) {
- if (variabile != 0) {
- j--;
- i++;
- }
- if (x[i][j] == variabile && x[i][j] != 0) {
- contatore++;
- if (contatore == x[0].length) {
- return true;
- }
- } else {
- return false;
- }
- }
- return false;
- }
- boolean tictactoe(int[][] a) {
- if(a[0][0] == 0){return false;}
- if(a[0][0] == 1){return true;}
- if(a[0][0] == 2){return true;}
- if (riga(a) == true) { return true;}
- if (colonna(a) == true) {return true;}
- if (diagonalePrima(a) == true) {return true;}
- if (diagonaleSeconda(a) == true) {return true;}
- return false;
- }
- int[][] x = {{1,2,0}, {2,1,1}, {2,0,2}};
- printM(x);
- System.out.println(x[0].length);
- System.out.println(riga(x));
- System.out.println(colonna(x));
- System.out.println(diagonalePrima(x));
- System.out.println(diagonaleSeconda(x));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement