构造函数没有按我预期的那样工作 [英] Constructor didn't work as I expected

查看:60
本文介绍了构造函数没有按我预期的那样工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我正在使用Eclipse和Java进行十六进制桌面游戏.为此,我尝试创建两个类:Hex和Board.
这是十六进制类的代码:

Hello everyone!
I''m doing a hex board game by using Eclipse and Java. For this purpose, I''m trying to create 2 classes: Hex and Board.
Here is the code for Hex class:

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import javax.swing.*;

public class Hex {
	
	private List<Point2D.Double> Points = new ArrayList<Point2D.Double>();
	private double side;
	private double h;
	private double r;
	private int orientation;
	private double x;
	private double y;

	public List<Point2D.Double> getPoints(){
		return Points;
	}
	
	
	/// <param name="side">length of one side of the hexagon</param>
	public Hex(double x, double y, double side, int orientation)
	{
		Initialize(x, y, side, orientation);
	}

	public Hex(Point2D.Double point, double side, int orientation)
	{
		Initialize(point.getX(), point.getY(), side, orientation);
	}

	public Hex()
	{ }

	/// <summary>
	/// Sets internal fields and calls CalculateVertices()
	/// </summary>
	private void Initialize(double x, double y, double side, int orientation)
	{
		this.x = x;
		this.y = y;
		this.side = side;
		this.orientation = orientation;
		CalculateVertices();
	}

	/// <summary>
	/// Calculates the vertices of the hex based on orientation. Assumes that Points[0] contains a value.
	/// </summary>
	
	private static double CalculateH(double side)
	{
	    return Math.sin(Math.toRadians(30))*side;
	}

	public static double CalculateR(double side)
	{
	    return Math.cos(Math.toRadians(30))*side;
	    //ConvertToFloat(System.Math.Cos(DegreesToRadians(30)) * side);
	} 
		
	private void CalculateVertices()
	{
		//  
		//  h = short length (outside)
		//  r = long length (outside)
		//  side = length of a side of the hexagon, all 6 are equal length
		//
		//  h = sin (30 degrees) x side
		//  r = cos (30 degrees) x side
		//
		//		 h
		//	     ---
		//   ----     |r
		//  /    \    |          
		// /      \   |
		// \      /
		//  \____/
		//
		// Flat orientation (scale is off)
		//
        //     /\
		//    /  \
		//   /    \
		//   |    |
		//   |    |
		//   |    |
		//   \    /
		//    \  /
		//     \/
		// Pointy orientation (scale is off)
     
		h = CalculateH(side);
		r = CalculateR(side);

		switch (orientation)
		{ 
			case 0://Flat orientation
				// x,y coordinates are top left point
				Points.add(new Point2D.Double(x, y));				
				Points.add(new Point2D.Double(x + side, y));
				Points.add(new Point2D.Double(x + side + h, y + r));
				Points.add(new Point2D.Double(x + side, y + r + r));
				Points.add(new Point2D.Double(x, y + r + r));
				Points.add(new Point2D.Double(x - h, y + r));
				break;
			case 1://Pointy orientation
				//x,y coordinates are top center point
				Points.add(new Point2D.Double(x, y));				
				Points.add(new Point2D.Double(x + r, y + h));
				Points.add(new Point2D.Double(x + r, y + side + h));
				Points.add(new Point2D.Double(x, y + side + h + h));
				Points.add(new Point2D.Double(x - r, y + side + h));
				Points.add(new Point2D.Double(x - r, y + h));
				break;
			default:
				break;
		
		}
		
	}

}


而Board类的代码是(顺便说一句,board是许多十六进制的集合):


and the code for Board class is (by the way board is a collection of many hexes):

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import javax.swing.*;

/// <summary>
/// Represents a 2D hexagonal board
/// </summary>
public class Board{
    private Hex hexes[][];
    private int width;
    private int height;
    private int xOffset;
    private int yOffset;
    private double side;
    private double pixelWidth;
    private double pixelHeight;
    private int orientation;
    public double getPixelWidth(){
        return pixelWidth;
    }
    public double getPixelHeight(){
        return pixelHeight;
    }
    public Hex[][] getHexes(){
        return hexes;
    }
    /// <param name="width">Board width</param>
    /// <param name="height">Board height</param>
    /// <param name="side">Hexagon side length</param>
    /// <param name="orientation">Orientation of the hexagons</param>
    public Board(int width, int height, int side, int orientation)
    {
        Initialize(width, height, side, orientation, 0, 0);
    }

