Import java.util.inputmismatchexception;import java.util.random;import java.util.scanner;import javax.swing.JOptionPane;/** * Gobang Games. For the double versus the Man-machine battle (the machine is random input, does not add to the exact algorithm) draw the situation did not make judgments. * <p> * on the 15*15 board, there are 572 things to win. <br> * in a Gobang game, computers have to know what winning combinations they have, so we have to figure out the total number of winning combinations. <br> * (1) calculates the number of winning combinations in the horizontal direction, the winning combination of each line is: 11, a total of 15 rows, so the number of winning combinations in the horizontal direction is: 11*15=165. <br> * (2) calculates the total number of winning combinations in the vertical direction, the winning combination of each column is: 11, a total of 15 columns, so the winning combination of the vertical direction is:11*15=165.<br> * (3) calculates the total number of winning combinations in the positive diagonal direction, The total number of winning combinations on the positive diagonal is 11+ (10+9+...+2+1) *2=121.<br> * (4) calculates the total number of winning combinations in the counter-diagonal direction, the total number of winning combinations on the counter diagonal is 11+ (10+9+...+2+1) * 2=121.<br> * So, the total number of victories is 165+165+121+121=572. * * @author frank * */public class NewGoBang{// Static code block STATIC{SYSTEM.OUT.PRINTLN ("Welcome to Gobang World!") ");} public final static int size = 15; String[][] board = new string[size][size];boolean flag = true;// creates an arbitrary random object. Random ran = new random ();/** * draw the chessboard. "Ten" */public void initboard () {for (int x = 0; x < board.length; x++) {for (int y = 0; y < board.length; y++) { board[x][y] = "Ten";// ╂ ,╋}}printarray (board);} /** * determine if a piece has five consecutive . * * @param row * the horizontal axis * @param of chess pieces column * the ordinate * @param of chess pieces str * array of strings * @param c * string argument (that is, pawn) */public void isfive (int row, int column, String[][] str, String c) {// transverse judgment int m, l;m = 1; l = 1;while ((column + m) < size && str[row][column + m] == c) {l++;m++;} m = 1;while ((column - m) >= 0 && str[row][column - m] == c) {l++;m++;} ShowMessage (l, c);// longitudinal judgement m = 1; l = 1;while ((row + m) < size && str[row + m ][column] == c) {l++;m++;} m = 1;while ((row - m) >= 0 && str[row - m][ column] == c) {l++;m++;} ShowMessage (l, c);// oblique judgment Italic (row, column, str, c);} Oblique judgment Public void italic (int row, int column, string[][] str, STRING&NBSP;C) {int l = 1;// defines a pointer, more than five wins. int m = 1;// Oblique judgment while (row + m) < size && (column + m) < size&& str[row + m][column + m] == c) {l++;m++;} m = 1;//m Reset to 1;while ((row - m) >= 0 && (column - &NBSP;M) >= 0&& str[row - m][column - m] == c) {L++;m++ ;} ShowMessage (l, c);// inverse oblique judgment m = 1; l = 1;while (row + m) < size && (column - m ) { >= 0&& str[row + m][column - m] == c) {L++;m++;} m = 1;//m Reset to 1;while ((row - m) >= 0 && (column + &NBSP;M) < size&& str[row - m][column + m] == c) {L++;m ++;} ShowMessage (l, c);} Determine if it equals five public void showmessage (int point, string c) {if (POint >= 5) {System.out.println ( "Congratulations + c + " Chess wins!) ")//can use the following sentence to pop up a dialog box, more beautiful.
Joptionpane.showmessagedialog (null, "Congratulations + c + " Chess win!) Of course, you have to end the process after winning. System.exit (0);} else{point = 0;}} /* Here you can choose to play double or man-machine battle. don't get too excited, man-machine PvP is just random input, and it doesn't add any core algorithms. Follow the opportunity I will continue to update. */public void show () {System.out.println ("Double battle Please enter: 1, Man-Machine battle Please enter 2. "); /input data with scanner. Scanner sc = new scanner (system.in); Int option = sc.nextint ();// Here you can choose between a double battle or a man-machine battle. Switch (option) { //duo vs. Case 1:while (flag) {System.out.println ("Ask black to enter the coordinates of the pawn: (e.g., 1 3)"); Maninput (""); SYSTEM.OUT.PRINTLN ("Please white enter the coordinates of the pawn: (e.g.: 1 3)"); Maninput ("0");} The man-machine battle case 2:while (flag) {System.out.println ("Please enter the coordinates of the pawn: (e.g.: 1 3)"); Maninput (") SYSTEM.OUT.PRINTLN ("Computer:"); computer ("0");} Machine battle, used to quickly debug case 3: while (flag) {System.out.println ("pc A:"); Computer (""); SYSTEM.OUT.PRINTLN ("Computer B:"); computer ("0");}} Computer randomly. Public boolean computer (string c) {int row = ran.nextint (size); Int column = ran.nextint (size); while (board[row][column] != "ten") {row = ran.nextint (size); column = ran.nextint (size);} if (board[row][column] == "ten") {System.out.println (" + row + ", " + column + ")" Board[row][column] = c;printarray (board); Isfive (Row, column, board , c);} Else return false;return true;} Artificial. Input coordinate coordinateprivate void maninput (string c)// int row,int column{try{ Scanner scan = new scanner (system.in); &NBSP;INT&NBsp;row = scan.nextint (); int column = scan.nextint ();if (board[row][column ] == "Ten")//row>=0&&column>=0&&row<size&&column<size&&{ System.out.println (" + row + ", " + column + "); Board[row][column] = c;// Optional characters ' ★ ', ' █ ', ' ▓ ', ' ', ' ' ╋ ', ' "' 0printArray (board); Isfive (Row, column, board, &NBSP;C);} else{
system.out.println ("the position has a pawn, please put it in another position!") "); Maninput (c);}} catch (arrayindexoutofboundsexception e) {
System.out.println ("Beyond the board position, please re-enter!") "); Maninput (c);} catch (Inputmismatchexception e) {//joptionpane.showmessagedialog (null, "the number entered is not valid, please re-enter!") "); System.out.println ("The number entered is not valid, please re-enter!") "); Maninput (c);} System.out.println ("I am beautiful debugging, I am everywhere!") ");} Prints a two-dimensional array of type String private void PrintArray (string[][] arr) {for (int i = 0; i < arr.length; i++) {for (int j = 0; J < A Rr[i].length; J + +) {System.out.print (arr[i][j]);} System.out.println ("");}} public static void Main (string[] args) {Newgobang Gobang = new Newgobang (); Gobang.initboard (); Gobang.show ();}}
This article is from the "card Dark Mile" blog, please be sure to keep this source http://kaanli.blog.51cto.com/6744784/1631302
Java implementation Simple Gobang (although a lot of bugs, but will continue to improve)