Java连接四个胜利检查

时间:2013-12-04 04:43:39

标签: java

我是编程的初学者,我一直尽可能地教自己。我需要帮助以更简单的方式检查胜利而不是硬编码每个可能的组合。 我不知道该怎么做。 这是我目前的代码:

import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class connectfour extends JFrame implements ActionListener
{
    JLabel board[][] = new JLabel[8][7];
    //JLabel board[] = new JLabel[64];
    JButton action[] = new JButton[8];
    JButton start;
    JButton clear;
    JFrame Frame = new JFrame();
    ImageIcon red = new ImageIcon("red piece.jpeg");
    ImageIcon black = new ImageIcon("blackpiece.jpeg");

    boolean players = true;
    //Integer[] numclick = new Integer[8];
    int x;

    int numclick1 = 7;
    int numclick2 = 7;
    int numclick3 = 7;
    int numclick4 = 7;
    int numclick5 = 7;
    int numclick6 = 7;
    int numclick7 = 7;
    int numclick0 = 7;


    public connectfour()
    {
        Frame.setSize(100,120);
        Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        Frame.setLayout(null);
        Frame.setVisible(true);

        start = new JButton("Start");
        start.setBounds(0,0,100,100);
        start.setVisible(true);
        start.addActionListener(this);
        Frame.add(start);
    }

    public void game()
    {

        for(int x = 0; x < 8 ; x++)
        {
            action[x] = new JButton();
            action[x].setSize(100,40);
            action[x].setLocation((x*100) + 50, 0);
            action[x].addActionListener(this);
            action[x].setVisible(true);
            Frame.add(action[x]);
        }
        /**
        board[1][1] = new JLabel();
        board[1][1].setBounds(50,40,100,100);
        board[1][1].setVisible(true);
        board[1][1].setOpaque(true);
        board[1][1].setBorder(BorderFactory.createLineBorder(Color.black));
        Frame.add(board[1][1]);
        **/
        for(int i = 0; i < 8; i++)
        {
            for(int j = 0; j < 7; j++)
            {
            board[i][j] = new JLabel();
            board[i][j].setSize(100,100);
            board[i][j].setLocation((i*100)+50,(j*100)+40);
            board[i][j].setOpaque(true);
            board[i][j].setVisible(true);
            board[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
            Frame.add(board[i][j]);

            }
        }   

        clear = new JButton("Clear");
        clear.setBounds(850,100,100,50);
        clear.addActionListener(this);
        clear.setVisible(true);
        Frame.add(clear);


    }
    public void boardsize()
   {
        Frame.setSize(950,800);
   }

    public static void main(String args[])
    {
        new connectfour();
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == start)
        {
            boardsize();
            start.setVisible(false);
            game();

        }
        for(x = 0;x < 8 ;x ++)
        {
            if(e.getSource() == action[x])
            {
                //numclick[x]++;
                if(x == 0)
                {
                    numclick0--;
                    if(players == true)
                    {
                        board[x][numclick0].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick0].setIcon(black);
                        players = true;
                        break;
                    }

                }
                if(x == 1)
                {
                    numclick1--;
                    if(players == true)
                    {
                        board[x][numclick1].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick1].setIcon(black);
                        players = true;
                        break;
                    }
                }
                if(x == 2)
                {
                    numclick2--;
                    if(players == true)
                    {
                        board[x][numclick2].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick2].setIcon(black);
                        players = true;
                        break;
                    }
                }
                if(x == 3)
                {
                    numclick3--;
                    if(players == true)
                    {
                        board[x][numclick3].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick3].setIcon(black);
                        players = true;
                        break;
                    }
                }
                if(x == 4)
                {
                    numclick4--;
                    if(players == true)
                    {
                        board[x][numclick4].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick4].setIcon(black);
                        players = true;
                        break;
                    }
                }
                if(x == 5)
                {
                    numclick5--;
                    if(players == true)
                    {
                        board[x][numclick5].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick5].setIcon(black);
                        players = true;
                        break;
                    }
                }
                if(x == 6)
                {
                    numclick6--;
                    if(players == true)
                    {
                        board[x][numclick6].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick6].setIcon(black);
                        players = true;
                        break;
                    }
                }
                if(x == 7)
                {
                    numclick7--;
                    if(players == true)
                    {
                        board[x][numclick7].setIcon(red);
                        players = false;
                        break;
                    }
                    if(players == false)
                    {
                        board[x][numclick7].setIcon(black);
                        players = true;
                        break;
                    }
                }

                System.out.println(x);
                System.out.println();


            }
        }   
        if(e.getSource() == clear)
        {
            for(int x = 0; x < 8; x++)
            {
                for(int y = 0; y < 7; y++)
                {
                    board[x][y].setIcon(null);

                    numclick1 = 7;
                    numclick2 = 7;
                    numclick3 = 7;
                    numclick4 = 7;
                    numclick5 = 7;
                    numclick6 = 7;
                    numclick7 = 7;
                    numclick0 = 7;

                    players = true;

                    for(int j = 0; j < 8 ; j++)
                    {
                        action[j].setEnabled(true);
                    }

                }
            }
        }
        if(numclick0 == 0)
        {
            action[0].setEnabled(false);
        }
        if(numclick1 == 0)
        {
            action[1].setEnabled(false);
        }
        if(numclick2 == 0)
        {
            action[2].setEnabled(false);
        }
        if(numclick3 == 0)
        {
            action[3].setEnabled(false);
        }
        if(numclick4 == 0)
        {
            action[4].setEnabled(false);
        }
        if(numclick5 == 0)
        {
            action[5].setEnabled(false);
        }
        if(numclick6 == 0)
        {
            action[6].setEnabled(false);
        }
        if(numclick7 == 0)
        {
            action[7].setEnabled(false);
        }
    }

    public void winner()
    {

    }
}

1 个答案:

答案 0 :(得分:3)

我会使用一种递归方法来检查水平,垂直和两个对角线。

当我阅读你的代码时,我意识到你没有跟踪(可能已经错过了)玩家所在的位置......我推荐并为此调用数组grid[][]将数组映射到你的JLabels将走了很长的路。

我举一个负面垂直检查的例子..

 public Boolean checkVertical(Boolean player, int x, int y){
      if(solveHelper(player, x, y, -1, 0) => 4) return true;
      return false;
 }

public int solveHelper(Boolean player, int x, int y, int addX, int addY){
     if(x == 0 || x == size || y == 0 || y == size || grid[x][y].player != player) 
          return 0;
     return solverHelper(player, x+addX, y+addY, addX, addY) + 1);
}

现在,您如何为自己创建和使用这些方法?

您需要为每个水平,垂直和两个对角线创建新方法,以检查您在solveHelperaddX中使用不同属性调用addY的所有方法符合你想要的方向。例如,如果您想要检查水平线,则需要通过addY == 1addY == -1创建addX == 0solveHelper + solverHelper,并更改这两个值。

其他说明......

您需要记住的一些事情是如何连接四个实际运行。当您单击某一行时,该片段将落到该特定列中最小的未占用元素。在编写游戏逻辑时,您应该记住一些事项。

干杯。