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.Diagnostics;
using System.Media;
using static System.Console;
using System.Reflection;
using static System.Console;
//┌─┐
//│ │
@ -73,113 +73,122 @@ namespace testexetrisathlon
{
bool playing = true;
GameState state = GameState.menu;
while (playing)
try
{
switch (state)
while (playing)
{
case GameState.menu:
Clear();
gameOver.Stop();
intro.PlayLooping();
SetCursorPosition(0, 1);
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" +
switch (state)
{
case GameState.menu:
Clear();
gameOver.Stop();
intro.PlayLooping();
SetCursorPosition(0, 1);
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" +
" testexetrisathlon v." + assembly.GetName().Version.ToString());
SetCursorPosition(10, 18);
WriteLine("Controls: Space");
SetCursorPosition(11, 19);
WriteLine("Up, Down, Right");
SetCursorPosition(11, 20);
WriteLine("Left");
SetCursorPosition(10, 22);
WriteLine("Press s to start");
SetCursorPosition(10, 23);
WriteLine("Press x to exit");
SetCursorPosition(0, 26);
WriteLine("Icon made by Freepik from www.flaticon.com");
string tmp = ReadKey(true).KeyChar.ToString().ToLower();
switch (tmp)
{
case "s":
intro.Stop();
state = GameState.game;
" testexetrisathlon v." + assembly.GetName().Version.ToString());
SetCursorPosition(10, 18);
WriteLine("Controls: Space");
SetCursorPosition(11, 19);
WriteLine("Up, Down, Right");
SetCursorPosition(11, 20);
WriteLine("Left");
SetCursorPosition(10, 22);
WriteLine("Press s to start");
SetCursorPosition(10, 23);
WriteLine("Press x to exit");
SetCursorPosition(0, 26);
WriteLine("Icon made by Freepik from www.flaticon.com");
string tmp = ReadKey(true).KeyChar.ToString().ToLower();
switch (tmp)
{
case "s":
intro.Stop();
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();
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"))
{
Draw();
SetCursorPosition(0, 0);
WriteLine("┌───────────────────┐");
WriteLine("│ Game Over │");
WriteLine("│ Replay? (Y/N) │");
WriteLine("├───────────────────┤");
input = ReadKey().KeyChar.ToString().ToLower();
}
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();
Draw();
SetCursorPosition(0, 0);
WriteLine("┌───────────────────┐");
WriteLine("│ Game Over │");
WriteLine("│ Replay? (Y/N) │");
WriteLine("├───────────────────┤");
input = ReadKey().KeyChar.ToString().ToLower();
}
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;
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()
{
@ -253,9 +262,9 @@ namespace testexetrisathlon
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;
dropRate = 300 - 22 * level;
dropRate = 300 - (22 * level);
}
private static void Input()
{
@ -278,7 +287,7 @@ namespace testexetrisathlon
if (key.Key == ConsoleKey.DownArrow & isKeyPressed)
tet.Drop();
if (key.Key == ConsoleKey.UpArrow & isKeyPressed)
for (; tet.isSomethingBelow() != true;)
for (; tet.isSomethingBelow!= true;)
tet.Drop();
if (key.Key == ConsoleKey.Spacebar & isKeyPressed)
{
@ -292,10 +301,10 @@ namespace testexetrisathlon
{
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)
{
SetCursorPosition(1 + 2 * j, i);
SetCursorPosition((2 * j) + 1, i);
Write(sqr);
}
else

View File

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

View File

@ -1,3 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<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>
<RootNamespace>testexetrisathlon</RootNamespace>
<AssemblyName>testexetrisathlon</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">