- import java.util.Scanner;
- /*游戏的逻辑类*/
- public class BlackJack {
- private Deck mydeck;
- public BlackJack(){
- }
- /*开始游戏*/
- public void play(){
- Scanner myscanner = new Scanner(System.in); //获得输入
- String choice; //玩家的选择
- mydeck = new Deck(); //牌桌
- out:
- /*设定为无线循环*/
- while(true){
- System.out.println("do you want more cards? Y or N"); //问玩家是否还要牌
- choice = myscanner.nextLine(); //读取选择
- if(choice.equalsIgnoreCase("n")){ //如果玩家选择不要牌
- /* 这里要判断一下电脑的牌是不是小于15
- 如果小于15的话,电脑应该继续叫牌的*/
- while(mydeck.cHands.getpoint()<15){
- mydeck.nextround(false);
- }
- /*把电脑的牌设定为全部打印,不然会输的不甘心的*/
- mydeck.cHands.setmine(true);
- /*判断结果*/
- switch(mydeck.checkcards()){
- case 0:
- System.out.println("you lose");
- break;
- case 1:
- System.out.println("you win");
- break;
- case 2:
- System.out.println("equal");
- break;
- }
- System.out.println(mydeck);
- break out;
- }else if(choice.equalsIgnoreCase("y")){ //玩家继续要牌
- /*发一轮牌*/
- mydeck.nextround(true);
- /*这里要判断是否大于21,爆掉。或者正好是21,赢了*/
- switch(mydeck.checkcards()){
- case 3:
- mydeck.cHands.setmine(true);
- System.out.println(mydeck);
- System.out.println("you both has over 21");
- break out;
- case 4:
- mydeck.cHands.setmine(true);
- System.out.println(mydeck);
- System.out.println("you win");
- break out;
- case 5:
- mydeck.cHands.setmine(true);
- System.out.println(mydeck);
- System.out.println("you lose");
- break out;
- }
- System.out.println(mydeck);
- }else{
- System.out.println(mydeck);
- System.out.println("the input is not vaild");
- }
- }
- }
- }
- import java.util.Collections;
- import java.util.Stack;
- public class Deck {
- Hands pHands; //the player's hand
- Hands cHands; //the computer's hand
- Stack<Cards> allCards; //it store all kinds of Cards
- /*构造函数*/
- public Deck(){
- this.initialgame();
- }
- /* 初始化牌*/
- private void initialcards(){
- for(Rank rank:Rank.values()){
- for(Suit suit:Suit.values()){
- allCards.push(new Cards(rank,suit));
- }
- }
- Collections.shuffle(allCards);
- }
- /*初始化游戏*/
- public void initialgame(){
- pHands = new Hands(true); //确认pHands表示玩家
- cHands = new Hands(false); //确认cHands表示电脑
- allCards = new Stack<Cards>();
- this.initialcards();
- /*发玩家的牌*/
- pHands.addCard(allCards.pop());
- pHands.addCard(allCards.pop());
- /*发电脑的牌*/
- cHands.addCard(allCards.pop());
- cHands.addCard(allCards.pop());
- /*答应嗲一次,大家手中的牌*/
- System.out.println(this);
- }
- /* 打印现在双方的的牌*/
- @Override
- public String toString() {
- String returnstring;
- returnstring ="the cards in coputer's: /n";
- returnstring+=cHands.toString();
- returnstring+="the cards in your hand: /n";
- returnstring+=pHands.toString();
- return returnstring;
- }
- /*发牌,这里电脑的策略为小于15点,就需要发牌,如果变量onemore为true,则表示玩家要牌*/
- public void nextround(boolean onemore){ //if the player want one more card,then the onemore is true,otherwise,it's false
- if(cHands.getpoint()< 15){ //when the computer's points is less than 11,it will ask another one
- cHands.addCard(allCards.pop());
- }
- if(onemore){
- pHands.addCard(allCards.pop());
- }
- }
- /*比较大小*/
- public int checkcards(){
- int whowin = -1;
- if(pHands.getpoint()==21&&cHands.getpoint()==21){
- whowin = 2; //when whowin = 2 ,equal
- }else if(cHands.getpoint()==21){
- whowin = 0; //when whowin = 0, computers win
- }else if(pHands.getpoint()==21){
- whowin = 1; //when whowin = 1, players win
- }else if(pHands.getpoint()>21&&cHands.getpoint()>21){// whe whowin = 3,both has over 21;
- whowin = 3;
- }else if(cHands.getpoint()>21){ // when whowin = 4,computer has over 21;
- whowin = 4;
- }else if(pHands.getpoint()>21){
- whowin = 5;
- }else{
- if(pHands.getpoint()==cHands.getpoint()){
- whowin = 2;
- }
- else if(pHands.compare(cHands)){
- whowin = 1;
- }else{
- whowin = 0;
- }
- }
- return whowin;
- }
- }
- import java.util.Stack;
- public class Hands {
- private boolean mine; //to check the hands is player's of not
- private Stack<Cards> myCards; //the Stack store cards;
- public Hands(boolean mine){ //if the cards is player's,the the mine should be true,otherwise should be false;
- this.mine = mine;
- myCards = new Stack<Cards>();
- }
- /*add a card*/
- public void addCard(Cards card){
- myCards.add(card);
- }
- //设置这张牌是否是自己
- public void setmine(boolean mine){
- this.mine = mine;
- }
- //获得牌的点数
- public int getpoint(){
- int mypoint=0;
- for(Cards card:myCards){
- mypoint +=card.getRank();
- }
- return mypoint;
- }
- //输出手中的牌
- public String toString() {
- // TODO Auto-generated method stub
- String returnstring = "";
- int size = myCards.size()-1;
- if(myCards.empty()){ //the hands is empty
- return "the hands is empty";
- }
- else if(mine){ //the Hand is players
- for(Cards card:myCards){
- returnstring +=card.toString()+"/n";
- }
- returnstring += "you points are "
- + String.valueOf(this.getpoint())
- + "/n";
- }else{ //the Hand is not players
- for(int i=0;i<size;i++){
- returnstring += myCards.get(i).toString()+"/n";
- }
- }
- return returnstring;
- }
- public boolean compare(Hands newone){ //return true if it's bigger than the input one ,otherwise return false
- return(this.getpoint()>newone.getpoint());
- }
- }
- /*此类代表一张牌*/
- public class Cards {
- private Rank rank; //大小
- private Suit suit; //花色
- /* 构造函数*/
- public Cards(Rank rank,Suit suit){
- this.rank = rank;
- this.suit = suit;
- }
- public int getRank() {
- return rank.value();
- }
- public Suit getSuit() {
- return suit;
- }
- @Override
- public String toString() {
- return rank.toString() + " of " + suit.toString();
- }
- }
- /*花色类*/
- public enum Suit {
- Clubs,
- Diamonds,
- Hearts,
- Spades
- }
- /*牌大小类*/
- public enum Rank {
- Ace(1), deuce(2), three(3), four(4), five(5), six(6), seven(7),
- eight(8), nine(9), ten(10), Jack(10), Queen(10), King(10);
- Rank(int value) {
- this.value = value;
- }
- int value() {
- return value;
- }
- private final int value; //用于比较牌的大小
- }