    /// <param name="width">Board width</param>
    /// <param name="height">Board height</param>
    /// <param name="side">Hexagon side length</param>
    /// <param name="orientation">Orientation of the hexagons</param>
    /// <param name="xOffset">X coordinate offset</param>
    /// <param name="yOffset">Y coordinate offset</param>
    public Board(int width, int height, int side, int orientation, int xOffset, int yOffset)
    {
        Initialize(width, height, side, orientation, xOffset, yOffset);
    }

    /// <summary>
    /// Sets internal fields and creates board
    /// </summary>
    /// <param name="width">Board width</param>
    /// <param name="height">Board height</param>
    /// <param name="side">Hexagon side length</param>
    /// <param name="orientation">Orientation of the hexagons</param>
    /// <param name="xOffset">X coordinate offset</param>
    /// <param name="yOffset">Y coordinate offset</param>

    private void Initialize(int width, int height, int side, int orientation, int xOffset, int yOffset)
    {
        this.width = width;
        this.height = height;
        this.xOffset = xOffset;
        this.yOffset = yOffset;
        this.side = side;
        this.orientation = orientation;
        //hexes = new Hex[height, width]; //opposite of what we'd expect
        //this.boardState = new BoardState();

        double h = CalculateH(side); // short side
        double r = CalculateR(side); // long side

        //
        // Calculate pixel info..remove?
        // because of staggering, need to add an extra r/h
        double hexWidth = 0;
        double hexHeight = 0;
        switch (orientation)
        {
            case 0:
                hexWidth = side + h;
                hexHeight = r + r;
                this.pixelWidth = (width * hexWidth) + h;
                this.pixelHeight = (height * hexHeight) + r;
                break;
            case 1:
                hexWidth = r + r;
                hexHeight = side + h;
                this.pixelWidth = (width * hexWidth) + r;
                this.pixelHeight = (height * hexHeight) + h;
                break;
            default:
                break;
        }

        boolean inTopRow = false;
        boolean inBottomRow = false;
        boolean inLeftColumn = false;
        boolean inRightColumn = false;
        boolean isTopLeft = false;
        boolean isTopRight = false;
        boolean isBotomLeft = false;
        boolean isBottomRight = false;


        // i = y coordinate (rows), j = x coordinate (columns) of the hex tiles 2D plane
        for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    // Set position booleans
                        if (i == 0)
                        {
                            inTopRow = true;
                        }
                        else
                        {
                            inTopRow = false;
                        }

                        if (i == height - 1)
                        {
                            inBottomRow = true;
                        }
                        else
                        {
                            inBottomRow = false;
                        }

                        if (j == 0)
                        {
                            inLeftColumn = true;
                        }
                        else
                        {
                            inLeftColumn = false;
                        }

                        if (j == width - 1)
                        {
                            inRightColumn = true;
                        }
                        else
                        {
                            inRightColumn = false;
                        }

                        if (inTopRow && inLeftColumn)
                        {
                            isTopLeft = true;
                        }
                        else
                        {
                            isTopLeft = false;
                        }

                        if (inTopRow && inRightColumn)
                        {
                            isTopRight = true;
                        }
                        else
                        {
                            isTopRight = false;
                        }

                        if (inBottomRow && inLeftColumn)
                        {
                            isBotomLeft = true;
                        }
                        else
                        {
                            isBotomLeft = false;
                        }

                        if (inBottomRow && inRightColumn)
                        {
                            isBottomRight = true;
                        }
                        else
                        {
                            isBottomRight = false;
                        }

                    //
                    // Calculate Hex positions
                    //
                    if (isTopLeft)
                    {
                        //First hex
                        switch (orientation)
                        {
                            case 0://Flat orientation
                                hexes[0][0] = new Hex(0 + h + xOffset, 0 + yOffset, side, orientation);
                                break;
                            case 1:
                                hexes[0][0] = new Hex(0 + r + xOffset, 0 + yOffset, side, orientation);
                                break;
                            default:
                                break;
                        }

                    }
                    else
                    {
                        switch (orientation)
                        {
                            case 0:
                                if (inLeftColumn)
                                {
                                    // Calculate from hex above
                                    hexes[i][j] = new Hex(hexes[i - 1][j].getPoints().get(4), side, orientation);
                                }
                                else
                                {



                                    // Calculate from Hex to the left and need to stagger the columns
                                    if (j % 2 == 0)
                                    {
                                        // Calculate from Hex to left's Upper Right Vertice plus h and R offset
                                        double x = hexes[i][ j - 1].getPoints().get(1).getX();
                                        double y = hexes[i][ j - 1].getPoints().get(1).getY();//upperRight
                                        x += h;
                                        y -= r;
                                        hexes[i][j] = new Hex(x, y, side, orientation);
                                    }
                                    else
                                    {
                                        // Calculate from Hex to left's Middle Right Vertice
                                        hexes[i][j] = new Hex(hexes[i][j - 1].getPoints().get(2), side, orientation);//MiddleRight
                                    }
                                }
                                break;
                            case 1:

                                if (inLeftColumn)
                                {
                                    // Calculate from hex above and need to stagger the rows
                                    if (i % 2 == 0)
                                    {
                                        hexes[i][j] = new Hex(hexes[i - 1][j].getPoints().get(4), side, orientation);//BottomLeft
                                    }
                                    else
                                    {
                                        hexes[i][j] = new Hex(hexes[i - 1][j].getPoints().get(2), side, orientation);//BottomRight
                                    }

                                }
                                else
                                {
                                    // Calculate from Hex to the left
                                    double x = hexes[i][j - 1].getPoints().get(1).getX();//UpperRight
                                    double y = hexes[i][j - 1].getPoints().get(1).getY();//UpperRight
                                    x += r;
                                    y -= h;
                                    hexes[i][j] = new Hex(x, y, side, orientation);
                                }
                                break;
                            default:
                                break;
                        }


                    }


                }
            }



        }
    private static double CalculateH(double side)
    {
        return Math.sin(Math.toRadians(30))*side;
    }

    public static double CalculateR(double side)
    {
        return Math.cos(Math.toRadians(30))*side;
    }


}



