【问题标题】:Random position in string array? Java字符串数组中的随机位置?爪哇
【发布时间】:2015-10-31 00:48:20
【问题描述】:

致力于构建井字游戏。我正在尝试使用尽可能简单的方法。我目前正在努力让 CPU 随机选择阵列中的一个点来放置“O”。我该怎么做呢?这是我目前所拥有的。

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!                
            }
        }
}

【问题讨论】:

    标签: java arrays 2d


    【解决方案1】:

    您需要找出棋盘上可供计算机移动的位置。然后在可用的位置中随机选择一个进行移动。

    在下面的例子中,我使用一个列表来存储棋盘上的可用点,并使用 Collections.shuffle() 对列表进行随机化,以达到随机移动的目的。

    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);
        }
    }
    

    【讨论】:

    • 所以说 缺少一个“符号”。我该如何填写?谢谢。我以前从未使用过这些
    • @pewpew 是 java 泛型,查看 docs.oracle.com/javase/tutorial/java/generics 了解更多关于泛型的信息; Spot 是我创建的用于包装行和列的简单类。已更新我的代码
    【解决方案2】:

    你可以这样做,生成 0-2 之间的两个数字,如果没有位置,则将其放置在索引 [x][y]

    Random generator = new Random(); 
    import java.util.*;
    int x = generator.nextInt(3);
    int y = generator.nextInt(3);
    if place is not taken:
    theBoard[x][y];
    if place is taken generate again:
    int x = generator.nextInt(3);
    int y = generator.nextInt(3);
    

    【讨论】:

    • 感谢您的意见
    猜你喜欢
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2013-12-25
    • 2011-12-29
    相关资源
    最近更新 更多