在字符串数组中任意位置? Java的字符串、组中、位置、Java

2023-09-08 09:26:47 作者:自强不息

合作建设一个井字游戏。我试图用简单的方法成为可能。目前,我正在努力让CPU在随机选择的阵列将O在一个地方。我该如何去这样做?这是我到目前为止所。

 进口java.util.Scanner中;
导入了java.util.Random;
公共类播放器

{
    字符串球员=X;
    字符串的cpu =O;
    //私人INT [] [] theBoard =新INT [3] [3];

    私有String [] [] theBoard = {{,,},{,,},{,,}} ;;
    随机cpuInput =新的随机();

    私人董事会委员会1;
    公共静态扫描仪扫描=新的扫描仪(System.in);

公开播放器(董事会板,字符串船内)
    {
        theBoard = theBoard;

    }

    公共无效computerMove()
    {
        串间距=;
        的for(int i = 0; I< theBoard.length;我++)
        {
            对于(INT J = 0; J< theBoard [I] .length; J ++)
            {

                // INT随机= cpuInput.nextInt(theBoard [I] [J]);
                theBoard [2] [2] =(CPU); //困在这里!
            }
        }
}
 

解决方案

您需要找出主板上的可点的电脑,使一招。然后可用点之间,你可以随机选择一个使一招。

Java开发技术之字符串String类的特点

在下面的例子中,我用一个列表来存储电路板上的可用点,并使用Collections.shuffle(),以随机列表来实现做随机移动的目的。

 类点{
    INT列;
    INT关口;

    公共点(INT行,诠释列){
        this.row =行;
        this.col =关口;
    }
}

公共无效computerMove(){
    串间距=;

    //首先找出可移动的电脑
    名单<点> availableSpots =新的ArrayList<>();
    的for(int i = 0; I< theBoard.length ++我){
        对于(INT J = 0; J< theBoard [I] .length; ++ j)条{
            如果(theBoard [I] [J]。.equals(间距)){
                availableSpots.add(新点(I,J));
            }
        }
    }

    //如果没有可用的动作,什么都不做,游戏已经结束
    如果(availableSpots.isEmpty()){
        的System.out.println(没有更多的斑点可用在黑板上。);
        返回;
    } 其他 {
        //洗牌的名单,使之成为随机orderedd
        Collections.shuffle(availableSpots);

        //获取随机列表中的第一个
        现货现货= availableSpots.get(0);
        theBoard [spot.row] [spot.col] =(CPU);
    }
}
 

Working on building a tic tac toe game. I'm trying to use as simple of methods as possible. I'm currently working on getting the cpu to randomly choose a spot in the array to place the "O" at. How do I go about doing this? This is what I have so far.

import java.util.Scanner;
import java.util.Random;
public class Player

{
    String player = "X";
    String cpu = "O";
    //private int[][] theBoard= new int [3][3] ;

    private String[][] theBoard=  {{" "," "," "}, {" "," "," "}, {" ", " "," "}};;
    Random cpuInput = new Random(); 

    private Board board1;
    public static Scanner scan = new Scanner(System.in);

public Player(Board board, String inBoard )
    {
        theBoard = theBoard;

    }

    public void computerMove()
    {
        String spacing = " ";
        for (int i = 0; i < theBoard.length; i++)
        {
            for (int j = 0; j < theBoard[i].length; j++)
            {

                //int random = cpuInput.nextInt(theBoard[i][j]);
                theBoard[2][2] = (cpu); //STUCK HERE!                
            }
        }
}

解决方案

You need to find out the available spots on the board for computer to make a move. Then among the available spots you can randomly choose one to make a move.

In the below example, I use a list to store the available spots on the board and use Collections.shuffle() to randomize the list to achieve the purpose of making a random move.

class Spot {
    int row;
    int col;

    public Spot(int row, int col) {
        this.row = row;
        this.col = col;
    }
}

public void computerMove() {
    String spacing = " ";

    // firstly finding out the available moves for the computer
    List<Spot> availableSpots = new ArrayList<>();
    for (int i = 0; i < theBoard.length; ++i) {
        for (int j = 0; j < theBoard[i].length; ++j) {
            if (theBoard[i][j].equals(spacing)) {
                availableSpots.add(new Spot(i, j));
            }
        }
    }

    // if no available moves, do nothing, game has ended
    if (availableSpots.isEmpty()) {
        System.out.println("No more spots available on the board.");
        return;
    } else {
        // shuffle the list so that it becomes randomly orderedd
        Collections.shuffle(availableSpots);

        // get the first one of the random list
        Spot spot = availableSpots.get(0);
        theBoard[spot.row][spot.col] = (cpu);
    }
}