- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我一直在尝试找出一种方法来计算每次有人在我制作的井字游戏 GUI 游戏中获胜的时间。我已经制定了方法来检查 tic tac toe 棋盘上是否有 X 或 O 的任何获胜组合。我只是困惑于如何在每次调用 checkWinsX 和 checkWinsO 时进行计数。我尝试制作另一种方法,为 X 或 O 添加一个点,但我有点失败了,哈哈。
public class ticTacToeFrame extends javax.swing.JFrame {
int turn = 0; //Deterines the turn
int winsX; //Tracks total wins for X
int winsO; //Tracks total wins for O
public ticTacToeFrame() {
initComponents();
}
public void clearAll() {
ulBUT.setText("");
umBUT.setText("");
urBUT.setText("");
mlBUT.setText("");
centerBUT.setText("");
mrBUT.setText("");
llBUT.setText("");
lmBUT.setText("");
lrBUT.setText("");
statusTF.setText("");
}
////////////////////////////////////////////////////////////////////////
// I tried to make something of this sort multiple times here to count the checkWins, and so far this is the one that would seem to start off the best in my head
////////////////////////////////////////////////////////////////////////
public static int wins() {
int winsX = 0;
int winsO = 0;
}
public void checkWinX(int wins) {
//Top row horizontal check for winning
if (ulBUT.getText().equals("X") && umBUT.getText().equals("X") && urBUT.getText().equals("X")) {
statusTF.setText("X Wins Horizontally In The Top Row!");
}
//Middle row horizontal check for winning
else if (mlBUT.getText().equals("X") && centerBUT.getText().equals("X") && mrBUT.getText().equals("X")) {
statusTF.setText("X Wins Horizontally In The Middle Row!");
}
//Bottom row horizontal check for winning
else if (llBUT.getText().equals("X") && lmBUT.getText().equals("X") && lrBUT.getText().equals("X")) {
statusTF.setText("X Wins Horizontally In The Bottom Row!");
}
//Diagonal check for winning
else if (ulBUT.getText().equals("X") && centerBUT.getText().equals("X") && lrBUT.getText().equals("X")) {
statusTF.setText("X Wins Diagonnaly!");
}
else if (urBUT.getText().equals("X") && centerBUT.getText().equals("X") && llBUT.getText().equals("X")) {
statusTF.setText("X Wins Diagonnaly!");
}
}
public void checkWinO() {
//Top row horizontal check for winning
if (ulBUT.getText().equals("O") && umBUT.getText().equals("O") && urBUT.getText().equals("O")) {
statusTF.setText("O Wins Horizontally In The Top Row!");
}
//Middle row horizontal check for winning
else if (mlBUT.getText().equals("O") && centerBUT.getText().equals("O") && mrBUT.getText().equals("O")) {
statusTF.setText("O Wins Horizontally In The Middle Row!");
}
//Bottom row horizontal check for winning
else if (llBUT.getText().equals("O") && lmBUT.getText().equals("O") && lrBUT.getText().equals("O")) {
statusTF.setText("O Wins Horizontally In The Bottom Row!");
}
//Diagonal check for winning
else if (ulBUT.getText().equals("O") && centerBUT.getText().equals("O") && lrBUT.getText().equals("O")) {
statusTF.setText("O Wins Diagonnaly!");
}
else if (urBUT.getText().equals("O") && centerBUT.getText().equals("O") && llBUT.getText().equals("O")) {
statusTF.setText("O Wins Diagonnaly!");
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
ulPAN = new javax.swing.JPanel();
ulBUT = new javax.swing.JButton();
umBUT = new javax.swing.JButton();
urBUT = new javax.swing.JButton();
mlBUT = new javax.swing.JButton();
centerBUT = new javax.swing.JButton();
mrBUT = new javax.swing.JButton();
llBUT = new javax.swing.JButton();
lmBUT = new javax.swing.JButton();
lrBUT = new javax.swing.JButton();
statusTF = new javax.swing.JTextField();
oWinsTF = new javax.swing.JTextField();
xWinsTF = new javax.swing.JTextField();
resetGameBUT = new javax.swing.JButton();
turnTF = new javax.swing.JTextField();
tiesTF1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Tic Tac Toe | John Gironda");
setBackground(new java.awt.Color(87, 87, 87));
ulPAN.setLayout(new java.awt.GridLayout(3, 3));
ulBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ulBUTActionPerformed(evt);
}
});
ulPAN.add(ulBUT);
umBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
umBUTActionPerformed(evt);
}
});
ulPAN.add(umBUT);
urBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
urBUTActionPerformed(evt);
}
});
ulPAN.add(urBUT);
mlBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mlBUTActionPerformed(evt);
}
});
ulPAN.add(mlBUT);
centerBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
centerBUTActionPerformed(evt);
}
});
ulPAN.add(centerBUT);
mrBUT.setToolTipText("");
mrBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mrBUTActionPerformed(evt);
}
});
ulPAN.add(mrBUT);
llBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
llBUTActionPerformed(evt);
}
});
ulPAN.add(llBUT);
lmBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lmBUTActionPerformed(evt);
}
});
ulPAN.add(lmBUT);
lrBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
lrBUTActionPerformed(evt);
}
});
ulPAN.add(lrBUT);
statusTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
statusTFActionPerformed(evt);
}
});
oWinsTF.setText("Total O Wins: 0");
oWinsTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oWinsTFActionPerformed(evt);
}
});
xWinsTF.setText("Total X Wins: 0");
xWinsTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
xWinsTFActionPerformed(evt);
}
});
resetGameBUT.setText("RESET");
resetGameBUT.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetGameBUTActionPerformed(evt);
}
});
turnTF.setText("Turn: X");
turnTF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
turnTFActionPerformed(evt);
}
});
tiesTF1.setText("Total Ties: 0");
tiesTF1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tiesTF1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(16, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(ulPAN, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(statusTF)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(xWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(turnTF, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tiesTF1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(oWinsTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(resetGameBUT, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(16, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(ulPAN, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(154, 154, 154)
.addComponent(resetGameBUT, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(xWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(statusTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(oWinsTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tiesTF1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(turnTF, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ulBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (ulBUT.getText().equals("")) {
if (turn % 2 == 0) {
ulBUT.setText("X");
} else {
ulBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void umBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (umBUT.getText().equals("")) {
if (turn % 2 == 0) {
umBUT.setText("X");
} else {
umBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void urBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (urBUT.getText().equals("")) {
if (turn % 2 == 0) {
urBUT.setText("X");
} else {
urBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void mlBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (mlBUT.getText().equals("")) {
if (turn % 2 == 0) {
mlBUT.setText("X");
} else {
mlBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void centerBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (centerBUT.getText().equals("")) {
if (turn % 2 == 0) {
centerBUT.setText("X");
} else {
centerBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void mrBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (mrBUT.getText().equals("")) {
if (turn % 2 == 0) {
mrBUT.setText("X");
} else {
mrBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void llBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (llBUT.getText().equals("")) {
if (turn % 2 == 0) {
llBUT.setText("X");
} else {
llBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void lmBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (lmBUT.getText().equals("")) {
if (turn % 2 == 0) {
lmBUT.setText("X");
} else {
lmBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void lrBUTActionPerformed(java.awt.event.ActionEvent evt) {
if (lrBUT.getText().equals("")) {
if (turn % 2 == 0) {
lrBUT.setText("X");
} else {
lrBUT.setText("O");
}
turn++;
checkWinX();
checkWinO();
}
}
private void resetGameBUTActionPerformed(java.awt.event.ActionEvent evt) {
clearAll();
}
private void statusTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void xWinsTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void oWinsTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void turnTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void tiesTF1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ticTacToeFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ticTacToeFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton centerBUT;
private javax.swing.JButton llBUT;
private javax.swing.JButton lmBUT;
private javax.swing.JButton lrBUT;
private javax.swing.JButton mlBUT;
private javax.swing.JButton mrBUT;
private javax.swing.JTextField oWinsTF;
private javax.swing.JButton resetGameBUT;
private javax.swing.JTextField statusTF;
private javax.swing.JTextField tiesTF1;
private javax.swing.JTextField turnTF;
private javax.swing.JButton ulBUT;
private javax.swing.JPanel ulPAN;
private javax.swing.JButton umBUT;
private javax.swing.JButton urBUT;
private javax.swing.JTextField xWinsTF;
// End of variables declaration
}
最佳答案
我必须告诉你,你的做法完全错误。从游戏模型开始
public class TicTacToeModel {
Boolean[][] board; //use Boolean class rather than boolean primative so you can set board spaces that haven't been taken yet null
public void setSpace (int x, int y, boolean isX) throws SpaceTakenException { ... }
public boolean getSpace(int x, int y) { ... }
public Boolean calculateWinner() { ... } // calculate if there's a winner, and return its Boolean value if so. Otherwise null
}
完成并运行后,您可以根据您的模型调整 GUI。
关于java - 如何计算 Java 程序中 X 和 O 的获胜次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61787569/
你好,我有一张 table : from | to | item | count ------- Jack | Danie| food | 10 Danie| Maria| food | 2 Ja
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在尝试解决以下面试问题 Given two arrays firstDay and lastDay representing the intervals in days of possible m
这个问题已经有答案了: Explanation of a output of a C program involving fork() (2 个回答) 已关闭 9 年前。 这是我从我的研究所去年的试卷
如何在 html 页面上重复一个 div X 次,可以说我想设置方差来声明重复次数。重复这个部分 5 次,我假设它是用 JS 的。 black BLUE WHITE strip 我
我目前使用类中的函数将数据插入数据库,如果每行成功插入(从 csv 文件),则会记录一条消息(logMessage 函数),以显示哪一行成功或失败。但是我想要已导入数据库的成功执行的计数。我遇到了一些
这个问题可能看起来非常基础,但我很难弄清楚如何做。我有一个整数,我需要使用 for 循环来循环整数次。 首先,我尝试了—— fn main() { let number = 10; // An
我正在准备 CS 125 期末考试,其中(简要地)介绍了 Big O Notation。 鉴于: Mergesort 的最佳运行时间为 O(N lg(N)),最坏运行时间为 O(N lg (N)) 有
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我正在构建一个简单的程序来计算骰子实验中数字的频率,但我尝试扩展它并将最大 throw 次数增加到巨大的数字,通过反复试验,我发现最大限制为519253。 使用这个最大值,我也无法创建任何新数组,它会
这是一道面试题 There is an airline company that wants to provide new updates to all of its flight attendant
我正在尝试以一种可以节省我无数小时的繁琐数据输入的方式实现 Excel 自动化。这是我的问题。 我们需要为所有库存打印条形码,其中包括 4,000 种型号,每种型号都有特定数量。 Shopify是我们
我想根据给定的预定义级别(从级别 1 到级别 6)分离代码中的所有内容,现在我的 JSON 读取 $scope.myJson=[{ id: 1, level: 1, name: "any
我创建了一个菜单,它使用一些 CSS 和 jquery 在悬停时显示其子菜单。事情是,如果用户在菜单项上多次悬停,它会有点滑稽。这是网址:http://91.202.168.37/~ibi/ ,这是
假设我对每小时的事件数进行了如下统计: np.random.seed(42) idx = pd.date_range('2017-01-01', '2017-01-14', freq='1H') df
我想确保我正确理解了这个概念: 在 Hadoop 权威指南中指出:“设计文件系统的目标始终是减少与要传输的数据量相比的查找次数。”在此声明中,作者指的是 Hadoop 逻辑 block 的“seeks
我有一个用 C++11 编写的程序,我想计算 std::vector 的 move 和复制(构造和赋值)次数。对象。有办法吗? 最好的问候 最佳答案 否。 std::vector<>的执行没有办法做到
我们组织的帐户空间不足,我们一直在尝试剔除一些较旧的存储库。问题在于一些较旧的存储库可能仍然是事件服务的依赖项(即使它们多年未更新)。 我知道我们可以跟踪克隆,但据我所知,我们看不到直接下载/pull
我是一名优秀的程序员,十分优秀!