Chapter 1 of crazy Java Handouts

Source: Internet
Author: User

Chapter 1 of crazy Java Handouts
1 package com. test; 2 3 public class Chessboard {4 // defines a two-dimensional array as a Chessboard 5 private String [] [] board; 6 // defines the size of the board 7 public static final int BOARD_SIZE = 22; 8 9 // initialize the checker 10 public void initBoard () {11 board = new String [BOARD_SIZE] [BOARD_SIZE]; // create a two-dimensional array 12 // define the initial value, nested loop 13 for (int I = 0; I <BOARD_SIZE; I ++) {14 for (int j = 0; j <BOARD_SIZE; j ++) {15 board [I] [j] = "10"; 16} 17} 18} 19 // The meaning of the test function is unknown 20 public void test () {21 Object [] [] array = new Object [10] [10]; 22 for (int I = 0; I <array. length; I ++) {23 for (int j = 0; j <array. length; j ++) {24 array [I] [j] = new Object (); 25} 26} 27} 28 // print the checker 29 public void printBoard () {30 for (int I = 0; I <BOARD_SIZE; I ++) {31 for (int j = 0; j <BOARD_SIZE; j ++) {32 System. out. print (board [I] [j]); 33} 34 System. out. print ("\ n"); 35} 36} 37 // place the chess piece 38 public void setBoard (int posX, int posY, String chessman) {39 this. board [posX] [posY] = chessman; 40} 41 // returns the checker 42 public String [] [] getBoard () {43 return this. board; 44} 45 46}Chessboard. java 1 package com. test; 2 3 public enum Chessman {4 BLACK ("●"), WHITE ("○ "); 5 // set the attributes of two static pawns. 6 // equivalent to public static final Chessman BLACK = new Chessman ("●") 7 private String chessman; 8 // After setting, you can pass. BLACK. set chessman to 9 private Chessman (String chessman) {10 this. chessman = chessman; 11} 12 // return the 13 public String getChessman () {14 return this. chessman; 15} 16}Chessman. java

The Chessboard and Chessman)

Finally, gameGobangGame. java

