In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve simple console Gobang game, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The details are as follows
GobangMain this class is the main method of the game, mainly used to control the execution of the game, it is worth noting that the format of the input coordinates is 3pr 4 style, can not be other formats, there can be no spaces.
Package com.qf.Gobang;import java.util.Scanner;import org.omg.CORBA.PUBLIC_MEMBER;public class GobangMain {public static String white = "white"; public static String black = "black"; public static boolean color=true; public static String spoint;// storage coordinates public static void main (String [] args) {Gobang gobang = new Gobang (); Scanner scanner=new Scanner (System.in); while (true) {System.out.println (please + (color?white:black) + "falling child:"); spoint=scanner.next () / / get the coordinates Point point=gobang.analysisPoint (spoint); / / parse the coordinates and return the coordinate object if (gobang.luoZi (point,color)) {gobang.printMap (); if (gobang.isWin (point,color)) {System.out.println ("" + (color?white:black) + "win!") ; break;} colorless colorants;}}
Point class
Public class Point {public Point (int x, int y) {super (); this.x = x; this.y = y;} int x; int y;}
The Gobang class is a game class, which mainly contains the judgment of the game, the end of the game, and so on.
Package com.qf.Gobang;import java.awt.Event;import java.util.Scanner;public class Gobang {public int n = 20ramp / the scale of the map public String color;// confirms whether it is white or black public String mark = "╋"; public String white = "○"; public String black = "●"; public String [] [] map = new String [n] [n] Public String [] coordinate = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖", "⒗", "⒘", "⒙", "⒚", ""}; public Gobang () {/ / initialize the map init () } / / initialize the map public void init () {for (int I = 0; I
< n; i++) { for (int j = 0; j < n; j++) { if (i == n - 1) { map[i][j] = coordinate[j]; } else if (j == n - 1) { map[i][j] = coordinate[i]; } else { map[i][j] = mark; } } } printMap(); } // 打印地图 public void printMap() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(map[i][j]); } System.out.println(); } } // 解析坐标 public Point analysisPoint(String point) { String[] points = point.split(","); int x = Integer.parseInt(points[0]) - 1; int y = Integer.parseInt(points[1]) - 1; return new Point(x, y); } // 落子 public boolean luoZi(Point point, Boolean color) { // 判断是否越界 if (point.x < 0 || point.y >18 | | point.y
< 0 || point.y >18) {return false;} / / determine whether there are any other sub-if (map [point.x] [point.y]! = mark) {return false;} map [point.x] [point.y] = color? White: black; return true;} / / decide whether to win or lose public boolean isWin (Point point, boolean color) {/ / Longitudinal int zxS = 0 / for vertically (int I = 0; I
< 5; i++) { if (point.x - i < 0) { break; } if (map[point.x - i][point.y].equals(color ? white : black)) { zxS++; } else { break; } } int zxX = 0;// 纵向下 for (int i = 1; i < 5; i++) { if (point.x + i >18) {break;} if (map [point.x + I] [point.y] .equals (color? White: black) {zxX++;} else {break;}} / / horizontal int hxZ = 0 for / horizontal left for (horizontal I = 0; I
< 5; i++) { if (point.y - i < 0) { break; } if (map[point.x][point.y - i].equals(color ? white : black)) { hxZ++; } else { break; } } int hxY = 0;// 横向右 for (int i = 1; i < 5; i++) { if (point.y + i >18) {break;} if (map [point.x] [point.y + I] .equals (color? White: black) {hxY++;} else {break;}} / / positive oblique int zxxS = 0 X for / positive oblique for (int I = 0; I
< 5; i++) { if (point.y + i >18 | | point.x-I
< 0) { break; } if (map[point.x - i][point.y + i].equals(color ? white : black)) { zxxS++; } else { break; } } int zxxX = 0;// 正斜下 for (int i = 1; i < 5; i++) { if (point.y - i < 0 || point.x + i >18) {break;} if (map [point.x + I] [point.y-I] .equals (color? White: black) {zxxX++;} else {break;}} / / Anti-skew int fxxS = 0 X X-for (int I = 0; I)
< 5; i++) { if (point.y - i < 0 || point.x - i < 0) { break; } if (map[point.x - i][point.y - i].equals(color ? white : black)) { fxxS++; } else { break; } } int fxxX = 0;// 反斜下 for (int i = 1; i < 5; i++) { if (point.y + i >18 | | point.x + I > 18) {break;} if (map [point.x + I] [point.y + I] .equals (color? White: black) {fxxX++;} else {break;}} System.out.println (); System.out.print ("reverse oblique ↖:" + fxxS+ "\ t"); System.out.print ("Vertical ↑:" + zxS+ "\ t"); System.out.print ("positive oblique upward ↗:" + zxxS); System.out.println (); System.out.print ("horizontal left ←:" + hxZ+ "\ t\ t") System.out.print ("horizontal right →:" + hxY); System.out.println (); System.out.print ("straight downhill ↙:" + zxxX+ "\ t"); System.out.print ("vertical downslope ↓:" + zxX+ "\ t"); System.out.print ("reverse sloping ↘:" + fxxX); System.out.println (); if (zxS + zxX > 4 | | hxY + hxZ > 4 | zxxS + zxxX > 4 | fxxS + fxxX > 4) {return true;} return false;}}
The above is all the content of this article "how to implement simple console Gobang game in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.