如何为Tic tac Toe游戏逻辑引用JButtons对象(Java中的Tic tac Toe游戏)
我正在做一个井字游戏。我添加了带有ActionListeners的9个JButton。每个按钮都正确地侦听操作事件。最后,我正在研究游戏的逻辑部分,但我被困在如何进行这项工作上 如果你看一下我的TictoeButton类,我决定给扩展JButton的TictoeButton对象两个实例变量:一个表示按钮编号的整数变量(因此我知道按下了哪个按钮。数字0是第一个数字)以及一个字符变量,该变量将被分配一个“o”字符到字符数组中,玩家1称为boardLogic,玩家2称为“x” 问题是我不知道如何在我的TicTacToeBoard类中引用TicTacToeButton数组中的元素来执行游戏中实际按下的JButton。Java对象中是否有一个方法可以告诉您是按下了编号为0的JButton还是按下了编号为1的JButton,这与我在下面所述的代码相对应如何为Tic tac Toe游戏逻辑引用JButtons对象(Java中的Tic tac Toe游戏),java,swing,object,reference,jbutton,Java,Swing,Object,Reference,Jbutton,我正在做一个井字游戏。我添加了带有ActionListeners的9个JButton。每个按钮都正确地侦听操作事件。最后,我正在研究游戏的逻辑部分,但我被困在如何进行这项工作上 如果你看一下我的TictoeButton类,我决定给扩展JButton的TictoeButton对象两个实例变量:一个表示按钮编号的整数变量(因此我知道按下了哪个按钮。数字0是第一个数字)以及一个字符变量,该变量将被分配一个“o”字符到字符数组中,玩家1称为boardLogic,玩家2称为“x” 问题是我不知道如何在我的
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
/*
* The TicTacToeBoard is an object that has characterisitics of a JFrame
*/
public class TicTacToeBoard extends JFrame {
/* create an array of characters that holds 9 elements
* to create a model for the tic-tac-toe board logic since the board has 9 boxes
* create the board from a separate class to minimize bugs and for easier debugging
* simulate the game's logic by creating an array of characters
*/
public static char[] boardLogic = {};
// number of buttons in the game
static int numOfButtons = 9;
// TicTacToeButton is an object that extends JButton
static TicTacToeButton[] buttons;
public TicTacToeBoard() {
// setTitle to the frame
setTitle("TicTacToe Game");
/* call this method to the GameFrame object if you do not call this
* method the JFrame subclass will not actually close
*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* set size to an appropriate size create a dimension object
* notice my Dimension object does not have a variable this is
* a prefer way to create this object since no further
* operations will be perform on this object besides this operation
*/
setSize(new Dimension(1000, 1000));
// upon creating the object set the location of the frame to the center of the screen
setLocation(new Point(0, 0));
// prevent the user from resizing the GameFrame object
// uncomment bottom to prevent window maximumization
//setResizable(false);
}
public void printButtons(Container contentPane) {
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(3, 3));
// the idea of boardLogic is creating a 9 element character array to simulate the game's logic
boardLogic = new char[numOfButtons];
// creates the objects of the type JButtons
buttons = new TicTacToeButton[numOfButtons];
// create 9 buttons for the game using a for loop
for (int i = 0; i < buttons.length; i++) {
// create a new JButton object every loop
buttons[i] = new TicTacToeButton(i);
// set a default value. I use '-' for simplicity.
boardLogic[i] = buttons[i].getButtonChar();
// and add it to the frame
gamePanel.add(buttons[i]);
}
contentPane.add(gamePanel, BorderLayout.CENTER);
}
}
导入java.awt.BorderLayout;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.GridLayout;
导入java.awt.Point;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
/*
*TictoeBoard是具有JFrame特性的对象
*/
公共类TictoeBoard扩展JFrame{
/*创建一个包含9个元素的字符数组
*创建tic tac趾板逻辑模型,因为该板有9个框
*从一个单独的类创建电路板,以最小化错误并简化调试
*通过创建一组角色来模拟游戏的逻辑
*/
公共静态字符[]boardLogic={};
//游戏中的按钮数
静态int numobutons=9;
//TicTacToeButton是一个扩展JButton的对象
静态按钮[]按钮;
公共交通委员会(){
//设置框架的标题
片名(“Tictatcoe游戏”);
/*如果不调用此方法,请将此方法调用到GameFrame对象
*方法JFrame子类实际上不会关闭
*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*将“大小”设置为适当的大小创建标注对象
*请注意,我的标注对象没有变量,这是
*创建此对象的首选方法,因为没有进一步的
*除此操作外,还将在此对象上执行其他操作
*/
设置尺寸(新尺寸(1000,1000));
//创建对象时,将框架的位置设置为屏幕的中心
设置位置(新点(0,0));
//阻止用户调整游戏框架对象的大小
//取消底部注释以防止窗口最大化
//可设置大小(假);
}
公共空白打印按钮(容器内容窗格){
JPanel gamePanel=新的JPanel();
setLayout(新的GridLayout(3,3));
//boardLogic的理念是创建一个9元素的角色数组来模拟游戏的逻辑
boardLogic=新字符[numOfButtons];
//创建JButtons类型的对象
按钮=新的TictoEbutton[numOfButtons];
//使用for循环为游戏创建9个按钮
对于(int i=0;i
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
/*类设计是我们将创建一个具有特性的类
*一个JButton可以做什么,但也可以听到动作事件
*/
公共类TictoeButton扩展JButton实现ActionListener{
/*使用字符串类型表示的名称引用我的按钮
*现在将其设置为空。我将使用for循环命名jbutton
*程序将使用分配给每个按钮的按钮编号
*创建游戏的逻辑
*/
私有int按钮编号=0;
//默认情况下,分配给每个按钮的每个字符都会分配一个连字符
private char buttonChar='-';
公共按钮(int按钮编号){
//调用JButton超类构造函数来初始化JButton
超级();
//将参数的名称设置为JButton对象的数据成员
this.buttonNumber=按钮编号;
//创建JButton后,按钮将向其自身添加ActionListener
addActionListener(此);
}
//检索按钮的对象数据成员的get方法
public int getButtonNumber(){
返回按钮编号;
}
//每次用户按下按钮时,将数字10设置为按钮编号
公共无效设置按钮图标(字符按钮图标){
如果(buttonChar='O'| | buttonChar=='X'){
this.buttonChar=buttonChar;
}
}
公共静态void printary(){
对于(int i=0;iimport java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
/* the class design is that we will create a class that has characteristics
* of what a JButton can do but can also hear for action events
*/
public class TicTacToeButton extends JButton implements ActionListener {
/* referring my Buttons with names denoted as String type
* set it empty for now. I will name the JButtons using a for loop
* the program will use buttonNumber assigned to each button
* to create the game's logic
*/
private int buttonNumber = 0;
// Each character assigned to each button is assigned a hyphen by default
private char buttonChar = '-';
public TicTacToeButton(int buttonNumber) {
// call the JButton super class constructor to initialize the JButtons
super();
// set the name of the parameter to the data member of the JButton object
this.buttonNumber = buttonNumber;
// upon creating of the JButton the button will add ActionListener to itself
addActionListener(this);
}
// a get method that retrieves the button's object data member
public int getButtonNumber() {
return buttonNumber;
}
// everytime the user press a button set the number 10 as the button number
public void setButtonChar(char buttonChar) {
if (buttonChar == 'O' || buttonChar == 'X') {
this.buttonChar = buttonChar;
}
}
public static void printArray() {
for (int i = 0; i < TicTacToeBoard.boardLogic.length; i++) {
System.out.println(TicTacToeBoard.boardLogic[i]);
}
}
public char getButtonChar() {
return buttonChar;
}
public void actionPerformed(ActionEvent e) {
/* both cases must be true before this code can happen
* After Player A turn, then is Player B turn
*/
if (e.getSource() instanceof JButton && TicTacToeBoard.currentPlayerTurn.equals(TicTacToeMain.firstPlayerName)) {
// Player A uses circle
/* the state of the image for the Buttons will change depending on the player's turn
* Player A will use the cross sign , Player B will use Circle Sign
*/
ImageIcon icon = new ImageIcon("buttonImages/circle-sign.png");
TicTacToeBoard.currentPlayerTurn = TicTacToeMain.secondPlayerName;
// set the appropriate picture as the JButton icon
setIcon(icon);
// prevent user from clicking the button more than once
setEnabled(false);
//increment buttonClick variable by one for each mouse click
TicTacToeBoard.buttonClicks += 1;
//notify both players whose turn is it
TicTacToeMain.contentPane.remove(TicTacToeBoard.currentPlayerTurnLabel);
TicTacToeBoard.printCurrentPlayerTurnLabel(TicTacToeMain.contentPane);
// Tests the Winning conditions of the player's
TicTacToeBoardLogic boardLogic = new TicTacToeBoardLogic();
boardLogic.checksWinningConditions();
} else {// After Player B turn, then is Player A turn
// Player B uses cross
/* the state of the image for the Buttons will change depending on the player's turn
* Player A will use the cross sign , Player B will use Circle Sign
*/
ImageIcon icon = new ImageIcon("buttonImages/x-sign.jpg");
TicTacToeBoard.currentPlayerTurn = TicTacToeMain.firstPlayerName;
// set the appropriate picture as the JButton icon
setIcon(icon);
// prevent user from clicking the button more than once
setEnabled(false);
//increment buttonClick variable by one for each mouse click
TicTacToeBoard.buttonClicks += 1;
//notify both players whose turn is it
TicTacToeMain.contentPane.remove(TicTacToeBoard.currentPlayerTurnLabel);
TicTacToeBoard.printCurrentPlayerTurnLabel(TicTacToeMain.contentPane);
}
// if all buttons been pressed, the game makes a decision
// Tests the Winning conditions of the player's
TicTacToeBoardLogic boardLogic = new TicTacToeBoardLogic();
boardLogic.checksWinningConditions();
}
}
public TicTacToeButton(int buttonNumber) {
// call the JButton super class constructor to initialize the JButtons
super();
// set the name of the parameter to the data member of the JButton object
this.buttonNumber = buttonNumber;
// upon creating of the JButton the button will add ActionListener to itself
addActionListener(this);
setActionCommand(Integer.toString(buttonNumber));*************
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window implements ActionListener{
static Buttons[] buttons = new Buttons[2];
public Window(){
JFrame f = new JFrame();
f.setSize(400,400);
JPanel p = new JPanel();
GridLayout gl = new GridLayout(1,2);
p.setLayout(gl);
for(int i = 0; i<2; i++){
buttons[i] = new Buttons(i);
buttons[i].addActionListener(this);
p.add(buttons[i]);
}
f.add(p);
f.setVisible(true);
}
public static void main(String... args){
new Window();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == buttons[0] ){
System.out.println("0");
}
}
}
import javax.swing.JButton;
public class Buttons extends JButton{
int num = 0;
public Buttons(int num){
this.num = num;
}
}