Example of a five-game player created in Java

Source: Internet
Author: User

1. This example is output based on the java Console

2. This instance does not contain a winning or losing judgment algorithm, which needs to be improved.

3. This example is used for teaching knowledge points such as arrays, classes and objects, input and output, and process control.

PlayGo. java

Package cn.edu. lecheng; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; public class PlayGo {private static final int BOARD_SIZE = 10;/*** @ param args * Creator: TOM * creation date: 2013-7-16 * @ throws IOException */public static void main (String [] args) throws IOException {// TODO Auto-generated method stubString inputStr = ""; int stepCount = 1; // initialize the Board board = new Board (BOARD_SIZE); Position position = new Position (BOARD_SIZE); // board [3] [6] = "◎ "; // board [2] [6] = "◆"; // output the initialized checkerboard System. out. println ("start of the game, initialize the board:"); board. printBoard (); System. out. println ("the pawns used by PLAYER 1: ◎; the pawns used by PLAYER 2: ◆. "); BufferedReader br = new BufferedReader (new InputStreamReader (System. in); try {System. out. print ("Step 1st -- enter the column and column coordinates of the pawns, separated by commas:"); while (inputStr = br. readLine ())! = Null) {if (position. checkPositionFormat (inputStr) {// determines whether the input coordinate format is correct if (board. checkBoard (inputStr) {board. play (inputStr, stepCount); board. printBoard (); stepCount ++;} else {System. out. println ("this location already has a child. Please try again. "); Continue ;}} else {System. out. println (" the input coordinates are incorrect! Enter "); continue;} System. out. print ("Step" + stepCount + "-- enter the column and column coordinates of the pawns, separated by commas:") ;}} catch (IOException ioe) {System. out. println (ioe. getMessage ());}}}

Board. java

Package cn.edu. lecheng; public class Board {private String [] [] board; private int boardSize = 0; // constructor public Board (int size) {// TODO Auto-generated constructor stubboardSize = size; board = new String [size] [size]; for (int I = 0; I <size; I ++) {for (int j = 0; j <size; j ++) {if (I = 0 & j = 0) {board [I] [j] = ""; // upper left corner} else if (I = 0 & j! = 0) {board [I] [j] = (new Integer (j )). toString () + ""; // column number. spaces are alignment tables} else if (I! = 0 & j = 0) {board [I] [j] = (new Integer (I )). toString (); // row number} else {board [I] [j] = "member" ;}}}/*** output board ** Creator: TOM * creation date: 2013-7-17 */public void printBoard () {for (int I = 0; I <boardSize; I ++) {for (int j = 0; j <boardSize; j ++) {System. out. print (board [I] [j]);} System. out. println ("") ;}}/*** sub-item * @ param pos * @ param step * Creator: TOM * creation date: */public void play (String pos, int step) {String [] TempStr = pos. split (","); int row = Integer. parseInt (tempStr [0]); int col = Integer. parseInt (tempStr [1]); if (step % 2! = 0) {board [row] [col] = "◎" ;}else {board [row] [col] = "◆ ";}} /*** check whether a child exists at this position * @ param pos * @ return: No child returns true; if a child returns false * Creator: TOM * creation date: 2013-7-17 */public boolean checkBoard (String pos) {boolean flag = false; String [] tempStr = pos. split (","); int row = Integer. parseInt (tempStr [0]); int col = Integer. parseInt (tempStr [1]); if (board [row] [col]. equals ("alias") {flag = true;} return flag ;}}

Position. java

Package cn.edu. lecheng; public class Position {private int bSize = 0; public Position (int size) {bSize = size ;} /*** check the coordinate input format * @ param pos * @ return * Creator: TOM * creation date: 2013-7-18 */public boolean checkPositionFormat (String pos) {boolean flag = false; string [] tempStr = pos. split (","); try {if (Integer. parseInt (tempStr [0])> bSize-1 | Integer. parseInt (tempStr [0]) <= 0) {return false;} if (Integer. parseInt (tempStr [1])> bSize-1 | Integer. parseInt (tempStr [1]) <= 0) {return false;} flag = true;} catch (Exception e) {flag = false;} return flag ;}}

Demo Effect

Related Article

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.