Java实验一:博饼

文章目录



一、实验内容

模拟博饼游戏:

  1. 提醒用户输入玩家数(6-10)。
  2. 循环为每个玩家生成六个筛子点数(1-6),根据上图的规则判断所产生的骰子对应的奖项,并输出。
  3. 游戏结束时(所有奖项已经出完),输出每个玩家所获得的奖项以及每个奖项的个数。

二、设计思路

(1)设计骰子类,实现随机掷骰子的功能;

(2)构建玩家类,记录玩家获得的每种奖励的个数;

(3)构建博饼游戏类,判断每次掷骰子时应获得的奖励,判断是否已发放所有的奖励;

三、实验内容




1.博饼类

记录游戏规则:

package game;

public class BoCake
{
	public static final int A = 1;
	public static final int B = 2;
	public static final int C = 4;
	public static final int D = 8;
	public static final int E= 16;
	public static final int F = 32;
	//牺牲一个存储空间,增加程序可读性
	public int[] reward1=new int[7]; //记录状元
	public int reward2=0;
	public int reward3=0;
	public int reward4=0;
	public int reward5=0;
	public int reward6=0;
	
	public BoCake()
	{
		for(int i=1;i<7;i++)
			reward1[i]=0;
		reward2=0;
		reward3=0;
		reward4=0;
		reward5=0;
		reward6=0;
	}
	
	
//判断奖励
	public void judgeReward(Dice dice,Player player)
	{
		int x=0; //记录点数为4的骰子数目
		int a=0,b=0,c=0,d=0,e=0,f=0; 
		for(int i=1;i<7;i++) 
		{
			if(dice.die[i]==4) x++;
			else if(a==0) a=dice.die[i];
			else if(b==0) b=dice.die[i];
			else if(c==0) c=dice.die[i];
			else if(d==0) d=dice.die[i];
			else if(e==0) e=dice.die[i];
			else if(f==0) f=dice.die[i];
		}
		
		//判断奖励 状元等级
		//六博红
		if(x==6) 
		{
			reward1[2]=1;  System.out.println("恭喜有机会获得状元!");
		}
		//六博黑
		else if(x==0 && a==b && b==c && c==d && d==e && e==f) 
		{
			reward1[3]=1;
			System.out.println("恭喜有机会获得状元!");
		}
		//五王
		else if(x==5) 
		{
			if(a==6) reward1[4]=6;
			else if(a==5) reward1[4]=5;
			else if(a==3) reward1[4]=3;
			else if(a==2) reward1[4]=2;
			else if(a==1) reward1[4]=1;
			System.out.println("恭喜有机会获得状元!\n");
		}
		//五子登科
		else if(a==b && b==c && c==d && d==e)
		{
			if(x==1 && reward6<F) {reward6++; player.result[6]++;}
			if(a==6) reward1[5]=6;
			else if(a==5) reward1[5]=5;
			else if(a==3) reward1[5]=3;
			else if(a==2) reward1[5]=2;
			else if(a==1) reward1[5]=1;
			System.out.println("恭喜有机会获得状元!\n");
		}
		//状元
		else if(x==4)
		{
			if(a==1 && b==1) 
			{
				reward1[1]=1;
				System.out.println("恭喜获得状元!");
			}
			else reward1[6]=1;
			System.out.println("恭喜有机会获得状元!");
		}
		
		//榜眼
		else if(x==1 && a!=b && b!=c && c!=d && d!=e && a!=c && a!=d && a!=e && b!=d && b!=e && c!=e)
		{
			if(reward2<B) {reward2++; player.result[2]++; System.out.println("恭喜获得对堂!");}
			else  { System.out.println("很遗憾,对堂名额已抢光。");}
		}
			
		//探花
		else if(x==3) 
		{
			if(reward3<C) {reward3++; player.result[3]++; System.out.println("恭喜获得三红!");}
			else  { System.out.println("很遗憾,三红名额已抢光。");}
		}
		//进士
		else if(a==b && b==c && c==d )
		{
			if(x==2) 
			{
				if(reward5<E) {reward5++; player.result[5]++; System.out.println("恭喜获得二举!");}
				else  { System.out.println("很遗憾,二举名额已抢光。");}
			}
			else if(x==1)
			{
				if(reward6<F) {reward6++; player.result[6]++; System.out.println("恭喜获得一秀!");}
				else  { System.out.println("很遗憾,一秀名额已抢光。");}
			}
			if(reward4<D) {reward4++; player.result[4]++; System.out.println("恭喜获得四进!");}
			else  { System.out.println("很遗憾,四进名额已抢光。");}
		}
		//举人
		else if(x==2) 
		{
			if(reward5<E) {reward5++; player.result[5]++; System.out.println("恭喜获得二举!");}
			else  { System.out.println("很遗憾,二举名额已抢光。");}
		}
		//秀才
		else if(x==1) 
		{
			if(reward6<F) {reward6++; player.result[6]++;; System.out.println("恭喜获得一秀!");}
			else  { System.out.println("很遗憾,一秀名额已抢光。");}
		}
	}

//判断游戏结束
	public boolean judgeOver()
	{
		if(!(reward2==2 && reward3==4 && reward4==8 && reward5==16 && reward6==32) )
			return false;
	   for(int i=1;i<7;i++)
		   if(reward1[i]!=0) return true;
	   return false;	
	}


}




2.骰子类

随机骰子点数:

package game;

public class Dice {
	public int[] die=new int[7];  //骰子点数
	public Dice()
	{
		for(int i = 1;i<7;i++)
			 die[i] =0;	
	}
	