1 package com. test; 2 3 import java. io. bufferedReader; // input 4 import java. io. inputStreamReader; // output 5 6 public class GobangGame {7 private final int WIN_COUNT = 5; // set the number of winning pawns 8 private int posX = 0; 9 private int posY = 0; 10 private Chessboard chessboard; // an instance of the Chessboard class 11 12 // call the chessboard instance 13 public GobangGame (Chessboard chessboard) {14 this. chessboard = chessboard; 15} 16 // receives the x and y values in inputStr and assigns them to pos X/Y 17 public boolean isValid (String inputStr) {18 String [] posStrArr = inputStr. split (","); 19 // splits the string into character arrays with the separator "," 20 try {21 posX = Integer. parseInt (posStrArr [0])-1; 22 posY = Integer. parseInt (posStrArr [1])-1; 23} catch (NumberFormatException e) {24 chessboard. printBoard (); 25 System. out. println ("pls input position like num, num"); 26 return false; 27} 28 if (posX <0 | posX> = Chessboard. BOARD_SIZE | posY <0 | posY> = Chessboard. BOARD_SIZE) {29 chessboard. printBoard (); 30 System. out. println ("reinput"); 31 return false; 32} 33 // receives chessboard 34 String [] [] board = chessboard. getBoard (); 35 36 37 if (board [posX] [posY]! = "10") {38 chessboard. printBoard (); 39 System. out. println ("re input"); 40 return false; 41} 42 return true; 43} 44 // main game flow function, with figure 45 public void start () throws Exception {46 boolean isOver = false; 47 chessboard. initBoard (); 48 chessboard. printBoard (); 49 BufferedReader br = new BufferedReader (new InputStreamReader (System. in); 50 String inputStr = null; 51 // If the input is not empty, start to loop 52 while (inputStr = br. readLine ())! = Null) {53 isOver = false; 54 // verify that it is valid (it is a chessboard coordinate) 55 if (! IsValid (inputStr) {56 continue; 57} 58 // set chessman 59 String Chessman = chessman. BLACK. getChessman (); 60 chessboard. setBoard (posX, posY, chessman); 61 // determine whether to win 62 if (isWon (posX, posY, chessman) {63 isOver = true; 64} else {65 int [] computerPosArr = computerDo (); 66 chessman = Chessman. WHITE. getChessman (); 67 chessboard. setBoard (computerPosArr [0], computerPosArr [1], chessman); 68 if (isWon (computerPosArr [0], comp UterPosArr [1], chessman) {69 isOver = true; 70} 71} 72 // determine whether to end 73 if (isOver) {74 if (isReplay (chessman )) {75 chessboard. initBoard (); 76 chessboard. printBoard (); 77 continue; 78} 79 break; 80} 81 // perform the next round of 82 chessboard. printBoard (); 83 System. out. println ("pls input the num"); 84} 85} 86 // whether to restart the game 87 public boolean isReplay (String chessman) throws Exception {88 chessboard. printBoard (); 89 String Message = chessman. equals (Chessman. BLACK. getChessman ())? "You win": "you lose"; 90 System. out. println (message + "paly again? (Y/N) "); 91 BufferedReader br = new BufferedReader (new InputStreamReader (System. in); 92 if (br. readLine (). equals ("y") {93 return true; 94} 95 return false; 96} 97 // generate a computer chess piece (random generation, no logic) 98 public int [] computerDo () {99 int posX = (int) (Math. random () * (Chessboard. BOARD_SIZE-1); 100 int posY = (int) (Math. random () * Chessboard. BOARD_SIZE-1); 101 String [] [] board = chessboard. getBoard (); 102 // verify that the coordinates of the next chess game on the computer do not have a pawn Number of 103 Hile (board [posX] [posY]! = "10") {104 posX = (int) (Math. random () * Chessboard. BOARD_SIZE-1); 105 posY = (int) (Math. random () * Chessboard. BOARD_SIZE-1); 106} 107 int [] result = {posX, posY}; 108 return result; 109} 110 // victory decision (the method is to check around the current pawn, non-traversal checkboard method) 111 public boolean isWon (int posX, int posY, String ico) {112 int startX = 0; // The starting X coordinate of the straight line is 113 int startY = 0; // The start of the straight line Y coordinate 114 int endX = Chessboard. BOARD_SIZE-1; // linear end X coordinate 115 int endY = endX; // linear end Y coordinate 116 int sameCou Nt = 0; // Number of adjacent pawns on the same line 117 int temp = 0; 118 // calculate the minimum X coordinate and Y coordinate 119 temp = posX-WIN_COUNT + 1; 120 startX = temp <0? 0: temp; // if less than 0, then 0121 temp = posY-WIN_COUNT + 1; 122 startY = temp <0? 0: temp; 123 // calculate the maximum X coordinate of the pawns and Y coordinate 124 temp = posX + WIN_COUNT-1; 125 endX = temp> Chessboard. BOARD_SIZE-1? Chessboard. BOARD_SIZE-1: temp; 126 temp = posY + WIN_COUNT-1; 127 endY = temp> Chessboard. BOARD_SIZE-1? Chessboard. BOARD_SIZE-1: temp; 128 // calculate the number of identical adjacent pawns from left to right 129 String [] [] board = chessboard. getBoard (); 130 // 131 in the vertical direction for (int I = startY; I <endY; I ++) {132 if (board [posX] [I] = ico & board [posX] [I + 1] = ico) {133 sameCount ++; 134} else if (sameCount! = WIN_COUNT-1) {135 sameCount = 0; 136} 137} 138 // horizontal direction 139 if (sameCount = 0) {140 for (int I = startX; I <endX; I ++) {141 if (board [I] [posY] = ico & board [I + 1] [posY] = ico) {142 sameCount ++; 143} else if (sameCount! = WIN_COUNT-1) {144 sameCount = 0; 145} 146} 147 if (sameCount = 0) {148 int j = startY; 149 for (int I = startX; <endX; I ++) {151 if (j <endY) {152 if (board [I] [j] = ico & board [I + 1] [j + 1] = ico) {153 sameCount ++; 154} else if (sameCount! = WIN_COUNT-1) {155 sameCount = 0; 156} 157 j ++; 158} 159} 160 return sameCount = WIN_COUNT-1? True: false; 162} 163 164 165 public static void main (String [] args) throws Exception {166 GobangGame gb = new GobangGame (new Chessboard ()); // create instance 167 gb. start (); // start Program 168} 169} 170

The start () function writes multiple if statements, which is less readable. Attach the flowchart (^... ^! Handwriting dregs)

-END

-^

-When there were several past injuries in the world, the mountain remains chilling.

-2016-09-06

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.