- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的井字棋游戏即将完成,但我不知道如何让我的重置按钮重新开始游戏。我还希望当有人获胜或平局时游戏会重置。任何帮助将不胜感激。
这是我的代码(抱歉,代码太多):
package tictactoe;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TicTacToe
{
public static void main(String[] args)
{
JFrame ticTacToe = new TicTacToeFrame();
ticTacToe.setTitle("Phantom TicTacToe Game!");
ticTacToe.setSize(600, 600);
ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ticTacToe.setLocationRelativeTo(null);
ticTacToe.setVisible(true);
}
}
class TicTacToeFrame extends JFrame implements ActionListener
{
private char whoseTurn = 'X';
private boolean gameOver = false;
private Cell[][] cells = new Cell[3][3];
JLabel jlblStatus = new JLabel("X's turn to play");
public TicTacToeFrame()
{
JButton btn = new JButton("Reset");
JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0));
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
panel.add(cells[i][j] = new Cell());
add(btn, BorderLayout.SOUTH);
add(panel, BorderLayout.CENTER);
add(jlblStatus, BorderLayout.NORTH);
jlblStatus.setOpaque(true);
jlblStatus.setBackground(Color.YELLOW);
}
public boolean isFull()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (cells[i][j].getToken() == ' ')
return false;
return true;
}
public boolean isWon(char token)
{
for (int i = 0; i < 3; i++)
if ((cells[i][0].getToken() == token)
&& (cells[i][1].getToken() == token)
&& (cells[i][2].getToken() == token))
{
return true;
}
for (int j = 0; j < 3; j++)
if ((cells[0][j].getToken() == token)
&& (cells[1][j].getToken() == token)
&& (cells[2][j].getToken() == token))
{
return true;
}
if ((cells[0][0].getToken() == token)
&& (cells[1][1].getToken() == token)
&& (cells[2][2].getToken() == token))
{
return true;
}
if ((cells[0][2].getToken() == token)
&& (cells[1][1].getToken() == token)
&& (cells[2][0].getToken() == token))
{
return true;
}
return false;
}
@Override
public void actionPerformed(ActionEvent e)
{
JButton bttn = (JButton) e.getSource();
if(bttn == btn)
{
}
}
public class Cell extends JPanel
{
private char token = ' ';
public Cell()
{
setBorder(new LineBorder(Color.black, 1));
addMouseListener(new MyMouseListener());
}
public char getToken()
{
return token;
}
public void setToken(char c)
{
token = c;
repaint();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (token == 'X')
{
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
}
else if (token == 'O')
{
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
}
private class MyMouseListener extends MouseAdapter
{
@Override
public void mouseClicked(MouseEvent e)
{
if (gameOver)
return;
if (token == ' ' && whoseTurn != ' ')
setToken(whoseTurn);
if (isWon(whoseTurn))
{
jlblStatus.setText(whoseTurn + " won! Game over!");
whoseTurn = ' ';
gameOver = true;
}
else if (isFull())
{
jlblStatus.setText("Tie game! Game over!");
whoseTurn = ' ';
gameOver = true;
}
else
{
whoseTurn = (whoseTurn == 'X') ? 'O' : 'X';
jlblStatus.setText(whoseTurn + "'s turn.");
}
}
}
}
}
最佳答案
您需要重置程序的状态,包括重新创建或重置某些用户界面(需要更改标签和带有九个单元格的面板)。
需要注意的问题:
TicTacToeFrame
有一个 actionPerformed
方法并实现 ActionListener
接口(interface),但重置按钮还没有 Action 监听器;whoseTurn
和 gameOver
字段设置为其初始值,在 jlblStatus
标签中设置文本,然后
面板
中删除所有单元格并添加新单元格(就像您在构造函数中所做的那样 - 这可能是 initializeCells
方法)或Cell
类添加一个 reset
方法,用于清除 token
字段,为所有单元格调用此重置方法,并告诉 panel
重新绘制)。需要记住的小事情:
panel
和button
组件,请将它们更改为字段(而不是局部变量);actionPerformed
方法中的 if 语句使用 ==
运算符而不是 equals
方法 - 您确定吗? (提示:参见What is the difference between == vs equals() in Java?)。关于java - TicTacToe GUI 应用程序重置按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33990277/
我有一个“设置首选项”屏幕。它有一个 ListPreference 和一个 CheckBoxPreference。当我选择 ListPreference 的一项时,我想更改应用程序的日期格式。另外,通
我试图找到创 build 置/配置窗口的示例。单击菜单项中的“选项”操作可启动设置窗口。我想弄清楚如何从主窗口打开第二个窗口。以及新窗口如何将设置信息返回主窗口。尝试使用 QDialog 或一些继承的
我在 Lnux 上有 Qt 应用程序。我想为此创建一个可执行文件/设置以便在 Windows 上分发它并且不需要安装 Qt。我通过包含所有 dll 为此创建了可执行文件但要运行它,用户需要进入文件夹。
我正在尝试创建一个有点动态的 html 类,它根据类末尾包含的数字设置宽度 %。注意:类名将始终以“gallery-item-”开头 示例:div.gallery-item-20 = 20% 宽度 我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
在我的应用程序中,我想记住一些变量,例如,如果用户登录过一次,那么他们将在下次重新打开应用程序时登录,或者如果他们决定禁用某些提醒,应用程序可以检查该变量是否是错误的,将不再显示该提醒。理想情况下,这
我在 Netbeans 中开发了一个应用程序,它连接到远程计算机的消息队列并发送消息。该应用程序还有其他功能。项目完成后,我清理并构建应用程序,然后 Netbeans 创建一个 jar 文件。 但我的
我创建了一个 Outlook 加载项,需要创建一个设置以使其可分发(我是新手,所以请原谅新手评论) Outlook -2010 Vs -2010 .Net 4.0 我读了一些地方,最简单的方法就是发
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to make installer pack of Java swing Application Proje
这个问题肯定已经被很多人解决过很多次了,但是经过几个小时的研究,我仍然没有找到我要找的东西。 我有一个 ExportSettings.settings 文件,其中包含一堆设置( bool 值、字符串、
我想为我的项目创建一个安装程序,以便它可以安装在任何电脑上而无需安装头文件。我怎样才能做到这一点? 最佳答案 一般有两种分发程序的方法: 源代码分发(要构建的源代码)。最常见的方法是使用 GNU au
如何在这样的动态壁纸中创 build 置 Activity ? Example Picture 我只用一个简单的文本构建了设置 Activity ,但遇到了一些问题。第一个问题是我不能为此 Activ
我用 GUI 创建了一个简单的软件。它有几个源文件。我可以在我的编辑器中运行该项目。我认为它已经为 1.0 版本做好了准备。但我不知道如何为我的软件创 build 置/安装程序。 源代码是python
我的 SettingsActivity当前扩展了 Android Studio 生成的类,AppCompatPreferenceActivity扩展 PreferenceActivity . Acti
我正在使用 .NET 为 IE 开发工具栏。目前,我使用 gacutil 插入我的 .NET 程序集,并使用 regasm 注册我的 COM 程序集。 我想为项目创建一个设置 (MSI),但我似乎无法
在为设置页面创建 Activity 后,我注意到 if (mCurrentValue !== value) 中的 mCurrentValue !== value 返回警告: Identity equa
我在 Visual Studio 10 中创建了一个项目,该项目使用 Mysql 数据库和 Crystalreports 以及 它。但是我不知道如何进行自动安装 Mysql 和 Crystalrepo
我正在尝试在我的 C# 项目中使用 Sqlite 数据库,并且我在 IDE 中做得很好。我的问题是当我为我的项目制作安装包并安装它时,程序无法访问 sqlite 数据库。我也知道这是因为用户没有访问文
我有一个大型 Web 应用程序(带有 11 子系统的 ErP),我想使用 Microsoft WebPI 为它创建一个设置。 目前,我们每周向客户发送一次应用程序(用于每周更新)。 我们在此应用程序中
所以我对工资单申请的最终查询是 - 如何为薪资申请创 build 置? 我需要知道的一切- 如何将设置项目添加到我现有的解决方案 如何将解决方案中的文件添加到安装项目中,以及添加哪些文件添加和在什么文
我是一名优秀的程序员,十分优秀!