【问题标题】:java array winning algorithm for game [closed]游戏的java数组获胜算法[关闭]
【发布时间】:2017-01-24 18:10:05
【问题描述】:

为了这个学期的独立学习,我制作了 Oh-Mok 游戏,它基本上是井字游戏,但在 15 x 15 棋盘上。我使用具有开关功能的二维按钮阵列来加载黑色图片、白色图片和 null(这只是未点击的按钮)。

我已经完成了一切,但现在我想实现一个方法,当用户获胜时会显示一条消息。 (人们与其他人对抗,而不是计算机)。问题是,我不知道如何根据点击次数不断更新。我将在下面附上主要的游戏类和按钮类。 (我在加载开始屏幕的主方法中调用新开始)。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class OhMok 
{
    //declaring variables
    JPanel mainPanel = new JPanel(); 
    WhiteBlackButton board[][] = new WhiteBlackButton[15][15]; 

    //constructor
    public OhMok()
    {
        //creating frame
        JFrame mainFrame = new JFrame("Oh-Mok v1.2 by Adam Romano"); 
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(new Dimension(600,600));
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);

        mainPanel.setLayout(new GridLayout(15,15));

        //adding buttons to frame
        for(int i = 0; i < board.length; i++)
        {
            for(int j = 0; j < board[i].length; j++ )
            {
                board[i][j] = new WhiteBlackButton(); 
                board[i][j].setBackground(new Color(238,221,130));
                mainPanel.add(board[i][j]); 
            }

        }

        mainFrame.add(mainPanel); 
    }


    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new Start(); 
            }
        });
    }
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class WhiteBlackButton extends JButton implements ActionListener
{
    //setting variables
    ImageIcon white, black; 
    int value = 0; 

    //constructors
    public WhiteBlackButton()
    {
        //instantiating Image Icons with black/white
        white = new ImageIcon(this.getClass().getResource("white.png")); 
        black = new ImageIcon(this.getClass().getResource("black.png")); 
        this.addActionListener(this);
    }

    //on click-event handling
    public void actionPerformed(ActionEvent e)
    {
        value++; 
        value %= 3; 

        //switching between black, white, nothing
        switch(value)
        {
            case 0:
            setIcon(null); 
            break; 

            case 1:
            setIcon(white); 
            break; 

            case 2: 
            setIcon(black); 
            break; 
        }
    }
}

【问题讨论】:

  • 看看下面我的解决方案。
  • “我的问题是我不知道如何检查二维数组中的水平、垂直和对角线获胜。”:那么你还没有“完成所有事情”跨度>

标签: java arrays algorithm swing


【解决方案1】:

一种解决方案是在 OhMok 类中创建一个名为 checkForWin 的方法,该方法检查是否有人获胜并执行适当的响应。然后,您可以使用board[i][j] = new WhiteBlackButton(this)OhMok 对象传递给构造函数中的按钮,并将对它的引用存储在WhiteBlackButton 类中。每次按下按钮后,使用checkForWin 按钮调用该引用。

既然你正在学习,我不会为你实现这个;上述策略应该足以为您指明正确的方向。让我知道它是否有效!

【讨论】:

  • 我的问题是我不知道如何检查二维数组中的水平、垂直和对角线获胜。
  • 我建议您将检查分成三种不同的方法,每个方向一种。水平和垂直看起来很简单,然后对角线有点困难但并非不可能。发布您的方法,我们可以提供帮助!
  • @aromano31 对于checkForWin 方法,我建议为每个WhiteBlackButton 对象添加value,因为您没有维护游戏状态的原始表示。假设玩家 1 的点击值为 1,玩家 2 的点击值为 2,那么如果您得到 sum==15,则玩家 1 获胜,否则如果您获得 30,则玩家 2 获胜。对角线情况的获胜条件相同。
【解决方案2】:

我的问题是我不知道如何检查二维数组中的水平、垂直和对角线获胜。

较慢的方法

有两种方法可以解决这个问题。 较慢的方式 为每个用户输入在垂直、水平和对角线上循环遍历 15 x 15 阵列。

  • 15 条垂直线、15 条水平线和 2 条对角线相当于 ((15+15+2)*15)= 每回合 480 次检查。

更快的方法

更快的方法不需要循环。你只需要维护:

  • 1 个大小为 15 的数组(其中大小可以动态跟随棋盘大小)来存储所有的总和。
  • 1 个大小为 15 的数组(其中大小可以动态跟随棋盘大小)来存储所有的总和。
  • 2 个变量来存储 2 个对角线方向的总和(NW-SE & NE-SW)。

1) 将所有数组元素和变量初始化为0。

2) 自定义并为您的 JButton 添加 2 个属性 - rowValuecolValue。每个按钮都会记住自己的位置。

3) 为每个玩家预定义一个素数作为输入值(我选择117

4) 每次用户点击未占用按钮时,根据当前用户,将用户的输入值相应添加到数组中。

假设用户 1 选择 row 0column 3[0][3]。然后你会更新为:

int[] rowSum = new int[boardSize];
int[] colSum = new int[boardSize];
int diagNwSe = 0, diagNeSw = 0;

rowSum[r] += playerVal;    //where r (rowValue) is 0
colSum[c] += playerVal;    //where c (colValue) is 3
if(r == c)                 //if a diagonal NW-SE button is pressed
    diagNwSe += playerVal; //where playerVal is 1 for player 1, 17 for player 2
if(r+c == boardSize-1)     //if a diagonal NE-SW button is pressed
    diagNeSw += playerVal;

检查获胜者

//Winning num for player 1: 1 * 15 = 15
//Winning num for player 2: 17 * 15 = 255
public boolean getWinner(Player p, int boardSize){
    int win = p.getPlayerVal() * boardSize;  //get winning number for current player
    if(rowSum[r] == win || colSum[c] == win || diagNeSw == win || diagNwSe == win)
        //player wins!
    //nobody wins
}

您可能还有一个Player 类,它保存一组“点击历史”,因此每个玩家都有自己的一组数组来记住他们选择了哪些位置。如果这样做,则无需为每个玩家选择质数。

【讨论】:

  • 以及如何将 rowValue 和 colValue 添加到我的 JButton
  • @aromano31 只需创建一个新类并扩展 JButton。之后,只需在新类中添加您需要的任何属性。
  • @user3437460:您可能错过了aromano 再次提出的同样问题,但重新措辞,请求其他人为他完成这项工作。这很快被版主压制了,我很惊讶没有禁止他进入该网站。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多