C#21点 练习

using System;
public class initCard {
    string[] suit = { "\x3", "\x4", "\x5", "\x6" };
    string[] rank = { "A ", "2 ", "3 ", "4 ", "5 ", "6 ", "7 ", "8 ", "9 ", "10", "J ", "Q ", "K " };
    public string[][] intCards = new string[4][];
    public initCard() {
        for (int i = 0; i < 4; i++) {
            intCards[i] = new string[13];
            for (int j = 0; j < 13; j++) {
                intCards[i][j] = suit[i] + rank[j];
            }
        }
    }
}
public class deal {
    Random ran = new Random();
    int rani1, ranj1, rani2, ranj2, count1, count2, count = 0;
    public string[] player1 = new string[10];
    public string[] player2 = new string[10];
    string cont = "y";
    public deal(string[][] dealCards) {
        dealCard(dealCards);
        while (cont == "Y" || cont == "y") {
            dealCard(dealCards);
            Console.WriteLine("------------------------------");
            if (count1 == 21 && count2 == 21) {
                Console.WriteLine("玩家1、2都BlackJack!,按任意键退出游戏");
                Console.ReadKey();
                return;
            }
            else if (count1 == 21) {
                Console.WriteLine("玩家1 BlackJack!,按任意键退出游戏");
                Console.ReadKey();
                return;
            }
            else if (count2 == 21) {
                Console.WriteLine("玩家2 BlackJack!,按任意键退出游戏");
                Console.ReadKey();
                return;
            }
            else if (count2 > 21 && count1 > 21) {
                Console.WriteLine("玩家1、2都爆!,按任意键退出游戏");
                Console.ReadKey();
                return;
            }
            else if (count1 > 21) {
                Console.WriteLine("玩家1爆!,按任意键退出游戏");
                Console.ReadKey();
                return;
            }
            else if (count2 > 21) {
                Console.WriteLine("玩家2爆!,按任意键退出游戏");
                Console.ReadKey();
                return;
            }
            Console.Write("还要牌吗?按Y或y发牌,按其它键结束发牌:  ");
            cont = Console.ReadKey().Key.ToString();
            Console.WriteLine();
        }
        calc(count1, count2);
    }
    private void dealCard(string[][] dealCards) {
        Console.WriteLine("------------------------------");
        Console.WriteLine("开始发牌:");
        rani1 = ran.Next(0, 4);
        ranj1 = ran.Next(0, 13);
        rani2 = ran.Next(0, 4);
        ranj2 = ran.Next(0, 13);
        while (true) {
            if (dealCards[rani1][ranj1] != "used" && dealCards[rani2][ranj2] != "used" && rani1 != rani2) {
                player1[count] = dealCards[rani1][ranj1];
                dealCards[rani1][ranj1] = "used";
                player2[count] = dealCards[rani2][ranj2];
                dealCards[rani2][ranj2] = "used";
                break;
            }
            else {
                rani1 = ran.Next(0, 4);
                ranj1 = ran.Next(0, 13);
                rani2 = ran.Next(0, 4);
                ranj2 = ran.Next(0, 13);
            }
        }
        if (count > 0) {
            player1[count] += player1[count - 1];
            player2[count] += player2[count - 1];
        }
        switch (ranj1) {
            case (0):
                ranj1 += 10;
                break;
            case (10):
                ranj1 = 9;
                break;
            case (11):
                ranj1 = 9;
                break;
            case (12):
                ranj1 = 9;
                break;
        }
        switch (ranj2) {
            case (0):
                ranj2 += 10;
                break;
            case (10):
                ranj2 = 9;
                break;
            case (11):
                ranj2 = 9;
                break;
            case (12):
                ranj2 = 9;
                break;
        }
        count1 += ranj1 + 1;
        count2 += ranj2 + 1;
        Console.WriteLine("玩家1的牌:{0},点数:{1}", player1[count], count1);
        Console.WriteLine("玩家2的牌:{0},点数:{1}", player2[count], count2);
        count++;
    }
    private void calc(int count1, int count2) {
        if (count1 > count2) {
            Console.WriteLine("玩家1赢!,按任意键退出游戏");
            Console.ReadKey();
        }
        else if (count1 < count2) {
            Console.WriteLine("玩家2赢!,按任意键退出游戏");
            Console.ReadKey();
        }
        else {
            Console.WriteLine("平局!,按任意键退出游戏");
            Console.ReadKey();
        }
    }
}
class BlackJack {
    static void Main() {
        initCard initCards = new initCard();
        deal dealCards = new deal(initCards.intCards);
    }
}


很多偷懒没完善了,先这样了

你可能感兴趣的:(C#)