我成功调用了Hex类的构造函数



I successfully called the constructor for Hex class

Hex myHex = new Hex(50, 50, 50, 1);


我确实曾尝试调用Board类的构造函数,但不幸的是,它没有用.


I did tried to call the constructor for Board class, but unfortunately, it didn''t work.

Board myBoard = new Board(4, 2, 50, 1);


我不擅长调试代码,现在正在研究中.
任何人都可以告诉我代码中的问题是什么!
非常感谢您的帮助!
祝你有美好的一天!


I''m not good at debugging the code, I''m studying it now.
Could anyone tell me what is the problem in my code, please!
I''ll really appreciate your help!
Have a nice day!

推荐答案

成员7993235写道:
Member 7993235 wrote:

I''m不擅长调试代码,我正在研究它.

I''m not good at debugging the code, I''m studying it now.


这不是一个很好的借口.不要学习调试-调试.

比问这样的问题要有效得多.

您的问题几乎没有用.您甚至都没有解释问题是什么.这是什么意思它没有用".对于软件开发人员来说,没有没有用";软件开发人员提供全面的问题报告,发布完整的异常转储,调试代码等.但是它们永远不会被草率的没有用"来解释.

至于调试研究:没有太多的研究.在您之前的研究是几代人的体面工程师.您只需要使用这些工具.研究其他东西-数百个未解决的问题等着您来解决.

—SA


This is not a good excuse. Don''t study debugging — debug.

It will be much more productive than asking such questions.

You question is nearly useless. You don''t even explain what''s the problem is. What does it mean "it didn''t work". For a software developer, there is no "didn''t work"; a software developer provides comprehensive issue reports, posts full exception dump, debug code, etc. We all have unresolved problems; but they never are explained by the sloppy "didn''t work".

As to study of the debugging: there is not much to study. It was studies be generations of decent engineers before you; you only need to use the tools. Study something else — hundreds of unresolved problems wait for you to work on them.

—SA


我建​​议您回顾一下如何设计课程.构造函数中的代码太多,无法找出失败时出了什么问题.更改程序,以便构造函数只保存变量的值,然后显式调用Initialize()方法.您的Board类中还应该有类似AddHex()的东西,因此您可以分别添加这些对象,以再次降低构造函数的复杂性.
I would suggest you review how you have designed your classes. You have far too much code in your constructors to be able to figure out what is going wrong when it fails. Change your program so the constructor just saves the values of your variables and then make explicit calls to your Initialize() methods. You should also have something like AddHex() in your Board class, so you can add these objects individually, again to reduce the complexity of your constructor.


成员7993235写道:
Member 7993235 wrote:

构造函数无法正常工作

,这意味着: 构造函数被观察到行为与其预期之一不匹配" .
您发布了代码(一件好事),但没有向我们解释什么是预期的行为,什么是观察到的行为.没有此类信息很难提供任何帮助.

That means: "constructor observed behaviour doesn''t match its expected one".
You posted the code (a good thing) but didn''t explain us what is the expected behaviour and what is the observed one. Without such info is very hard providing any help.


这篇关于构造函数没有按我预期的那样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