📄 greedsystem.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/*GreedSystem为游戏的主框架类,包括用户图形界面的生成和对其它各类的“调用”
在所有的类中处于总指挥的地位
*/
public class GreedSystem{
public static JFrame gameFrame; // 游戏主框架
public static Container contentPane;
public static JPanel intermediate;
public static JPanel playerPanel; // 用来显示游戏者姓名
public static JPanel scorePanel; // 用来显示游戏者总积分
public static JPanel jps[];
public static JPanel itemPanel; // 用来显示每个游戏者的相关信息的项目
public static JPanel infoPanel; // 用来显示相关信息的内容
public static JPanel jpi[];
//public static JPanel choicePanel; // 原本为添加“重新游戏”和“结束游戏”两个按钮,现取消
public static JPanel subjectPanel;
//public static JButton restartButton; // “重新游戏”按钮,现取消
//public static JButton exitButton; // “结束游戏”按钮,现取消
public static int playerNumber; // 记录游戏者人数的静态变量
public static String playerName[]; // 记录游戏者姓名的字符串变量组
/*游戏主框架生成方法,其中要调用Enrollment.java中的getPlayerNumber()
和getplayerName()两个方法
*/
public void createFrame(){
gameFrame=new JFrame("GreedGame"); // 创建游戏主框架
contentPane=gameFrame.getContentPane(); // 获取框架的ContentPane
gameFrame.setSize(400,250); // 设置主框架大小
gameFrame.setLocation(310,63); // 设置主框架在屏幕上显示的位置
gameFrame.setVisible(true);
subjectPanel=new JPanel();
contentPane.add(subjectPanel,"North");
subjectPanel.setLayout(new GridLayout(1,4));
subjectPanel.add(new JLabel("游戏者",JLabel.CENTER));
subjectPanel.add(new JLabel("总积分",JLabel.CENTER));
subjectPanel.add(new JLabel("项目",JLabel.CENTER));
subjectPanel.add(new JLabel("状态",JLabel.CENTER));
intermediate=new JPanel(); // 设置放置以下四个Panel的过渡型Panel
intermediate.setLayout(new GridLayout(1,4));
contentPane.add(intermediate,"Center");
playerPanel=new JPanel(); // 游戏者Panel,显示所有的游戏者姓名
playerPanel.setBackground(Color.CYAN);
playerPanel.setLayout(new GridLayout(5,1));
intermediate.add(playerPanel);
scorePanel=new JPanel(); // 积分Panel,显示所有游戏者总积分
scorePanel.setBackground(Color.WHITE);
scorePanel.setLayout(new GridLayout(5,1));
jps=new JPanel[5];
for(int i=0;i<5;i++){
jps[i]=new JPanel();
jps[i].setBackground(Color.WHITE);
scorePanel.add(jps[i]);
}
intermediate.add(scorePanel);
itemPanel=new JPanel(); // 信息显示Panel,显示如下信息条
itemPanel.setBackground(Color.CYAN);
itemPanel.setLayout(new GridLayout(4,1));
JLabel j1=new JLabel("现在游戏者",JLabel.CENTER);
JLabel j2=new JLabel("是否入局",JLabel.CENTER);
JLabel j3=new JLabel("获得骰子点数",JLabel.CENTER);
itemPanel.add(j1);
itemPanel.add(j2);
itemPanel.add(j3);
intermediate.add(itemPanel);
infoPanel=new JPanel(); // 信息Panel,显示当前游戏者的信息,信息类型为“信息显示Panel”所给出的
infoPanel.setLayout(new GridLayout(4,1));
jpi=new JPanel[4];
for(int i=0;i<4;i++){
jpi[i]=new JPanel();
jpi[i].setBackground(Color.WHITE);
infoPanel.add(jpi[i]);
}
infoPanel.setBackground(Color.WHITE);
intermediate.add(infoPanel);
gameFrame.validate(); // 刷新主框架,使其显示以上设置的所有内容
JOptionPane.showMessageDialog(null," 现在开始游戏","开始",JOptionPane.INFORMATION_MESSAGE);
playerNumber=Enrollment.getPlayerNumber();// 调用方法,获取游戏者人数
playerName=new String[playerNumber];
Enrollment.getPlayerName(playerNumber,playerName,gameFrame); // 调用方法,获取游戏者姓名
for(int i=0;i<playerNumber;i++){
playerPanel.add(new JLabel(playerName[i],JLabel.CENTER));
}
//gameFrame.pack();
gameFrame.validate();
}
public static int count = 0;
public static JLabel getNumber;
/*“游戏开始方法”,调用此方法,游戏正式开始*/
public void startGame(){
boolean flag[] = new boolean[playerNumber]; // “flag”变量用来标识游戏者是否入局
int score[] = new int[playerNumber]; // “score”变量用来记录游戏者的总积分
boolean gameover = false; // “gameover”变量用来记录游戏是否结束
JLabel jls[]=new JLabel[playerNumber];
for(int i=0;i<playerNumber;i++){
jls[i] = new JLabel(score[count] + "分", JLabel.CENTER);
jps[i].add(jls[i]);
}
JLabel labelNowplayer=new JLabel(playerName[count],JLabel.CENTER);
jpi[0].add(labelNowplayer);
JLabel labelEntrance=new JLabel("否",JLabel.CENTER); // 初始化“是否入局”标签
jpi[1].add(labelEntrance);
getNumber=new JLabel("无",JLabel.CENTER); // 初始化“获得骰子点数”标签
jpi[2].add(getNumber);
gameFrame.validate();
while (gameover != true) { // gameover用来控制整个游戏是否结束,每当某个游戏者结束一局比赛,就返回一个gameover值,
getNumber.setVisible(false); // 判断游戏是否结束
jpi[2].remove(getNumber);
getNumber=new JLabel("无",JLabel.CENTER);
jpi[2].add(getNumber);
labelNowplayer.setVisible(false);
jpi[0].remove(labelNowplayer);
labelNowplayer=new JLabel(playerName[count],JLabel.CENTER);
jpi[0].add(labelNowplayer);
gameFrame.validate();
/*以上两段程序设置游戏者在一局游戏开始时信息显示的初始值*/
/*以下判断语句用于判断游戏者是否入局,并根据结果在信息栏中显示*/
if (flag[count] == false) {
jpi[1].remove(labelEntrance);
labelEntrance=new JLabel("否",JLabel.CENTER);
jpi[1].add(labelEntrance);
gameFrame.validate();
JOptionPane.showMessageDialog(null, " "+GreedSystem.playerName[count] + "开始游戏\n 尚未入局",
"游戏", JOptionPane.INFORMATION_MESSAGE);
}
else {
jpi[1].remove(labelEntrance);
labelEntrance=new JLabel("是",JLabel.CENTER);
jpi[1].add(labelEntrance);
gameFrame.validate();
JOptionPane.showMessageDialog(null, " "+GreedSystem.playerName[count] + "开始游戏\n 已经入局",
"游戏", JOptionPane.INFORMATION_MESSAGE);
}
Judgement.flag=flag[count];
Judgement.continuing=true;
Judgement.success=false;
Generate.remain=6;
Score.initial();
/*以下语句用于判断游戏者一次游戏后是否继续的判断,其中调用了其它类的方法*/
while(Judgement.continuing==true&&Generate.remain!=0){
Generate.numberGenerate();
Generate.scoreCalculation();
Judgement.continuing();
Score.accumulate();
}
if(Generate.remain==0){ // remain记录骰子的数量,初始值是6,如果游戏者连续游戏,其数量将减少
JOptionPane.showMessageDialog(null,"骰子用完,本轮游戏结束\n本轮共积分"+Score.getScore(),
"游戏",JOptionPane.INFORMATION_MESSAGE);
flag[count]=Judgement.flag;
}
else if(Judgement.success==true){ // success记录在一局游戏中,是游戏者放弃某次继续游戏的机会,还是由于未得分而失去继续游戏的机会
JOptionPane.showMessageDialog(null," 本轮游戏共积分"+Score.getScore(),
"游戏",JOptionPane.INFORMATION_MESSAGE);
flag[count]=Judgement.flag;
}
else{
Score.initial();
}
score[count]=score[count]+Score.getScore(); // 每位游戏者结束一轮游戏后,记录其最新积分
jps[count].remove(jls[count]);
jls[count]=new JLabel(score[count]+"分",JLabel.CENTER);
jps[count].add(jls[count]);
gameFrame.validate();
if(score[count]>=3000){ // 如果游戏者的总积分超过3000分,则游戏者获胜,游戏结束
JOptionPane.showMessageDialog(null,"恭喜"+playerName[count]+",您已获胜,本次游戏结束",
"游戏结束",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
System.out.println(score[count]);
count=(++count%playerNumber);
}
}
}
/*类Generate的主要功能是产生骰子点数以及计算得分*/
class Generate{
public static int number[];
public static int remain;
private static Random randomNumber=new Random();
private static int amount []=new int[6];
public static int score;
public static void numberGenerate(){ // 产生骰子点数的方法
number = new int[remain];
for (int i = 0; i < 6; i++) {
amount[i] = 0;
}
for (int i = 0; i < remain; i++) {
number[i] = randomNumber.nextInt(6) + 1;
amount[number[i] - 1]++;
}
String num=" ";
for(int i=0;i<remain;i++){
num=new String(num+number[i]+" ");
}
GreedSystem.getNumber.setVisible(false);
GreedSystem.jpi[2].remove(GreedSystem.getNumber);
GreedSystem.getNumber=new JLabel(num,JLabel.CENTER);
GreedSystem.jpi[2].add(GreedSystem.getNumber);
GreedSystem.gameFrame.validate();
}
public static void scoreCalculation(){ // 骰子点数对应分数的计算
if(amount[0]==6){
score=3000;
remain=0;
}
else if(amount[0]>=3){
score=1000;
remain=remain-3;
}
else if(amount[1]>=3){
score=200;
remain=remain-3;
}
else if(amount[2]>=3){
score=300;
remain=remain-3;
}
else if(amount[3]>=3){
score=400;
remain=remain-3;
}
else if(amount[4]>=3){
score=500;
remain=remain-3;
}
else if(amount[5]>=3){
score=600;
remain=remain-3;
}
else if(amount[0]>=1){
score=100;
remain=remain-1;
}
else if(amount[4]>=1){
score=50;
remain=remain-1;
}
else{
score=0;
}
}
}
/*类Judge主要功能是对于游戏者获得分数后的游戏控制以及游戏者选择的处理*/
class Judgement{
public static boolean success;
public static boolean flag;
public static boolean continuing;
public static void initial(boolean f){
flag=f;
}
/*entrance当游戏者尚未入局时被调用*/
public static void entrance(){
if(Generate.score<300){
JOptionPane.showMessageDialog(null,GreedSystem.playerName[GreedSystem.count]+"本次游戏得分"+Generate.score+",本轮游戏结束",
"游戏",JOptionPane.INFORMATION_MESSAGE);
continuing=false;
}
else{
int returnValue=JOptionPane.showConfirmDialog(null,GreedSystem.playerName[GreedSystem.count]+"本次游戏得分"+Generate.score+",是否继续游戏?","游戏",JOptionPane.YES_NO_OPTION);
if(returnValue ==JOptionPane.YES_OPTION) {
continuing=true;
flag=true;
}
else {
continuing = false;
success=true;
flag=true;
}
}
}
public static void continuing(){
if(flag==false){ // 如果游戏者尚未入局,则调用entrance方法
entrance();
}
else if(Generate.score!=0){ // 如果得分不为0,则可以继续游戏
int returnValue = JOptionPane.showConfirmDialog(null,
GreedSystem.playerName[GreedSystem.count]+"本次游戏得分" + Generate.score + "是否继续游戏?", "游戏",
JOptionPane.YES_NO_OPTION);
if (returnValue ==JOptionPane.YES_OPTION) { // 游戏者若选择“是”,则continuing标志为“true”
continuing=true;
}
else {
continuing=false; // 游戏者选择“否”,continuing标志为“否”,但“success”标志为“ture”,表示是游戏者主动放弃继续游戏
success=true;
}
}
else { // 如果游戏者得分为0,则结束本轮游戏
JOptionPane.showMessageDialog(null,GreedSystem.playerName[GreedSystem.count]+"本次游戏得分" + Generate.score + ",本轮游戏结束",
"游戏", JOptionPane.INFORMATION_MESSAGE);
continuing=false;
}
}
}
/*Score用来记录每个游戏者每局游戏的总积分*/
class Score{
private static int scoreAll;
public static void initial(){ // 每位游戏者开始一轮游戏时,初始化scoreAll
scoreAll=0;
}
public static void accumulate(){
scoreAll=scoreAll+Generate.score;
}
public static int getScore(){
return Score.scoreAll;
}
}
/*主方法入口*/
class MainGreedSystem{
public static void main(String args[]){
GreedSystem greed=new GreedSystem();
greed.createFrame();
greed.startGame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -