完成基诺Java代码?

完成基诺Java代码?,java,Java,这就是我遇到的问题。我正在做一个基诺游戏的水晶代码,我给了。到目前为止,我已经能够选择80个中的20个,并将它们变成红色。然而,当我按下按钮时,它会显示“游戏是平局”,窗口关闭 我试图输入一个代码,它将随机抽取20个数字,不重复,然后它们变为黄色,然而,如果CPU和人匹配一个数字,它将变为绿色。我不知道如何开始这段代码,也不知道如何去做。以下是我所拥有的: import java.awt.*; import javax.swing.*; import java.awt.event.*; imp

这就是我遇到的问题。我正在做一个基诺游戏的水晶代码,我给了。到目前为止,我已经能够选择80个中的20个,并将它们变成红色。然而,当我按下按钮时,它会显示“游戏是平局”,窗口关闭

我试图输入一个代码,它将随机抽取20个数字,不重复,然后它们变为黄色,然而,如果CPU和人匹配一个数字,它将变为绿色。我不知道如何开始这段代码,也不知道如何去做。以下是我所拥有的:

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.lang.String;

public class Kenogame {

  // constants
  public static final int WIDTH = 4;
  public static final int HEIGHT = 20;
  public static final int INITIAL = 0;
  public static final int RED = 1;
  public static final int YELLOW = 3;
  public static final int CHECKED = 4;

  private int turn = RED; // to track which player should play next

  private int[][] playerGrid; // to record each player's move
  private int[][] shadowGrid; // to keep track of which atoms have been FOUND
  private int[][] crystalGrid; // to extract a single crystal from playerGrid

  private int row, column; // position of most recently added atom
  private int lowX, lowY, highX, highY; // corner coordinates of current crystal
  private int player1Score = 0;
  private int player2Score = 0;

  // GUI related fields
  private JButton[] buttonArray;
  // private JTextField scoreField1;
  // private JTextField scoreField2;
  // private JLabel labelRED;           // Label "Red" on GUI
  // private JLabel labelYELLOW;    // Label "Yellow" on GUI
  private JLabel labelTurn; // Label displays whose turn is next
  private int numberToSelect = 20;
  Kenogame() {
    createGUIAndPlay();
  }

  private void createGUIAndPlay() {
    final JFrame f = new JFrame();
    // create the panels
    JPanel topPanel = new JPanel(new BorderLayout());
    JPanel buttonPanel = new JPanel(new GridLayout(WIDTH, HEIGHT));
    JPanel labelPanel = new JPanel();

    // represents the 2D grid of buttons on the GUI
    buttonArray = new JButton[WIDTH * HEIGHT];


    // stores the positions of atoms in both player's crystals
    playerGrid = new int[WIDTH][HEIGHT];
    // shadowGrid keeps track of which atoms have been found 
    shadowGrid = new int[WIDTH][HEIGHT];
    // used to store a crystal to determine if it is a perfect crystal
    crystalGrid = new int[WIDTH][HEIGHT];


    JButton endGameButton = new JButton("Start Draw");
    // labelRED = new JLabel("Red");

    // scoreField1 = new JTextField(3);
    // scoreField1.setEditable(false);
    //labelYELLOW = new JLabel("Yellow");
    labelTurn = new JLabel(Integer.toString(numberToSelect), Label.LEFT);
    Dimension dim = labelTurn.getPreferredSize();
    labelTurn.setPreferredSize(new Dimension(dim.width + 100, dim.height + 10));
    // scoreField2 = new JTextField(3);
    // scoreField2.setEditable(false);
    // scoreField1.setText("0");
    // scoreField2.setText("0");

    // create the buttons on which players will make their moves
    for (int i = 0; i < HEIGHT * WIDTH; i++) {

      buttonArray[i] = new JButton(Integer.toString(i + 1));
      buttonPanel.add(buttonArray[i]);
    }
    final Color buttColor = buttonArray[0].getBackground();
    // add the action listener to the buttons
    for (int i = 0; i < HEIGHT * WIDTH; i++) {
      buttonArray[i].setActionCommand(Integer.toString(i));
      buttonArray[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          JButton button = (JButton) e.getSource();
          if (button.getBackground() == Color.RED) {
            button.setBackground(buttColor);
            numberToSelect++;
          } else {
            if (numberToSelect > 0) {
              button.setBackground(Color.RED);
              numberToSelect--;
            }
          }
          //    button.setEnabled(false);
          int buttonIndex = Integer.valueOf(button.getActionCommand());
          row = buttonIndex / WIDTH;
          column = buttonIndex % WIDTH;
          // playMove();

          labelTurn.setText(Integer.toString(numberToSelect));
          // updateGUI();
        }
      });
    }

    endGameButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        String s;
        //  Need to add "ball draw here" for Keno 
        //  20 random numbers between 1 and 80

        // each time a "ball" matches one selected on the Keno ticket add to a counter
        // at end look up payout for the number of matches corresponding to the counter
        if (player1Score > player2Score)
          s = "RED wins the game";
        else if (player1Score < player2Score)
          s = "YELLOW wins the game";
        else
          s = "Game is a draw";
        JOptionPane.showMessageDialog(f, s, "Game Over", JOptionPane.PLAIN_MESSAGE);
        System.exit(1);
      }
    });

    labelPanel.add(endGameButton);
    labelPanel.add(labelTurn);
    topPanel.add(labelPanel, BorderLayout.NORTH);
    topPanel.add(buttonPanel, BorderLayout.CENTER);
    f.add(topPanel);
    f.setSize(1000, 400);
    f.setTitle("Keno");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }

  private void playMove() {
    playerGrid[row][column] = turn;
    if (turn == RED) {
      turn = YELLOW;
    } else {
      turn = RED;
    }
  }

  private void initialize() {
    highX = highY = Integer.MIN_VALUE;
    lowX = lowY = Integer.MAX_VALUE;
    for (int row = 0; row < HEIGHT; row++)
      for (int column = 0; column < WIDTH; column++) {
        crystalGrid[row][column] = INITIAL;
      }
  }

  public static void main(String[] args) {
    new Keno();
  }
}

