The two-dimensional array is used as a wuziqi game (based on the console)

Source: Internet
Author: User

 

Package com.asia. test; import Java. util. secret; public class Test2 {public static void main (string [] ARGs) {login console = New Login (system. in); char [] Name = {'null', 'black', 'white'}; int COUNT = 0; // counter, used to calculate who played the game char [] [] qi = new char [16] [16]; // put the Board into a two-dimensional array while (true) {int ROW = 0; // initialize the index of the row and Col received from the console as 0int Col = 0; If (COUNT = 0) {print (QI, name [count], row, col); count ++;} If (COUNT = 1) {s Ystem. out. println ("please Black (@) Play chess (I j):"); ROW = console. nextint (16) % 16; // indexcol of the row received from the console = console. nextint (16) % 16; // indexstring condition = Print (QI, name [count], row, col) of the col received from the console; If (condition. length () = 1) {// count ++;} If (condition. length () = 2) {// An error occurred while playing chess. the position to be played has already passed the system. out. println ("This position already has a pawn, and again");} If (condition. length () = 3) {// The Player wins the system. out. println ("Black is awesome, white is a cloud, Black wins"); bre AK;} If (condition. length () = 4) {system. out. println (""); break ;}} if (COUNT = 2) {system. out. println ("please white (*) Play chess (I j):"); ROW = console. nextint (16); Col = console. nextint (16); string condition = Print (QI, name [count], row, col); If (condition. length () = 1) {// print the checkboard count --;} If (condition. length () = 2) {// An error occurred while playing chess. the position to be played has already passed the system. out. println ("This position already has a pawn, then play the game again");} If (condition. length () = 3) {system. O Ut. println (""); break;} If (condition. length () = 4) {system. out. println (",, "); break ;}}}} /*** @ Param Qi indicates board mounting. * @ Param name: Who is playing the game? * return true-correct and false-chess Error, re-run */public static string print (char [] [] Qi, char name, int row, int col) {string B = "NONE "; // It is the correct string STR = "0123456789 abcdef" to initialize playing chess; // print the first line of system. out. print (""); For (INT I = 0; I <Str. length (); I ++) {sys TEM. out. print (Str. charat (I);} system. out. println (); For (INT I = 0; I <Qi. length; I ++) {system. out. print (Str. charat (I); For (Int J = 0; j <Qi [I]. length; j ++) {If (name = 'kong') {// The default display of the board is '. 'qi [I] [J] = '. ';} If (name = 'black') & (I = row) & (j = col )) {If (Qi [I] [J]! = '. ') {// Returns falseb = "youqi";} else {Qi [I] [J] = '@'; // The Position of the black player after playing the game is '@' If (WIN (QI, '@', row, col) {B = "";}}} if (name = 'white') & (I = row) & (j = col) {If (Qi [I] [J]! = '. ') {B = "youqi";} else {Qi [I] [J] = '*'; // The Position of the black player after playing the game is '*' If (WIN (QI, '*', row, col) {B = "" ;}} system. out. print (Qi [I] [J]);} system. out. println () ;}return B ;}public static Boolean win (char [] [] Qi, char CH, int row, int col) {Boolean B = false; boolean NS = N2s (QI, CH, row, col); Boolean we = w2e (QI, CH, row, col); Boolean wnes = wn2es (QI, CH, row, COL); Boolean enws = en2ws (QI, CH, row, col); B = NS | we | wnes | enws; return B ;} /* from north to south */public static Boolean N2s (char [] [] Qi, char CH, int row, int col) {Boolean win = false; int North = 0; // beginint South = 0; // endwhile (Qi [row] [col] = CH) {row --; If (row <0) {break ;}} row ++; // row must be ++ at this time. Otherwise, wait until the following while (..) in the Qi [row] [col] Will Never == chnorth = row; while (Qi [row] [col] = CH) {row ++; If (row> 15) {break ;}} South = row; If (South-North) >=5) {win = true;} return win ;} /* from west to east */public static Boolean w2e (char [] [] Qi, char CH, int row, int col) {Boolean win = false; int West = 0; // beginint East = 0; // endwhile (Qi [row] [col] = CH) {col --; If (COL <0) {break ;}} Col ++; // Col must be ++ at this time. Otherwise, wait until the following while (..) in Qi [row] [col] Will Never == chwest = Col; while (Qi [row] [col] = CH) {Col ++; If (COL> 15) {break ;}} East = Col; If (East-West) >=5) {win = true;} return win ;} /* from northwest China to southeast */public static Boolean wn2es (char [] [] Qi, char CH, int row, int col) {Boolean win = false; int wn = 0; // beginint es = 0; // endwhile (Qi [row] [col] = CH) {row --; col --; if (row <0 | Col <0) {break;} row ++; // The row and Col must be ++ at this time. Otherwise, wait until the following while (..) in the Qi [row] [col] Will Never = chcol ++; wn = row; while (Qi [row] [col] = CH) {row ++; col ++; If (row> 15 | Col> 15) {break ;}} es = row; If (ES-wn)> = 5) {win = true;} return win;}/* from Northeast China to Southwest China */public static Boolean en2ws (char [] [] Qi, char CH, int row, int col) {Boolean win = false; int en = 0; // beginint Ws = 0; // endwhile (Qi [row] [col] = CH) {row --; col --; if (row <0 | Col <0) {break;} row ++; // The row and Col must be ++ at this time. Otherwise, wait until the following while (..) in the Qi [row] [col] Will Never = chcol ++; en = row; while (Qi [row] [col] = CH) {row ++; col ++; If (row> 15 | Col> 15) {break ;}} Ws = row; If (WS-en)> = 5) {win = true;} return win ;}}

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.