随机数发生器和TIC TAC TOE

时间:2014-03-27 03:29:15

标签: java for-loop random tic-tac-toe

我目前正在努力思考课堂上提出的问题。在本学期早些时候,我有一项任务,我们必须使用GUI构建TIC TAC TOE游戏。快速查看我的程序是我有一个9 JButton数组,每个按钮也有事件监听器。这段代码非常简单直接。这是捕获,本周我们得到了我们的TIC TAC TOE程序,我们现在假设现在将AI作为播放器2。这并不像它看起来那么难,但是我需要一些来帮助我想象一个for语句或者一个while语句可以通过我的JButton数组9,如果已经按下了一个按钮,那么转到新的可用按钮。我有一些伪代码,我一直在脑力激荡,但似乎我的逻辑方法就是这样做。

伪代码

Random  number = New Randon(); 
JButton [] button = New JButton [9];

public void player2_AI()
{
    for( int x = 0; x <= button.length; x++)
    {
         button[x] = randomNumbers.nextInt(9) + 1
        if ( button[x].setEnabled(false))
        {
            button[x] = randomNumbers.nextInt(9) + [x]
        }
    }
}

为了帮助每个人想象我试图解决这个问题的方法,我会上传我的源代码,以便你们告诉我什么是最简单的方法让玩家2做出自己的决定(人造的)情报)

added 3/26/14 

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

public class TicTacToe extends JFrame
{
private final int HEIGHT = 450;
private final int WIDTH = 500;
private static JButton [] button = new JButton[9];
private static Action [] playerTurn = new Action[9];
// private static JLabel [] label;
private int player = 1;
private Font arial = new Font("Arial", Font.BOLD, 20);
private static int lockButtons = 0;
private boolean game = false;
private static int currentPlayer;


public TicTacToe ()
{
    setTitle( "TTT");
    setSize( HEIGHT, WIDTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




    setLayout(new GridLayout(4,3));


    int num = 0;
    for(int i = 0; i < button.length; i++ )
    {

        button[i] = new JButton( "B" + (i + 1));
        playerTurn[i] = new Action();
        add(button[i]);
        button[i].setBorder(BorderFactory.createLineBorder(Color.black,10));
        button[i].setFont(arial);
        button[i].addActionListener(playerTurn[i]);
    } 


    JButton exit = new JButton(" Exit ");
    JButton playAgain = new JButton(" Play Again "); 
    add(exit, BorderLayout.SOUTH);
    add(new JSeparator(), BorderLayout.CENTER);
    add(playAgain, BorderLayout.SOUTH);
    exit.setBorder(BorderFactory.createLineBorder(Color.green,10));

    //
    //add(exit);
    //add(playAgain);



    setVisible(true);
}

private class Action implements ActionListener
{
    public void actionPerformed(ActionEvent playerMove)
    {
        //Get button pressed using GetSource Command
        JButton whatPlayer=(JButton)(playerMove.getSource());

            if(player == 1)
            {
                player++;
                currentPlayer = 1 ;
                whatPlayer.setText("player1");
                whatPlayer.setEnabled(false); 
                whatPlayer.isEnabled(); 
                validate(); 
                return;
            }               
            if (player == 2)
            {
                player--;
                currentPlayer = 2;
                whatPlayer.setText("player2");
                whatPlayer.setEnabled(false);
                whatPlayer.isEnabled();
                validate(); 
                return;
            }
    }
    public void validate()
    {
        if(button[0].getText().equals(button[1].getText()) && button[1].getText().equals(button[2].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
            return;
        }
        else if(button[3].getText().equals(button[4].getText()) && button[4].getText().equals(button[5].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[3].getText());
            gameOver();
            return;
        }
        else if(button[6].getText().equals(button[7].getText()) && button[7].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[6].getText());
            gameOver();
            return;
        }
        else if(button[0].getText().equals(button[3].getText()) && button[3].getText().equals(button[6].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
            return;
        }
        else if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
            gameOver();
            return;
        }   
        else if(button[2].getText().equals(button[5].getText()) && button[5].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
            gameOver();
            return;
        }
        else if(button[0].getText().equals(button[4].getText()) && button[4].getText().equals(button[8].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
            gameOver();
            return;
        }
        else if(button[2].getText().equals(button[4].getText()) && button[4].getText().equals(button[6].getText()))
        {
            JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
            gameOver();
            return;
        }

        int i;

        for(i=0;i<button.length;i++)
        {
            if(button[i].isEnabled())
            {
                break;
            }
        }


        if(i == button.length)
        {
            JOptionPane.showMessageDialog(null,"This was a Draw");
        }
    }

    public void gameOver()
    {
        for( int x = 0; x < button.length; x++)
        {
            button[x].setEnabled(false);
        }
    }


}
public static void main(String[] arg)
{
    new TicTacToe();
}   
}

0 个答案:

没有答案