// green for a match, yellow for a no-match.
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.util.*;
导入java.lang.String;
公共级基诺加梅{
//常数
公共静态最终整数宽度=4;
公共静态最终内部高度=20;
公共静态最终整数初始值=0;
公共静态最终整数红色=1;
公共静态最终int黄色=3;
公共静态最终检查整数=4;
private int turn=RED;//跟踪下一个玩家
private int[][]playerGrid;//记录每个玩家的移动
private int[][]shadowGrid;//跟踪已找到的原子
private int[][]crystalGrid;//从playerGrid提取单晶
private int row,column;//最近添加的atom的位置
private int lowX,lowY,highX,highY;//当前晶体的角坐标
私有int player1Score=0;
私有int player2Score=0;
//GUI相关字段
私有JButton[]按钮数组;
//私有JTextField scoreField1;
//私有JTextField scoreField2;
//私有JLabel labelRED;//GUI上的标签“Red”
//私有JLabel labelYELLOW;//GUI上的标签“黄色”
私有JLabel labelTurn;//标签显示下一个轮到谁
专用整数选择=20;
基诺加梅(){
createGUIAndPlay();
}
私有void createGUIAndPlay(){
最终JFrame f=新JFrame();
//创建面板
JPanel-topPanel=newjpanel(newborderlayout());
JPanel buttonPanel=新JPanel(新网格布局(宽度、高度));
JPanel labelPanel=新的JPanel();
//表示GUI上按钮的二维网格
buttonArray=新的JButton[宽度*高度];
//存储两个玩家晶体中原子的位置
playerGrid=新整数[宽度][高度];
//阴影栅格追踪已发现的原子
阴影栅格=新整数[宽度][高度];
//用于存储晶体以确定其是否为完美晶体
crystalGrid=新整数[宽度][高度];
JButton endGameButton=新JButton(“开始绘制”);
//labelRED=新的JLabel(“红色”);
//scoreField1=新的JTextField(3);
//scoreField1.setEditable(假);
//labelYELLOW=新的JLabel(“黄色”);
labelTurn=newjlabel(Integer.toString(numberToSelect),Label.LEFT);
维度dim=labelTurn.getPreferredSize();
labelTurn.setPreferredSize(新尺寸(尺寸宽度+100,尺寸高度+10));
//scoreField2=新的JTextField(3);
//scoreField2.setEditable(假);
//scoreField1.setText(“0”);
//scoreField2.setText(“0”);
//创建玩家移动的按钮
对于(int i=0;i<高度*宽度;i++){
buttonArray[i]=新的JButton(Integer.toString(i+1));
buttonPanel.add(buttonArray[i]);
}
最终颜色buttColor=buttonArray[0]。getBackground();
//将操作侦听器添加到按钮
对于(int i=0;i<高度*宽度;i++){
buttonArray[i].setActionCommand(Integer.toString(i));
buttonArray[i].addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
JButton按钮=(JButton)e.getSource();
if(button.getBackground()==Color.RED){
按钮.立根背景(对接色);
numberToSelect++;
}否则{
如果(数字选择>0){
按钮。背景(颜色。红色);
数字选择--;
}
}
//按钮。设置启用(错误);
int buttonIndex=Integer.valueOf(button.getActionCommand());
行=按钮索引/宽度;
列=按钮索引%WIDTH;
//playMove();
labelTurn.setText(Integer.toString(numberToSelect));
//updateGUI();
}
});
}
endGameButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
字符串s;
//需要为基诺添加“这里打球”
//20个介于1和80之间的随机数
//每次一个“球”与基诺牌上选定的一个相匹配时,添加到计数器
//在结束时,查找对应于计数器的匹配数
如果(player1Score>player2Score)
s=“红色赢得比赛”;
否则如果(player1Score
我在底部有4行应该插入代码的地方。如果是的话,我会非常感激的