Java的游戏井字winCondition? [英] Java TicTacToe game winCondition?

查看:208
本文介绍了Java的游戏井字winCondition?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么会让一个双赢的条件,以我的井字棋游戏?我还需要使它所以当玩家完成后,它会问他们是否想重新播放。如果你想出一个办法,你能告诉我为什么会这样。这里是我的code:

 进口java.awt中的*。
    java.awt.event中导入*。
    进口的javax.swing *。    @燮pressWarnings(串行)
    公共类游戏扩展的JFrame {    JFrame的gameWindow =新的JFrame(井字棋);
    私人的JButton按钮[] =新的JButton [9];
    私人字符串标记=;
    私人诠释计数= 0;    公共静态无效的主要(字串[] args){
        新游戏();
    }    公共游戏(){        HandlerClass处理程序=新HandlerClass();        //设置在屏幕上的按钮
        的for(int i = 0; I< buttons.length;我++){
            按钮[i] =的新的JButton(标记);
            按钮[I] .addActionListener(处理);
            gameWindow.add(按钮[I]);
        }        //设置窗口的外观
        gameWindow.setLayout(新的GridLayout(3,3));
        gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameWindow.setSize(300,300);
        gameWindow.setVisible(真);    }    私有类HandlerClass实现的ActionListener {        @覆盖
        公共无效的actionPerformed(ActionEvent的事件){            JButton的点击=(JButton的)event.getSource();            如果(计数%2 == 0){
                标志=X;
                click.setBackground(Color.YELLOW);
            }其他{
                马克=O;
                click.setBackground(Color.CYAN);
            }            click.setText(标记);
            click.setEnabled(假);
            的System.out.println(数+\\ n+(计数%2));
            算上++;
        }    }
}


解决方案

试试这个方法来找出赢家。

我所提到的所有赚钱的仓位。你必须检查所有每次点击的位置。

 公共字符串getWinner(){
    INT [] [] winPositions =新INT [] [] {{0,1,2},{3,4,5},{6,7,8},
                                         {0,3,6},{1,4,7},{2,7,8},
                                         {0,4,8},{2,4,6}};    对于(INT []的位置:winPositions){
        如果(键[位置[0]的getText()长度()方式> 0
                &功放;&安培;按钮[位置[0]。gettext的()。等于(按钮[位置[1]。gettext的())
                &功放;&安培;按钮[位置[1]]。的getText()。等于(按钮[位置[2]]的getText())){
            返回按钮[位置[0]的getText()。
        }
    }    返回null;
}


添加它到底在你的的actionPerformed()方法。

 公共无效的actionPerformed(ActionEvent的事件){        ...        串赢家= getWinner();
        如果(赢家!= NULL){
            的System.out.println(冠军+就是赢家);
            gameWindow.dispose();
        }
    }

How would I make a win condition to my tic-tac-toe game? I need to also make it so when the player is done, that it will ask them if they want to play again. If you figure out a way, can you please tell me why that is so. Here is my code:

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

    @SuppressWarnings("serial")
    public class game extends JFrame{

    JFrame gameWindow = new JFrame("Tic-Tac-Toe");
    private JButton buttons[] = new JButton[9];
    private String mark = "";
    private int count = 0;

    public static void main(String[] args){
        new game();
    }

    public game(){

        HandlerClass handler = new HandlerClass();

        // Sets buttons on the screen
        for(int i = 0; i < buttons.length; i++){
            buttons[i] = new JButton(mark);
            buttons[i].addActionListener(handler);
            gameWindow.add(buttons[i]);
        }

        // Sets the looks of the window
        gameWindow.setLayout(new GridLayout(3,3));
        gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameWindow.setSize(300,300);
        gameWindow.setVisible(true);

    }

    private class HandlerClass implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent event) {

            JButton click = (JButton) event.getSource();

            if(count % 2 == 0){
                mark = "X";
                click.setBackground(Color.YELLOW);
            }else{
                mark = "O";
                click.setBackground(Color.CYAN);
            }

            click.setText(mark);
            click.setEnabled(false);


            System.out.println(count + "\n" + (count % 2));
            count++;
        }

    }
}

解决方案

Try this method to find out the winner.

I have mentioned all the winning positions. You have to check all the position on each click.

public String getWinner() {
    int[][] winPositions = new int[][] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, 
                                         { 0, 3, 6 }, { 1, 4, 7 }, { 2, 7, 8 }, 
                                         { 0, 4, 8 }, { 2, 4, 6 } };

    for (int[] positions : winPositions) {
        if (buttons[positions[0]].getText().length() > 0
                && buttons[positions[0]].getText().equals(buttons[positions[1]].getText())
                && buttons[positions[1]].getText().equals(buttons[positions[2]].getText())) {
            return buttons[positions[0]].getText();
        }
    }

    return null;
}


Add it in your actionPerformed() method in the end.

    public void actionPerformed(ActionEvent event) {

        ...

        String winner = getWinner();
        if (winner != null) {
            System.out.println(winner + " is winner");
            gameWindow.dispose();
        }
    }

这篇关于Java的游戏井字winCondition?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