public class Card {
private String mask;
private int value;
private int icon;
private String iconMask;
private boolean isShow;
public Card(String mask, int value, int icon, String iconMask,boolean isShow){
this.mask = mask;
this.value = value;
this.icon = icon;
this.iconMask = iconMask;
this.isShow = isShow;
}
public boolean isShow() {
return isShow;
}
public void setShow(boolean isShow) {
this.isShow = isShow;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getIconMask() {
return iconMask;
}
public void setIconMask(String iconMask) {
this.iconMask = iconMask;
}
public String getMask() {
return mask;
}
public void setMask(String mask) {
this.mask = mask;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class BlackJack {
private static ArrayList<Card> cards = new ArrayList<Card>();
private static ArrayList<Card> cardsComputer = new ArrayList<Card>();
private static ArrayList<Card> cardsPlayer = new ArrayList<Card>();
public static void main(String[] args){
System.out.println("21点游戏");
System.out.println("****************");
System.out.println("A表示1点,2-10表示2-10点,J、Q、K表示10点");
reset();
System.exit(0);
}
public static int sendCardInit(int id){
int counter = 1;
int point = 0;
if(id == 1){
point = sendCard(id,counter)+sendCard(id,counter+1);
System.out.println("玩家的总点数: "+point);
}else{
int x = sendCard(id,counter);
counter = counter +1;
int y = sendCard(id,counter);
point = x+y;
System.out.println("电脑可能的点数: "+(point-x+1)+"~"+(point-x+10));
//System.out.println(point);
}
return point;
}
public static int sendCard(int id,int counter){
//printCards();
int sum = 0;
Card curr = cards.get(0);
if(id == 0){
if(counter == 1){
System.out.println("第"+counter+"张牌: *");
sum = sum +curr.getValue();
//System.out.println(sum);
//System.out.println(curr.getIconMask()+curr.getMask());
}else{
sum = sum +curr.getValue();
System.out.print("第"+counter+"张牌: ");
System.out.println(curr.getIconMask()+curr.getMask());
}
cardsComputer.add(cards.get(0));
}else{
sum = sum +curr.getValue();
System.out.print("第"+counter+"张牌: ");
System.out.println(curr.getIconMask()+curr.getMask());
cardsPlayer.add(cards.get(0));
}
cards.remove(0);
return sum;
}
public static int extraCard(int id, int counter,int point){
int sum = point;
System.out.print("第"+(counter+2)+"张牌: ");
System.out.println(cards.get(0).getIconMask()+cards.get(0).getMask());
sum = sum +cards.get(0).getValue();
if(id == 0){
cardsComputer.add(cards.get(0));
}else{
cardsPlayer.add(cards.get(0));
}
cards.remove(0);
return sum;
}
public static void printCards(){
for(int i=0; i<cards.size();i++){
System.out.print(cards.get(i).getIconMask()+cards.get(i).getMask()+" ;");
}
}
public static void init(){
initMethod(1);
initMethod(2);
initMethod(3);
initMethod(4);
}
public static void initMethod(int icon){
int index = 0;
String iconMask= "";
//初始化花色、字标
if(icon == 1){
index = 0;
iconMask = "♥";
}else if(icon == 2){
index = 13;
iconMask = "♦";
}else if(icon == 3){
index = 26;
iconMask = "♠";
}else if(icon == 4){
index = 39;
iconMask = "♣";
}
for(int i = index;i < index+13;i++){
if(i == index+0){
Card c = new Card("A",i-index+1,icon,iconMask,true);
cards.add(c);
}else if(i >index && i <10+index){
Card c =new Card(String.valueOf(i+1-index),i-index+1,icon,iconMask,true);
cards.add(c);
}else if(i >=10+index && i <13+index){
if(i == 10+index){
Card c =new Card("J",10,icon,iconMask,true);
cards.add(c);
}else if(i == 11+index){
Card c =new Card("Q",10,icon,iconMask,true);
cards.add(c);
}else if(i == 12+index){
Card c =new Card("K",10,icon,iconMask,true);
cards.add(c);
}
}
}
}
public static void printComputer(){
System.out.println("电脑的牌: ");
for(int j =0; j< cardsComputer.size();j++){
System.out.print("第"+(j+1)+"张牌: ");
System.out.println(cardsComputer.get(j).getIconMask()+cardsComputer.get(j).getMask());
}
}
public static void printPlayer(){
System.out.println("玩家的牌: ");
for(int j =0; j< cardsPlayer.size();j++){
System.out.print("第"+(j+1)+"张牌: ");
System.out.println(cardsPlayer.get(j).getIconMask()+cardsPlayer.get(j).getMask());
}
}
public static void reset(){
init();
Collections.shuffle(cards);
System.out.println("电脑的牌: ");
int computer = sendCardInit(0);
System.out.println("----------------");
System.out.println("玩家的牌: ");
int player = sendCardInit(1);
System.out.println("----------------");
System.out.println("请输入指令:");
System.out.println("输入K表示开牌;输入A表示加牌;输入R表示重置;输入Q表示退出");
Scanner s = new Scanner(System.in);
while(true){
String command = s.nextLine();
if(command.equals("K")||command.equals("k")){
if(player>21){
System.out.println("****************");
printComputer();
System.out.println("电脑的最终点数: " + computer);
System.out.println("----------------");
printPlayer();
System.out.println("玩家的最终点数: " + player);
System.out.println("真遗憾,您输了,电脑获胜!");
}
if(computer<16){
System.out.println("电脑加牌");
for(int j =0; j< cardsComputer.size();j++){
System.out.print("第"+(j+1)+"张牌: ");
System.out.println(cardsComputer.get(j).getIconMask()+cardsComputer.get(j).getMask());
}
}
int extraCounter = 0;
while(computer<16){
computer = extraCard(0,extraCounter+1,computer);
extraCounter = extraCounter+1;
}
if(computer > 21){
System.out.println("****************");
printComputer();
System.out.println("电脑的最终点数: " + computer);
System.out.println("----------------");
printPlayer();
System.out.println("玩家的最终点数: " + player);
System.out.println("真厉害,您赢了,玩家获胜!");
}else if(computer > player){
System.out.println("****************");
printComputer();
System.out.println("电脑的最终点数: " + computer);
System.out.println("----------------");
printPlayer();
System.out.println("玩家的最终点数: " + player);
System.out.println("真遗憾,您输了,电脑获胜!");
}else if(computer < player){
System.out.println("****************");
printComputer();
System.out.println("电脑的最终点数: " + computer);
System.out.println("----------------");
printPlayer();
System.out.println("玩家的最终点数: " + player);
System.out.println("真厉害,您赢了,玩家获胜!");
}else{
System.out.println("****************");
printComputer();
System.out.println("电脑的最终点数: " + computer);
System.out.println("----------------");
printPlayer();
System.out.println("玩家的最终点数: " + player);
System.out.println("平局,无人获胜!");
}
}else if(command.equals("A")||command.equals("a")){
System.out.println("玩家加牌");
for(int j =0; j< cardsPlayer.size();j++){
System.out.print("第"+(j+1)+"张牌: ");
System.out.println(cardsPlayer.get(j).getIconMask()+cardsPlayer.get(j).getMask());
}
player =extraCard(1,cardsPlayer.size()-2+1,player);
System.out.println("玩家的总点数: "+player);
if(player>21){
System.out.println("****************");
printComputer();
System.out.println("电脑的最终点数: " + computer);
System.out.println("----------------");
printPlayer();
System.out.println("玩家的最终点数: " + player);
System.out.println("真遗憾,您输了,电脑获胜!");
}
}else if(command.equals("R")||command.equals("r")){
System.out.println("21点游戏");
System.out.println("****************");
System.out.println("A表示1点,2-10表示2-10点,J、Q、K表示10点");
cards.clear();
cardsComputer.clear();
cardsPlayer.clear();
init();
Collections.shuffle(cards);
System.out.println("电脑的牌: ");
computer = sendCardInit(0);
System.out.println("----------------");
System.out.println("玩家的牌: ");
player = sendCardInit(1);
System.out.println("----------------");
print();
}else if(command.equals("Q")||command.equals("q")){
System.err.println("成功退出游戏");
break;
}else{
System.err.println("无效输入!输入K表示开牌;输入A表示加牌;输入R表示重置;输入Q表示退出");
}
}
s.close();
}
public static void print(){
System.out.println("请输入指令:");
System.out.println("输入K表示开牌;输入A表示加牌;输入R表示重置;输入Q表示退出");
}
}