21點 (BlackJack) 範例
import java.util.*;
public class blackjack
{
public static void rejack(int[] jack,int count) -->重置
{
jack[count]=jack[count]+1;
if(jack[count]==4)
{
jack[0]=jack[0]+1;
}
if(jack[0]==13)
{
System.out.println("Oops,重新洗牌");
for(int e=0;e<=13;e++)
{
jack[e]=0;
}
}
}
public static void main(String[] args)
{
int[] jack=new int[14];
int you=0;
int com=0;
int count;
int[] five=new int[2];
boolean re;
String cho;
Scanner yesno=new Scanner(System.in);
for(int i=0;i<=12;i++)
{
jack[i]=0;
}
do
{
you=(int)(Math.random()*13)+1;
rejack(jack,you);
you=pockOfyou(you);
com=(int)(Math.random()*13)+1;
rejack(jack,com);
five[0]=1;
com=pockOfcom(com,com);
five[1]=1;
System.out.println("\t" + "computer's pock is " + com);
System.out.print("要牌? (y/n)");
cho=yesno.next();
System.out.println();
do
{
do -->發牌
{
count=(int)(Math.random()*13)+1;
}while(jack[count-1]==4);
rejack(jack,count);
count=pockOfyou(count);
you=you+count;
System.out.println(" total " + you);
five[0]=five[0]+1;
if(five[0]==5) -->判斷是否過五關
{
cho="n";
System.out.println("過五關!!結束你的回合!!");
}
else if(you<21)
{
System.out.print("要牌? (y/n)");
cho=yesno.next();
System.out.println();
}
}while(over(you)&&cho.equals("y"));
while(com<=16&&five[1]!=5)
{
do
{
count=(int)(Math.random()*13)+1;
}while(jack[count-1]>4);
rejack(jack,count);
pockOfcom(count,com);
com=com+count;
five[1]=five[1]+1;
}
if(five[0]==5&&five[1]==5) -->輸贏判斷
{
System.out.println("computer 五關!!" + " You 五關!! " + "=> You win!");
}
else if(five[1]==5)
{
System.out.println("computer 五關!!" + " You " + you + "=> You lose!");
}
else if(five[0]==5)
{
System.out.println("computer " + com + " You 五關!! " + "=> You win!");
}
else if(you>21&&com>21)
{
System.out.println("computer " + com + " You " + you + "=> All bomb!");
}
else if(you>21)
{
System.out.println("computer " + com + " You " + you + "=> You bomb!");
}
else if(com>21)
{
System.out.println("computer " + com + " You " + you + "=> You win!");
}
else if(you>com)
{
System.out.println("computer " + com + " You " + you + "=> You win!");
}
else
{
System.out.println("computer " + com + " You " + you + "=> You lose!");
}
System.out.print("Again? (y/n)");
cho=yesno.next();
if(cho.equals("y"))
{
re=true;
}
else
{
re=false;
}
}while(re==true
}
public static int pockOfcom(int count,int com) -->電腦的牌組
{
if(count==11||count==12||count==13)
{
count=10;
}
else if(count==1)
{
if(com<=10)
{
count=11;
}
else
{
count=1;
}
}
return count;
}
public static boolean over(int total) -->確認有無超過21點
{
if(total<21)
{
return true;
}
else
{
return false;
}
}
public static int pockOfyou(int count) -->自己的牌組
{
Scanner yesno=new Scanner(System.in);
if(count==1)
{
System.out.print("Your pock is " + "A" + "請問要為1或11?");
count=yesno.nextInt();
System.out.print("Your pock is " + "A,");
}
else if(count==11)
{
System.out.print("Your pock is " + "J,");
count=10;
}
else if(count==12)
{
System.out.print("Your pock is " + "Q,");
count=10;
}
else if(count==13)
{
System.out.print("Your pock is " + "K,");
count=10;
}
else
{
System.out.print("Your pock is " + count + ",");
}
return count;
}
}
如果您喜歡我的文章,請在文章最末按5下Like!
我將得到LikeCoin的回饋:)
回饋由LikeCoin基金會出資,您只要註冊/登入帳號(FB、Google帳號都可以註冊,流程超快),按L五次左鍵,可以贊助我的文章且完全不會花到錢!
支持創作,正向交流:)
沒有留言:
張貼留言