Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple backgammon Game with java

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

This article mainly explains "how to achieve simple backgammon in java". 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 realize simple backgammon game with java".

Use of the JOptionPane class

Write programs to achieve a simple game of backgammon. In backgammon, the two sides take turns playing chess on a 3 × 3 chessboard, one side is shown by * and the other side is represented by O. If three pieces of a side occupy the same row, column or diagonal, that side wins. If the chessboard is full of pieces, but no one wins, there will be a draw. In the program, one side is the user, and the user enters the position of each chess game on the interface; the position of the other side playing chess is randomly and automatically generated. The sample interface is shown in the figure.

Tip: (1) use the method input provided in the Scanner class or the JOptionPane class, and the output use the method in the System.out or the method provided by the JOptionPane class. (2) String or StringBuffer classes can be used for string processing.

Implementation code:

Import java.util.Random;import javax.swing.*;public class Main {public static boolean check (StringBuffer st,int pos) {/ / check whether the pieces on the pos win or not char des=st.charAt (pos); char [] s=new char [10]; for (int iTunes 1)

Tags: One side three pieces method chess piece position content draw chessboard user computer interface program learning input practical deeper effective keep going one line code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Tech Info Huawei Linux Shulou Technology