首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Othello游戏板的Java初始化

Othello游戏板的Java初始化
EN

Stack Overflow用户
提问于 2016-09-18 03:52:29
回答 1查看 1.5K关注 0票数 0

我正在尝试用Java初始化Othello的游戏板。它应该提示用户输入两个播放器名称,将player 1设置为暗片,将player 2设置为亮片,但是当我试图编译时,会得到以下错误:

代码语言:javascript
复制
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: core.Disc.setColor

at core.Board.initObjects(Board.java:44) "board[3][3].setColor(Constants.LIGHT);"

at core.Board.<init>(Board.java:24) "initObjects();"

at core.Game.initObjects(Game.java:30) "board = new Board();"

at core.Game.<init>(Game.java:25) "initObjects();"

at othello.Othello.main(Othello.java:17) "Game game = new Game();"

有人能提供一些关于该做什么的见解吗?我不知道怎么回事

如果格式化不好的话很抱歉。不确定如何为多个Java类设置格式。

Board.java

代码语言:javascript
复制
package core;

// board class
public class Board {

    // member variable board (always make access modifier 'private'        
    // Disc = data type (class)
    private Disc[][] board;

    // constructor (same name as class)
    public Board(){

        // method call for initObjects
        initObjects();
    }

    private void initObjects(){

        // declaring size of array // new is used to allocate memory
        board = new Disc[Constants.ROWS][Constants.COLUMNS];

        // looping and initializing board
        for(int row = 0; row < Constants.ROWS; row++){

            for (int col = 0; col < Constants.COLUMNS; col++){

                // calling no argument constructor
                // for class Disc
                board[row][col] = new Disc();
            }
        }

        // setColor is part of class Disc
        board[3][3].setColor(Constants.LIGHT);
        board[3][4].setColor(Constants.DARK);
        board[4][3].setColor(Constants.DARK);
        board[4][4].setColor(Constants.LIGHT);
    }



    /**
     * @return the board
     */
    public Disc[][] getBoard() {
        return board;
    }

    /**
     * @param board the board to set
     */
    public void setBoard(Disc[][] board) {
        this.board = board;
    }

}

Constants.java

代码语言:javascript
复制
package core;

import java.awt.Color;

public class Constants {

    public static Color DARK = Color.BLACK;
    public static Color LIGHT = Color.WHITE;
    public static int PLAYER_ONE = 0;
    public static int PLAYER_TWO = 1;
    public static int ROWS = 8;
    public static int COLUMNS = 8;
    public static int MAX_PLAYERS = 2;

}

Disc.java

代码语言:javascript
复制
package core;

import java.awt.Color;


public class Disc {

    // member variable
    private Color discColor;

    /**
     * @return the discColor
     */
    public Color getDiscColor() {
        return discColor;
    }

    /**
     * @param discColor the discColor to set
     */
    public void setDiscColor(Color discColor) {
        this.discColor = discColor;
    }

}

Game.java

代码语言:javascript
复制
package core;

import java.util.ArrayList;
import javax.swing.JOptionPane;


public class Game {


    // member variables
    private ArrayList<Player> players;
    private Board board;

    public Game(){

        // initObjects method call
        initObjects();
    }

    private void initObjects(){

        board = new Board();
        createPlayers();
        printPlayers();
    }

    private void createPlayers(){

        players = new ArrayList<Player>();

        for(int i=0; i < 2; i++){

            String name = JOptionPane.showInputDialog(null, "Enter player's name");
            Player player = new Player();
            player.setName(name);

            if(i == Constants.PLAYER_ONE)
                player.setDiscColor(Constants.DARK);
            else if(i == Constants.PLAYER_TWO)
                player.setDiscColor(Constants.LIGHT);

        Player.add(players);

    }   
}

    private void printPlayers()
{
    System.out.println("The game has the following players:");


    for(Player name : getPlayers())
    {
        System.out.println("Player " + name.getName() + " is playing disc color " + name.getDiscColor()); 
    }
}

    /**
     * @return the players
     */
    public ArrayList<Player> getPlayers() {
        return players;
    }

    /**
     * @param players the players to set
     */
    public void setPlayers(ArrayList<Player> players) {
        this.players = players;
    }

    /**
     * @return the board
     */
    public Board getBoard() {
        return board;
    }

    /**
     * @param board the board to set
     */
    public void setBoard(Board board) {
        this.board = board;
    }

}

Player.java

代码语言:javascript
复制
package core;

import java.awt.Color;


public class Player {

    private String name;

    private Color discColor;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the discColor
     */
    public Color getDiscColor() {
        return discColor;
    }

    /**
     * @param discColor the discColor to set
     */
    public void setDiscColor(Color discColor) {
        this.discColor = discColor;
    }

}

Othello.java

代码语言:javascript
复制
public class Othello {

public static void main(String[] args) {
    Game game = new Game();
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-18 05:51:33

变量boardDisc的2D数组。

Disc没有方法setColor()

正确的方法名是setDiscColor()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39553909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档