Advertisement
vovanhoangtuan

Bầu cua - Hoàng Tuân

Apr 29th, 2020
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BauCua
  8. {
  9.     class Program
  10.     {
  11.  
  12.         protected static int origRow;
  13.         protected static int origCol;
  14.  
  15.  
  16.         protected static void WriteAt(string s, int x, int y)
  17.         {
  18.             try
  19.             {
  20.                 Console.SetCursorPosition(origCol + x, origRow + y);
  21.                 Console.Write(s);
  22.             }
  23.             catch (ArgumentOutOfRangeException e)
  24.             {
  25.                 Console.Clear();
  26.                 Console.WriteLine(e.Message);
  27.             }
  28.         }
  29.         static void Main(string[] args)
  30.         {
  31.  
  32.             Console.OutputEncoding = Encoding.Unicode;
  33.             origRow = Console.CursorTop;
  34.             origCol = Console.CursorLeft;
  35.  
  36.             int x = 0, y = 0;
  37.             render(x, y);
  38.  
  39.             while (true)
  40.             {
  41.                 object ch = Console.ReadKey(false).Key;
  42.  
  43.                 switch (ch)
  44.                 {
  45.                     case ConsoleKey.Escape:
  46.                         Environment.Exit(0);
  47.                         return;
  48.                     case ConsoleKey.UpArrow:
  49.                         y--;
  50.                         render(x, y);
  51.                         break;
  52.                     case ConsoleKey.DownArrow:
  53.                         y++;
  54.                         render(x, y);
  55.                         break;
  56.                     case ConsoleKey.RightArrow:
  57.                         x++;
  58.                         render(x, y);
  59.                         break;
  60.                     case ConsoleKey.LeftArrow:
  61.                         x--;
  62.                         render(x, y);
  63.                         break;
  64.                     case ConsoleKey.Enter:
  65.                         xuly(x, y);
  66.                         break;
  67.                 }
  68.             }
  69.  
  70.  
  71.         }
  72.  
  73.  
  74.         static void xuly(int x, int y)
  75.         {
  76.             string[] cobac = {"Nai", "Bầu", "Gà", "Cá", "Cua", "Tôm"};
  77.             Random ran = new Random();
  78.             int so = ran.Next(0, 5);
  79.             int current = 0;
  80.             if (y >= 0 && y <= 4)
  81.             {
  82.                 if (x < 5) current = 0;
  83.                 else if (x <= 10) current = 1;
  84.                 else if (x <= 15) current = 2;
  85.             }
  86.             else if (y <= 8)
  87.             {
  88.                 if (x < 5) current = 3;
  89.                 else if (x <= 10) current = 4;
  90.                 else if (x <= 15) current = 5;
  91.             }
  92.            
  93.             Console.SetCursorPosition(0, 10);
  94.             Console.WriteLine($"PC : {cobac[so]} \nYou: {cobac[current]} ");
  95.             if (so == current) Console.WriteLine("Bạn thắng rồi !! Hehe");
  96.             else Console.WriteLine("Bạn thua rồi !! Sad :( ");
  97.  
  98.         }
  99.         static void render(int x, int y)
  100.         {
  101.             Console.Clear();
  102.             for (int i = 1; i < 16; i++)
  103.             {
  104.                 for (int j = 0; j <= 8; j += 4)
  105.                 {
  106.                     WriteAt("-", i, j);
  107.                 }
  108.             }
  109.  
  110.             for (int i = 1; i < 8; i++)
  111.             {
  112.                 for (int j = 0; j <= 15; j += 5)
  113.                 {
  114.                     WriteAt("|", j, i);
  115.                 }
  116.             }
  117.  
  118.             for (int i = 0; i <= 15; i += 5)
  119.             {
  120.                 for (int j = 0; j <= 8; j += 4)
  121.                 {
  122.                     WriteAt("+", i, j);
  123.                 }
  124.             }
  125.             WriteAt("Nai", 2, 2);
  126.             WriteAt("Bầu", 7, 2);
  127.             WriteAt("Gà", 12, 2);
  128.  
  129.             WriteAt("Cá", 2, 6);
  130.             WriteAt("Cua", 7, 6);
  131.             WriteAt("Tôm", 12, 6);
  132.             Console.SetCursorPosition(x, y);
  133.             Console.Write("*");
  134.  
  135.         }
  136.  
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement