Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
В 2018 году такие статьи не могут вызвать большой интерес для широкой аудитории.
Неужели кто-то еще использует WinForms?
case Keys.A:
for (int i = 0; i < 4; i++)
shape[1, i]--;
if (FindMistake())
for (int i = 0; i < 4; i++)
shape[1, i]++;
break;
case Keys.D:
for (int i = 0; i < 4; i++)
shape[1, i]++;
if (FindMistake())
for (int i = 0; i < 4; i++)
shape[1, i]--;
break; case Keys.A: Keys.D:
for (int i = 0; i < 4; i++)
shape[1, i] = e.KeyCode == Keys.A ? shape[1, i]-1 : shape[1, i]+1;
if (FindMistake())
for (int i = 0; i < 4; i++)
shape[1, i] = e.KeyCode == Keys.A ? shape[1, i]+1 : shape[1, i]-1;
break;Как насчет
...
shape[1, i] += e.KeyCode == Keys.A ? -1 : 1;
...(from i in Enumerable.Range(0, field.GetLength(1)) where (Enumerable.Range(0, field.GetLength(0)).Select(j => field[j, i]).Sum() >= width - 1) select i).ToArray().Take(1)
...
public Graphics gr;
private PictureBox FieldPictureBox { get; }
private Timer TickTimer { get; }
[STAThread]
public static void Main() {
var frm = new Form1();
frm.ShowDialog();
}
private Form1() {
KeyDown += Form1_KeyDown;
FieldPictureBox = new PictureBox {Dock = DockStyle.Fill, Parent = this};
TickTimer = new Timer {Interval = 1000, Enabled = true};
TickTimer.Tick += TickTimer_Tick;
...
хотя есть и такие извращенцы, которые пишут linq запросы в несколько строчекНу зачем же вы так. LINQ в несколько строчек может быть вполне себе читаемым. Как раз тот случай, когда можно добавлять строки кода ради читаемости.
var result = source.Select(dto => dto.ToModel())
.Where(model => model.IsActive) // 2 where for a simpler debug
.Where(model => model.MatchesSecurityPolicy())
.GroupBy(model => model.businessGroupKey)
.ToDictionary(
group => groupKey,
group => group.Sum(elem => elem.Karma));
Можно и в 32 строчки вместиться:
using System;using System.Linq;using System.Drawing;using System.Windows.Forms;
public partial class Form1:Form{public const int width=15,height=25,k=15;public
int[,]shape=new int[2,4];public int[,]field=new int[width,height];public Bitmap
bitfield=new Bitmap(k*(width+1)+1,k*(height+3)+1);public Graphics gr;public
Form1(){InitializeComponent();gr=Graphics.FromImage(bitfield);for(int a=0;a<
width;a++)field[a,height-1]=1;for(int a=0;a<height;a++){field[0,a]=1;field[width
-1,a]=1;}SetShape();}public void FillField(){gr.Clear(Color.Black);for(int a=0;a
<width;a++)for(int b=0;b<height;b++)if(field[a,b]==1){gr.FillRectangle(Brushes.
Green,a*k,b*k,k,k);gr.DrawRectangle(Pens.Black,a*k,b*k,k,k);}for(int a=0;a<4;a++
){gr.FillRectangle(Brushes.Red,shape[1,a]*k,shape[0,a]*k,k,k);gr.DrawRectangle(
Pens.Black,shape[1,a]*k,shape[0,a]*k,k,k);}FieldPictureBox.Image=bitfield;}void
h(object a,System.EventArgs b){if(field[8,3]==1)Environment.Exit(0);for(int c=0;
c<4;c++)shape[0,c]++;for(int c=height-2;c>2;c--){int d=(from t in Enumerable.
Range(0,field.GetLength(0)).Select(e=>field[e,c]).ToArray()where t==1 select t).
Count();if(d==width)for(int f=c;f>1;f--)for(int g=1;g<width-1;g++)field[g,f]=
field[g,f-1];}if(FindMistake()){for(int c=0;c<4;c++)field[shape[1,c],--shape[0,c
]]++;SetShape();}FillField();}void i(object a,KeyEventArgs b){switch(b.KeyCode){
case Keys.A:for(int c=0;c<4;c++)shape[1,c]--;if(FindMistake())for(int c=0;c<4;c
++)shape[1,c]++;break;case Keys.D:for(int c=0;c<4;c++)shape[1,c]++;if(
FindMistake())for(int c=0;c<4;c++)shape[1,c]--;break;case Keys.W:var d=new int[2
,4];Array.Copy(shape,d,shape.Length);int e=0,f=0;for(int c=0;c<4;c++){if(shape[0
,c]>f)f=shape[0,c];if(shape[1,c]>e)e=shape[1,c];}for(int c=0;c<4;c++){int g=
shape[0,c];shape[0,c]=f-(e-shape[1,c])-1;shape[1,c]=e-(3-(f-g))+1;}if(
FindMistake())Array.Copy(d,shape,shape.Length);break;}}public void SetShape(){
var a=new Random(DateTime.Now.Millisecond);switch(a.Next(7)){case 0:shape=new
int[,]{{2,3,4,5},{8,8,8,8}};break;case 1:shape=new int[,]{{2,3,2,3},{8,8,9,9}};
break;case 2:shape=new int[,]{{2,3,4,4},{8,8,8,9}};break;case 3:shape=new int[,]
{{2,3,4,4},{8,8,8,7}};break;case 4:shape=new int[,]{{3,3,4,4},{7,8,8,9}};break;
case 5:shape=new int[,]{{3,3,4,4},{9,8,8,7}};break;case 6:shape=new int[,]{{3,4,
4,4},{8,7,8,9}};break;}}public bool FindMistake(){for(int a=0;a<4;a++)if(shape[1
,a]>=width||shape[0,a]>=height||shape[1,a]<=0||shape[0,a]<=0||field[shape[1,a],
shape[0,a]]==1)return true;return false;}}var cross = Enumerable.Range(0, field.GetLength(0)).Count(j => field[j, i] == 1);
Тетрис на C# в 100 строк