C#使用二维数组模拟斗地主
本文实例讲述了C#使用二维数组模拟斗地主的方法。分享给大家供大家参考。具体如下:
packagecom.pb.demo; importjava.util.Arrays; importjava.util.Random; /** *扑克牌随机发牌♠♥♣♦二维数组实现 * */ publicclassPuker{ publicstaticvoidmain(String[]args){ //定义数组 String[][]puker=newString[5][]; puker[0]=newString[]{"♠A","♠2","♠3","♠4","♠5","♠6","♠7", "♠8","♠9","♠10","♠J","♠Q","♠K"}; puker[1]=newString[]{"♥A","♥2","♥3","♥4","♥5","♥6","♥7", "♥8","♥9","♥10","♥J","♥Q","♥K"}; puker[2]=newString[]{"♣A","♣2","♣3","♣4","♣5","♣6","♣7", "♣8","♣9","♣10","♣J","♣Q","♣K"}; puker[3]=newString[]{"♦A","♦2","♦3","♦4","♦5","♦6","♦7", "♦8","♦9","♦10","♦J","♦Q","♦K"}; puker[4]=newString[]{"大王","小王"}; //定义3个玩家和底牌 String[]player1=newString[17]; String[]player2=newString[17]; String[]player3=newString[17]; String[]temp=newString[3]; //二维数据洗牌 Randomrandom=newRandom(); inti1=0,i2=0,j1=0,j2=0;//定义4个变量,用来存放数组的下标 //洗牌1000次 for(inti=0;i<1000;i++){ //下标i1等于,随机0~4的整数一维的下标 i1=random.nextInt(5);//0~4之间 if(i1==4){ //如果一维的下标是4,则2维的的元素只有2个大王和小王,只有2个元素 j1=random.nextInt(2); }else{ //如果不是4,则有13张牌 j1=random.nextInt(13);//0~12共13个元素 } //因为最短的数组是2个元素,所以要2次,如果是多个就要多次 i2=random.nextInt(5); if(i2==4){ j2=random.nextInt(2); }else{ j2=random.nextInt(13); } //开始洗牌 Stringtmp=puker[i1][j1]; puker[i1][j1]=puker[i2][j2];//洗牌法,交换,打乱顺序 puker[i2][j2]=tmp; } //洗牌后的牌 System.out.println("===========洗牌后的顺序============"); for(inti=0;i<puker.length;i++){ for(intj=0;j<puker[i].length;j++){ System.out.print(puker[i][j]+""); } } //开始发牌 for(inti=0;i<54;i++){ intp=i%3;//定义发给哪个玩家 intk=i/3-1;//定义轮次 if(i<3){ //先扣下三张底牌 temp[i]=puker[i/13][i%13];//从前向回取3张,估做底牌 }elseif(p==0){ player1[k]=puker[i/13][i%13]; }elseif(p==1){ player2[k]=puker[i/13][i%13]; }elseif(p==2){ player3[k]=puker[i/13][i%13]; } } System.out.println("\n==========发牌完成============="); //玩家一 System.out.println("玩家一"+Arrays.toString(player1)); //玩家二 System.out.println("玩家二"+Arrays.toString(player2)); //玩家三 System.out.println("玩家三"+Arrays.toString(player3)); //底牌 System.out.println("底牌"+Arrays.toString(temp)); } }
希望本文所述对大家的C#程序设计有所帮助。