Advertisement
Grisha1549

lab2_2.0

May 15th, 2019
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.29 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. using System.Threading;
  7. using System.IO;
  8.  
  9. namespace lab2_2._0
  10. {
  11.     public struct position
  12.     {
  13.         public int x;
  14.         public int y;
  15.         public static bool operator ==(position a, position b)
  16.         {
  17.             if (a.x == b.x && a.y == b.y)
  18.                 return true;
  19.             else return false;
  20.         }
  21.         public static bool operator !=(position a, position b)
  22.         {
  23.             if (a.x == b.x && a.y == b.y)
  24.                 return false;
  25.             else return true;
  26.         }
  27.     }
  28.  
  29.     class Game
  30.     {
  31.         static string[] LVL;
  32.         static char[,] Map,map;
  33.         public static position user1,user2,end1,end2;
  34.         static int checkint = 0;
  35.         static int userscore;
  36.  
  37.  
  38.         public static int check()
  39.         {
  40.             if (user1 == end1 && user2 == end2) return 1;
  41.             else return 0;
  42.         }
  43.  
  44.         public static void rendering()
  45.         {
  46.             Console.Clear();
  47.             string[] output=new string[LVL.Length];
  48.             for (int i = 0; i < LVL.Length; i++)
  49.             {
  50.                 for (int j = 0; j < LVL[i].Length; j++)
  51.                 {
  52.                     switch (map[i, j])
  53.                     {
  54.                         case '#':
  55.                             Console.BackgroundColor = ConsoleColor.White;
  56.                             Console.ForegroundColor = ConsoleColor.Black;
  57.                             break;
  58.                         case 'X':
  59.                             Console.BackgroundColor = ConsoleColor.White;
  60.                             Console.ForegroundColor = ConsoleColor.Red;
  61.                             break;
  62.                         case 'M':
  63.                             Console.BackgroundColor = ConsoleColor.White;
  64.                             Console.ForegroundColor = ConsoleColor.DarkBlue;
  65.                             break;
  66.                         case '_':
  67.                             Console.BackgroundColor = ConsoleColor.White;
  68.                             Console.ForegroundColor = ConsoleColor.Green;
  69.                             break;
  70.                     }
  71.                     Console.Write(map[i, j]);
  72.                 }
  73.                 Console.Write('\n');
  74.             }
  75.             //        output[i] += map[i, j];
  76.             //foreach (string i in output)
  77.             //    Console.WriteLine(i);
  78.             Console.WriteLine("Score {0}", userscore);
  79.             Console.WriteLine("Press F2 to save \nPress F2 to reload");
  80.             if (check() == 1) checkint = 1;
  81.         }
  82.         public static void move(ConsoleKey way)
  83.         {
  84.             switch (way)
  85.             {
  86.                 case ConsoleKey.UpArrow:
  87.                     if (map[user1.y - 1, user1.x] != '#')
  88.                     {
  89.                         map[user1.y, user1.x] = Map[user1.y,user1.x];
  90.                         user1.y--;
  91.                         if (map[user1.y, user1.x] == 'X') userscore += 10;
  92.                         map[user1.y, user1.x] = 'M';
  93.                     }
  94.                     if (map[user2.y + 1, user2.x] != '#')
  95.                     {
  96.                         map[user2.y, user2.x] = Map[user2.y, user2.x];
  97.                         user2.y++;
  98.                         if (map[user2.y, user2.x] == 'X') userscore += 10;
  99.                         map[user2.y, user2.x] = 'M';
  100.                     }
  101.                     rendering();
  102.                     break;
  103.                 case ConsoleKey.DownArrow:
  104.                     if (map[user1.y + 1, user1.x] != '#')
  105.                     {
  106.                         map[user1.y, user1.x] = Map[user1.y, user1.x];
  107.                         user1.y++;
  108.                         if (map[user1.y, user1.x] == 'X') userscore += 10;
  109.                         map[user1.y, user1.x] = 'M';
  110.                     }
  111.                     if (map[user2.y - 1, user2.x] != '#')
  112.                     {
  113.                         map[user2.y, user2.x] = Map[user2.y, user2.x];
  114.                         user2.y--;
  115.                         if (map[user2.y, user2.x] == 'X') userscore += 10;
  116.                         map[user2.y, user2.x] = 'M';
  117.                     }
  118.                     rendering();
  119.                     break;
  120.                 case ConsoleKey.RightArrow:
  121.                     if (map[user1.y, user1.x + 1] != '#')
  122.                     {
  123.                         map[user1.y, user1.x] = Map[user1.y, user1.x];
  124.                         user1.x++;
  125.                         if (map[user1.y, user1.x] == 'X') userscore += 10;
  126.                         map[user1.y, user1.x] = 'M';
  127.                     }
  128.                     if (map[user2.y, user2.x - 1] != '#')
  129.                     {
  130.                         map[user2.y, user2.x] = Map[user2.y, user2.x];
  131.                         user2.x--;
  132.                         if (map[user2.y, user2.x] == 'X') userscore += 10;
  133.                         map[user2.y, user2.x] = 'M';
  134.                     }
  135.                     rendering();
  136.                     break;
  137.                 case ConsoleKey.LeftArrow:
  138.                     if (map[user1.y, user1.x - 1] != '#')
  139.                     {
  140.                         map[user1.y, user1.x] = Map[user1.y, user1.x];
  141.                         user1.x--;
  142.                         if (map[user1.y, user1.x] == 'X') userscore += 10;
  143.                         map[user1.y, user1.x] = 'M';
  144.                     }
  145.                     if (map[user2.y, user2.x + 1] != '#')
  146.                     {
  147.                         map[user2.y, user2.x] = Map[user2.y, user2.x];
  148.                         user2.x++;
  149.                         if (map[user2.y, user2.x] == 'X') userscore += 10;
  150.                         map[user2.y, user2.x] = 'M';
  151.                     }
  152.                     rendering();
  153.                     break;
  154.  
  155.             }
  156.         }
  157.         static position startuser1, startuser2;
  158.         static int startscore;
  159.         public static void newgame(int x1, int y1, int x2, int y2,int score)
  160.         {
  161.             user1.x = x1; user1.y = y1; startuser1.x = x1; startuser1.y = y1;
  162.             user2.x = x2; user2.y = y2; startuser2.x = x2; startuser2.y = y2;
  163.             userscore = score; startscore = score;
  164.             start();
  165.         }
  166.  
  167.         public static void start()
  168.         {
  169.             LVL = File.ReadAllLines("map.map");
  170.             Map = new char[LVL.Length, LVL[0].Length];
  171.             map = new char[LVL.Length, LVL[0].Length];
  172.             for (int i = 0; i < LVL.Length; i++)
  173.                 for (int j = 0; j < LVL[i].Length; j++)
  174.                 {
  175.                     Map[i, j] = LVL[i][j];
  176.                     map[i, j] = Map[i, j];
  177.                 }
  178.            
  179.             end1.x = 4; end1.y = 3;
  180.             end2.x = 9; end2.y = 3;
  181.  
  182.             map[user1.y, user1.x] = 'M';
  183.             map[user2.y, user2.x] = 'M';
  184.             rendering();
  185.             play();
  186.         }
  187.  
  188.         public static void play()
  189.         {
  190.             while (true)
  191.             {
  192.                 userscore++;
  193.                 if (checkint == 1) { Console.BackgroundColor = ConsoleColor.DarkRed; checkint = 0; Console.WriteLine("Press to enter pleas");Console.ResetColor(); Console.ReadKey(); end(); break; }
  194.                 ConsoleKey button = Console.ReadKey().Key;
  195.                 if (button == ConsoleKey.Escape) break;
  196.                 if (button == ConsoleKey.F2)
  197.                 {
  198.                     Save.save(user1.x, user1.y, user2.x, user2.y,userscore);
  199.                     Console.Clear();
  200.                     Console.WriteLine("Игра сохарнена");
  201.                     Thread.Sleep(1000);
  202.                     rendering();
  203.                 }
  204.                 else if (button == ConsoleKey.F5) {newgame(startuser1.x, startuser1.y, startuser2.x, startuser2.y,startscore); break; }
  205.                 else Game.move(button);
  206.  
  207.             }
  208.         }
  209.         public static void end()
  210.         {
  211.             Console.Clear();
  212.             Console.BackgroundColor = ConsoleColor.DarkRed;
  213.             Console.WriteLine("You are win");
  214.             Console.ResetColor();
  215.             Console.Write("Введите свое имя:");
  216.             string line;
  217.             line =Console.ReadLine();
  218.             Console.WriteLine("line {0} {1}",line,userscore);
  219.             line += ' ';
  220.             line += Convert.ToString(userscore);
  221.             using (var sw = new StreamWriter("Score.score",true))
  222.             {
  223.                 sw.WriteLine(line);
  224.             }
  225.             Output.tableofrecords('r');
  226.        }
  227.     }
  228.     struct rating
  229.     {
  230.         public int score;
  231.         public string name;
  232.         public int id;
  233.     }
  234.    
  235.     class Output
  236.     {
  237.         public static int Compare(rating o1, rating o2)
  238.         {
  239.             return (o1.score).CompareTo(o2.score);
  240.         }
  241.         static string[] table;
  242.         static List<rating> output = new List<rating>();
  243.         private static int id;
  244.         public static void tableofrecords(char c)
  245.         {
  246.            
  247.             table= File.ReadAllLines("Score.score");
  248.             for(int i=0;i<table.Length;i++)
  249.             {
  250.                 string[] s = table[i].Split(' ');
  251.                 rating element;
  252.                 element.name = s[0];
  253.                 element.score = Convert.ToInt32(s[1]);
  254.                 element.id = i;
  255.                 output.Add(element);
  256.             }
  257.             if (output.Count() == 0) Console.WriteLine("Список рекордов пуст");
  258.             else
  259.             {
  260.                 if (c == 'r')
  261.                 id = output[output.Count - 1].id;
  262.                 output.Sort(Compare);
  263.                 int k = 0;
  264.                 Console.Clear();
  265.                 for (int i = 0; i < output.Count && i < 4; i++)
  266.                 {
  267.                     if (c == 'r')
  268.                     {
  269.                         if (output[i].id == id)
  270.                         {
  271.                             k++;
  272.                             Console.BackgroundColor = ConsoleColor.DarkRed;
  273.                             Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
  274.                             Console.ResetColor();
  275.                         }
  276.                         else Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
  277.  
  278.                     }
  279.                     else Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
  280.                 }
  281.                 if (c == 'r')
  282.                 {
  283.                     if (k == 1) { if (output.Count >= 5) Console.WriteLine("{0,2}.{1,-10}{2,4}", 5, output[4].name, output[4].score); }
  284.                     else
  285.                     {
  286.                         int i;
  287.                         for (i = 0; i < output.Count; i++)
  288.                         {
  289.                             if (output[i].id == id) break;
  290.                         }
  291.                         Console.BackgroundColor = ConsoleColor.DarkRed;
  292.                         Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
  293.                         Console.ResetColor();
  294.                     }
  295.                 }
  296.                 else if (output.Count >= 5) Console.WriteLine("{0,2}.{1,-10}{2,4}", 5, output[4].name, output[4].score);
  297.             }
  298.             output.Clear();
  299.             Console.ReadKey();
  300.                
  301.         }
  302.     }
  303.     class Save
  304.     {
  305.         public static void save(int x1, int y1, int x2, int y2,int score)
  306.         {
  307.             Convert.ToString(x1);
  308.             string line = Convert.ToString(x1) + ' ' + Convert.ToString(y2) + ' ' + Convert.ToString(x2) + ' ' + Convert.ToString(y2) + ' ' + Convert.ToString(score);
  309.             using (StreamWriter sw = new StreamWriter("Info.info"))
  310.             {
  311.                 sw.WriteLine(line);
  312.             }
  313.  
  314.         }
  315.         public static void download()
  316.         {
  317.             string[] s,s1;
  318.  
  319.             s=File.ReadAllLines("Info.info");
  320.             s1 = s[0].Split(' ');
  321.             int x1 = Convert.ToInt32(s1[0]);
  322.             int y1 = Convert.ToInt32(s1[1]);
  323.             int x2 = Convert.ToInt32(s1[2]);
  324.             int y2 = Convert.ToInt32(s1[3]);
  325.             int score= Convert.ToInt32(s1[4]);
  326.  
  327.             Game.newgame(x1,y1,x2,y2,score);
  328.         }
  329.  
  330.     }
  331.  
  332.     class Program
  333.     {
  334.         static void menu()
  335.         {
  336.             char switchcase = ' ';
  337.             while (switchcase != 'd')
  338.             {
  339.                 Console.Clear();
  340.                 Console.WriteLine("Главное меню: \n" +
  341.                     "a.Начать игру\n" +
  342.                     "b.Загрузка \n" +
  343.                     "c.Таблица рекордов \n" +
  344.                     "d.Выход");
  345.                 switchcase = Console.ReadKey().KeyChar;
  346.                 switch (switchcase)
  347.                 {
  348.                     case 'a':
  349.                         Game.newgame(1,3,12,3,0);
  350.                         break;
  351.  
  352.                     case 'b':
  353.                         Save.download();
  354.                         break;
  355.  
  356.                     case 'c':
  357.                         Output.tableofrecords('t');
  358.                         break;
  359.                 }
  360.  
  361.             }
  362.         }
  363.         static void Main()
  364.         {
  365.             Program.menu();
  366.         }
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement