Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code of realizing backgammon Mini Game with Java

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

This article mainly explains "how to write the code for Java to achieve backgammon Mini Game", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to write the code for Java to achieve backgammon Mini Game.

The operation of the game is shown in the figure:

Principle:

Two-dimensional array mainly with the help of 3x3

Achieve:

1. Main- main program

Public class Main {public static void main (String [] args) {Player player=new Player (); / / player Game game=new Game (player); / / one cycle represents a game while (true) {game.Init (); / / initialize game.play (); / / start the game game.destory (); / / release boolean q=player.queryContinue () / / after a game is over, ask the user whether to start the next game if (! Q) {System.out.println ("Welcome to continue the game next time!") ; break;}}

2. AI- computer

Import java.util.Random;// returns the falling child position of the AI object / / an array represents the row and column starting from 0 / so the valid range is [0Power2] public class AI {private final Random random=new Random (); public int [] getPosition () {int r=random.nextInt (3); / / generate the random integer [012 int c=random.nextInt (3); return new int [] {rcot c};}}

3. Player- players

Import java.util.Scanner;// returns the player's drop position / / indicates the row and column with an array starting from 0 / so the valid range is [0L2] public class Player {private final Scanner sc=new Scanner (System.in); public int [] getPosition () {System.out.println ("Please enter the position of the falling child, the valid range of rows and rows is [0L2]"); System.out.print ("> >") Int rtraining c; while (true) {System.out.print (">"); r=sc.nextInt (); c=sc.nextInt (); if (r > = 0&&r=0&&c)

Tags: Position turn chessboard player three sons status blank code Mini Game valid regret diagonal object draw array range test content diagonal positive or negative Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn macOS NVidia OPPO Reno