用于游戏的java数组获胜算法

时间:2017-01-24 18:10:06

标签: java arrays algorithm swing

对于本学期的独立学习,我制作了游戏Oh-Mok,它基本上是tic tac toe,但是在15乘15的板上。我使用了具有开关功能的2-D按钮阵列来加载黑色图片,白色图片和空白(这只是未点击的按钮)。

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

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

2 个答案:

答案 0 :(得分:1)

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

因为你在学习我不会为你实现这个;上述策略应该足以让您指出正确的方向。让我知道它是否成功!

答案 1 :(得分:1)

  

我的问题是我不知道如何检查二维阵列中的水平,垂直和对角线胜利。

较慢的方法

这个问题有2种方法。 较慢的方式 ,它为每个用户输入垂直,水平和对角地循环遍历15 x 15阵列。

  • 15垂直,15水平&amp; 2条对角线相当于每回合((15+15+2)*15)= 480 检查。

更快的方法

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

  • 1个大小为15的数组(大小可以动态跟随电路板大小)以存储所有行的总和
  • 1个大小为15的数组(大小可以动态跟随电路板大小)以存储所有列的总和
  • 2个变量,用于存储2个对角线方向的总和(NW-SE&amp; NE-SW)。

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

2)自定义并为您的JButtons添加2个属性 - rowValue&amp; colValue。每个按钮都会记住它自己的位置。

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课程,其中包含一组&#34;点击的历史记录&#34;所以每个玩家都有自己的阵列来记住他们选择的哪个位置。如果你这样做,就不需要为每个玩家选择一个素数。