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;
- namespace lab1_2048
- {
- class Program
- {
- static void Main(string[] args)
- {
- while (true)
- {
- PlayingField GameField = new PlayingField();
- while (!PlayingField.EndOfThisGame)
- {
- Underground.logic(Underground.keyboard(GameField.Field), GameField.Field, PlayingField.LineWidth);
- }
- Console.Write("Вы {0},хотите сыграть ещё?(да/нет)", PlayingField.WordOfEnd);
- if (Console.ReadLine() == "да")
- PlayingField.EndOfThisGame = false;
- else
- break;
- }
- Console.Clear();
- }
- }
- class PlayingField //добавить в конструктор метод вывода поля
- {
- public const int LineWidth = 4;
- static public string WordOfEnd ;
- static public bool EndOfThisGame = false;
- public int[,] Field = new int[LineWidth, LineWidth];
- public PlayingField()
- {
- Random rand = new Random();
- Field[rand.Next(0, 2), rand.Next(0, 4)] = rand.Next(0, 10) == 9 ? 4 : 2;
- Field[rand.Next(2, 4), rand.Next(0, 4)] = rand.Next(0, 10) == 9 ? 4 : 2;
- WordOfEnd = "проиграли";
- }
- }
- class Underground
- {
- static public void ViewField(int[,] Field)
- {
- string copy0 = "|---------------|";
- string copy1 = "\n\t\t\t\t";
- Console.Write("\n\n\n\n\n\n"+copy1); Console.BackgroundColor = ConsoleColor.Red; Console.Write(copy0); Console.ResetColor();
- for (int i = 0; i < PlayingField.LineWidth; i++)
- {
- Console.Write(copy1); Console.BackgroundColor = ConsoleColor.Red;
- for (int j = 0; j < PlayingField.LineWidth; j++)
- {
- string word;
- if (Field[i, j] == 0)
- word = " ";
- else
- word =Convert.ToString( Field[i, j]);
- if(Field[i,j]<9){
- Console.Write("| {0} " , word);
- }
- else if (Field[i, j] > 9 && Field[i, j] < 100) {
- Console.Write("| {0}", word);
- }
- else
- Console.Write("|{0}", word);
- }
- Console.Write("|"); Console.ResetColor();
- Console.Write(copy1); Console.BackgroundColor = ConsoleColor.Red; Console.Write(copy0); Console.ResetColor();
- }
- Console.Write(copy1);
- }
- static public int keyboard( int[,] Field)//вывод игрового поля и чтение клавиши
- {
- while (true)
- { ViewField(Field);
- ConsoleKeyInfo key = Console.ReadKey();
- Console.Clear();
- if ( key.Key == ConsoleKey.W || key.Key == ConsoleKey.UpArrow)
- return 1;
- else if (key.Key == ConsoleKey.S || key.Key == ConsoleKey.DownArrow)
- return 2;
- else if (key.Key == ConsoleKey.A || key.Key == ConsoleKey.LeftArrow)
- return 3;
- else if (key.Key == ConsoleKey.D || key.Key == ConsoleKey.RightArrow)
- return 4;
- }
- }
- static public void logic(int num,int[,] field,int n)// доделать передачу по ссылке
- {
- switch (num)
- {
- case 1:// up
- for (int i = 1; i < n; i++)
- {
- for (int j = 1; j < n; j++)
- {
- }
- }
- break;
- case 2:// down
- for (int i = 1; i < n; i++)
- {
- for (int j = 1; j< n; j++)
- {
- }
- }
- break;
- case 3:// left
- for (int c = 0; c < n * 2; c++)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 1; j < n; j++)
- {
- if ((field[i, j - 1] == field[i, j]) ^ (field[i, j - 1] == 0 && field[i, j] != 0))
- {
- field[i, j - 1] += field[i, j];
- field[i, j] = 0;
- }
- }
- }
- }
- break;
- case 4:// right
- for (int c = 0; c < n * 2; c++)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = n-1; j >0; j--)
- {
- if ((field[i, j - 1] == field[i, j]) ^ (field[i, j - 1] != 0 && field[i, j] == 0))
- {
- field[i, j ] += field[i, j-1];
- field[i, j-1] = 0;
- }
- }
- }
- }
- break;
- default:
- Console.WriteLine("хз как но вышла ошибка");
- break;
- }
- Add(field,n);
- }
- static public void Add(int[,] Field,int n)//доделать
- {
- int numer = 0;
- Random rand = new Random();
- for(int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- if (!(Field[i, j] == 0))
- numer++;
- if ( Field[i,j]==2048)
- {
- PlayingField.WordOfEnd = "выйграли";
- PlayingField.EndOfThisGame = true;
- return;
- }
- }
- }
- if (numer == 15)
- {
- PlayingField.EndOfThisGame = true;
- return;
- }
- Console.WriteLine(n);
- while (true)
- {
- int i = rand.Next(0, 4), j = i = rand.Next(0, 4);
- if (Field[i, j] == 0)
- {
- Field[i, j] = rand.Next(0, 10) == 9 ? 4 : 2;
- return;
- }
- }
- }
- }
- }
- /*тест вывода
- * Random rand = new Random();
- int[,] f = new int[4, 4];
- for(int o = 0; o < 4; o++)
- {
- for (int i = 0; i < 4; i++)
- f[o, i] = rand.Next(0, 5);
- }
- Underground.ViewField(f);
- Console.ReadKey();
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement