[英]Moving counters along a 2d array
I have three classes and am trying to make a game whereby users move along a grid depending on what is rolled by a die. 我有三个课程,正在尝试制作一个游戏,使用户根据骰子滚动的物体沿着网格移动。
I have my main BoardGame class, containing the GUI and the counters which currently are Jlabel images (i'm open to suggestions as to what I could use instead of a JLabel - i wasnt so sure myself). 我有我的主要BoardGame类,其中包含GUI和计数器,这些计数器和计数器当前是Jlabel图像(我可以就可以代替JLabel使用的内容提出建议-我不确定自己是什么)。 I have a Grid class which I have arranged into a 2D array and called an instance of in the BoardGame class, and I have a die class which rolls a random number from 1-6.
我有一个Grid类,已将其布置成2D数组,并在BoardGame类中称为的实例,并且我有一个die类,该类从1-6随机滚动。
I am trying to get me counters to start at the first square on the grid, and then advance in a left-to-right-right-to-left fashion. 我试图让我的计数器从网格的第一个正方形开始,然后以从左到右,从右到左的方式前进。 I am unsure however of how to make the counters move through the grid in the first place.
但是,我不确定如何首先使计数器在网格中移动。 Hopefully, if I can figure this out, I believe I can then implement them moving a specific amount via the die.
希望,如果我能弄清楚这一点,我相信我可以实现它们通过冲模移动特定量。
Thanks for the help in advance 我在这里先向您的帮助表示感谢
GameBoard class: GameBoard类:
public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel GreenDot;
private JLabel redDot;
private JButton startButton;
private Grid grid;
private Die die;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Grid grid = new Grid();
GameBoard inst = new GameBoard(grid);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public GameBoard(Grid grid) {
super();
this.grid = grid;
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
redDot = new JLabel();
getContentPane().add(redDot);
redDot.setText("jLabel1");
redDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/download.png")));
redDot.setBounds(220, 434, 20, 12);
redDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
GreenDot = new JLabel();
getContentPane().add(GreenDot);
GreenDot.setText("jLabel1");
GreenDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/3d-green-ball-th.png")));
GreenDot.setBounds(222, 453, 21, 13);
GreenDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Game");
startButton.setBounds(64, 443, 83, 23);
}
{
Board = new JLabel();
getContentPane().add(Board);
Board.setLayout(null);
Board.setText("jLabel1");
Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
Board.setBounds(204, -1, 742, 484);
}
pack();
this.setSize(963, 523);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
} }
Grid class: 网格类别:
public class Grid {
int[][] multi = {
{ 0, 0,-1, 0, 0,-1, 0,-1, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 0,-1, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}
};
} }
Die class: 模具类:
public class Die {
public Die() {
}
public void dieRoll() {
int SIDES = 6;
int roll = (int) (Math.random() * SIDES) + 1;
System.out.println(roll);
}
} }
A simple way of doing this would be to change the die class so it has 一种简单的方法是更改die类,以便
{
private int sides;
public Die(int numSides){
sides = numSides;
}
public int roll(){
return (int) (Math.random() * SIDES) + 1
}
}
Then you can roll a six sided die like so 然后,您可以像这样滚动六面模具
//this creates the die
Die sixSides = new Die(6);
//this rolls the die and prints the roll
System.out.print("That roll got you a " + sixSides.roll());
to move along a 2D array, all you need to do is use the modulus and you need to create a point object class 要沿着2D数组移动,您所需要做的就是使用模数,并且需要创建一个点对象类
public position move(int num, int x, int y){//input is dice roll int then current position
for(int i = 0; i < num;i++){
if(i%10==0){
y++;
x = 0;
}else{
x++;
}
}
return new point(x,y);
}
To access the x and y positions of the point object you would need to write get and set methods for the object. 要访问点对象的x和y位置,您需要为该对象编写get和set方法。 something like this
像这样的东西
point player1 = new point(0,0);
int xposition = player1.getX
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.