Functions are now way smaller thanks to LINQ

This commit is contained in:
CreepyCrafter24 2019-11-10 15:56:18 +01:00
parent b1badbd938
commit e65562f91a
4 changed files with 150 additions and 264 deletions

View File

@ -2,8 +2,8 @@
using System.Linq; using System.Linq;
using System.Diagnostics; using System.Diagnostics;
using System.Media; using System.Media;
using static System.Console;
using System.Reflection; using System.Reflection;
using static System.Console;
//┌─┐ //┌─┐
//│ │ //│ │
@ -73,113 +73,122 @@ namespace testexetrisathlon
{ {
bool playing = true; bool playing = true;
GameState state = GameState.menu; GameState state = GameState.menu;
while (playing) try
{ {
switch (state) while (playing)
{ {
case GameState.menu: switch (state)
Clear(); {
gameOver.Stop(); case GameState.menu:
intro.PlayLooping(); Clear();
SetCursorPosition(0, 1); gameOver.Stop();
Write( intro.PlayLooping();
" ▀▀▀██████▄▄▄\r\n" + SetCursorPosition(0, 1);
" ▀▀▀████▄\r\n" + Write(
" ▄███████▀ ▀███▄\r\n" + " ▀▀▀██████▄▄▄\r\n" +
" ▄███████▀ ▀███▄\r\n" + " ▀▀▀████▄\r\n" +
" ▄████████ ███▄\r\n" + " ▄███████▀ ▀███▄\r\n" +
" ██████████▄ ███▌\r\n" + " ▄███████▀ ▀███▄\r\n" +
" ▀█████▀ ▀███▄ ▐███\r\n" + " ▄████████ ███▄\r\n" +
" ▀█▀ ▀███▄ ▐███\r\n" + " ██████████▄ ███▌\r\n" +
" ▀███▄ ███▌\r\n" + " ▀█████▀ ▀███▄ ▐███\r\n" +
" ▄██▄ ▀███▄ ▐███\r\n" + " ▀█▀ ▀███▄ ▐███\r\n" +
" ▄██████▄ ▀███▄███\r\n" + " ▀███▄ ███▌\r\n" +
" █████▀▀████▄▄ ▄█████\r\n" + " ▄██▄ ▀███▄ ▐███\r\n" +
" ████▀ ▀▀█████▄▄▄▄█████████▄\r\n" + " ▄██████▄ ▀███▄███\r\n" +
" ▀▀ ▀▀██████▀▀ ▀▀██\r\n\r\n" + " █████▀▀████▄▄ ▄█████\r\n" +
" ████▀ ▀▀█████▄▄▄▄█████████▄\r\n" +
" ▀▀ ▀▀██████▀▀ ▀▀██\r\n\r\n" +
" testexetrisathlon v." + assembly.GetName().Version.ToString()); " testexetrisathlon v." + assembly.GetName().Version.ToString());
SetCursorPosition(10, 18); SetCursorPosition(10, 18);
WriteLine("Controls: Space"); WriteLine("Controls: Space");
SetCursorPosition(11, 19); SetCursorPosition(11, 19);
WriteLine("Up, Down, Right"); WriteLine("Up, Down, Right");
SetCursorPosition(11, 20); SetCursorPosition(11, 20);
WriteLine("Left"); WriteLine("Left");
SetCursorPosition(10, 22); SetCursorPosition(10, 22);
WriteLine("Press s to start"); WriteLine("Press s to start");
SetCursorPosition(10, 23); SetCursorPosition(10, 23);
WriteLine("Press x to exit"); WriteLine("Press x to exit");
SetCursorPosition(0, 26); SetCursorPosition(0, 26);
WriteLine("Icon made by Freepik from www.flaticon.com"); WriteLine("Icon made by Freepik from www.flaticon.com");
string tmp = ReadKey(true).KeyChar.ToString().ToLower(); string tmp = ReadKey(true).KeyChar.ToString().ToLower();
switch (tmp) switch (tmp)
{ {
case "s": case "s":
intro.Stop(); intro.Stop();
state = GameState.game; state = GameState.game;
Clear();
DrawBorder();
break;
case "x":
state = GameState.exit;
break;
}
break;
case GameState.game:
inGame.PlayLooping();
dropTimer.Start();
SetCursorPosition(25, 0);
WriteLine("Level " + level);
SetCursorPosition(25, 1);
WriteLine("Score " + score);
SetCursorPosition(25, 2);
WriteLine("LinesCleared " + linesCleared);
nexttet = new Tetrominoe();
tet = nexttet;
tet.Spawn();
nexttet = new Tetrominoe();
Update();
inGame.Stop();
state = GameState.gameOver;
break;
case GameState.gameOver:
gameOver.PlayLooping();
string input = "";
while ((input != "y") && (input != "n"))
{
Clear(); Clear();
DrawBorder(); DrawBorder();
break; Draw();
case "x": SetCursorPosition(0, 0);
state = GameState.exit; WriteLine("┌───────────────────┐");
break; WriteLine("│ Game Over │");
} WriteLine("│ Replay? (Y/N) │");
break; WriteLine("├───────────────────┤");
case GameState.game: input = ReadKey().KeyChar.ToString().ToLower();
inGame.PlayLooping(); }
dropTimer.Start(); grid = new int[23, 10];
SetCursorPosition(25, 0); droppedtetrominoeLocationGrid = new int[23, 10];
WriteLine("Level " + level); dropTimer = new Stopwatch();
SetCursorPosition(25, 1); inputTimer = new Stopwatch();
WriteLine("Score " + score); dropRate = 300;
SetCursorPosition(25, 2); isDropped = false;
WriteLine("LinesCleared " + linesCleared); isKeyPressed = false;
nexttet = new Tetrominoe(); linesCleared = 0;
tet = nexttet; score = 0;
tet.Spawn(); level = 1;
nexttet = new Tetrominoe(); GC.Collect();
Update();
inGame.Stop();
state = GameState.gameOver;
break;
case GameState.gameOver:
gameOver.PlayLooping();
string input = "";
while ((input != "y") && (input != "n"))
{
Clear(); Clear();
DrawBorder(); DrawBorder();
Draw(); if (input == "y")
SetCursorPosition(0, 0); state = GameState.game;
WriteLine("┌───────────────────┐"); else
WriteLine("│ Game Over │"); state = GameState.menu;
WriteLine("│ Replay? (Y/N) │"); break;
WriteLine("├───────────────────┤"); default:
input = ReadKey().KeyChar.ToString().ToLower(); playing = false;
} break;
grid = new int[23, 10]; }
droppedtetrominoeLocationGrid = new int[23, 10];
dropTimer = new Stopwatch();
inputTimer = new Stopwatch();
dropRate = 300;
isDropped = false;
isKeyPressed = false;
linesCleared = 0;
score = 0;
level = 1;
GC.Collect();
Clear();
DrawBorder();
if (input == "y")
state = GameState.game;
else
state = GameState.menu;
break;
default:
playing = false;
break;
} }
} }
finally
{
intro.Dispose();
inGame.Dispose();
gameOver.Dispose();
}
} }
private static void Update() private static void Update()
{ {
@ -253,9 +262,9 @@ namespace testexetrisathlon
Draw(); Draw();
} }
} }
score += (int)Math.Round(Math.Sqrt(Math.Max(combo * 50 - 50, 0)) * 5); score += (int)Math.Round(Math.Sqrt(Math.Max((combo * 50) - 50, 0)) * 5);
level = (int)Math.Round(Math.Sqrt(score * 0.01)) + 1; level = (int)Math.Round(Math.Sqrt(score * 0.01)) + 1;
dropRate = 300 - 22 * level; dropRate = 300 - (22 * level);
} }
private static void Input() private static void Input()
{ {
@ -278,7 +287,7 @@ namespace testexetrisathlon
if (key.Key == ConsoleKey.DownArrow & isKeyPressed) if (key.Key == ConsoleKey.DownArrow & isKeyPressed)
tet.Drop(); tet.Drop();
if (key.Key == ConsoleKey.UpArrow & isKeyPressed) if (key.Key == ConsoleKey.UpArrow & isKeyPressed)
for (; tet.isSomethingBelow() != true;) for (; tet.isSomethingBelow!= true;)
tet.Drop(); tet.Drop();
if (key.Key == ConsoleKey.Spacebar & isKeyPressed) if (key.Key == ConsoleKey.Spacebar & isKeyPressed)
{ {
@ -292,10 +301,10 @@ namespace testexetrisathlon
{ {
for (int j = 0; j < 10; j++) for (int j = 0; j < 10; j++)
{ {
SetCursorPosition(1 + 2 * j, i); SetCursorPosition((2 * j) + 1, i);
if (grid[i, j] == 1 | droppedtetrominoeLocationGrid[i, j] == 1) if (grid[i, j] == 1 | droppedtetrominoeLocationGrid[i, j] == 1)
{ {
SetCursorPosition(1 + 2 * j, i); SetCursorPosition((2 * j) + 1, i);
Write(sqr); Write(sqr);
} }
else else

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security;
using static System.Console; using static System.Console;
#pragma warning disable IDE1006 #pragma warning disable IDE1006
namespace testexetrisathlon namespace testexetrisathlon
@ -32,11 +33,11 @@ namespace testexetrisathlon
public Tetrominoe() public Tetrominoe()
{ {
shape = tetrominoes[Program.rnd.Next(0, tetrominoes.Count)]; shape = tetrominoes[Program.rnd.Next(0, tetrominoes.Count)];
for (int i = 23; i < 33; ++i) for (int i = 0; i < 10; i++)
{ {
for (int j = 3; j < 10; j++) for (int j = 0; j < 7; j++)
{ {
SetCursorPosition(i, j); SetCursorPosition(i + 23, j + 3);
Write(" "); Write(" ");
} }
} }
@ -47,7 +48,7 @@ namespace testexetrisathlon
{ {
if (shape[i, j] == 1) if (shape[i, j] == 1)
{ {
SetCursorPosition(((10 - shape.GetLength(1)) / 2 + j) * 2 + 20, i + 5); SetCursorPosition(30 - shape.GetLength(1) + (2 * j), i + 5);
Write(Program.sqr); Write(Program.sqr);
} }
} }
@ -61,7 +62,7 @@ namespace testexetrisathlon
{ {
if (shape[i, j] == 1) if (shape[i, j] == 1)
{ {
location.Add(new int[] { i, (10 - shape.GetLength(1)) / 2 + j }); location.Add(new int[] { i, 5 - (shape.GetLength(1) / 2) + j });
} }
} }
} }
@ -69,21 +70,15 @@ namespace testexetrisathlon
} }
public void Drop() public void Drop()
{ {
if (isSomethingBelow()) if (isSomethingBelow)
{ {
for (int i = 0; i < 4; i++) location.ForEach(s => Program.droppedtetrominoeLocationGrid[s[0], s[1]] = 1);
{
Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] = 1;
}
Program.isDropped = true; Program.isDropped = true;
Beep(800, 200); Beep(800, 200);
} }
else else
{ {
for (int numCount = 0; numCount < 4; numCount++) location.ForEach(s => s[0]++);
{
location[numCount][0] += 1;
}
Update(); Update();
} }
} }
@ -96,77 +91,33 @@ namespace testexetrisathlon
{ {
if (shape[i, j] == 1) if (shape[i, j] == 1)
{ {
templocation.Add(new int[] { i, (10 - shape.GetLength(1)) / 2 + j }); templocation.Add(new int[] { i, ((10 - shape.GetLength(1)) / 2) + j });
} }
} }
} }
if (shape == tetrominoes[0]) if (shape == tetrominoes[1])
{
for (int i = 0; i < location.Count; i++)
{
templocation[i] = TransformMatrix(location[i], location[2]);
}
}
else if (shape == tetrominoes[3])
{
for (int i = 0; i < location.Count; i++)
{
templocation[i] = TransformMatrix(location[i], location[3]);
}
}
else if (shape == tetrominoes[1])
return; return;
else for (int i = 0; i < location.Count; i++)
{ templocation[i] = TransformMatrix(location[i], location[(shape == tetrominoes[3]) ? 3 : 2]);
for (int i = 0; i < location.Count; i++)
{
templocation[i] = TransformMatrix(location[i], location[2]);
}
}
for (int count = 0; isOverlayLeft(templocation) != false | isOverlayRight(templocation) != false | isOverlayBelow(templocation) != false; count++) for (int count = 0; isOverlayLeft(templocation) != false | isOverlayRight(templocation) != false | isOverlayBelow(templocation) != false; count++)
{ {
if (isOverlayLeft(templocation) == true) if (isOverlayLeft(templocation) == true)
{
for (int i = 0; i < location.Count; i++) for (int i = 0; i < location.Count; i++)
{ templocation[i][1]++;
templocation[i][1] += 1;
}
}
if (isOverlayRight(templocation) == true) if (isOverlayRight(templocation) == true)
{
for (int i = 0; i < location.Count; i++) for (int i = 0; i < location.Count; i++)
{ templocation[i][1]--;
templocation[i][1] -= 1;
}
}
if (isOverlayBelow(templocation) == true) if (isOverlayBelow(templocation) == true)
{
for (int i = 0; i < location.Count; i++) for (int i = 0; i < location.Count; i++)
{ templocation[i][0]--;
templocation[i][0] -= 1;
}
}
if (count == 3) if (count == 3)
{
return; return;
}
} }
location = templocation; location = templocation;
} }
public bool notFalse(bool? inp) => (inp ?? true); public static int[] TransformMatrix(int[] coord, int[] axis) => new int[] { axis[0] - axis[1] + coord[1], axis[0] + axis[1] - coord[0] };
public int[] TransformMatrix(int[] coord, int[] axis) => new int[] { axis[0] - axis[1] + coord[1], axis[0] + axis[1] - coord[0] }; public bool isSomethingBelow => location.Where(s => s[0] + 1 >= 23 || s[0] + 1 < 23 & Program.droppedtetrominoeLocationGrid[s[0] + 1, s[1]] == 1).Count() > 0;
public bool isSomethingBelow() public static bool? isOverlayBelow(List<int[]> location)
{
for (int i = 0; i < 4; i++)
{
if (location[i][0] + 1 >= 23)
return true;
if (location[i][0] + 1 < 23 & Program.droppedtetrominoeLocationGrid[location[i][0] + 1, location[i][1]] == 1)
return true;
}
return false;
}
public bool? isOverlayBelow(List<int[]> location)
{ {
List<int> ycoords = new List<int>(); List<int> ycoords = new List<int>();
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
@ -177,42 +128,17 @@ namespace testexetrisathlon
if (location[i][0] < 0 | location[i][1] < 0 | location[i][1] > 9) if (location[i][0] < 0 | location[i][1] < 0 | location[i][1] > 9)
return null; return null;
} }
for (int i = 0; i < 4; i++) return location.Where(s => (ycoords.Max() - ycoords.Min() == 3) ?
{ ((ycoords.Max() == s[0] | ycoords.Max() - 1 == s[0]) & (Program.droppedtetrominoeLocationGrid[s[0], s[1]] == 1)) :
if (ycoords.Max() - ycoords.Min() == 3) ((ycoords.Max() == s[0]) & (Program.droppedtetrominoeLocationGrid[s[0], s[1]] == 1))).Count() > 0;
{
if ((ycoords.Max() == location[i][0] | ycoords.Max() - 1 == location[i][0]) & (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1))
{
return true;
}
}
else
{
if ((ycoords.Max() == location[i][0]) & (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1))
{
return true;
}
}
}
return false;
} }
public bool isSomethingLeft() public bool isSomethingLeft() => location.Where(s => s[1] == 0 || Program.droppedtetrominoeLocationGrid[s[0], s[1] - 1] == 1).Count() > 0;
{ public static bool? isOverlayLeft(List<int[]> location)
for (int i = 0; i < 4; i++)
{
if (location[i][1] == 0)
return true;
else if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1] - 1] == 1)
return true;
}
return false;
}
public bool? isOverlayLeft(List<int[]> location)
{ {
List<int> xcoords = new List<int>(); List<int> xcoords = new List<int>();
xcoords.AddRange(location.Select(s => s[1]));
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
xcoords.Add(location[i][1]);
if (location[i][1] < 0) if (location[i][1] < 0)
return true; return true;
if (location[i][1] > 9) if (location[i][1] > 9)
@ -220,48 +146,17 @@ namespace testexetrisathlon
if (location[i][0] >= 23 | location[i][0] < 0) if (location[i][0] >= 23 | location[i][0] < 0)
return null; return null;
} }
for (int i = 0; i < 4; i++) return location.Where(s => (xcoords.Max() - xcoords.Min() == 3) ?
{ (xcoords.Min() == s[1] | xcoords.Min() + 1 == s[1] && Program.droppedtetrominoeLocationGrid[s[0], s[1]] == 1) :
if (xcoords.Max() - xcoords.Min() == 3) (xcoords.Min() == s[1] && Program.droppedtetrominoeLocationGrid[s[0], s[1]] == 1)).Count() > 0;
{
if (xcoords.Min() == location[i][1] | xcoords.Min() + 1 == location[i][1])
{
if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)
{
return true;
}
}
}
else
{
if (xcoords.Min() == location[i][1])
{
if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)
{
return true;
}
}
}
}
return false;
} }
public bool isSomethingRight() public bool isSomethingRight() => location.Where(s => s[1] == 9 || Program.droppedtetrominoeLocationGrid[s[0], s[1] + 1] == 1).Count() > 0;
{ public static bool? isOverlayRight(List<int[]> location)
for (int i = 0; i < 4; i++)
{
if (location[i][1] == 9)
return true;
else if (Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1] + 1] == 1)
return true;
}
return false;
}
public bool? isOverlayRight(List<int[]> location)
{ {
List<int> xcoords = new List<int>(); List<int> xcoords = new List<int>();
xcoords.AddRange(location.Select(s => s[1]));
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
xcoords.Add(location[i][1]);
if (location[i][1] > 9) if (location[i][1] > 9)
return true; return true;
if (location[i][1] < 0) if (location[i][1] < 0)
@ -269,24 +164,9 @@ namespace testexetrisathlon
if (location[i][0] >= 23 | location[i][0] < 0) if (location[i][0] >= 23 | location[i][0] < 0)
return null; return null;
} }
for (int i = 0; i < 4; i++) return location.Where(s => (xcoords.Max() - xcoords.Min() == 3) ?
{ ((xcoords.Max() == s[1] | xcoords.Max() - 1 == s[1]) & Program.droppedtetrominoeLocationGrid[s[0], s[1]] == 1) :
if (xcoords.Max() - xcoords.Min() == 3) (xcoords.Max() == s[1] & Program.droppedtetrominoeLocationGrid[s[0], s[1]] == 1)).Count() > 0;
{
if ((xcoords.Max() == location[i][1] | xcoords.Max() - 1 == location[i][1]) & Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)
{
return true;
}
}
else
{
if (xcoords.Max() == location[i][1] & Program.droppedtetrominoeLocationGrid[location[i][0], location[i][1]] == 1)
{
return true;
}
}
}
return false;
} }
public void Update() public void Update()
{ {
@ -297,10 +177,7 @@ namespace testexetrisathlon
Program.grid[i, j] = 0; Program.grid[i, j] = 0;
} }
} }
for (int i = 0; i < 4; i++) location.ForEach(s => Program.grid[s[0], s[1]] = 1);
{
Program.grid[location[i][0], location[i][1]] = 1;
}
Program.Draw(); Program.Draw();
} }
} }

View File

@ -1,3 +1,3 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>

View File

@ -7,7 +7,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<RootNamespace>testexetrisathlon</RootNamespace> <RootNamespace>testexetrisathlon</RootNamespace>
<AssemblyName>testexetrisathlon</AssemblyName> <AssemblyName>testexetrisathlon</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">