Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JavaSE to realize backgammon

Shulou Source: shulou.com Published: 2022-05-31 18:33:12 10月25日 Update

The main content of this article is to explain "how to use JavaSE to achieve Gobang game", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use JavaSE to realize the game of backgammon.

Using object-oriented thinking, the realization of backgammon is divided into four modules:

Main function public class main {public static void main (String [] args) {player player1 = new player (); game game1 = new game (player1); while (true) {game1.initialize (); game1.play (); game1.destroy (); boolean b = player1.queryContinue () If (! B) {System.out.println ("Welcome to play next time"); break;} chessboard module design

Chessboard initialization

Accept the player to drop the son and return to the current chessboard state

Accept AI drop and return to the current chessboard state

Judge the winner, draw or continue the game, and the new game resets the board.

Import java.util.Arrays;public class chessboard {private static final int blank = 0; private static final int circle = 1; private static final int cross = 2; private int [] [] array = {{blank, blank},} Public boolean moveCircleAt (int row,int column) {if (Array [row] [column]! = blank) {return false;} array [row] [column] = circle; return true;} public boolean moveCrossAt (int row,int column) {if (Array [row] [column]! = blank) {return false } array [row] [column] = fork; return true;} public static final int CIRCLE_WIN = 0; public static final int CROSS_WIN = 1; public static final int DRAW = 2; public static final int CONTINUE = 3; private static String display (int I) {switch (I) {case blank: return "; case circle: return" @ " Case fork: return "*"; default:return ";} @ Override public String toString () {String s =" +-+\ n "; for (int I = 0 position I)

Tags: Blank player position chessboard three pieces module input design content work legal valid legitimate round draw situation machine status or learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Xiaomi Shulou Technology macOS