广告位联系
返回顶部
分享到

C#编写控制台程序纸牌游戏教程

C语言 来源:互联网搜集 作者:秩名 发布时间:2019-08-21 21:11:58 人浏览
摘要

废话不多说,直接上代码: using System;using System.Collections.Generic;using System.Text;using System.Threading; namespace ConsoleApplication2{ class Game { string[] str = new string[52]; int[] number = new int[52]; int i = 0; int[] a = ne

废话不多说,直接上代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
 
namespace ConsoleApplication2
{
 class Game
 {
  string[] str = new string[52];
  int[] number = new int[52];
  int i = 0;
  int[] a = new int[4];
  int[] b = new int[4];
  int[] c = new int[4];
  int[] d = new int[4];
  int[] e = new int[4];
  Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
  public void getnum(int[] number, int minnum, int maxnum)
  {
 
   int flag;
 
   flag = number[i] = ra.Next(0, 52);
   int f = 0;
 
   while (f < i)
   {
    if (number[f] == flag)
     getnum(number, 0, 52);
    f++;
  }
 
 
  }
 
  public void GetRandomNum()//给number随机赋从0到51不同的值
  {
   while (i < 52)
   {
    int flag;
 
    flag = number[i] = ra.Next(0, 52);
    int f = 0;
    while (f < i)
    {
     if (number[f] == flag)
     {
      getnum(number, 0, 52);
      break;
     }
     f++;
    }
    i++;
   }
  }
 
  public void EnterCard()//给每个字符串数组赋上牌值
  {
   str[0] = "1 of the club";
   str[1] = "2 of the club";
   str[2] = "3 of the club";
   str[3] = "4 of the club";
   str[4] = "5 of the club";
   str[5] = "6 of the club";
   str[6] = "7 of the club";
   str[7] = "8 of the club";
   str[8] = "9 of the club";
   str[9] = "10 of the club";
   str[10] = "Jack of the club";
   str[11] = "queen of the club";
   str[12] = "king of the club";
 
   str[13] = "1 of the diamond";
   str[14] = "2 of the diamond";
   str[15] = "3 of the diamond";
   str[16] = "4 of the diamond";
   str[17] = "5 of the diamond";
   str[18] = "6 of the diamond";
   str[19] = "7 of the diamond";
   str[20] = "8 of the diamond";
   str[21] = "9 of the diamond";
   str[22] = "10 of the diamond";
   str[23] = "Jack of the diamond";
   str[24] = "queen of the diamond";
   str[25] = "king of the diamond";
 
 
   str[26] = "1 of the heart";
   str[27] = "2 of the heart";
   str[28] = "3 of the heart";
   str[29] = "4 of the heart";
   str[30] = "5 of the heart";
   str[31] = "6 of the heart";
   str[32] = "7 of the heart";
   str[33] = "8 of the heart";
   str[34] = "9 of the heart";
   str[35] = "10 of the heart";
   str[36] = "Jack of the heart";
   str[37] = "queen of the heart";
   str[38] = "king of the heart";
 
   str[39] = "1 of the spade";
   str[40] = "2 of the spade";
   str[41] = "3 of the spade";
   str[42] = "4 of the spade";
   str[43] = "5 of the spade";
   str[44] = "6 of the spade";
   str[45] = "7 of the spade";
   str[46] = "8 of the spade";
   str[47] = "9 of the spade";
   str[48] = "10 of the spade";
   str[49] = "Jack of the spade";
   str[50] = "queen of the spade";
   str[51] = "king of the spade";
 
 
  }
 
  public void FaPai(int PersonNum)//发牌
  {
   for (int j = 0; j < PersonNum; j++)
   {
 
    a[j] = number[j * 5];
    b[j] = number[j * 5 + 1];
    c[j] = number[j * 5 + 2];
    d[j] = number[j * 5 + 3];
    e[j] = number[j * 5 + 4];
   }
  }
 
  public void Print(string[] PersonName, int j)//显示玩家的牌
  {
   Console.WriteLine("玩家{0}的牌", PersonName[j]);
   Console.WriteLine("1. {0}", str[a[j]]);
   Console.WriteLine("2. {0}", str[b[j]]);
   Console.WriteLine("3. {0}", str[c[j]]);
   Console.WriteLine("4. {0}", str[d[j]]);
   Console.WriteLine("5. {0}", str[e[j]]);
  }
 
  public void Print1(string[] PersonName, int j)//显示玩家的牌
  {
   Console.WriteLine("玩家{0}剩下的牌", PersonName[j]);
   Console.WriteLine("1. {0}", str[a[j]]);
   Console.WriteLine("2. {0}", str[b[j]]);
   Console.WriteLine("3. {0}", str[c[j]]);
   Console.WriteLine("4. {0}", str[d[j]]);
   Console.WriteLine("5. {0}", str[e[j]]);
   Console.WriteLine("按ENTER建进入下一个玩家");
   Console.ReadLine();
  }
 
public bool CompareAns(int j, string[] PersonName, int n, bool bool2)//对玩家的牌与胜利的结果进行比较
  {
   int min = 0, max = 0;
   max = (a[j] > b[j]) ? a[j] : b[j];
   max = (c[j] > max) ? c[j] : max;
   max = (d[j] > max) ? d[j] : max;
   max = (e[j] > max) ? e[j] : max;
   min = (a[j] < b[j]) ? a[j] : b[j];
   min = (c[j] < min) ? c[j] : min;
   min = (d[j] < min) ? d[j] : min;
   min = (e[j] < min) ? e[j] : min;
 
   if (min >= 0 && max < 13 || min >= 13 && max < 26 || min >= 26 && max < 39 || min >= 39 && max < 52)
   {
    Console.Clear();
    Console.WriteLine("恭喜玩家{0}赢得游戏!", PersonName[j]);
    bool2 = false; 
   }
    
   //判断是否还有底牌可供玩家拿取
   else if (n==52)
   {
    Console.Clear();
    Console.WriteLine("没有底牌了!!重新开始。");
    bool2 = false; 
   } 
   return bool2;
  }
 
  public void TakeCard(string[] PersonName, int PersonNum)//玩家打牌
  {
   GetRandomNum();
   int n = 1, botton=7;
 
   int f = 0;
   int up = 0, up1 = 0;
   bool bool2 = true;
   bool bool1 = false;
   // 发牌
   FaPai(PersonNum);
   //玩家打牌
   n = 5 * PersonNum;
 
   while (bool2)
   {
    for (int j = 0; j < PersonNum; j++)
    {
     //显示玩家的牌
     Console.Clear();
     if (bool1)
     {
      Console.Write("上一玩家打出的牌是");
      Console.WriteLine("{0}", str[up]);
     }
     bool1 = true;
     Print(PersonName, j);
 
 //玩家从剩下的牌中拿牌或拿上一玩家扔出来的牌
 
     char D='f';
     if (n == 5 * PersonNum)
     {
      try
      {
       Console.WriteLine("按D从剩下的牌中拿一张牌");
       D = Convert.ToChar(Console.ReadLine());
      }
      catch (Exception e)
      {
       Console.WriteLine(e.Message);
       Thread.Sleep(1000);
       System.Environment.Exit(0);
      }
       
      if (D == 'D' || D == 'd')
      {
 
       f = number[n];
       n++;
       Print(PersonName, j);
       Console.WriteLine("6. {0}", str[f]);
 
 
      }
 
 
     }
     else
     {
      Console.WriteLine("按D从剩下的牌中拿一张牌或者按T拿被上一玩家打的牌");
      D = Convert.ToChar(Console.ReadLine());
      if (D == 'D' || D == 'd')
      {
 
       f = number[n];
       n++;
       Print(PersonName, j);
       Console.WriteLine("6. {0}", str[f]);
 
 
      }
      else if (D == 't' || D == 'T')
      {
       Print(PersonName, j);
       Console.WriteLine("6. {0}", str[up]);
 
      }
 
     }
 
     //打牌
     try
     {
      Console.WriteLine("输入你想要打出牌的序号:");
      botton = Convert.ToInt32(Console.ReadLine());
     }
     catch (Exception e)
     {
      Console.WriteLine(e.Message);
      Thread.Sleep(1000);
      System.Environment.Exit(0);
     } 
     switch (botton)
     {
    case 1:
       {
        Console.WriteLine("被打出的牌是{0}", str[a[j]]);
        up1 = up; up = a[j]; a[j] = b[j]; b[j] = c[j]; c[j] = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
       }
 
    case 2:
       {
        Console.WriteLine("被打出的牌是{0}", str[b[j]]);
        up1 = up; up = b[j]; b[j] = c[j]; c[j] = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
       }
     
    case 3:
       {
        Console.WriteLine("被打出的牌是{0}", str[c[j]]);
        up1 = up; up = c[j]; c[j] = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
 
       }
 
    case 4:
       {
        Console.WriteLine("被打出的牌是{0}", str[d[j]]);
        up1 = up; up = d[j]; d[j] = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
 
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
       }
    case 5:
       {
        Console.WriteLine("被打出的牌是{0}", str[e[j]]);
        up1 = up; up = e[j];
        if (D == 'd' || D == 'D')
         e[j] = f;
        else if (D == 't' || D == 'T')
         e[j] = up1;
        Print1(PersonName, j);
 
        break;
 
       }
 
    case 6:
       {
        Console.WriteLine("被打出的牌是{0}", str[f]);
        up = f;
        Print1(PersonName, j);
        break;
       }
      default:
       Console.WriteLine("输入错误"); break;
     }
     //判断结果
     bool2 = CompareAns(j,PersonName,n,bool2);
     if (bool2 == false) break;
 
    }
   }
  }
 
 
 }
 
 class ExeMain
 {
    static void Main(string[] args)
  {
 
   int PersonNum=0;
   Game obj = new Game();
   Console.WriteLine("第一个输入的名字将作为第一个开始拿牌的人:");
   Console.WriteLine("游戏的人数:");
   try
   {
    PersonNum = Convert.ToInt32(Console.ReadLine());
   }
   catch (Exception e)
   {
    Console.WriteLine(e.Message);
    Thread.Sleep(1000);
    System.Environment.Exit(0);
   }
   string[] PersonName = new string[PersonNum];
   for (int i = 0; i < PersonNum; i++)
   {
    Console.WriteLine("输入玩家{0}的名字", i + 1);
    PersonName[i] = Console.ReadLine();
   }
   obj.EnterCard();//给每个字符串数组赋值
 
   obj.TakeCard(PersonName, PersonNum);//给每个玩家发牌
   Console.ReadLine();
 
 
  }
 }


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://www.jb51.net/article/168129.htm
相关文章
  • C++中类的六大默认成员函数的介绍

    C++中类的六大默认成员函数的介绍
    一、类的默认成员函数 二、构造函数Date(形参列表) 构造函数主要完成初始化对象,相当于C语言阶段写的Init函数。 默认构造函数:无参的构
  • C/C++实现遍历文件夹最全方法总结介绍

    C/C++实现遍历文件夹最全方法总结介绍
    一、filesystem(推荐) 在c++17中,引入了文件系统,使用起来非常方便 在VS中,可以直接在项目属性中调整: 只要是C++17即以上都可 然后头文件
  • C语言实现手写Map(数组+链表+红黑树)的代码

    C语言实现手写Map(数组+链表+红黑树)的代码
    要求 需要准备数组集合(List) 数据结构 需要准备单向链表(Linked) 数据结构 需要准备红黑树(Rbtree)数据结构 需要准备红黑树和链表适配策略
  • MySQL系列教程之使用C语言来连接数据库

    MySQL系列教程之使用C语言来连接数据库
    写在前面 知道了 Java中使用 JDBC编程 来连接数据库了,但是使用 C语言 来连接数据库却总是连接不上去~ 立即安排一波使用 C语言连接 MySQL数
  • 基于C语言实现简单学生成绩管理系统

    基于C语言实现简单学生成绩管理系统
    一、系统主要功能 1、密码登录 2、输入数据 3、查询成绩 4、修改成绩 5、输出所有学生成绩 6、退出系统 二、代码实现 1 2 3 4 5 6 7 8 9 10 11
  • C语言实现共享单车管理系统

    C语言实现共享单车管理系统
    1.功能模块图; 2.各个模块详细的功能描述。 1.登陆:登陆分为用户登陆,管理员登陆以及维修员登录,登陆后不同的用户所执行的操作
  • C++继承与菱形继承的介绍

    C++继承与菱形继承的介绍
    继承的概念和定义 继承机制是面向对象程序设计的一种实现代码复用的重要手段,它允许程序员在保持原有类特性的基础上进行拓展,增加
  • C/C++指针介绍与使用介绍

    C/C++指针介绍与使用介绍
    什么是指针 C/C++语言拥有在程序运行时获得变量的地址和操作地址的能力,这种用来操作地址的特殊类型变量被称作指针。 翻译翻译什么
  • C++进程的创建和进程ID标识介绍
    进程的ID 进程的ID,可称为PID。它是进程的唯一标识,类似于我们的身份证号是唯一标识,因为名字可能会和其他人相同,生日可能会与其他
  • C++分析如何用虚析构与纯虚析构处理内存泄漏

    C++分析如何用虚析构与纯虚析构处理内存泄漏
    一、问题引入 使用多态时,如果有一些子类的成员开辟在堆区,那么在父类执行完毕释放后,没有办法去释放子类的内存,这样会导致内存
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计