曹冲称象的典故大家都知道,下面是模拟程序:
import javax.swing.*;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.SystemColor;
import java.math.BigDecimal;
public class ScaleElephant {
private int shipWeight = 0;
private int elephantWeight = 0;
private int playerCount = 0;
private int bestCount = 0;
private int resElephantWeight = 0;
private int[] bestChoice = new int[10000];
private boolean hasGotShipWeight = false;
private boolean hasGotEleWeight = false;
private boolean hasShowBestChoice = false;
private boolean hasDoneBestChoice = false;
private JTextField showShipWeight;
private JTextField showElephantWeight;
private JTextArea showPlayerChoice = new JTextArea();
public void initPageShipElephant() {
JFrame topJFrame = new JFrame("曹冲称象游戏");
topJFrame.setFont(new Font("宋体", Font.BOLD, 16));
topJFrame.setBounds(400, 200, 1000, 800);
topJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
ImageIcon img = new ImageIcon("background.jpg");
g.drawImage(img.getImage(), 0, 0, topJFrame.getWidth(), topJFrame.getHeight(), topJFrame);
}
};
contentPanel.setLayout(null);
topJFrame.getContentPane().add(contentPanel);
JLabel selectShip = new JLabel("\u2460\u9009\u8239");
selectShip.setFont(new Font("宋体", Font.BOLD, 25));
selectShip.setBounds(80, 101, 123, 24);
selectShip.setVisible(true);
contentPanel.add(selectShip);
JLabel selectElephant = new JLabel("\u2461\u9009\u8C61");
selectElephant.setFont(new Font("宋体", Font.BOLD, 25));
selectElephant.setBounds(80, 263, 123, 30);
contentPanel.add(selectElephant);
JLabel selectStone = new JLabel("\u2462\u9009\u77F3\u5934");
selectStone.setFont(new Font("宋体", Font.BOLD, 25));
selectStone.setBounds(80, 406, 134, 30);
contentPanel.add(selectStone);
ImageIcon isShip = new ImageIcon("ship.jpg");
isShip.setImage(isShip.getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT));
JButton sShip = new JButton(isShip);
sShip.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectShips(1023);
}
});
sShip.setBounds(217, 65, 60, 60);
contentPanel.add(sShip);
ImageIcon imShip = new ImageIcon("ship.jpg");
imShip.setImage(imShip.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
JButton mShip = new JButton(imShip);
mShip.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectShips(4095);
}
});
mShip.setBounds(349, 45, 80, 80);
contentPanel.add(mShip);
ImageIcon ibShip = new ImageIcon("ship.jpg");
ibShip.setImage(ibShip.getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT));
JButton bShip = new JButton(ibShip);
bShip.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectShips(8191);
}
});
bShip.setBounds(528, 25, 100, 100);
contentPanel.add(bShip);
ImageIcon ibbShip = new ImageIcon("ship.jpg");
ibbShip.setImage(ibbShip.getImage().getScaledInstance(120, 120, Image.SCALE_DEFAULT));
JButton bbShip = new JButton(ibbShip);
bbShip.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectShips(16393);
}
});
bbShip.setBounds(740, 5, 120, 120);
contentPanel.add(bbShip);
ImageIcon isElephant = new ImageIcon("elephant.jpg");
isElephant.setImage(isElephant.getImage().getScaledInstance(60, 60, Image.SCALE_DEFAULT));
JButton sElephant = new JButton(isElephant);
sElephant.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectElephants(1024, 1);
}
});
sElephant.setBounds(217, 233, 60, 60);
contentPanel.add(sElephant);
ImageIcon imElephant = new ImageIcon("elephant.jpg");
imElephant.setImage(imElephant.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
JButton mElephant = new JButton(imElephant);
mElephant.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectElephants(1024, 3072);
}
});
mElephant.setBounds(349, 216, 80, 80);
contentPanel.add(mElephant);
ImageIcon ibElephant = new ImageIcon("elephant.jpg");
ibElephant.setImage(ibElephant.getImage().getScaledInstance(100, 100, Image.SCALE_DEFAULT));
JButton bElephant = new JButton(ibElephant);
bElephant.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectElephants(4096, 4097);
}
});
bElephant.setBounds(528, 196, 100, 100);
contentPanel.add(bElephant);
ImageIcon ibbElephant = new ImageIcon("elephant.jpg");
ibbElephant.setImage(ibbElephant.getImage().getScaledInstance(120, 120, Image.SCALE_DEFAULT));
JButton bbElephant = new JButton(ibbElephant);
bbElephant.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectElephants(8193, 8191);
}
});
bbElephant.setBounds(740, 176, 120, 120);
contentPanel.add(bbElephant);
JPanel selectStones = new JPanel();
selectStones.setBounds(209, 370, 759, 100);
contentPanel.add(selectStones);
JButton value_1 = new JButton("1");
value_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_1.getText());
}
});
value_1.setFont(new Font("宋体", Font.PLAIN, 20));
value_1.setBounds(28, 13, 76, 27);
JButton value_2 = new JButton("2");
value_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_2.getText());
}
});
value_2.setFont(new Font("宋体", Font.PLAIN, 20));
value_2.setBounds(127, 13, 66, 27);
JButton value_4 = new JButton("4");
value_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_4.getText());
}
});
value_4.setFont(new Font("宋体", Font.PLAIN, 20));
value_4.setBounds(227, 13, 65, 27);
JButton value_8 = new JButton("8");
value_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_8.getText());
}
});
value_8.setFont(new Font("宋体", Font.PLAIN, 20));
value_8.setBounds(330, 13, 76, 27);
JButton value_16 = new JButton("16");
value_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_16.getText());
}
});
value_16.setFont(new Font("宋体", Font.PLAIN, 20));
value_16.setBounds(445, 13, 76, 27);
JButton value_32 = new JButton("32");
value_32.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_32.getText());
}
});
value_32.setFont(new Font("宋体", Font.PLAIN, 20));
value_32.setBounds(558, 13, 76, 27);
JButton value_64 = new JButton("64");
value_64.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_64.getText());
}
});
value_64.setFont(new Font("宋体", Font.PLAIN, 20));
value_64.setBounds(669, 13, 76, 27);
JButton value_128 = new JButton("128");
value_128.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_128.getText());
}
});
value_128.setFont(new Font("宋体", Font.PLAIN, 20));
value_128.setBounds(28, 58, 76, 27);
JButton value_256 = new JButton("256");
value_256.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_256.getText());
}
});
value_256.setFont(new Font("宋体", Font.PLAIN, 20));
value_256.setBounds(128, 58, 65, 27);
JButton value_512 = new JButton("512");
value_512.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_512.getText());
}
});
value_512.setFont(new Font("宋体", Font.PLAIN, 20));
value_512.setBounds(227, 58, 65, 27);
JButton value_1024 = new JButton("1024");
value_1024.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_1024.getText());
}
});
value_1024.setFont(new Font("宋体", Font.PLAIN, 20));
value_1024.setBounds(330, 58, 76, 27);
JButton value_2048 = new JButton("2048");
value_2048.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_2048.getText());
}
});
value_2048.setFont(new Font("宋体", Font.PLAIN, 20));
value_2048.setBounds(445, 58, 76, 27);
JButton value_4096 = new JButton("4096");
value_4096.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_4096.getText());
}
});
value_4096.setFont(new Font("宋体", Font.PLAIN, 20));
value_4096.setBounds(558, 58, 76, 27);
JButton value_8192 = new JButton("8192");
value_8192.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handlePlayChoice(value_8192.getText());
}
});
value_8192.setFont(new Font("宋体", Font.PLAIN, 20));
value_8192.setBounds(669, 58, 76, 27);
selectStones.setLayout(null);
selectStones.add(value_1);
selectStones.add(value_4);
selectStones.add(value_2);
selectStones.add(value_8);
selectStones.add(value_16);
selectStones.add(value_32);
selectStones.add(value_64);
selectStones.add(value_128);
selectStones.add(value_256);
selectStones.add(value_512);
selectStones.add(value_1024);
selectStones.add(value_2048);
selectStones.add(value_4096);
selectStones.add(value_8192);
JLabel tips = new JLabel("当前消息");
tips.setFont(new Font("宋体", Font.BOLD, 26));
tips.setBounds(84, 561, 111, 80);
contentPanel.add(tips);
JPanel namePanel = new JPanel();
namePanel.setBackground(SystemColor.menu);
namePanel.setBounds(0, 0, 70, 727);
contentPanel.add(namePanel);
namePanel.setLayout(null);
JLabel gameName = new JLabel("<html>曹<br>冲<br>称<br>象<br>游<br>戏<br><html>");
gameName.setBounds(0, 82, 70, 508);
namePanel.add(gameName);
gameName.setBackground(Color.WHITE);
gameName.setFont(new Font("楷体", Font.BOLD | Font.ITALIC, 50));
JPanel messagePanel = new JPanel();
messagePanel.setBackground(SystemColor.text);
messagePanel.setBounds(209, 516, 759, 198);
contentPanel.add(messagePanel);
messagePanel.setLayout(null);
JLabel shipTip = new JLabel("船的承载量:");
shipTip.setFont(new Font("宋体", Font.BOLD, 18));
shipTip.setBounds(14, 13, 114, 29);
messagePanel.add(shipTip);
showShipWeight = new JTextField();
showShipWeight.setEditable(false);
showShipWeight.setFont(new Font("宋体", Font.BOLD, 20));
showShipWeight.setBounds(131, 12, 156, 29);
messagePanel.add(showShipWeight);
showShipWeight.setColumns(10);
JLabel elephantTip = new JLabel("象的重量:");
elephantTip.setFont(new Font("宋体", Font.BOLD, 18));
elephantTip.setBounds(336, 13, 101, 29);
messagePanel.add(elephantTip);
showElephantWeight = new JTextField();
showElephantWeight.setEditable(false);
showElephantWeight.setFont(new Font("宋体", Font.BOLD, 20));
showElephantWeight.setBounds(427, 13, 250, 29);
showElephantWeight.setColumns(10);
messagePanel.add(showElephantWeight);
showPlayerChoice.setFont(new Font("宋体", Font.BOLD, 23));
showPlayerChoice.setBackground(SystemColor.menu);
showPlayerChoice.setEditable(false);
showPlayerChoice.setBounds(88, 54, 657, 131);
showPlayerChoice.setLineWrap(true);
showPlayerChoice.setWrapStyleWord(true);
messagePanel.add(showPlayerChoice);
JLabel stonesTip = new JLabel("<html>\u73A9\u5BB6\u9009<br>\u7684\u77F3\u5934\uFF1A<html>");
stonesTip.setBackground(SystemColor.menu);
stonesTip.setBounds(14, 35, 76, 85);
messagePanel.add(stonesTip);
stonesTip.setFont(new Font("宋体", Font.BOLD, 18));
JLabel namedShip = new JLabel("小船 中船 大船 巨船");
namedShip.setFont(new Font("宋体", Font.BOLD, 18));
namedShip.setBounds(217, 124, 676, 33);
contentPanel.add(namedShip);
JLabel namedElephant = new JLabel("小象 中象 大象 巨象");
namedElephant.setFont(new Font("宋体", Font.BOLD, 18));
namedElephant.setBounds(217, 295, 676, 33);
contentPanel.add(namedElephant);
JLabel tipName = new JLabel("提示");
tipName.setForeground(new Color(255, 0, 0));
tipName.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 18));
tipName.setBounds(90, 13, 45, 24);
contentPanel.add(tipName);
JButton tipsButton = new JButton(new ImageIcon("tips.jpg"));
tipsButton.setBounds(139, 5, 45, 45);
contentPanel.add(tipsButton);
tipsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BestChoice();
}
});
ImageIcon iTips = new ImageIcon("tips.jpg");
iTips.setImage(iTips.getImage().getScaledInstance(45, 45, Image.SCALE_DEFAULT));
gameName.setVisible(true);
JMenuBar menuBar = new JMenuBar();
menuBar.setBackground(Color.WHITE);
topJFrame.setJMenuBar(menuBar);
JMenuItem restart = new JMenuItem(" 重新开始");
restart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
topJFrame.dispose();
ScaleElephant sElephant = new ScaleElephant();
sElephant.initPageShipElephant();
}
});
restart.setFont(new Font("宋体", Font.PLAIN, 18));
JMenuItem quit = new JMenuItem("| 退出游戏");
quit.setFont(new Font("宋体", Font.PLAIN, 18));
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenuItem about = new JMenuItem("| 游戏说明");
about.setFont(new Font("宋体", Font.PLAIN, 18));
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFrame aboutJFrame = new JFrame("说明");
aboutJFrame.setBounds(700, 300, 450, 450);
aboutJFrame.getContentPane().setLayout(null);
JTextArea aboutArea = new JTextArea();
aboutArea.setFont(new Font("宋体", Font.BOLD, 26));
aboutArea.setEditable(false);
aboutArea.setBounds(14, 13, 400, 370);
aboutJFrame.getContentPane().add(aboutArea);
aboutArea.append("小船:1023\n中船:4095\n大船:8191\n巨船:16393\n");
aboutArea.append("小象:1 ~ 1024\n中象:1025 ~ 4096\n大象:4096 ~ 8192\n巨象:8193 ~ 16393\n");
aboutArea.append("所得分数=点击次数/最优次数\n\n");
aboutArea.append("由gnosed制作!");
aboutJFrame.setVisible(true);
}
});
menuBar.add(restart);
menuBar.add(quit);
menuBar.add(about);
topJFrame.setVisible(true);
}
public void selectShips(int key) {
if (!hasGotShipWeight) {
shipWeight = key;
showShipWeight.setText(Integer.toString(shipWeight));
hasGotShipWeight = true;
} else {
showMessage(" 已经选好船!");
}
}
public void selectElephants(int dist, int begin) {
if (!hasGotEleWeight) {
int temp = (int) (Math.random() * dist + begin);
if (temp > shipWeight)
showMessage(" 象太大了,船会沉,请重新选象!");
else {
elephantWeight = temp;
// elephantWeight = 8848;
resElephantWeight = elephantWeight;
showElephantWeight.setText("未知,请放石头称量!");
// showElephantWeight.setText(Integer.toString(elephantWeight));
hasGotEleWeight = true;
showMessage(" 可以选石头了!");
}
} else {
showMessage(" 已经选好象!");
}
}
public void showMessage(String s) {
JFrame smJFrame = new JFrame();
smJFrame.setBounds(700, 500, 330, 100);
smJFrame.setVisible(true);
JLabel Message = new JLabel(s);
Message.setFont(new Font("宋体", Font.BOLD, 20));
smJFrame.getContentPane().add(Message);
}
public void showPlayerChoiceMessage() {
JFrame pcJFrame = new JFrame("称象结束");
pcJFrame.setBounds(700, 300, 580, 400);
pcJFrame.getContentPane().setLayout(null);
JTextArea pcArea = new JTextArea();
pcArea.setFont(new Font("宋体", Font.BOLD, 26));
pcArea.setEditable(false);
pcArea.setBounds(14, 13, 535, 100);
pcArea.setLineWrap(true);
pcArea.setWrapStyleWord(true);
pcJFrame.getContentPane().add(pcArea);
pcJFrame.setVisible(true);
if (bestCount == 0) {
getBestChoice();
}
double ans = new BigDecimal((float) bestCount / playerCount).setScale(2, BigDecimal.ROUND_HALF_UP)
.doubleValue();
ans *= 100;
System.out.println("最优次数:" + bestCount);
pcArea.setText("点击次数:" + playerCount + "\n\n");
pcArea.append("所得分数:" + ans + "% ");
showBestChoiceMessage(pcJFrame, 120);
}
public void showBestChoiceMessage(JFrame jFrame, int args_y) {
JTextArea jArea = new JTextArea();
jArea.setFont(new Font("宋体", Font.BOLD, 26));
jArea.setEditable(false);
jArea.setBounds(14, args_y, 535, 208);
jArea.setLineWrap(true);
jArea.setWrapStyleWord(true);
jFrame.getContentPane().add(jArea);
jFrame.setVisible(true);
jArea.setText("\n最优次数:" + bestCount + " 象的重量:" + elephantWeight + "\n\n");
jArea.append(elephantWeight + " = ");
if (!hasDoneBestChoice) {
getBestChoice();
}
for (int i = 0; i < bestCount; i++) {
if (i == 0) {
jArea.append("" + bestChoice[i]);
} else {
jArea.append(" + " + bestChoice[i]);
}
}
}
public void getBestChoice() {
if (!hasShowBestChoice && !hasDoneBestChoice) {
int res = elephantWeight, key = 8192;
while (res > 0) {
System.out.println("返回:" + res);
while (res < key && key > 1) {
key /= 2;
}
res -= key;
if (!hasDoneBestChoice) {
bestChoice[bestCount++] = key;
}
}
hasDoneBestChoice = true;
}
}
public void BestChoice() {
if (shipWeight == 0) {
showMessage("没有选择船只!");
} else if (elephantWeight == 0) {
showMessage("没有选择大象!");
} else {
getBestChoice();
JFrame bcJFrame = new JFrame("最优选石头方案");
bcJFrame.setBounds(700, 300, 580, 281);
bcJFrame.getContentPane().setLayout(null);
showBestChoiceMessage(bcJFrame, 13);
hasShowBestChoice = true;
}
}
public void handlePlayChoice(String value) {
if (shipWeight == 0) {
showMessage("没有选择船只!");
} else if (elephantWeight == 0) {
showMessage("没有选择大象!");
} else {
playerCount++;
int stoneWeight = Integer.parseInt(value);
if (resElephantWeight >= stoneWeight) {
if (resElephantWeight != elephantWeight) {
showPlayerChoice.append(" + ");
} else {
showPlayerChoice.append("\n");
}
resElephantWeight -= stoneWeight;
System.out.println("大象剩余:" + resElephantWeight);
showPlayerChoice.append(value);
if (resElephantWeight == 0) {
showElephantWeight.setText(Integer.toString(elephantWeight));
showPlayerChoiceMessage();
}
} else {
showMessage("船要沉了!选小一点的!");
}
}
}
public static void main(String[] args) {
ScaleElephant sElephant = new ScaleElephant();
sElephant.initPageShipElephant();
}
}
界面截图:
想玩吗?试试IQ?