Using Java, Goji is written under console operation as a basic knowledge such as reviewing two-dimensional arrays, object-oriented, etc. w represents white chess, b represents black chess
import java.util.Scanner;public class MainMethod {public static char[][] c = new char[10][10];public static void main(String[] args) {MainMethod mainMethod = new MainMethod();mainMethod.init();int i;boolean color;int[] point = new int[2];Scanner scanner = new Scanner(System.in);for (i = 1; i < 100; i++) {mainMethod.draw();// Draw the chessboard System.out.println("Please enter the coordinates, the range is (0,0) to (9,9)");int x = scanner.nextInt();int y = scanner.nextInt();if (i % 2 == 1)color = true;elsecolor = false;point[0] = x;point[1] = y;if (mainMethod.paint(point, color) == false) {//System.out.println("The position under "is unreasonable, please start over");break;}if (mainMethod.win() == 1) {mainMethod.draw();System.out.println("Congratulations on winning the white chess!!");break;} else if (mainMethod.win() == -1) {mainMethod.draw();System.out.println("Congratulations on winning the black chess!!");break;}}if (i == 100)System.out.println("Trap");}public char[][] init() {// Initialize the board for (int i = 0; i < c.length; i++) {for (int j = 0; j < c.length; j++) {c[i][j] = '+';}}return c;}public boolean paint(int[] point, boolean colr) {// determine whether the position is correct. If the board is changed correctly, boolean b = true;int x = point[0];int y = point[1];if (inBoard(point) == false || c[x][y] == 'w' || c[x][y] == 'b' || x < 0) {b = false;return b;}else if (colr == true) {// If (c[x][y] == '+') {c[x][y] = 'w';}}else if (colr == true) {// If (c[x][y] == '+') {c[x][y] = 'w';}}else if (colr == false) {// If (c[x][y] == '+') {c[x][y] = 'b';}}return b;}public boolean inBoard(int[] point) {// Determine whether point is on the board int x = point[0];int y = point[1];if (x < 0 || y < 0 || x >= c.length || y >= c.length)return false;elsereturn true;}public int win() {// Determine win, 1 means white win, -1 black win, 0 no win, int whoWin = 0;int[] point = new int[2];outer: while (true) {for (int i = 0; i < c.length; i++) {for (int j = 0; j < c.length; j++) {if (c[i][j] == 'w') {// traverse the array and the first white chess point[0] = i;point[1] = j + 4;if (inBoard(point)) {// If there is a chessboard with 4 numbers to the right if (c[i][j + 1] == 'w' && c[i][j + 2] == 'w' && c[i][j + 3] == 'w' && c[i][j + 4] == 'w') {whoWin = 1;break outer;}}point[0] = i + 4;point[1] = j + 4;if (inBoard(point)) {// If there is a chessboard that counts 4 to the right and downwards if (c[i + 1][j + 1] == 'w' && c[i + 2][j + 2] == 'w' && c[i + 3][j + 3] == 'w'&& c[i + 4][j + 4] == 'w') {wWin = 1;break outer;}}point[0] = i + 4;point[1] = j;if (inBoard(point)) {// If there is a chessboard with 4 numbers downwards if there is a chessboard if there is (c[i + 1][j] == 'w' && c[i + 2][j] == 'w' && c[i + 3][j] == 'w' && c[i + 4][j] == 'w') {wWin = 1;break outer;}}point[0] = i - 4;point[1] = j + 4;if (inBoard(point)) {// If there is a chessboard if there is a chessboard if there is a chessboard if there is a chessboard if there is a chessboard if there is a chessboard if there is a chessboard if there is a chessboard if there is a chessboard if there is a 4 numbers downwards if there is (c[i - 1][j + 1] == 'w' && c[i - 2][j + 2] == 'w' && c[i - 3][j + 3] == 'w'&& c[i - 4][j + 4] == 'w') {wWin = 1;break outer;}}} else if (c[i][j] == 'b') {point[0] = i;point[1] = j + 4;if (inBoard(point)) {// If there is a chessboard with 4 numbers to the right if (c[i][j + 1] == 'b' && c[i][j + 2] == 'b' && c[i][j + 3] == 'b' && c[i][j + 4] == 'b') {whoWin = -1;break outer;}}point[0] = i + 4;point[1] = j + 4;if (inBoard(point)) {// If there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards (c[i + 1][j + 1] == 'b' && c[i + 2][j + 2] == 'b' && c[i + 3][j + 3] == 'b'&& c[i + 4][j + 4] == 'b') {whoWin = -1;break outer;}}point[0] = i + 4;point[1] = j;if (inBoard(point)) {// If there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if there is a chessboard that counts 4 downwards if (c[i + 1][j] == 'b' && c[i + 2][j] == 'b' && c[i + 3][j] == 'b' && c[i + 4][j] == 'b') {whoWin = -1;break outer;}}point[0] = i - 4;point[1] = j + 4;if (inBoard(point)) {// If there is a chessboard that counts 4 left and downwards if (c[i - 1][j + 1] == 'b' && c[i - 2][j + 2] == 'b' && c[i - 3][j + 3] == 'b'&& c[i - 4][j + 4] == 'b') {whoWin = -1;break outer;}}}}}} whoWin = 0;break outer;}return whoWin;}public void draw() {// Draw the board for (int i = 0; i < c.length; i++) {for (int j = 0; j < c.length; j++) {System.out.print(c[i][j] + " ");}System.out.println();}}}Running results:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
I won't make it if anyone loses and wins. It's too long and takes up space. .
Summarize
The above is the entire content of this article about Java programming and implementing Gozi Renren's code examples, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!