我有一个2D数组 map [6] [6] ,这些值是在for循环中随机生成的
这是输出的样子(在整数值中)
有5种不同的砖颜色
我希望能够在随机生成的时候(水平和垂直)检查3-6相同的砖颜色,以及当用户更改砖值时
我也是Java的新手(3个月左右)所以我想要一个简单的方法来做到这一点(我不知道对象之类的东西)
答案 0 :(得分:0)
对不起约翰的延迟。假期的事情和客人。这是一年中的那个时候。 :)
无论如何。我很快开发了两个可运行的类,您可以使用它们来实现您实际命名的方法。它被称为 hasThreeInSix(),即使它不限于任何三个或六个,我们将给它的名称。
即使它是专门为2D整数数组设计的,也不需要花费太多精力来修改它。
下面的第一个可运行仅用于测试。它只是通过输出控制台(窗格)显示正在进行的操作
第二个可运行的是您可能想要应用于执行任务的内容。仔细查看 randomBoard()方法,以便了解我们如何使用 hasThreeInSix()方法。
以下是可运行的测试类的代码:
import java.util.ArrayList;
import java.util.Scanner;
public class TESTThreeAndSixMethod {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String input = "";
while (!input.equals("quit")) {
Echo("\nPress ENTER to generate random number blocks\n"
+ "for 2 dimensional array or enter 'quit' to exit:");
input = userInput.nextLine();
if (input.toLowerCase().equals("quit")) { System.exit(0); }
int[][] x = new int[6][6];
randomBoard(x);
}
userInput.close();
}
public static void randomBoard (int[][] x) {
int[][] map = new int[6][6];
Echo("");
for (int i = 0 ; i < x.length ; i++) {
String arrayString = "";
for (int j = 0 ; j < x[i].length ; j++) {
int tmp = 1 + (int) (Math.random () * 5);
map[i][j] = tmp;
arrayString+= String.valueOf(map[i][j]);
}
// Play with the maxHorizontal, maxVertical, and numberToCheckFor
// parameters. What you see here now will never allow any array
// row of elements contain 3 of any same consecutive single digit
// values either horizontally of vertically. Nowhere should there
// ever be any three of the same digits either vertically or hor-
// zontally. If you just want to deal with a specific number from
// 0 to 9 then change the -1 to that number.
String result = hasThreeInSix(map, i, 3, 3, -1);
if (!"".equals(result)) {
Echo(arrayString + " -- " + result);
//i--;
}
else { Echo(arrayString); }
}
}
/**
* Detect specified same single digit elements either or horizontally and or vertically within
* a 2D Array. This method is to be used during the creation of the 2D Array so as to eliminate
* specified same elements from being placed into the Array.<br><br>
*
* @param array (Integer 2D Array) the array being created.<br>
*
* @param currentIndexRow (Integer) The most currently added ROW index number.<br>
*
* @param maxHorizontal (Integer) The number of same digit elements to detect horizontally. If
* 0 is supplied then no horizontal check is done. 1 can not be supplied. If it is then the method
* simply returns null string which ultimately indicates that the array row is good. 1 is not
* permitted because this will just lead to a infinite loop.<br>
*
* @param maxVertical (Integer) The same number of digit elements to detect vertically. If 0 is
* supplied then no Vertical check is done. 1 can not be supplied. If it is then the method simply
* returns null string which ultimately indicates that the array row is good. 1 is not permitted
* because this will just lead to a infinite loop.<br>
*
* @param numberToCheckFor (Integer) The number to check for to see if there is a repetitive
* sequence horizontally or vertically within the Array. Any single digit number from 0 to 9
* can be suppled. If -1 is supplied then this method will check for repetitive sequences for
* ALL numbers from 0 to 9 both horizontally and vertically.<br>
*
* @return (String) If a null string ("") is returned then this indicates that the Array Row is
* good. If a vertical and or horizontal sequence is detected then a string is returned indicating
* either the row sequence value for horizontal detection or the rows and columns detected for
* vertical detection.
*/
public static String hasThreeInSix(int[][] array, int currentIndexRow, int maxHorizontal,
int maxVertical, int numberToCheckFor) {
String indexString = "";
int n = 1, check = numberToCheckFor;
if (maxHorizontal > 0) {
// Check for horizontal values...
String aStrg = "";
if (maxHorizontal < 2 || maxVertical < 2) {
Echo("\n\u001B[31mmaxHorizontal and or maxVertical Parameter Error!\n"
+ "\u001B[34mNeither of these parameters can be less than 2\n"
+ "otherwise an infinite loop will occure! Exiting Method!\u001B[39;49m");
return "";
}
for (int i = 0; i < array[currentIndexRow].length; i++) {
aStrg+= array[currentIndexRow][i];
}
if (numberToCheckFor == -1) { n = 10; }
for (int c = 0; c < n; c++) {
String valToCheckFor = "";
if (numberToCheckFor == -1) { check = c; }
for (int i = 1; i <= maxHorizontal; i++) {
valToCheckFor+= String.valueOf(check);
}
if (aStrg.contains(valToCheckFor)) { return "Horizontal " + maxHorizontal + " Detected! (" + aStrg + ")"; }
}
}
if (maxVertical > 0) {
// Check for vertical values...
ArrayList<String> listString = new ArrayList<>();
for (int i = 0 ; i < array.length ; i++) {
String aString = "";
for (int j = 0 ; j < array[i].length ; j++) {
aString+= String.valueOf(array[i][j]);
}
if (!"000000".equals(aString)) { listString.add(aString); }
}
n = 1; check = numberToCheckFor;
if (numberToCheckFor == -1) { n = 10; }
for (int c = 0; c < n; c++) {
if (numberToCheckFor == -1) { check = c; }
String itemToLocate = String.valueOf(check);
String foundInfo = "";
for (int i = 0; i < listString.get(0).length(); i++) {
int counter = 0;
for (int j = 0; j < listString.size(); j++) {
if (listString.get(j).substring(i,i+1).equals(itemToLocate)) {
if ("".equals(foundInfo)) { foundInfo = "Vertical " + maxVertical + " Detection in Column " + (i+1) + " On Rows: -> " + (j+1); }
else { foundInfo+= ", " + (j+1); }
counter++;
}
else {counter = 0; foundInfo = ""; }
if (counter == maxVertical) {
indexString = foundInfo;
break;
}
}
if (!"".equals(indexString)) { break; }
}
if (!"".equals(indexString)) { break; }
}
}
return indexString;
}
private static void Echo(Object object) {
System.out.println(object);
}
}
以下是可运行类的代码,其中包含可用的 hasThreeInSix()方法:
import java.util.ArrayList;
import java.util.Scanner;
public class ThreeAndSixMethod {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String input = "";
while (!input.equals("quit")) {
Echo("\nPress ENTER to generate random number blocks\n"
+ "for 2 dimensional array or enter 'quit' to exit:");
input = userInput.nextLine();
if (input.toLowerCase().equals("quit")) { System.exit(0); }
int[][] x = new int[6][6];
randomBoard(x);
}
userInput.close();
}
public static void randomBoard (int[][] x) {
int[][] map = new int[6][6];
Echo("");
for (int i = 0 ; i < x.length ; i++) {
String arrayString = "";
for (int j = 0 ; j < x[i].length ; j++) {
int tmp = 1 + (int) (Math.random () * 5);
map[i][j] = tmp;
// you can remove the line below if you don't want to
// display anything in output.
arrayString+= String.valueOf(map[i][j]);
}
// Play with the maxHorizontal, maxVertical, and numberToCheckFor
// parameters. What you see here now will never allow any array
// row of elements contain 3 of any same consecutive single digit
// values either horizontally of vertically. Nowhere should there
// ever be any three of the same digits either vertically or hor-
// zontally. If you just want to deal with a specific number from
// 0 to 9 then change the -1 to that number.
String result = hasThreeInSix(map, i, 3, 3, -1);
if (!"".equals(result)) { i--; }
// You can remove the else{} line below if you
// don't want to display anything in output.
else { Echo(arrayString); }
}
}
/**
* Detect specified same single digit elements either or horizontally and or vertically within
* a 2D Array. This method is to be used during the creation of the 2D Array so as to eliminate
* specified same elements from being placed into the Array.<br><br>
*
* @param array (Integer 2D Array) the array being created.<br>
*
* @param currentIndexRow (Integer) The most currently added ROW index number.<br>
*
* @param maxHorizontal (Integer) The number of same digit elements to detect horizontally. If
* 0 is supplied then no horizontal check is done. 1 can not be supplied. If it is then the method
* simply returns null string which ultimately indicates that the array row is good. 1 is not
* permitted because this will just lead to a infinite loop.<br>
*
* @param maxVertical (Integer) The same number of digit elements to detect vertically. If 0 is
* supplied then no Vertical check is done. 1 can not be supplied. If it is then the method simply
* returns null string which ultimately indicates that the array row is good. 1 is not permitted
* because this will just lead to a infinite loop.<br>
*
* @param numberToCheckFor (Integer) The number to check for to see if there is a repetitive
* sequence horizontally or vertically within the Array. Any single digit number from 0 to 9
* can be suppled. If -1 is supplied then this method will check for repetitive sequences for
* ALL numbers from 0 to 9 both horizontally and vertically.<br>
*
* @return (String) If a null string ("") is returned then this indicates that the Array Row is
* good. If a vertical and or horizontal sequence is detected then a string is returned indicating
* either the row sequence value for horizontal detection or the rows and columns detected for
* vertical detection.
*/
public static String hasThreeInSix(int[][] array, int currentIndexRow, int maxHorizontal,
int maxVertical, int numberToCheckFor) {
String indexString = "";
int n = 1, check = numberToCheckFor;
if (maxHorizontal > 0) {
// Check for horizontal values...
String aStrg = "";
if (maxHorizontal < 2 || maxVertical < 2) {
Echo("\n\u001B[31mmaxHorizontal and or maxVertical Parameter Error!\n"
+ "\u001B[34mNeither of these parameters can be less than 2\n"
+ "otherwise an infinite loop will occure! Exiting Method!\u001B[39;49m");
return "";
}
for (int i = 0; i < array[currentIndexRow].length; i++) {
aStrg+= array[currentIndexRow][i];
}
if (numberToCheckFor == -1) { n = 10; }
for (int c = 0; c < n; c++) {
String valToCheckFor = "";
if (numberToCheckFor == -1) { check = c; }
for (int i = 1; i <= maxHorizontal; i++) {
valToCheckFor+= String.valueOf(check);
}
if (aStrg.contains(valToCheckFor)) { return "Horizontal " + maxHorizontal + " Detected! (" + aStrg + ")"; }
}
}
if (maxVertical > 0) {
// Check for vertical values...
ArrayList<String> listString = new ArrayList<>();
for (int i = 0 ; i < array.length ; i++) {
String aString = "";
for (int j = 0 ; j < array[i].length ; j++) {
aString+= String.valueOf(array[i][j]);
}
if (!"000000".equals(aString)) { listString.add(aString); }
}
n = 1; check = numberToCheckFor;
if (numberToCheckFor == -1) { n = 10; }
for (int c = 0; c < n; c++) {
if (numberToCheckFor == -1) { check = c; }
String itemToLocate = String.valueOf(check);
String foundInfo = "";
for (int i = 0; i < listString.get(0).length(); i++) {
int counter = 0;
for (int j = 0; j < listString.size(); j++) {
if (listString.get(j).substring(i,i+1).equals(itemToLocate)) {
if ("".equals(foundInfo)) { foundInfo = "Vertical " + maxVertical + " Detection in Column " + (i+1) + " On Rows: -> " + (j+1); }
else { foundInfo+= ", " + (j+1); }
counter++;
}
else {counter = 0; foundInfo = ""; }
if (counter == maxVertical) {
indexString = foundInfo;
break;
}
}
if (!"".equals(indexString)) { break; }
}
if (!"".equals(indexString)) { break; }
}
}
return indexString;
}
private static void Echo(Object object) {
System.out.println(object);
}
}
我希望这会有所帮助。