投色子 游戏

投色子游戏用Java怎么实现?

下面菜鸡波就来教大家用Java实现一个不带窗体的投色子判胜游戏。
首先要制订游戏规则。我们这里采用人机对抗的形式。首先,人方投三次骰子,个得三个点数,然后,计算机投三次骰子得三个点数,比较人机双方谁的点数大?判定哪方胜出。
对于人方,会出现三种不同的情况:1,三个点数相同    2,只有两个点数相同    3,个点数都不相同。     对于计算机,也会出现和人一样的结果。
下图是具体判胜图示:
package two048Page;

import java.util.Random;

public class JiQi {

	private  int  dianShu;
	Random   suiJiShu ;
	public   JiQi()
	{
		suiJiShu = new Random();
		dianShu  = suiJiShu.nextInt(6);
	}
	
	int   getDianShu()
	{
		return  dianShu;
	}
}
判胜代码:
package two048Page;

public class Print {

	Print(int jione , int jitwo ,int jithree , int renone ,int rentwo ,int renthree)
	{
		this.jione = jione;
		this.jitwo = jitwo;
		this.jithree = jithree;
		this.renone = renone;
		this.rentwo = rentwo;
		this.renthree = renthree;
		renSan=3;
		renRe =2;
		renBuTong = 1;
		jiSan =3;
		jiRe = 2;
		jiBuTong = 1;
	}
	private  int jione , jitwo ,jithree , renone ,rentwo ,renthree ;
	private  int  renSan ,renRe ,renBuTong  , jiSan ,jiRe ,jiBuTong;
	
	public  void PanDuan()
	{
		int  renZhi;//记录人的点数是那种情况
		int  jiZhi; //记录 机器点数是那种情况
		if(renone == rentwo  && rentwo == renthree)
		{
			renZhi=renSan;
		}
		else
		{
			if(renone != rentwo && rentwo != renthree)
			{
				renZhi = renBuTong;
			}
			else
			{
				renZhi = renRe;
			}
		}  //人值判断结束
		
		if(jione == jitwo  && jitwo == jithree)
		{
			jiZhi=jiSan;
		}
		else
		{
			if(jione != jitwo && jitwo != jithree)
			{
				jiZhi = jiBuTong;
			}
			else
			{
				jiZhi = jiRe;
			}
		}  //机值判断结束
		
		
		//以人值为十位,机值为个位 ,构成数字M
		//可以构成的数字有:33/23/13/32/22/12/31/21/11
		
		int M = renZhi*10+jiZhi;
		switch(M)
		{
			case  33:
			{
				if(renone > jione)  {System.out.println("人胜"); break;}
				else   {System.out.println("机胜"); break;}
				
			}
			case  22 :
			{
				if((renone+rentwo+renthree) > (jione+jitwo+jithree))
					{System.out.println("人胜"); break; }
				else
					{System.out.println("机胜"); break; }
			}
			case 11 :
			{
				if((renone+rentwo+renthree) > (jione+jitwo+jithree))
					{System.out.println("人胜"); break ; }
				else
					{System.out.println("机胜"); break ; }
			}
			case 23 :
			case 13 :
			case 12 :
			{
				System.out.println("机胜");
				break;
			}
			case 32 :
			case 31 :
			case 21 :
			{
				System.out.println("人胜");
				break;
			}
			default : System.out.println("人机判胜出现错误");
		}
	}
}
具体执行代码:
package two048Page;

import java.io.IOException;
import java.util.Scanner;

public class ZhiXin {

	public static void main(String[] args) throws IOException
	{
		int r[] = new int[3]  ,m;
		Scanner sc;
		for(int a=0 ; a<3 ;a++)
		{
			System.out.println("输入你产生的第"+ (a+1) +"个随机数:");
			sc = new  Scanner(System.in);
			r[a]  = sc.nextInt();
		} //人的筛子输入完毕
		
		
		//机器产生随机数作为筛子数
		JiQi one = new JiQi();
		JiQi two = new JiQi();
		JiQi three = new JiQi();
		//System.out.println(one.getDianShu() +"uuu"+ two.getDianShu()+"uuu" +three.getDianShu());
		Print  p = new Print(one.getDianShu() , two.getDianShu() ,three.getDianShu() ,r[0] ,r[1] ,r[2]);

		p.PanDuan();
		
	}

}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值