Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize Gobang Game

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use Java to achieve Gobang game". In daily operation, I believe many people have doubts about how to use Java to achieve Gobang game. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Java to achieve Gobang game". Next, please follow the editor to study!

The project for Java programming language prepared Gobang game (console version), using two-dimensional array, for loop, if statement, while () statement, Scanner Until package, this project is mainly the use of arrays. The functions of the project are falling, outputting the chessboard and judging whether to win or lose.

Code bar:

Package edu.qizi.gam; import java.util.Scanner; public class GobangTest {public static void main (String [] args) {int BoardSize = 20; / / implementation of the chessboard String [] [] board = new String [BoardSize] [BoardSize]; for (int I = 0; I

< board.length; i++) { for (int j = 0; j < board[i].length; j++) { board[i][j] = "+"; } } //输出棋盘 PrintBoard(board); while(true) { //黑棋落子 System.out.println("黑棋落子"); BlackMoveLater(board);// 每下一步黑棋要输出一下棋盘 PrintBoard(board); // 行判断黑棋 BlackRow(BoardSize, board);// 列判断黑棋 BlackLine(BoardSize, board);// 自左上--->

The lower right judges black chess, each time the first column of the second for cycle remains the same, the ladder approaches the lower left method BlackTopLeftToBottomRight_Line (BoardSize, board); / / from the top left-> the bottom right judges black chess, each time the beginning of the second for cycle remains the same, and the ladder approaches the upper right method BlackTopLeftToBottomRight_Row (BoardSize, board) / / judge black chess from the top right-> bottom left, the first column of each second for cycle remains the same, and the ladder approaches the lower-right method BlackTopRightToBottomLeft_Line (BoardSize, board); / / from the top right-> the lower left side judges black chess, each time the second for cycle starts with the same line, and the ladder approaches the upper left method BlackTopRightToBottomLeft_Row (BoardSize, board); / / White player System.out.println ("White fall"); WhiteMoveLater (board) / / output the chessboard PrintBoard (board) for each next white move; / / row judge white WhiteRow (BoardSize, board); / / column judge white WhiteLine (BoardSize, board); / / judge white from the top left-> bottom right, each time the first column of the second for cycle remains the same, and the ladder approaches the lower left method WhiteTopLeftToBottomRight_Line (BoardSize, board) / / judge white chess from the top left-> bottom right, each time the first line of the second for cycle remains the same, the ladder approaches the upper right method WhiteTopLeftToBottomRight_Row (BoardSize, board); / / from the top right-> the lower left side judges white chess, each time the first column of the second for cycle remains the same, and the ladder approaches the lower right method WhiteTopRightToBottomLeft_Line (BoardSize, board) / / from top right-> bottom left to judge white, each time the first line of the second for loop remains the same, and the ladder approaches the method WhiteTopRightToBottomLeft_Row (BoardSize, board) on the upper left;}}

Here is how to implement it

/ / public static void PrintBoard (String [] [] board) {for (int I = 0; I < board.length; iTunes +) {for (int j = 0; j < int [I] .length; jacks +) {System.out.print (board [I] [j] + ");} System.out.println ();}}

Realize the fall of black chess.

/ / public static void BlackMoveLater (String [] [] board) {while (true) {Scanner sc1=new Scanner (System.in); System.out.println ("Please enter the Abscissa of the black chessman [1murmur20]:"); int BlackBoard_x=sc1.nextInt () / / determine whether the Abscissa array is out of bounds if (BlackBoard_x20) {System.out.println ("the range of coordinates you entered is incorrect, the Abscissa is 1 to 20, please enter correctly:\ n"); continue;} Scanner sc2=new Scanner (System.in); System.out.println ("Please enter the vertical coordinates of the black chessman [1Mutomac 20]:"); int BlackBoard_y=sc2.nextInt () / / determine whether the array of ordinates is out of bounds if (BlackBoard_y20) {System.out.println ("the range of coordinates you entered is incorrect, take 1 to 20, please enter:\ n" correctly); continue } / / determine whether this position can fall into the chess piece if (continue; [Blackboard _ Xmur1] [BlackBoard_y-1] = "○") {System.out.println ("there are already pieces in this position, please put the pieces down to the empty position"); continue;} else {BlackBoard_y-1 [Blackboard _ Xmur1] [BlackBoard_y-1] = "Blackboard"; break;}

To realize the fall of white chess.

/ / public static void WhiteMoveLater (String [] [] board) {while (true) {Scanner sc1=new Scanner (System.in); System.out.println ("Please enter the Abscissa of the white player [1MMI: 20]:"); int WhiteBoard_x=sc1.nextInt () / / determine whether the Abscissa array is out of bounds if (WhiteBoard_x20) {System.out.println ("the range of coordinates you entered is incorrect, the Abscissa is 1 to 20, please enter correctly:\ n"); continue;} Scanner sc2=new Scanner (System.in); System.out.println ("Please enter the vertical coordinates of the white chessman [1MUMAT 20]:"); int WhiteBoard_y=sc2.nextInt () / / determine whether the array of ordinates is out of bounds if (WhiteBoard_y20) {System.out.println ("the range of coordinates you entered is incorrect, take 1 to 20, please enter:\ n" correctly); continue } / / determine whether this position can fall into the chess piece if (board [WhiteBoard _ xmur1] [WhiteBoard_y-1] = "●") {System.out.println ("there are already pieces in this position, please put the pieces down to the empty position\ n"); continue;} else [WhiteBoard_y-1] = "○"; break;}

To determine whether to win or lose, take Black as an example (there are six ways), listed below one by one

/ / implement row judgment black chess piece public static void BlackRow (int BoardSize, String [] [] board) {/ / first define a number number to count the element for (int row=0;row) in the read array of int number=0;// row by row.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report