	public void printDice()
	{
		System.out.println("骰子的点数:");
		for(int i = 1;i<7;i++)
			System.out.printf("%d  ",die[i]);
		System.out.print("\n");
	}
	
//随机扔骰子
	public void rollDice()
	{
		for(int i = 1;i<7;i++)
		 die[i] = 1 + ( int )(Math.random() * 6);		//m+(int)(Math.Random()*(n-m+1))
	}	

}

3.玩家类

package game;

public class Player 
{
	public static final int A = 1;
	public static final int B = 2;
	public static final int C = 4;
	public static final int D = 8;
	public static final int E= 16;
	public static final int F = 32;
	public int id; //玩家号码
	public int[] result=new int[7]; //记录玩家结果
	
	public Player()
	{
		id=0;
		for(int i=1;i<7;i++)
		result[i]=0;	
	}
	
//判断玩家所获奖励
	public void winReward(Dice dice)
	{
		int x=0; //记录点数为4的骰子数目
		int a=0,b=0,c=0,d=0,e=0,f=0; 
		for(int i=1;i<7;i++) 
		{
			if(dice.die[i]==4) x++;
			else if(a==0) a=dice.die[i];
			else if(b==0) b=dice.die[i];
			else if(c==0) c=dice.die[i];
			else if(d==0) d=dice.die[i];
			else if(e==0) e=dice.die[i];
			else if(f==0) f=dice.die[i];
		}
		
		//判断奖励 状元等级
		if(x==4 && a==1 && b==1 && (result[1]>1||result[1]==0)) result[1]=1;
		//六博红
		if(x==6 && (result[1]>2||result[1]==0)) result[1]=2;  
		//六博黑
		else if(x==0 && a==b && b==c && c==d && d==e && e==f && (result[1]>3||result[1]==0)) 
			result[1]=3;
		//五王
		else if(x==5) 
		{
			if(a==6 && (result[1]>4||result[1]==0)) result[1]=4;
			else if(a==5 && (result[1]>5||result[1]==0)) result[1]=5;
			else if(a==3 && (result[1]>6||result[1]==0)) result[1]=6;
			else if(a==2 && (result[1]>7||result[1]==0)) result[1]=7;
			else if(a==1 && (result[1]>8||result[1]==0)) result[1]=8;
		}
		//五子登科
		else if(a==b && b==c && c==d && d==e)
		{
			//if(x==1 && result[6]<F) result[6]++;
			if(a==6 && (result[1]>9||result[1]==0)) result[1]=9;
			else if(a==5 && (result[1]>10||result[1]==0)) result[1]=10;
			else if(a==3 && (result[1]>11||result[1]==0)) result[1]=11;
			else if(a==2 && (result[1]>12||result[1]==0)) result[1]=12;
			else if(a==1 && (result[1]>13||result[1]==0)) result[1]=13;
		}
		//状元
		else if(x==4 && result[1]==0)
		{
			
			result[1]=14;
		}

	}
}

 

 4. 玩游戏

package game;

import java.util.Scanner;

public class PlayGame {
	//主函数
		public static void main(String[] args) 
		{
			//指定用户数量
			System.out.println("请用户数量(6-10):");
			Scanner scanner= new Scanner(System.in);
			int n=scanner.nextInt();
			System.out.printf("共有用户:%d\n",n);
			n=n+1;
			
			//扔骰子
			Dice dice=new Dice() ;
			BoCake cake = new BoCake();
			Player[] player=new Player[n];
			for(int i=1;i<n;i++)
			{
				player[i]=new Player();
				player[i].id=i;
			}
			do
			{
				for(int i=1;i<n;i++)
				{
					System.out.printf("第%d号玩家:",i);
					dice.rollDice();
					player[i].winReward(dice);
					cake.judgeReward(dice,player[i]);
					System.out.println("\n");
					if(cake.judgeOver()) break;
				}
				
			}while(!cake.judgeOver());
			
			//先找出状元属于哪一种
			int win=0;
			if(cake.reward1[1]!=0) win=1;
			else if(cake.reward1[2]!=0) win=2;
			else if(cake.reward1[3]!=0) win=3;
			else if(cake.reward1[4]!=0)
			{
				if(cake.reward1[4]==6) win=4;
				else if(cake.reward1[4]==5) win=5;
				else if(cake.reward1[4]==3) win=6;
				else if(cake.reward1[4]==2) win=7;
				else if(cake.reward1[4]==1) win=8;
			}
			else if(cake.reward1[5]!=0)
			{
				if(cake.reward1[5]==6) win=9;
				else if(cake.reward1[5]==5) win=10;
				else if(cake.reward1[5]==3) win=11;
				else if(cake.reward1[5]==2) win=12;
				else if(cake.reward1[5]==1) win=13;
			}
			else if(cake.reward1[6]!=0) win=14;
			//再找出状元是哪一个人
			for(int i=1;i<n;i++)
				if(player[i].result[1]==win) 
				{
					player[i].result[1]=1;
					System.out.printf("恭喜第%d号玩家获得状元!\n",i);
				}
			
			//输出最终结果
			System.out.println("最终结果:");
			for(int i=1;i<n;i++)
			{
				System.out.printf("第%d号玩家所获奖励:",i);
				System.out.printf("状元%d个;",player[i].result[1]==1?1:0);
				System.out.printf("对堂%d个;",player[i].result[2]);
				System.out.printf("三红%d个;",player[i].result[3]);
				System.out.printf("四进%d个;",player[i].result[4]);
				System.out.printf("二举%d个;",player[i].result[5]);
				System.out.printf("一秀%d个\n",player[i].result[6]);
			
				
			}
			scanner.close();
			
		}
}

四、运行结果

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值