Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- using System.IO;
- namespace lab2_2._0
- {
- public struct position
- {
- public int x;
- public int y;
- public static bool operator ==(position a, position b)
- {
- if (a.x == b.x && a.y == b.y)
- return true;
- else return false;
- }
- public static bool operator !=(position a, position b)
- {
- if (a.x == b.x && a.y == b.y)
- return false;
- else return true;
- }
- }
- class Game
- {
- static string[] LVL;
- static char[,] Map,map;
- public static position user1,user2,end1,end2;
- static int checkint = 0;
- static int userscore;
- public static int check()
- {
- if (user1 == end1 && user2 == end2) return 1;
- else return 0;
- }
- public static void rendering()
- {
- Console.Clear();
- string[] output=new string[LVL.Length];
- for (int i = 0; i < LVL.Length; i++)
- {
- for (int j = 0; j < LVL[i].Length; j++)
- {
- switch (map[i, j])
- {
- case '#':
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Black;
- break;
- case 'X':
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Red;
- break;
- case 'M':
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.DarkBlue;
- break;
- case '_':
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Green;
- break;
- }
- Console.Write(map[i, j]);
- }
- Console.Write('\n');
- }
- // output[i] += map[i, j];
- //foreach (string i in output)
- // Console.WriteLine(i);
- Console.WriteLine("Score {0}", userscore);
- Console.WriteLine("Press F2 to save \nPress F2 to reload");
- if (check() == 1) checkint = 1;
- }
- public static void move(ConsoleKey way)
- {
- switch (way)
- {
- case ConsoleKey.UpArrow:
- if (map[user1.y - 1, user1.x] != '#')
- {
- map[user1.y, user1.x] = Map[user1.y,user1.x];
- user1.y--;
- if (map[user1.y, user1.x] == 'X') userscore += 10;
- map[user1.y, user1.x] = 'M';
- }
- if (map[user2.y + 1, user2.x] != '#')
- {
- map[user2.y, user2.x] = Map[user2.y, user2.x];
- user2.y++;
- if (map[user2.y, user2.x] == 'X') userscore += 10;
- map[user2.y, user2.x] = 'M';
- }
- rendering();
- break;
- case ConsoleKey.DownArrow:
- if (map[user1.y + 1, user1.x] != '#')
- {
- map[user1.y, user1.x] = Map[user1.y, user1.x];
- user1.y++;
- if (map[user1.y, user1.x] == 'X') userscore += 10;
- map[user1.y, user1.x] = 'M';
- }
- if (map[user2.y - 1, user2.x] != '#')
- {
- map[user2.y, user2.x] = Map[user2.y, user2.x];
- user2.y--;
- if (map[user2.y, user2.x] == 'X') userscore += 10;
- map[user2.y, user2.x] = 'M';
- }
- rendering();
- break;
- case ConsoleKey.RightArrow:
- if (map[user1.y, user1.x + 1] != '#')
- {
- map[user1.y, user1.x] = Map[user1.y, user1.x];
- user1.x++;
- if (map[user1.y, user1.x] == 'X') userscore += 10;
- map[user1.y, user1.x] = 'M';
- }
- if (map[user2.y, user2.x - 1] != '#')
- {
- map[user2.y, user2.x] = Map[user2.y, user2.x];
- user2.x--;
- if (map[user2.y, user2.x] == 'X') userscore += 10;
- map[user2.y, user2.x] = 'M';
- }
- rendering();
- break;
- case ConsoleKey.LeftArrow:
- if (map[user1.y, user1.x - 1] != '#')
- {
- map[user1.y, user1.x] = Map[user1.y, user1.x];
- user1.x--;
- if (map[user1.y, user1.x] == 'X') userscore += 10;
- map[user1.y, user1.x] = 'M';
- }
- if (map[user2.y, user2.x + 1] != '#')
- {
- map[user2.y, user2.x] = Map[user2.y, user2.x];
- user2.x++;
- if (map[user2.y, user2.x] == 'X') userscore += 10;
- map[user2.y, user2.x] = 'M';
- }
- rendering();
- break;
- }
- }
- static position startuser1, startuser2;
- static int startscore;
- public static void newgame(int x1, int y1, int x2, int y2,int score)
- {
- user1.x = x1; user1.y = y1; startuser1.x = x1; startuser1.y = y1;
- user2.x = x2; user2.y = y2; startuser2.x = x2; startuser2.y = y2;
- userscore = score; startscore = score;
- start();
- }
- public static void start()
- {
- LVL = File.ReadAllLines("map.map");
- Map = new char[LVL.Length, LVL[0].Length];
- map = new char[LVL.Length, LVL[0].Length];
- for (int i = 0; i < LVL.Length; i++)
- for (int j = 0; j < LVL[i].Length; j++)
- {
- Map[i, j] = LVL[i][j];
- map[i, j] = Map[i, j];
- }
- end1.x = 4; end1.y = 3;
- end2.x = 9; end2.y = 3;
- map[user1.y, user1.x] = 'M';
- map[user2.y, user2.x] = 'M';
- rendering();
- play();
- }
- public static void play()
- {
- while (true)
- {
- userscore++;
- if (checkint == 1) { Console.BackgroundColor = ConsoleColor.DarkRed; checkint = 0; Console.WriteLine("Press to enter pleas");Console.ResetColor(); Console.ReadKey(); end(); break; }
- ConsoleKey button = Console.ReadKey().Key;
- if (button == ConsoleKey.Escape) break;
- if (button == ConsoleKey.F2)
- {
- Save.save(user1.x, user1.y, user2.x, user2.y,userscore);
- Console.Clear();
- Console.WriteLine("Игра сохарнена");
- Thread.Sleep(1000);
- rendering();
- }
- else if (button == ConsoleKey.F5) {newgame(startuser1.x, startuser1.y, startuser2.x, startuser2.y,startscore); break; }
- else Game.move(button);
- }
- }
- public static void end()
- {
- Console.Clear();
- Console.BackgroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("You are win");
- Console.ResetColor();
- Console.Write("Введите свое имя:");
- string line;
- line =Console.ReadLine();
- Console.WriteLine("line {0} {1}",line,userscore);
- line += ' ';
- line += Convert.ToString(userscore);
- using (var sw = new StreamWriter("Score.score",true))
- {
- sw.WriteLine(line);
- }
- Output.tableofrecords('r');
- }
- }
- struct rating
- {
- public int score;
- public string name;
- public int id;
- }
- class Output
- {
- public static int Compare(rating o1, rating o2)
- {
- return (o1.score).CompareTo(o2.score);
- }
- static string[] table;
- static List<rating> output = new List<rating>();
- private static int id;
- public static void tableofrecords(char c)
- {
- table= File.ReadAllLines("Score.score");
- for(int i=0;i<table.Length;i++)
- {
- string[] s = table[i].Split(' ');
- rating element;
- element.name = s[0];
- element.score = Convert.ToInt32(s[1]);
- element.id = i;
- output.Add(element);
- }
- if (output.Count() == 0) Console.WriteLine("Список рекордов пуст");
- else
- {
- if (c == 'r')
- id = output[output.Count - 1].id;
- output.Sort(Compare);
- int k = 0;
- Console.Clear();
- for (int i = 0; i < output.Count && i < 4; i++)
- {
- if (c == 'r')
- {
- if (output[i].id == id)
- {
- k++;
- Console.BackgroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
- Console.ResetColor();
- }
- else Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
- }
- else Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
- }
- if (c == 'r')
- {
- if (k == 1) { if (output.Count >= 5) Console.WriteLine("{0,2}.{1,-10}{2,4}", 5, output[4].name, output[4].score); }
- else
- {
- int i;
- for (i = 0; i < output.Count; i++)
- {
- if (output[i].id == id) break;
- }
- Console.BackgroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("{0,2}.{1,-10}{2,4}", i + 1, output[i].name, output[i].score);
- Console.ResetColor();
- }
- }
- else if (output.Count >= 5) Console.WriteLine("{0,2}.{1,-10}{2,4}", 5, output[4].name, output[4].score);
- }
- output.Clear();
- Console.ReadKey();
- }
- }
- class Save
- {
- public static void save(int x1, int y1, int x2, int y2,int score)
- {
- Convert.ToString(x1);
- string line = Convert.ToString(x1) + ' ' + Convert.ToString(y2) + ' ' + Convert.ToString(x2) + ' ' + Convert.ToString(y2) + ' ' + Convert.ToString(score);
- using (StreamWriter sw = new StreamWriter("Info.info"))
- {
- sw.WriteLine(line);
- }
- }
- public static void download()
- {
- string[] s,s1;
- s=File.ReadAllLines("Info.info");
- s1 = s[0].Split(' ');
- int x1 = Convert.ToInt32(s1[0]);
- int y1 = Convert.ToInt32(s1[1]);
- int x2 = Convert.ToInt32(s1[2]);
- int y2 = Convert.ToInt32(s1[3]);
- int score= Convert.ToInt32(s1[4]);
- Game.newgame(x1,y1,x2,y2,score);
- }
- }
- class Program
- {
- static void menu()
- {
- char switchcase = ' ';
- while (switchcase != 'd')
- {
- Console.Clear();
- Console.WriteLine("Главное меню: \n" +
- "a.Начать игру\n" +
- "b.Загрузка \n" +
- "c.Таблица рекордов \n" +
- "d.Выход");
- switchcase = Console.ReadKey().KeyChar;
- switch (switchcase)
- {
- case 'a':
- Game.newgame(1,3,12,3,0);
- break;
- case 'b':
- Save.download();
- break;
- case 'c':
- Output.tableofrecords('t');
- break;
- }
- }
- }
- static void Main()
- {
- Program.menu();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement