java游戏中的问题
我创造了一个简单的游戏,玩家可以和电脑玩。 我在回合的计时上遇到了一个问题,电脑应该是第一个移动的,而不是真正的玩家应该通过点击java游戏中的问题,java,swing,event-driven-design,Java,Swing,Event Driven Design,我创造了一个简单的游戏,玩家可以和电脑玩。 我在回合的计时上遇到了一个问题,电脑应该是第一个移动的,而不是真正的玩家应该通过点击LeftChoice或right choice按钮来移动 下面是我在代码方面的问题: public class GameForm extends javax.swing.JFrame { /** * Creates new form GameForm */ final int SIZE = 10; int CurrentSize = 10; int PC_SUM=
LeftChoice
或right choice
按钮来移动
下面是我在代码方面的问题:
public class GameForm extends javax.swing.JFrame {
/**
* Creates new form GameForm
*/
final int SIZE = 10;
int CurrentSize = 10;
int PC_SUM=0;
int HUMAN_SUM=0;
boolean PC_TURN = true;
int[] RandArr = new int[SIZE];
public GameForm() {
initComponents();
}
public void init(){
for(int i = 0 ; i<SIZE;i++){
RandArr[i] = (int)(Math.random()*100)+1;
}
jTextField3.setText("");
jTextField4.setText(Integer.toString(PC_SUM));
jTextField5.setText(Integer.toString(HUMAN_SUM));
}
public void HUMAN_updateLeft(){
HUMAN_SUM+=RandArr[0];
jTextField5.setText(Integer.toString(HUMAN_SUM));
jTextField1.setText(Arrays.toString(RandArr));
CurrentSize--;
int [] NewRand = new int[CurrentSize];
for(int i = 1 ; i<=CurrentSize;i++){
NewRand[i-1] = RandArr[i];
}
RandArr = NewRand;
jTextField2.setText(Arrays.toString(RandArr));
PC_TURN = true;
}
public void HUMAN_updateRight(){
HUMAN_SUM+=RandArr[CurrentSize-1];
jTextField5.setText(Integer.toString(HUMAN_SUM));
jTextField1.setText(Arrays.toString(RandArr));
CurrentSize--;
int [] NewRand = new int[CurrentSize];
for(int i = CurrentSize-1 ; i>=0;i--){
NewRand[i] = RandArr[i];
}
RandArr = NewRand;
jTextField2.setText(Arrays.toString(RandArr));
PC_TURN = true;
}
public static boolean WhoIsBigger(int[] arr){
int even=0,odd=0;
for(int i=0;i<arr.length;i+=2){
if(i%2==0){
even+=arr[i];
odd+=arr[i+1];
}
else{
odd+=arr[i];
even+=arr[i+1];
}
}
return even>odd;
}
public void PC_updateLeft(){
PC_SUM+=RandArr[0];
jTextField4.setText(Integer.toString(PC_SUM));
jTextField1.setText(Arrays.toString(RandArr));
CurrentSize--;
int [] NewRand = new int[CurrentSize];
for(int i = 1 ; i<=CurrentSize;i++){
NewRand[i-1] = RandArr[i];
}
RandArr = NewRand;
jTextField2.setText(Arrays.toString(RandArr));
}
public void PC_updateRight(){
PC_SUM+=RandArr[CurrentSize-1];
jTextField4.setText(Integer.toString(PC_SUM));
jTextField1.setText(Arrays.toString(RandArr));
CurrentSize--;
int [] NewRand = new int[CurrentSize];
for(int i = CurrentSize-1 ; i>=0;i--){
NewRand[i] = RandArr[i];
}
RandArr = NewRand;
jTextField2.setText(Arrays.toString(RandArr));
}
public void PC_TURN(){
if(WhoIsBigger(RandArr))
PC_updateLeft();
PC_updateRight();
}
public void FullGame(){
while(RandArr.length>0){
if(PC_TURN){
PC_TURN();
PC_TURN = false;
}
}
}
//start button listener
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
init();
jTextField2.setText(Arrays.toString(RandArr));
jTextField1.setText("-");
jButton1.setEnabled(false);
FullGame();
}
//left button listener
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
HUMAN_updateLeft();
}
//right button listener
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
HUMAN_updateRight();
}
公共类游戏形式扩展了javax.swing.JFrame{
/**
*创建新的游戏形式
*/
最终整数大小=10;
int CurrentSize=10;
int PC_SUM=0;
整数人和=0;
布尔值PC_TURN=true;
int[]RandArr=新的int[SIZE];
公共游戏形式(){
初始化组件();
}
公共void init(){
对于(int i=0;i您应该在左右按钮上附加一个clickListener
。然后当用户单击一个按钮时,将触发该事件。您应该在此处将PC_TURN更改为true,并在需要时重新运行循环
例如:
JButton yourButton = new JButton("Your Button");
yourButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PC_TURN = true;
// extra code
}
});
//Assuming this is called when the Start button is clicked
public void fullGame() {
takePCTurn();
}
public void takePCTurn() {
//Do PC's turn logic
//You might want to check if the Computer Won the game here
}
class PlayerTurnListener implements ActionListener() {
public void actionPerformed(ActionEvent e) {
//Do Player's turn logic
//You might want to check if the Player Won the game here
takePCTurn();
}
}
//We can create an instance of our listener and add it to both buttons
PlayerTurnListener playerTurnListener = new PlayerTurnListener();
leftButton.addActionListener(playerTurnListener);
rightButton.addActionListener(playerTurnListener);
您应该在左右按钮上附加一个clickListener
。然后当用户单击其中一个按钮时,事件将被触发。您应该在此处将PC\u TURN更改为true,并在需要时重新运行循环
例如:
JButton yourButton = new JButton("Your Button");
yourButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PC_TURN = true;
// extra code
}
});
//Assuming this is called when the Start button is clicked
public void fullGame() {
takePCTurn();
}
public void takePCTurn() {
//Do PC's turn logic
//You might want to check if the Computer Won the game here
}
class PlayerTurnListener implements ActionListener() {
public void actionPerformed(ActionEvent e) {
//Do Player's turn logic
//You might want to check if the Player Won the game here
takePCTurn();
}
}
//We can create an instance of our listener and add it to both buttons
PlayerTurnListener playerTurnListener = new PlayerTurnListener();
leftButton.addActionListener(playerTurnListener);
rightButton.addActionListener(playerTurnListener);
如果这是一个Swing或其他GUI,那么您需要去掉while循环
请记住,GUI程序是非线性的,是事件驱动的,因此,您可以根据轮到谁来更改程序的状态(使用一个变量指示轮到谁),然后根据状态更改程序的行为,而不是限制while循环(这可能会使您的GUI完全受到影响)
如果您需要更具体的帮助,那么您需要问一个更完整的问题,包括一个有足够代码和解释的问题,以便我们更好地理解您的问题(但不要问太多代码)
关于您发布的代码:
- 是的,一定要摆脱那个while循环
- 你的代码很难理解,主要是因为你的变量命名很差。与其使用像
jTextField1
这样的变量名,不如使用有逻辑意义的名称,使你的代码“自我调试”,比如humanScoreField
和computerScoreField
,等等
- 还要学习并遵循标准的Java命名实践,包括让所有变量以小写字母开头,类以大写字母开头,对所有变量使用camelCase,但常量都是大写的
- 告诉更多关于这段代码应该做什么,按钮和JTextfields代表什么,你想要什么行为,等等
如果这是一个Swing或其他GUI,那么您需要摆脱while循环
请记住,GUI程序是非线性的,是事件驱动的,因此,您可以根据轮到谁来更改程序的状态(使用一个变量指示轮到谁),然后根据状态更改程序的行为,而不是限制while循环(这可能会使您的GUI完全受到影响)
如果您需要更具体的帮助,那么您需要问一个更完整的问题,包括一个有足够代码和解释的问题,以便我们更好地理解您的问题(但不要问太多代码)
关于您发布的代码:
- 是的,一定要摆脱那个while循环
- 你的代码很难理解,主要是因为你的变量命名很差。与其使用像
jTextField1
这样的变量名,不如使用有逻辑意义的名称,使你的代码“自我调试”,比如humanScoreField
和computerScoreField
,等等
- 还要学习并遵循标准的Java命名实践,包括让所有变量以小写字母开头,类以大写字母开头,对所有变量使用camelCase,但常量都是大写的
- 告诉更多关于这段代码应该做什么,按钮和JTextfields代表什么,你想要什么行为,等等
首先,在命名变量时,您应该遵循Java命名约定。目前来看,PC\u TURN
看起来像是一个常量,虽然它不是常量,因为您正在更改它的值。因此更合适的名称应该是pcTurn
您似乎还有一个名为PC_TURN()
的方法,我假设这个方法会导致计算机轮到它并做一些事情。因此,如果将它命名为类似takePCTurn()的描述性方法会更好
。请注意始终使用camelCase。名称开头的大写字母应保留给类和接口,而不是变量或方法
方法名也是如此
public void FullGame()
应写为
public void fullGame()
保持这样的编码惯例可以让其他人更容易阅读和理解您的代码,并保持一切整洁。养成这样的好习惯。:)
我不知道左右按钮的声明位置或名称,但您需要为每个按钮添加一个事件侦听器,这会导致单击按钮时发生某些事情。我也不确定RandArr.length>0
的用途。由于这是一个作为一个GUI,它永远不会关闭,除非您明确地告诉它(例如单击关闭按钮)。因此,我将给您一个通用的解决方案
你基本上是想让玩家轮流触发电脑,直到游戏结束条件得到满足
例如:
JButton yourButton = new JButton("Your Button");
yourButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PC_TURN = true;
// extra code
}
});
//Assuming this is called when the Start button is clicked
public void fullGame() {
takePCTurn();
}
public void takePCTurn() {
//Do PC's turn logic
//You might want to check if the Computer Won the game here
}
class PlayerTurnListener implements ActionListener() {
public void actionPerformed(ActionEvent e) {
//Do Player's turn logic
//You might want to check if the Player Won the game here
takePCTurn();
}
}
//We can create an instance of our listener and add it to both buttons
PlayerTurnListener playerTurnListener = new PlayerTurnListener();
leftButton.addActionListener(playerTurnListener);
rightButton.addActionListener(playerTurnListener);
因此,首先发生的事情是fullGame()
被您的开始按钮调用,然后调用takePCTurn();
使计算机轮到它。现在,在玩家单击左键或右键之前不会发生任何事情。当他们这样做时,PlayerTurnListener
的操作执行()方法被调用,您可以在其中执行一些逻辑,然后takePCTurn();
wil