问题,我在我的connect4游戏的主要方法 [英] issue with my main method in my connect4 game
问题描述
林和我的主要方法,whenevr我运行我的code编译不能因为这个问题一个问题:
异常线程main显示java.lang.NullPointerException
在UI.ConnectFourFrame.refreshUI(ConnectFourFrame.java:96)
在UI.ConnectFourFrame(ConnectFourFrame.java:43)
在main.main(main.java:11)
我想看到的问题是什么,但我无法弄清楚,有人可以帮助我
包游戏;进口的java.util.ArrayList;公共类ConnectFourBoard {
//该常数被用来指示的宽度和高度,在细胞中,
//董事会。既然我们要一个8x8板,我们将其设置为8。
私有静态最终诠释WIDTH = 7;
私有静态最终诠释HEIGHT = 6;
私人ConnectFourCell [] [] currentPlayer; //当前状态的奥赛罗板与这两个定义
//字段:一个二维阵列保持每个单元的状态,
//一个布尔值,指示是否它是黑人球员的
//转(真)或白色玩家的回合(假)。
私人ConnectFourCell [] []细胞;
私人布尔isBlueTurn; //由于该板是一个模型,它需要通知意见的变化,
//它会采用跟踪名单的标准的监听器机制
听众和解雇事件//(通过调用这些方法
//监听器)时,事情的变化。
私人的ArrayList< ConnectFourListener>听众;
公共ConnectFourBoard()
{
//数组,在Java中,是对象,这意味着变量
//数组类型(如细胞,其类型为OthelloCell [] [])是
//真的引用说得清数组的生活。默认,
//引用指向空。因此,我们需要创建一个实际的
//二维阵列为单元指向。
细胞=新ConnectFourCell [WIDTH] [HEIGHT]
听众=新的ArrayList< ConnectFourListener>();
重启();
} 公共无效复位(){
的for(int i = 0; I< WIDTH;我++){
对于(INT J = 0; J< HEIGHT; J ++){ 细胞[I] [J] = ConnectFourCell.NONE;
}
} isBlueTurn = TRUE;
} 公共无效addConnectFourListener(ConnectFourListener监听器)
{
listeners.add(监听);
}
公共无效removeConnectFourListener(ConnectFourListener监听器)
{
如果(listeners.contains(监听))
{
listeners.remove(监听);
}
}
//这是相当标准的火事件的方法,我们已经建立
//所有季度,对应于监听器接口的每个方法之一。 私人无效fireBoardChanged()
{
对于(ConnectFourListener监听器:监听器)
{
listener.boardChanged();
}
}
私人无效fireGameOver()
{
对于(ConnectFourListener监听器:监听器)
{
listener.gameOver();
}
}
// isBlackTurn(),如果它是黑色的玩家的回合返回true,假
//如果是白色的玩家的回合。
公共布尔isBlueTurn()
{
返回isBlueTurn;
} 公众诠释的getWidth()
{
返回宽度;
} 公众诠释的getHeight(){ 返回HEIGHT;
}
// getBlackScore()计算的黑人球员得分。
公众诠释getBlackScore()
{
返回getScore(ConnectFourCell.BLUE);
}
// getWhiteScore()计算的白人球员得分。
公众诠释getWhiteScore()
{
返回getScore(ConnectFourCell.RED);
} // getScore()贯穿电路板上的所有细胞,并计数
//具有特定值(例如,黑色或白色)的细胞的数目。
//此方法使用每次计数他们的天真的方法
//这就是所谓的;它可能是更好的跟踪这个分数,因为我们
//去,更新它瓷砖添加和翻转。
私人诠释getScore(ConnectFourCell cellValue)
{
INT得分= 0; 的for(int i = 0; I< WIDTH;我++)
{
对于(INT J = 0; J< HEIGHT; J ++)
{
如果(细胞[I] [J] == cellValue)
{
得分++;
}
}
} 返回分数;
}
// getWhiteScore()计算的白人球员得分。
公众诠释getRedScore()
{
返回getScore(ConnectFourCell.RED);
} 公众诠释getBlueScore(){
// TODO自动生成方法存根
返回getScore(ConnectFourCell.BLUE);
}
公共ConnectFourCell getCell(INT X,int y)对
{
如果(!isValidCell(X,Y))
{
抛出新抛出:IllegalArgumentException(
(+ X +,+ Y +)是不是有效的电池);
} 返回细胞[X] [Y]
}
/ **
*本降法。
*
*放置一个检查到指定的高度,
*并返回宽度的检查土地上。
* /
INT降(INT HEIGHT){
如果(hasWon()){
返回-1;
} 为(;宽度10 6安培;&放大器; HEIGHT = 0;!WIDTH ++){}; 如果(WIDTH == 6){
//如果宽度是6,它通过所有6宽度去
//细胞,并不能找到一个空。
//因此,返回false以表示该
//删除操作失败。
返回-1;
}
//填写了检查程序的宽度height。
细胞[高度] [宽度] = currentPlayer [高度] [宽度]
//交替球员
// currentPlayer =(currentPlayer%2)+ 1;
返回宽度;
}
/ **
* toString方法
*
*返回一个字符串再此presentation
*连四(TM)游戏。
* /
公共字符串的toString(){
串returnString =;
对于(INT WIDTH = 5; WIDTH> = 0; WIDTH--){
对于(INT HEIGHT = 0;身高7;; HEIGHT ++){
returnString = returnString +细胞[高度] [宽度]
}
returnString = returnString +\\ n;
}
返回returnString;
}
/ **
*本hasWon方法。
*
*这个方法返回true,如果之一
*玩家在游戏中获胜。
* /
公共布尔hasWon(){
布尔状态= FALSE;
//检查水平双赢
对(INT WIDTH = 0; WIDTH 6;; WIDTH ++){
对(INT HEIGHT = 0; HEIGHT 4;; HEIGHT ++){
如果((细胞[高度] [宽度] == ConnectFourCell.NONE)及!&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 1] [宽度]和放大器;&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 2] [宽度]和放大器;&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 3] [宽度]){
状态= TRUE;
}
}
}
//检查垂直双赢
对(INT WIDTH = 0; WIDTH 3;; WIDTH ++){
对于(INT HEIGHT = 0;身高7;; HEIGHT ++){
如果((细胞[高度] [宽度] == ConnectFourCell.NONE)及!&安培;
细胞[高度] [宽度] ==细胞[高度] [宽度+ 1]安培;&安培;
细胞[高度] [宽度] ==细胞[高度] [宽度+ 2]安培;&安培;
细胞[高度] [宽度] ==细胞[高度] [宽度+ 3]){
状态= TRUE;
}
}
}
//检查对角线胜(正斜率)
对(INT WIDTH = 0; WIDTH 3;; WIDTH ++){
对(INT HEIGHT = 0; HEIGHT 4;; HEIGHT ++){
如果((细胞[高度] [宽度] == ConnectFourCell.NONE)及!&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 1] [宽度+ 1]安培;&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 2] [WIDTH + 2]安培;&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 3] [宽度+ 3]){
状态= TRUE;
}
}
}
//检查对角线胜(负斜率)
对(INT WIDTH = 3;宽度6;; WIDTH ++){
对(INT HEIGHT = 0; HEIGHT 4;; HEIGHT ++){
如果((细胞[高度] [宽度] == ConnectFourCell.NONE)及!&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 1] [WIDTH-1]安培;&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 2] [WIDTH-2]和放大器;&安培;
细胞[高度] [宽度] ==细胞[HEIGHT + 3] [宽度3]){
状态= TRUE;
}
}
}
返回状态;
}
私人布尔isValidCell(INT X,int y)对
{
返回X> = 0&放大器;&放大器; X - LT;宽度
&功放;&安培; X - GT; = 0&放大器;&放大器; X'LT; HEIGHT
&功放;&安培; Ÿ> = 0&放大器;&安培; Y'LT;宽度
&功放;&安培; Ÿ> = 0&放大器;&安培; Y'LT; HEIGHT;
}
}
包UI;进口java.awt中的*。
进口的javax.swing *。进口UI.ConnectFourBoardPanel;进口Game.ConnectFourBoard;
进口Game.ConnectFourListener;公共类ConnectFourFrame扩展JFrame的实现ConnectFourListener { //变量 私人ConnectFourBoard板; 私人的JLabel scoreLabel;
私人ConnectFourBoardPanel boardPanel;
私人的JLabel statusLabel;
公共ConnectFourFrame()
{
//该框架建立自己的模型。
ConnectFourBoard板=新ConnectFourBoard(); //我们需要的帧从董事会作为接收通知其
//状态变化。
的System.out.println(本);
board.addConnectFourListener(本); 的setTitle(信息学45 2011年春季:ConnectFour游戏);
的setSize(700,700);
的setResizable(真);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
。的getContentPane()的setBackground(Color.BLACK); buildUI();
refreshUI();
}
私人无效buildUI()
{
GridBagLayout的布局=新的GridBagLayout();
的getContentPane()的setLayout(布局)。 字体labelFont =新Font(SANSSERIF,Font.BOLD,18); scoreLabel =新的JLabel();
scoreLabel.setForeground(Color.WHITE);
scoreLabel.setFont(labelFont);
layout.setConstraints(
scoreLabel,
新的GridBagConstraints(
0,0,1,1,1.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
新插图(10,10,10,10),0,0));
的getContentPane()加(scoreLabel)。 boardPanel =新ConnectFourBoardPanel(板);
layout.setConstraints(
boardPanel,
新的GridBagConstraints(
0,1,1,1,1.0,1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
新插图(10,10,10,10),0,0));
的getContentPane()加(boardPanel)。 statusLabel =新的JLabel();
statusLabel.setForeground(Color.WHITE);
statusLabel.setFont(labelFont);
layout.setConstraints(
statusLabel,
新的GridBagConstraints(
0,2,1,1,1.0,0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
新插图(10,10,10,10),0,0));
的getContentPane()加(statusLabel)。
} 私人无效refreshUI()
{
//刷新的UI装置来改变在每个文本
//两个标签(得分和状态),也问
//板重绘自己。 scoreLabel.setText(
蓝:+ board.getBlueScore()+
红:+ board.getRedScore()); statusLabel.setText(
(board.isBlueTurn()蓝色的:红的)
+转); boardPanel.repaint();
} //这些是ConnectFourBoardListener事件处理方法。 公共无效boardChanged()
{
//当董事会的变化,我们将刷新整个UI。 (那里
//次当这种方法是效率太低,但它会
//做工精细我们相对简单的用户界面。) refreshUI();
}
公共无效GAMEOVER()
{
//当比赛结束后,我们会弹出一个显示的最后一个消息框
//比分,那么,用户退出消息框后,处置
//这个窗口,结束程序。 JOptionPane.showMessageDialog(
这个,
游戏结束\\ nFinal比分:+ scoreLabel.getText()
游戏结束,
JOptionPane.INFORMATION_MESSAGE); 处置();
}}
您初始化一个局部变量,而不是你的会员的VaR
ConnectFourBoard板=新ConnectFourBoard();
这行更改为:
=板新ConnectFourBoard();
Im having a problem with my main method, whenevr I run it my code cant compile because of this problem : "Exception in thread "main" java.lang.NullPointerException at UI.ConnectFourFrame.refreshUI(ConnectFourFrame.java:96) at UI.ConnectFourFrame.(ConnectFourFrame.java:43) at main.main(main.java:11)"
I am trying to see what the issue is but I cant figure it out, can someone help me
package Game;
import java.util.ArrayList;
public class ConnectFourBoard {
// This constant is used to indicate the width and height, in cells,
// of the board. Since we want an 8x8 board, we'll set it to 8.
private static final int WIDTH = 7;
private static final int HEIGHT = 6;
private ConnectFourCell[][] currentPlayer;
// The current state the Othello board is defined with these two
// fields: a two-dimensional array holding the state of each cell,
// and a boolean value indicating whether it's the black player's
// turn (true) or the white player's turn (false).
private ConnectFourCell[][] cells;
private boolean isBlueTurn;
// Since the board is a model and it needs to notify views of changes,
// it will employ the standard "listener" mechanism of tracking a list
// of listeners and firing "events" (by calling methods on those
// listeners) when things change.
private ArrayList<ConnectFourListener> listeners;
public ConnectFourBoard()
{
// Arrays, in Java, are objects, which means that variables of
// array types (like cells, which has type OthelloCell[][]) are
// really references that say where an array lives. By default,
// references point to null. So we'll need to create an actual
// two-dimensional array for "cells" to point to.
cells = new ConnectFourCell[WIDTH][HEIGHT];
listeners = new ArrayList<ConnectFourListener>();
reset();
}
public void reset(){
for (int i = 0; i<WIDTH ; i++){
for (int j = 0; j<HEIGHT; j++){
cells[i][j] = ConnectFourCell.NONE;
}
}
isBlueTurn = true;
}
public void addConnectFourListener(ConnectFourListener listener)
{
listeners.add(listener);
}
public void removeConnectFourListener(ConnectFourListener listener)
{
if (listeners.contains(listener))
{
listeners.remove(listener);
}
}
// These are fairly standard "fire event" methods that we've been building
// all quarter, one corresponding to each method in the listener interface.
private void fireBoardChanged()
{
for (ConnectFourListener listener : listeners)
{
listener.boardChanged();
}
}
private void fireGameOver()
{
for (ConnectFourListener listener : listeners)
{
listener.gameOver();
}
}
// isBlackTurn() returns true if it's the black player's turn, and false
// if it's the white player's turn.
public boolean isBlueTurn()
{
return isBlueTurn;
}
public int getWidth()
{
return WIDTH;
}
public int getHeight(){
return HEIGHT;
}
// getBlackScore() calculates the score for the black player.
public int getBlackScore()
{
return getScore(ConnectFourCell.BLUE);
}
// getWhiteScore() calculates the score for the white player.
public int getWhiteScore()
{
return getScore(ConnectFourCell.RED);
}
// getScore() runs through all the cells on the board and counts the
// number of cells that have a particular value (e.g., BLACK or WHITE).
// This method uses the naive approach of counting them each time
// it's called; it might be better to keep track of this score as we
// go, updating it as tiles are added and flipped.
private int getScore(ConnectFourCell cellValue)
{
int score = 0;
for (int i = 0; i < WIDTH; i++)
{
for (int j = 0; j < HEIGHT; j++)
{
if (cells[i][j] == cellValue)
{
score++;
}
}
}
return score;
}
// getWhiteScore() calculates the score for the white player.
public int getRedScore()
{
return getScore(ConnectFourCell.RED);
}
public int getBlueScore() {
// TODO Auto-generated method stub
return getScore(ConnectFourCell.BLUE);
}
public ConnectFourCell getCell(int x, int y)
{
if (!isValidCell(x, y))
{
throw new IllegalArgumentException(
"(" + x + ", " + y + ") is not a valid cell");
}
return cells[x][y];
}
/**
* The drop method.
*
* Drop a checker into the specified HEIGHT,
* and return the WIDTH that the checker lands on.
*/
int drop(int HEIGHT) {
if (hasWon()) {
return -1;
}
for ( ; WIDTH<6 && HEIGHT != 0; WIDTH++) { };
if (WIDTH==6) {
// if the WIDTH is 6, it went through all 6 WIDTHs
// of the cells, and couldn't find an empty one.
// Therefore, return false to indicate that this
// drop operation failed.
return -1;
}
// fill the WIDTH of that HEIGHT with a checker.
cells[HEIGHT][WIDTH] = currentPlayer[HEIGHT][WIDTH];
// alternate the players
//currentPlayer = (currentPlayer%2)+1;
return WIDTH;
}
/**
* The toString method
*
* Returns a String representation of this
* Connect Four (TM) game.
*/
public String toString() {
String returnString = "";
for (int WIDTH=5; WIDTH>=0; WIDTH--) {
for (int HEIGHT=0; HEIGHT<7; HEIGHT++) {
returnString = returnString + cells[HEIGHT][WIDTH];
}
returnString = returnString + "\n";
}
return returnString;
}
/**
* The hasWon method.
*
* This method returns true if one of the
* players has won the game.
*/
public boolean hasWon() {
boolean status = false;
// check for a horizontal win
for (int WIDTH=0; WIDTH<6; WIDTH++) {
for (int HEIGHT=0; HEIGHT<4; HEIGHT++) {
if (!(cells[HEIGHT][WIDTH] == ConnectFourCell.NONE) &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+1][WIDTH] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+2][WIDTH] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+3][WIDTH]) {
status = true;
}
}
}
// check for a vertical win
for (int WIDTH=0; WIDTH<3; WIDTH++) {
for (int HEIGHT=0; HEIGHT<7; HEIGHT++) {
if (!(cells[HEIGHT][WIDTH] == ConnectFourCell.NONE) &&
cells[HEIGHT][WIDTH] == cells[HEIGHT][WIDTH+1] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT][WIDTH+2] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT][WIDTH+3]) {
status = true;
}
}
}
// check for a diagonal win (positive slope)
for (int WIDTH=0; WIDTH<3; WIDTH++) {
for (int HEIGHT=0; HEIGHT<4; HEIGHT++) {
if (!(cells[HEIGHT][WIDTH] == ConnectFourCell.NONE) &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+1][WIDTH+1] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+2][WIDTH+2] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+3][WIDTH+3]) {
status = true;
}
}
}
// check for a diagonal win (negative slope)
for (int WIDTH=3; WIDTH<6; WIDTH++) {
for (int HEIGHT=0; HEIGHT<4; HEIGHT++) {
if (!(cells[HEIGHT][WIDTH] == ConnectFourCell.NONE) &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+1][WIDTH-1] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+2][WIDTH-2] &&
cells[HEIGHT][WIDTH] == cells[HEIGHT+3][WIDTH-3]) {
status = true;
}
}
}
return status;
}
private boolean isValidCell(int x, int y)
{
return x >= 0 && x < WIDTH
&& x>= 0 && x<HEIGHT
&& y >= 0 && y < WIDTH
&& y>= 0 && y<HEIGHT;
}
}
package UI;
import java.awt.*;
import javax.swing.*;
import UI.ConnectFourBoardPanel;
import Game.ConnectFourBoard;
import Game.ConnectFourListener;
public class ConnectFourFrame extends JFrame implements ConnectFourListener {
// Variables
private ConnectFourBoard board;
private JLabel scoreLabel;
private ConnectFourBoardPanel boardPanel;
private JLabel statusLabel;
public ConnectFourFrame()
{
// The frame builds its own model.
ConnectFourBoard board = new ConnectFourBoard();
// We want the frame to receive notifications from the board as its
// state changes.
System.out.println(this);
board.addConnectFourListener(this);
setTitle("Informatics 45 Spring 2011: ConnectFour Game");
setSize(700, 700);
setResizable(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setBackground(Color.BLACK);
buildUI();
refreshUI();
}
private void buildUI()
{
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
Font labelFont = new Font("SansSerif", Font.BOLD, 18);
scoreLabel = new JLabel();
scoreLabel.setForeground(Color.WHITE);
scoreLabel.setFont(labelFont);
layout.setConstraints(
scoreLabel,
new GridBagConstraints(
0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(10, 10, 10, 10), 0, 0));
getContentPane().add(scoreLabel);
boardPanel = new ConnectFourBoardPanel(board);
layout.setConstraints(
boardPanel,
new GridBagConstraints(
0, 1, 1, 1, 1.0, 1.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(10, 10, 10, 10), 0, 0));
getContentPane().add(boardPanel);
statusLabel = new JLabel();
statusLabel.setForeground(Color.WHITE);
statusLabel.setFont(labelFont);
layout.setConstraints(
statusLabel,
new GridBagConstraints(
0, 2, 1, 1, 1.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(10, 10, 10, 10), 0, 0));
getContentPane().add(statusLabel);
}
private void refreshUI()
{
// Refreshing the UI means to change the text in each of the
// two labels (the score and the status) and also to ask the
// board to repaint itself.
scoreLabel.setText(
"Blue: " + board.getBlueScore() +
" Red: " + board.getRedScore());
statusLabel.setText(
(board.isBlueTurn() ? "Blue's" : "Red's")
+ " turn");
boardPanel.repaint();
}
// These are the ConnectFourBoardListener event-handling methods.
public void boardChanged()
{
// When the board changes, we'll refresh the entire UI. (There
// are times when this approach is too inefficient, but it will
// work fine for our relatively simple UI.)
refreshUI();
}
public void gameOver()
{
// When the game is over, we'll pop up a message box showing the final
// score, then, after the user dismisses the message box, dispose of
// this window and end the program.
JOptionPane.showMessageDialog(
this,
"Game over!\nFinal score: " + scoreLabel.getText(),
"Game Over",
JOptionPane.INFORMATION_MESSAGE);
dispose();
}
}
your initialising a local var instead of your member var
ConnectFourBoard board = new ConnectFourBoard();
change that line to:
board = new ConnectFourBoard();
这篇关于问题,我在我的connect4游戏的主要方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!