Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Three In A Row Solver
import java.util.*; import java.lang.*; class Rextester { // Requires possible initial board to be set by user public static void main(String args[]) { // 1 = Blue, // 0 = White, //'-'= Empty String[] board = { "1---1-", "---11-", "--11--", "0-----", "------", "-0----" }; /* "------", "------", "------", "------", "------", "------" */ // Print initial board preset printBoard(board); // Solve lines in board until full... for (int in = 0; in < 6; in++) { // Solve Rows for (int s = 0; s < board.length; s++) { // Solve each horizontal line (row) board[s] = solveLine(board[s]); } // Solve Columns for (int s = 0; s < 6; s++) { // Compile a column, taking chars from lines String i = ""; for (String j : board) { i += j.charAt(s); } // Solve the line i = solveLine(i); // Place solved line back into board for (int j = 0; j < 6; j++) { StringBuilder o = new StringBuilder(board[j]); o.setCharAt(s, i.charAt(j)); board[j] = o.toString(); } } // Show step... printBoard(board); } // Print out final "solved" board System.out.println("--------------"); printBoard(board); } private static String solveLine(String i) { // Fill in rest if 3 blue/white pieces are set if (i.length()-i.replaceAll("1", "").length() == 3) { i = i.replaceAll("-", "0"); } else if (i.length()-i.replaceAll("0", "").length() == 3) { i = i.replaceAll("-", "1"); } // E.g. 1-1--- => 101--- i = i.replaceAll("1-1", "101"); i = i.replaceAll("0-0", "010"); // E.g. -11--- => 0110-- i = i.replaceAll("11-", "110"); i = i.replaceAll("00-", "001"); i = i.replaceAll("-11", "011"); i = i.replaceAll("-00", "100"); // E.g. 1-0--1 => 100101 i = i.replaceAll("1.0..1", "100101"); i = i.replaceAll("1..0.1", "101001"); i = i.replaceAll("0.1..0", "011010"); i = i.replaceAll("0..1.0", "010110"); // E.g. -10--1 => 010--1 i = i.replaceAll("-10(?=..1)", "010"); i = i.replaceAll("-01(?=..0)", "101"); i = i.replaceAll("(?<=1..)01-", "010"); i = i.replaceAll("(?<=0..)10-", "101"); // E.g. 1---1- => 1---10 i = i.replaceAll("-1(?=...1)", "01"); i = i.replaceAll("-0(?=...0)", "10"); i = i.replaceAll("(?<=1...)1-", "10"); i = i.replaceAll("(?<=0...)0-", "01"); // E.g. 1----1 => 10--01 i = i.replaceAll("1----1", "10--01"); i = i.replaceAll("0----0", "01--10"); // E.g. 110--- => 110--0 i = i.replaceAll("-(?=..011)", "0"); i = i.replaceAll("-(?=..100)", "1"); i = i.replaceAll("(?<=110..)-", "0"); i = i.replaceAll("(?<=001..)-", "1"); return i; } private static void printBoard(String[] bo) { System.out.println("\n"); for (String b: bo) { System.out.println(b.replaceAll("", " ")); } } }
run
|
edit
|
history
|
help
0
victory1
preincrement
Bubble Sort Java
Simple imitation for show banners on priorities
epeenconverter
Kochergina_4
quickSort_LinkedList
Sample for https://stackoverflow.com/a/49984959/4216641
Date_From_Str
To check whether a given number is smith number or not