Java program-console wuziqi

Source: Internet
Author: User

Java program-console wuziqi

For details about wuziqi in the console, refer to the Code annotations.

The Code is as follows:


Package test; import java. io. IOException; import java. util. required; // console simple Wuzi public class GoBang {// two-dimensional array as the private static char [] [] board = new char [16] [16]; // private static int cnt; // The number of chess pieces that have been downloaded indicates the private static char [] qizi = {'#', '@'} of the player's chess pieces {'#','@'}; // private, -1,-,-,}; // input detection private static int CheckInput (int x, int y) {if (board [x] [y]! = '+') Return 0; if (x <1 | x> 15 | y <1 | y> 15) return 0; return 1 ;} // border detection private static int Check (int x, int y) {if (x> 15 | x <1 | y> 15 | y <1) return 0; return 1;} // checks whether the player wins. You need to input the player number and the current point coordinate private static int CheckAns (int num, int x, int y) {if (cnt = 15*15) return 1; // judge whether five pieces are connected together in four directions: horizontal, vertical, left, and right. for (int I = 0; I <4; I ++) {int sum = 1; int xx = x + dx [I]; int yy = y + dy [I]; while (board [xx] [yy] = qizi [num] & Check (xx, yy) = 1) {sum ++; xx = xx + dx [I]; Yy = yy + dy [I];} xx = x + dx [7-i]; yy = y + dy [7-i]; while (board [xx] [yy] = qizi [num] & Check (xx, yy) = 1) {sum ++; xx = xx + dx [7-i]; yy = yy + dy [7-i];} if (sum> = 5) return 1;} return 0 ;} // initialize private static void Init () {cnt = 0; for (int I = 1; I <= 15; I ++) for (int j = 1; j <= 15; j ++) board [I] [j] = '+';} // outputs the private static void Print () {int I, j; for (I = 1; I <= 15; I ++) {for (j = 1; j <= 15; j ++) {System. out. print (board [I] [j]);} System. out. println () ;}} publ Ic static int run () {int x, y; Init (); // Number of the current player. The default value is 0. int num = 0; while (true) {Print (); system. out. print (num + 1 + "contestant, please enter the coordinates of your chess player, should be in the form of x y:"); then SC = new round (System. in); while (true) {x = SC. nextInt (); y = SC. nextInt (); if (CheckInput (x, y) = 1) break; System. out. print ("the input coordinate is invalid. Please enter again:");} board [x] [y] = qizi [num]; if (CheckAns (num, x, y) = 1) {return num + 1;} num = 1-num;} public static void main (String [] args) {System. out. printl N ("welcome to the lkl wuziqi! "); Int ans = run (); System. out. println (" congratulations "+ ans +" Winner ");}}


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.