我正在创建一个骰子游戏,其中2个玩家有他们自己的骰子,每个人都有自己的轮到扔他的骰子,玩家可以完全失去分数或获得分数取决于他们站在窗口的位置,游戏结束时,任何一个玩家首先站在终点车道上,获胜者是得分最高的玩家,我已经在游戏的设计到目前为止,但还没有在逻辑上工作。
这个链接是游戏应该是什么样子的图片:
我想知道我如何才能添加球员1和球员2在每个瓷砖上面的照片,所以基本上每次当一个球员玩他的回合,我希望他的名字开始移动取决于他掷骰子时得到的数字。
代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class diceGame {
public static void main(String[] args) {
new RollDicePanel();
}
}
class RollDicePanel extends JFrame {
private Dice myLeftDie;
private Dice myRightDie;
JPanel mainPanel;
JPanel topLeft,topCenter,topRight;
JPanel centerLeft,centerCenter,centerRight;
JPanel bottomLeft,bottomCenter,bottomRight;
JLabel tLeft,tRight,tCenter,cLeft,cCenter1,cCenter2,cRight,bLeft,bCenter,bRight;
RollDicePanel() {
myLeftDie = new Dice();
myRightDie = new Dice();
JButton rollButton1 = new JButton("Player 1");
rollButton1.setFont(new Font("Sansserif", Font.BOLD, 5));
rollButton1.addActionListener(new RollListener1());
JButton rollButton2 = new JButton("Player 2");
rollButton2.setFont(new Font("Sansserif", Font.BOLD, 5));
rollButton2.addActionListener(new RollListener2());
topLeft = new JPanel();
tLeft = new JLabel("+20");
tLeft.setFont(new Font("Sansserif", Font.BOLD, 20));
tLeft.setLayout(new FlowLayout(FlowLayout.CENTER));
topLeft.add(tLeft,BorderLayout.WEST);
topLeft.setBackground(Color.RED);
topRight = new JPanel();
tRight = new JLabel("-50");
tRight.setFont(new Font("Sansserif", Font.BOLD, 20));
topRight.add(tRight,BorderLayout.EAST);
topRight.setBackground(Color.ORANGE);
topCenter = new JPanel();
tCenter = new JLabel("Try Again");
tCenter.setFont(new Font("Sansserif", Font.BOLD, 20));
topCenter.add(tCenter,BorderLayout.CENTER);
topCenter.setBackground(Color.BLUE);
centerCenter = new JPanel();
cCenter1 = new JLabel("Player 1");
cCenter2 = new JLabel("Player 2");
centerCenter.add(rollButton1,BorderLayout.NORTH);
centerCenter.add(myLeftDie,BorderLayout.SOUTH);
centerCenter.add(rollButton2,BorderLayout.NORTH);
centerCenter.add(myRightDie,BorderLayout.EAST);
centerLeft = new JPanel();
cLeft = new JLabel("Finish");
cLeft.setFont(new Font("Sansserif", Font.BOLD, 20));
centerLeft.add(cLeft,BorderLayout.WEST);
centerLeft.setBackground(Color.YELLOW);
centerRight = new JPanel();
cRight = new JLabel("Lost All");
cRight.setFont(new Font("Sansserif", Font.BOLD, 20));
centerRight.add(cRight,BorderLayout.EAST);
centerRight.setBackground(Color.GREEN);
bottomRight = new JPanel();
bRight = new JLabel("+30");
bRight.setFont(new Font("Sansserif", Font.BOLD, 20));
bottomRight.add(bRight,BorderLayout.EAST);
bottomRight.setBackground(Color.CYAN);
bottomCenter = new JPanel();
bCenter = new JLabel("+10");
bCenter.setFont(new Font("Sansserif", Font.BOLD, 20));
bottomCenter.add(bCenter,BorderLayout.CENTER);
bottomCenter.setBackground(Color.magenta);
bottomLeft = new JPanel();
bLeft = new JLabel("-10");
bLeft.setFont(new Font("Sansserif", Font.BOLD, 20));
bLeft.setForeground(Color.WHITE);
bottomLeft.add(bLeft,BorderLayout.WEST);
bottomLeft.setBackground(Color.BLACK);
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3,3));
mainPanel.add(topLeft,BorderLayout.NORTH);
mainPanel.add(topCenter,BorderLayout.NORTH);
mainPanel.add(topRight,BorderLayout.NORTH);
mainPanel.add(centerLeft,BorderLayout.CENTER);
mainPanel.add(centerCenter,BorderLayout.CENTER);
mainPanel.add(centerRight,BorderLayout.CENTER);
mainPanel.add(bottomLeft,BorderLayout.SOUTH);
mainPanel.add(bottomCenter,BorderLayout.SOUTH);
mainPanel.add(bottomRight,BorderLayout.SOUTH);
this.setTitle("Dice Game");
this.setBounds(200, 200, 700, 700);
this.add(mainPanel);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
}
private class RollListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
myLeftDie.roll();
}
}
private class RollListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
myRightDie.roll();
}
}
}
class Dice extends JPanel {
private static final int SPOT_DIAMETER = 4;
private int myFaceValue;
public Dice() {
setBackground(Color.white);
setPreferredSize(new Dimension(15,15));
roll();
}
int roll() {
int val = (int)(6*Math.random() + 1);
setValue(val);
return val;
}
public int getValue() {
return myFaceValue;
}
public void setValue(int spots) {
myFaceValue = spots;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
g.drawRect(0, 0, w-1, h-1);
switch (myFaceValue) {
case 1: drawSpot(g, w/2, h/2);
break;
case 3: drawSpot(g, w/2, h/2);
case 2: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
break;
case 5: drawSpot(g, w/2, h/2);
case 4: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
break;
case 6: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
drawSpot(g, w/4, h/2);
drawSpot(g, 3*w/4, h/2);
break;
}
}
private void drawSpot(Graphics g, int x, int y) {
g.fillOval(x-SPOT_DIAMETER/2, y-SPOT_DIAMETER/2, SPOT_DIAMETER, SPOT_DIAMETER);
}
}
我把你的GUI修好了。
我通过调用SwingUtilities
Invokelater
方法启动了GUI。此方法确保在事件调度线程上创建和执行Swing组件。
我在类构造函数中创建了jframe
。我创建了一个外部JPanel
、八个内部JPanel
和一个中心JPanel
。JFrame
方法必须按特定顺序执行。这是我的秋千GUI使用的顺序。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DiceGame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new RollDicePanel();
}
});
}
}
class RollDicePanel extends JFrame {
private static final long serialVersionUID = 1L;
private Dice myLeftDie;
private Dice myRightDie;
public RollDicePanel() {
this.setTitle("Dice Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(createOuterPanel(), BorderLayout.CENTER);
this.pack();
this.setLocationByPlatform(true);
this.setVisible(true);
}
private JPanel createOuterPanel() {
JPanel panel = new JPanel(new GridLayout(0, 3));
panel.add(createInnerPanel("+20", new Color(152, 254, 152),
Color.BLACK));
panel.add(createInnerPanel("Try Again...", new Color(252, 252, 201),
Color.BLACK));
panel.add(createInnerPanel("-50", new Color(254, 152, 152),
Color.BLACK));
panel.add(createInnerPanel("Finish!",new Color(0, 203, 101),
Color.BLACK));
panel.add(createCenterPanel());
panel.add(createInnerPanel("Lost All",new Color(252, 48, 99),
Color.BLACK));
panel.add(createInnerPanel("-10", new Color(254, 203, 203),
Color.BLACK));
panel.add(createInnerPanel("+10", new Color(203, 254, 203),
Color.BLACK));
panel.add(createInnerPanel("+30", new Color(49, 253, 0),
Color.BLACK));
return panel;
}
private JPanel createInnerPanel(String text, Color backgroundColor,
Color foregroundColor) {
JPanel panel = new JPanel();
panel.setBackground(backgroundColor);
JLabel label = new JLabel(text);
label.setFont(new Font("Sansserif", Font.BOLD, 20));
label.setForeground(foregroundColor);
panel.add(label);
return panel;
}
private JPanel createCenterPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JButton rollButton1 = new JButton("Player 1");
rollButton1.setFont(new Font("Sansserif", Font.BOLD, 10));
rollButton1.addActionListener(new RollListener1());
panel.add(rollButton1, gbc);
gbc.gridx++;
JButton rollButton2 = new JButton("Player 2");
rollButton2.setFont(new Font("Sansserif", Font.BOLD, 10));
rollButton2.addActionListener(new RollListener2());
panel.add(rollButton2, gbc);
gbc.gridx = 0;
gbc.gridy++;
myLeftDie = new Dice();
panel.add(myLeftDie, gbc);
gbc.gridx++;
myRightDie = new Dice();
panel.add(myRightDie, gbc);
return panel;
}
private class RollListener1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
myLeftDie.roll();
}
}
private class RollListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
myRightDie.roll();
}
}
}
class Dice extends JPanel {
private static final long serialVersionUID = 1L;
private static final int SPOT_DIAMETER = 4;
private int myFaceValue;
public Dice() {
setBackground(Color.white);
setPreferredSize(new Dimension(15, 15));
roll();
}
int roll() {
int val = (int) (6 * Math.random() + 1);
setValue(val);
return val;
}
public int getValue() {
return myFaceValue;
}
public void setValue(int spots) {
myFaceValue = spots;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
g.drawRect(0, 0, w - 1, h - 1);
switch (myFaceValue) {
case 1:
drawSpot(g, w / 2, h / 2);
break;
case 3:
drawSpot(g, w / 2, h / 2);
case 2:
drawSpot(g, w / 4, h / 4);
drawSpot(g, 3 * w / 4, 3 * h / 4);
break;
case 5:
drawSpot(g, w / 2, h / 2);
case 4:
drawSpot(g, w / 4, h / 4);
drawSpot(g, 3 * w / 4, 3 * h / 4);
drawSpot(g, 3 * w / 4, h / 4);
drawSpot(g, w / 4, 3 * h / 4);
break;
case 6:
drawSpot(g, w / 4, h / 4);
drawSpot(g, 3 * w / 4, 3 * h / 4);
drawSpot(g, 3 * w / 4, h / 4);
drawSpot(g, w / 4, 3 * h / 4);
drawSpot(g, w / 4, h / 2);
drawSpot(g, 3 * w / 4, h / 2);
break;
}
}
private void drawSpot(Graphics g, int x, int y) {
g.fillOval(x - SPOT_DIAMETER / 2, y - SPOT_DIAMETER / 2,
SPOT_DIAMETER, SPOT_DIAMETER);
}
}
我走到前面,完成了比赛。
我创建了一个逻辑模型来保存棋盘方块和球员位置,并为球员1和球员2得分。
我修改了视图以使用逻辑模型。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class DiceGame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new RollDicePanel();
}
});
}
}
class RollDicePanel extends JFrame {
private static final long serialVersionUID = 1L;
private final DiceGameModel model;
private Dice myLeftDie;
private Dice myRightDie;
private InnerSquarePanel[] innerPanels;
private JTextField scoreField1;
private JTextField scoreField2;
public RollDicePanel() {
this.model = new DiceGameModel();
this.setTitle("Dice Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(createOuterPanel(), BorderLayout.CENTER);
this.pack();
this.setLocationByPlatform(true);
this.setVisible(true);
}
private JPanel createOuterPanel() {
int length = model.getBoardSquares().size();
this.innerPanels = new InnerSquarePanel[length];
for (int index = 0; index < length; index++) {
innerPanels[index] = new InnerSquarePanel(
model.getBoardSquares().get(index));
}
JPanel panel = new JPanel(new GridLayout(0, 3));
panel.add(innerPanels[1].getPanel());
panel.add(innerPanels[2].getPanel());
panel.add(innerPanels[3].getPanel());
panel.add(innerPanels[0].getPanel());
panel.add(createCenterPanel());
panel.add(innerPanels[4].getPanel());
panel.add(innerPanels[7].getPanel());
panel.add(innerPanels[6].getPanel());
panel.add(innerPanels[5].getPanel());
innerPanels[0].getPlayer1Label().setText("Player 1");
innerPanels[0].getPlayer2Label().setText("Player 2");
return panel;
}
private JPanel createCenterPanel() {
JPanel panel = new JPanel(new GridBagLayout());
Font font = new Font("Arial", Font.BOLD, 12);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
JButton rollButton1 = new JButton("Player 1");
rollButton1.setFont(font);
rollButton1.addActionListener(new RollListener1());
panel.add(rollButton1, gbc);
gbc.gridx++;
JButton rollButton2 = new JButton("Player 2");
rollButton2.setFont(font);
rollButton2.addActionListener(new RollListener2());
panel.add(rollButton2, gbc);
gbc.gridx = 0;
gbc.gridy++;
myLeftDie = new Dice();
panel.add(myLeftDie, gbc);
gbc.gridx++;
myRightDie = new Dice();
panel.add(myRightDie, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel label = new JLabel("Score");
label.setFont(font);
panel.add(label, gbc);
gbc.gridx++;
label = new JLabel("Score");
label.setFont(font);
panel.add(label, gbc);
gbc.gridx = 0;
gbc.gridy++;
scoreField1 = new JTextField(4);
scoreField1.setEditable(false);
scoreField1.setFont(font);
scoreField1.setHorizontalAlignment(JTextField.CENTER);
panel.add(scoreField1, gbc);
gbc.gridx++;
scoreField2 = new JTextField(4);
scoreField2.setEditable(false);
scoreField2.setFont(font);
scoreField2.setHorizontalAlignment(JTextField.CENTER);
panel.add(scoreField2, gbc);
return panel;
}
private class RollListener1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
int position = model.getPlayer1position();
int roll = myLeftDie.roll();
model.incrementPlayer1position(roll);
BoardSquare boardSquare = model.getBoardSquares().get(
model.getPlayer1position());
int score = boardSquare.execute(model.getPlayer1score());
model.setPlayer1score(score);
innerPanels[position].getPlayer1Label().setText(" ");
innerPanels[model.getPlayer1position()].getPlayer1Label().
setText("Player 1");
scoreField1.setText(Integer.toString(score));
}
}
private class RollListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
int position = model.getPlayer2position();
int roll = myRightDie.roll();
model.incrementPlayer2position(roll);
BoardSquare boardSquare = model.getBoardSquares().get(
model.getPlayer2position());
int score = boardSquare.execute(model.getPlayer2score());
model.setPlayer2score(score);
innerPanels[position].getPlayer2Label().setText(" ");
innerPanels[model.getPlayer2position()].getPlayer2Label().
setText("Player 2");
scoreField2.setText(Integer.toString(score));
}
}
}
class Dice extends JPanel {
private static final long serialVersionUID = 1L;
private static final int SPOT_DIAMETER = 4;
private int myFaceValue;
public Dice() {
setBackground(Color.white);
setPreferredSize(new Dimension(15, 15));
roll();
}
int roll() {
int val = (int) (6 * Math.random() + 1);
setValue(val);
return val;
}
public int getValue() {
return myFaceValue;
}
public void setValue(int spots) {
myFaceValue = spots;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
g.drawRect(0, 0, w - 1, h - 1);
switch (myFaceValue) {
case 1:
drawSpot(g, w / 2, h / 2);
break;
case 3:
drawSpot(g, w / 2, h / 2);
case 2:
drawSpot(g, w / 4, h / 4);
drawSpot(g, 3 * w / 4, 3 * h / 4);
break;
case 5:
drawSpot(g, w / 2, h / 2);
case 4:
drawSpot(g, w / 4, h / 4);
drawSpot(g, 3 * w / 4, 3 * h / 4);
drawSpot(g, 3 * w / 4, h / 4);
drawSpot(g, w / 4, 3 * h / 4);
break;
case 6:
drawSpot(g, w / 4, h / 4);
drawSpot(g, 3 * w / 4, 3 * h / 4);
drawSpot(g, 3 * w / 4, h / 4);
drawSpot(g, w / 4, 3 * h / 4);
drawSpot(g, w / 4, h / 2);
drawSpot(g, 3 * w / 4, h / 2);
break;
}
}
private void drawSpot(Graphics g, int x, int y) {
g.fillOval(x - SPOT_DIAMETER / 2, y - SPOT_DIAMETER / 2,
SPOT_DIAMETER, SPOT_DIAMETER);
}
}
class InnerSquarePanel {
private JLabel player1Label;
private JLabel player2Label;
private final JPanel panel;
public InnerSquarePanel(BoardSquare boardSquare) {
this.panel = createInnerPanel(boardSquare);
}
private JPanel createInnerPanel(BoardSquare boardSquare) {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBackground(boardSquare.getBackgroundColor());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 10, 10 ,10);
gbc.gridwidth = 2;
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel(boardSquare.getText());
label.setFont(new Font("Arial", Font.BOLD, 24));
label.setForeground(boardSquare.getForegroundColor());
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label, gbc);
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridwidth = 1;
gbc.gridy++;
player1Label = new JLabel(" ");
player1Label.setForeground(boardSquare.getForegroundColor());
player1Label.setHorizontalAlignment(JLabel.LEADING);
player1Label.setVerticalAlignment(JLabel.BOTTOM);
panel.add(player1Label, gbc);
gbc.anchor = GridBagConstraints.LINE_END;
gbc.gridx++;
player2Label = new JLabel(" ");
player2Label.setForeground(boardSquare.getForegroundColor());
player2Label.setHorizontalAlignment(JLabel.TRAILING);
player2Label.setVerticalAlignment(JLabel.BOTTOM);
panel.add(player2Label, gbc);
return panel;
}
public JLabel getPlayer1Label() {
return player1Label;
}
public JLabel getPlayer2Label() {
return player2Label;
}
public JPanel getPanel() {
return panel;
}
}
class DiceGameModel {
private int player1position;
private int player2position;
private int player1score;
private int player2score;
private final List<BoardSquare> boardSquares;
public DiceGameModel() {
this.player1position = 0;
this.player2position = 0;
this.player1score = 0;
this.player2score = 0;
this.boardSquares = new ArrayList<>();
boardSquares.add(new FinishSquare());
boardSquares.add(new Square1());
boardSquares.add(new Square2());
boardSquares.add(new Square3());
boardSquares.add(new Square4());
boardSquares.add(new Square5());
boardSquares.add(new Square6());
boardSquares.add(new Square7());
}
public int getPlayer1position() {
return player1position;
}
public void incrementPlayer1position(int increment) {
this.player1position = (player1position + increment) %
boardSquares.size();
}
public int getPlayer2position() {
return player2position;
}
public void incrementPlayer2position(int increment) {
this.player2position = (player2position + increment) %
boardSquares.size();
}
public int getPlayer1score() {
return player1score;
}
public void setPlayer1score(int player1score) {
this.player1score = player1score;
}
public int getPlayer2score() {
return player2score;
}
public void setPlayer2score(int player2score) {
this.player2score = player2score;
}
public List<BoardSquare> getBoardSquares() {
return boardSquares;
}
}
class FinishSquare extends BoardSquare {
public FinishSquare() {
super("Finish!", 0, new Color(0, 203, 101), Color.WHITE);
}
@Override
public int execute(int score) {
return score;
}
}
class Square1 extends BoardSquare {
public Square1() {
super("+20", 1, new Color(152, 254, 152), Color.BLACK);
}
@Override
public int execute(int score) {
return score + 20;
}
}
class Square2 extends BoardSquare {
public Square2() {
super("Try Again", 2, new Color(252, 252, 201), Color.BLACK);
}
@Override
public int execute(int score) {
return score;
}
}
class Square3 extends BoardSquare {
public Square3() {
super("-50", 3, new Color(254, 152, 152), Color.BLACK);
}
@Override
public int execute(int score) {
return score - 50;
}
}
class Square4 extends BoardSquare {
public Square4() {
super("Lost All", 4, new Color(252, 48, 99), Color.WHITE);
}
@Override
public int execute(int score) {
return 0;
}
}
class Square5 extends BoardSquare {
public Square5() {
super("+30", 5, new Color(49, 253, 0), Color.BLACK);
}
@Override
public int execute(int score) {
return score + 30;
}
}
class Square6 extends BoardSquare {
public Square6() {
super("+10", 6, new Color(203, 254, 203), Color.BLACK);
}
@Override
public int execute(int score) {
return score + 10;
}
}
class Square7 extends BoardSquare {
public Square7() {
super("-10", 7, new Color(254, 203, 203), Color.BLACK);
}
@Override
public int execute(int score) {
return score - 10;
}
}
abstract class BoardSquare {
private final int position;
private final Color backgroundColor;
private final Color foregroundColor;
private final String text;
public BoardSquare(String text, int position,
Color backgroundColor, Color foregroundColor) {
this.text = text;
this.position = position;
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
}
public abstract int execute(int score);
public int getPosition() {
return position;
}
public String getText() {
return text;
}
public Color getBackgroundColor() {
return backgroundColor;
}
public Color getForegroundColor() {
return foregroundColor;
}
}
有人能在这里给我指个正确的方向吗?我的游戏工作完美,但我想添加一些实际的互动/目标。谢谢
该程序应模拟滚动两个骰子,并计算和。添加一个循环,使程序可以玩10,000个游戏。添加计数器,计算玩家赢了多少次,输了多少次。在10,000场比赛结束时,计算获胜的概率[即赢/(赢+输)]并输出该值。从长远来看,谁会赢得最多的比赛,你还是房子?注意:要生成一个随机数x,其中0x≤<1,使用x=math.random();例如,乘以6并转换为一个整数,结果是一个介于0和5之间的整数。
我是一个C++初学者,我需要创建一个骰子游戏模拟掷两个骰子。我对头文件的使用感到很困惑。但首先,为什么我需要返回骰子的票面号码?其次,int roll函数做什么?来重置价值观和面孔?如果是,默认值是多少?而最后一个函数骰子(int n),我是否使用这个函数来控制骰子值的最大总和?函数必须有一个具有以下函数的类头文件:
提示:“写一个程序对着电脑玩猪游戏。在每个回合,当前玩家将掷出一对骰子并累积点数。目标是在你的对手之前达到100分或更多。(为了测试的目的,使用30点而不是100点)如果在任何回合中,玩家掷出一个1,则该回合积累的所有点数都将被没收,骰子的控制权将转移到另一个玩家身上。如果玩家在一个回合中滚动两个1,则该玩家失去迄今为止积累的所有点数,控制权转移到另一个玩家身上。玩家可以在每次掷骰后自愿交出骰子的
1-4:玩家从1-6随机向前移动 5:玩家从4-11向前移动一个随机量(随机8+4) 6:玩家移动到另一个玩家所在的位置(见下文) 我是新的代码编写(这是我的第四个程序编写),并没有广泛的知识知道什么是错误的。代码不需要熟练地完成,我只想让它正常工作。我们非常感谢您的帮助。 }
null