consolidate all repos to one for archive
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TetrisRPS;
|
||||
|
||||
public abstract class Block
|
||||
{
|
||||
protected abstract Position[][] Tiles { get; }
|
||||
|
||||
public abstract Position StartOffset { get; }
|
||||
|
||||
public abstract int Id { get; }
|
||||
|
||||
private int rotationState;
|
||||
private Position offset;
|
||||
|
||||
public Block()
|
||||
{
|
||||
offset = new Position(StartOffset.Row, StartOffset.Column);
|
||||
}
|
||||
|
||||
public IEnumerable<Position> TilePositions()
|
||||
{
|
||||
foreach (var tile in Tiles[rotationState])
|
||||
{
|
||||
yield return new Position(tile.Row + offset.Row, tile.Column + offset.Column);
|
||||
}
|
||||
}
|
||||
|
||||
public void RotateCW()
|
||||
{
|
||||
rotationState = (rotationState + 1) % Tiles.Length;
|
||||
}
|
||||
public void RotateCCW()
|
||||
{
|
||||
if (rotationState == 0)
|
||||
{
|
||||
rotationState = Tiles.Length - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
rotationState--;
|
||||
}
|
||||
}
|
||||
//Move by x rows and y columns.
|
||||
public void Move(int row, int column)
|
||||
{
|
||||
offset.Row += row;
|
||||
offset.Column += column;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
rotationState = 0;
|
||||
offset.Row = StartOffset.Row;
|
||||
offset.Column = StartOffset.Column;
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Microsoft.VisualBasic;
|
||||
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class BlockQueue
|
||||
{
|
||||
private readonly Block[] blocks = new Block[]
|
||||
{
|
||||
new IBlock(),
|
||||
new JBlock(),
|
||||
new LBlock(),
|
||||
new OBlock(),
|
||||
new SBlock(),
|
||||
new TBlock(),
|
||||
new ZBlock()
|
||||
};
|
||||
private readonly Random random = new();
|
||||
|
||||
public Block NextBlock { get; private set; }
|
||||
int curBlockID = 0;
|
||||
public BlockQueue()
|
||||
{
|
||||
NextBlock = RandomBlock();
|
||||
}
|
||||
private Block RandomBlock()
|
||||
{
|
||||
curBlockID++;
|
||||
curBlockID %= blocks.Length;
|
||||
return blocks[curBlockID].GetType().GetConstructor(Type.EmptyTypes).Invoke(null) as Block;
|
||||
}
|
||||
|
||||
public Block GetAndReplaceNextBlock()
|
||||
{
|
||||
Block block = NextBlock;
|
||||
do
|
||||
{
|
||||
NextBlock = RandomBlock();
|
||||
} while (block.Id == NextBlock.Id);
|
||||
return block;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,101 @@
|
||||
namespace TetrisRPS
|
||||
{
|
||||
public class GameGrid
|
||||
{
|
||||
private readonly int[,] grid;
|
||||
public int Rows { get; }
|
||||
public int Columns { get; }
|
||||
|
||||
// Easy access to the grid values.
|
||||
public int this[int row, int column]
|
||||
{
|
||||
get => grid[row, column];
|
||||
set => grid[row, column] = value;
|
||||
}
|
||||
|
||||
//So it could be made for differently sized grids.
|
||||
public GameGrid(int rows, int columns)
|
||||
{
|
||||
Rows = rows;
|
||||
Columns = columns;
|
||||
grid = new int[rows, columns];
|
||||
}
|
||||
|
||||
public bool IsInsideGrid(int row, int column)
|
||||
{
|
||||
return row >= 0 && row < Rows && column >= 0 && column < Columns;
|
||||
}
|
||||
|
||||
// Must be inside array and the value must be 0. -> all empty values are 0.
|
||||
public bool IsEmpty(int row, int column)
|
||||
{
|
||||
return IsInsideGrid(row, column) && grid[row, column] == 0;
|
||||
}
|
||||
|
||||
//Goes trough the entire row, if it isn't all zeroes it returns false.
|
||||
public bool IsRowFull(int row)
|
||||
{
|
||||
for (int column = 0; column < Columns; column++)
|
||||
{
|
||||
if (grid[row, column] == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//Goes trough the entire row, if it isn't all zeroes it returns false.
|
||||
public bool IsRowEmpty(int row)
|
||||
{
|
||||
for (int column = 0; column < Columns; column++)
|
||||
{
|
||||
if (grid[row, column] != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//Set entire row to 0.
|
||||
private void ClearRow(int row)
|
||||
{
|
||||
for (int column = 0; column < Columns; column++)
|
||||
{
|
||||
grid[row, column] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void MoveRowDown(int row, int numOfRows)
|
||||
{
|
||||
for (int column = 0; column < Columns; column++)
|
||||
{
|
||||
grid[row + numOfRows, column] = grid[row, column];
|
||||
grid[row, column] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int ClearFullRows()
|
||||
{
|
||||
int clearedRows = 0;
|
||||
for (int row = Rows - 1; row >= 0; row--)
|
||||
{
|
||||
if (IsRowFull(row))
|
||||
{
|
||||
ClearRow(row);
|
||||
clearedRows++;
|
||||
}
|
||||
else if (clearedRows > 0)
|
||||
{
|
||||
MoveRowDown(row, clearedRows);
|
||||
}
|
||||
}
|
||||
|
||||
return clearedRows;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,189 @@
|
||||
using System.ComponentModel.Design;
|
||||
using System.Windows.Ink;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class GameState
|
||||
{
|
||||
public Block currentBlock { get; set; }
|
||||
|
||||
public Block CurrentBlock
|
||||
{
|
||||
get => currentBlock;
|
||||
private set
|
||||
{
|
||||
currentBlock = value;
|
||||
currentBlock.Reset();
|
||||
}
|
||||
}
|
||||
public GameGrid GameGrid { get; }
|
||||
public BlockQueue BlockQueue { get; }
|
||||
|
||||
public bool IsGameOver { get; private set; }
|
||||
public Block HeldBlock { get; private set; }
|
||||
public bool CanHold { get; private set; }
|
||||
|
||||
public GameState()
|
||||
{
|
||||
GameGrid = new GameGrid(22, 10);
|
||||
BlockQueue = new BlockQueue();
|
||||
CurrentBlock = BlockQueue.GetAndReplaceNextBlock();
|
||||
CanHold = true;
|
||||
}
|
||||
// Holding the block
|
||||
public void HoldBlock()
|
||||
{
|
||||
if (!CanHold)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (HeldBlock == null)
|
||||
{
|
||||
HeldBlock = CurrentBlock;
|
||||
CurrentBlock = BlockQueue.GetAndReplaceNextBlock();
|
||||
}
|
||||
else
|
||||
{
|
||||
Block tmp = CurrentBlock;
|
||||
CurrentBlock = HeldBlock;
|
||||
HeldBlock = tmp;
|
||||
}
|
||||
CanHold = false;
|
||||
}
|
||||
|
||||
|
||||
// If any block is out of bounds, it will return true.
|
||||
private bool IsBlockOutOfBounds()
|
||||
{
|
||||
foreach (Position position in CurrentBlock.TilePositions())
|
||||
{
|
||||
if (!GameGrid.IsEmpty(position.Row, position.Column))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// If the block is out of bounds, it will rotate it back.
|
||||
private void RotateBlockCW()
|
||||
{
|
||||
CurrentBlock.RotateCW();
|
||||
if (IsBlockOutOfBounds())
|
||||
{
|
||||
CurrentBlock.RotateCCW();
|
||||
}
|
||||
}
|
||||
private void RotateBlockCCW()
|
||||
{
|
||||
CurrentBlock.RotateCCW();
|
||||
if (IsBlockOutOfBounds())
|
||||
{
|
||||
CurrentBlock.RotateCW();
|
||||
}
|
||||
}
|
||||
// If the block is out of bounds, it will move it back.
|
||||
private void MoveBlockLeft()
|
||||
{
|
||||
CurrentBlock.Move(0, -1);
|
||||
if (IsBlockOutOfBounds())
|
||||
{
|
||||
CurrentBlock.Move(0, 1);
|
||||
}
|
||||
}
|
||||
private void MoveBlockRight()
|
||||
{
|
||||
CurrentBlock.Move(0, 1);
|
||||
if (IsBlockOutOfBounds())
|
||||
{
|
||||
CurrentBlock.Move(0, -1);
|
||||
}
|
||||
}
|
||||
private bool IsGameOverCheck()
|
||||
{
|
||||
return !(GameGrid.IsRowEmpty(0) && GameGrid.IsRowEmpty(1));
|
||||
}
|
||||
|
||||
private void PlaceBlock()
|
||||
{
|
||||
foreach (Position position in CurrentBlock.TilePositions())
|
||||
{
|
||||
GameGrid[position.Row, position.Column] = CurrentBlock.Id;
|
||||
}
|
||||
GameGrid.ClearFullRows();
|
||||
if (IsGameOverCheck())
|
||||
{
|
||||
IsGameOver = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentBlock = BlockQueue.GetAndReplaceNextBlock();
|
||||
CanHold = true;
|
||||
}
|
||||
}
|
||||
public void MoveBlockDown()
|
||||
{
|
||||
CurrentBlock.Move(1, 0);
|
||||
if (IsBlockOutOfBounds())
|
||||
{
|
||||
CurrentBlock.Move(-1, 0);
|
||||
PlaceBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private int TileDropDistance(Position p)
|
||||
{
|
||||
int drop = 0;
|
||||
|
||||
while (GameGrid.IsEmpty(p.Row + drop + 1, p.Column))
|
||||
{
|
||||
drop++;
|
||||
}
|
||||
return drop;
|
||||
}
|
||||
|
||||
private int BlockDropDistance()
|
||||
{
|
||||
int drop = GameGrid.Rows;
|
||||
foreach (Position p in CurrentBlock.TilePositions())
|
||||
{
|
||||
drop = System.Math.Min(drop, TileDropDistance(p));
|
||||
}
|
||||
return drop;
|
||||
}
|
||||
private void DropBlock()
|
||||
{
|
||||
CurrentBlock.Move(BlockDropDistance(), 0);
|
||||
PlaceBlock();
|
||||
}
|
||||
|
||||
public void MoveBlock(int key)
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case 18:
|
||||
DropBlock();
|
||||
break;
|
||||
case 23:
|
||||
MoveBlockLeft();
|
||||
break;
|
||||
case 24:
|
||||
RotateBlockCW();
|
||||
break;
|
||||
case 25:
|
||||
MoveBlockRight();
|
||||
break;
|
||||
case 26:
|
||||
MoveBlockDown();
|
||||
break;
|
||||
case 46:
|
||||
HoldBlock();
|
||||
break;
|
||||
case 68:
|
||||
RotateBlockCCW();
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class IBlock : Block
|
||||
{
|
||||
private readonly Position[][] tiles = new Position[][]
|
||||
{
|
||||
new Position[] { new(1, 0), new(1, 1), new(1, 2), new(1, 3) },
|
||||
new Position[] { new(0, 2), new(1, 2), new(2, 2), new(3, 2) },
|
||||
new Position[] { new(2, 0), new(2, 1), new(2, 2), new(2, 3) },
|
||||
new Position[] { new(0, 1), new(1, 1), new(2, 1), new(3, 1) }
|
||||
};
|
||||
public override Position StartOffset => new(-1, 3);
|
||||
public override int Id => 1;
|
||||
protected override Position[][] Tiles => tiles;
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class JBlock : Block
|
||||
{
|
||||
private readonly Position[][] tiles = new Position[][]
|
||||
{
|
||||
new Position[] { new(0, 0), new(1, 0), new(2, 0), new(2, 1) },
|
||||
new Position[] { new(1, 0), new(1, 1), new(1, 2), new(0, 2) },
|
||||
new Position[] { new(0, 1), new(1, 1), new(2, 1), new(0, 0) },
|
||||
new Position[] { new(0, 0), new(0, 1), new(0, 2), new(1, 2 ) }
|
||||
};
|
||||
public override Position StartOffset => new(0, 3);
|
||||
public override int Id => 2;
|
||||
protected override Position[][] Tiles => tiles;
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class LBlock : Block
|
||||
{
|
||||
private readonly Position[][] tiles = new Position[][]
|
||||
{
|
||||
new Position[] { new(0, 2), new(1, 0), new(1, 1), new(1, 2) },
|
||||
new Position[] { new(0, 1), new(1, 1), new(2, 1), new(2, 2) },
|
||||
new Position[] { new(1, 0), new(1, 1), new(1, 2), new(2, 0) },
|
||||
new Position[] { new(0, 0), new(0, 1), new(1, 1), new(2, 1) }
|
||||
};
|
||||
public override Position StartOffset => new(0, 3);
|
||||
public override int Id => 3;
|
||||
protected override Position[][] Tiles => tiles;
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class OBlock : Block
|
||||
{
|
||||
private readonly Position[][] tiles = new Position[][]
|
||||
{
|
||||
new Position[] { new(0, 0), new(0, 1), new(1, 0), new(1, 1) }
|
||||
};
|
||||
public override Position StartOffset => new(0, 4);
|
||||
public override int Id => 4;
|
||||
protected override Position[][] Tiles => tiles;
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class Position
|
||||
{
|
||||
public int Row { get; set; }
|
||||
public int Column { get; set; }
|
||||
|
||||
public Position(int row, int column)
|
||||
{
|
||||
Row = row;
|
||||
Column = column;
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class SBlock : Block
|
||||
{
|
||||
private readonly Position[][] tiles = new Position[][]
|
||||
{
|
||||
new Position[] { new(0, 1), new(0, 2), new(1, 0), new(1, 1) },
|
||||
new Position[] { new(0, 0), new(1, 0), new(1, 1), new(2, 1) }
|
||||
};
|
||||
public override Position StartOffset => new(0, 3);
|
||||
public override int Id => 5;
|
||||
protected override Position[][] Tiles => tiles;
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class TBlock : Block
|
||||
{
|
||||
private readonly Position[][] tiles = new Position[][]
|
||||
{
|
||||
new Position[] { new(0, 1), new(1, 0), new(1, 1), new(1, 2) },
|
||||
new Position[] { new(0, 1), new(1, 1), new(1, 2), new(2, 1) },
|
||||
new Position[] { new(1, 0), new(1, 1), new(1, 2), new(2, 1) },
|
||||
new Position[] { new(0, 1), new(1, 0), new(1, 1), new(2, 1) }
|
||||
};
|
||||
public override Position StartOffset => new(0, 3);
|
||||
public override int Id => 6;
|
||||
protected override Position[][] Tiles => tiles;
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TetrisRPS
|
||||
{
|
||||
internal class Tetris
|
||||
{
|
||||
public int[,] array = new int[10, 20];
|
||||
// public int holding;
|
||||
// public int next;
|
||||
public int button;
|
||||
public void ButtonPress(int butt)
|
||||
{
|
||||
button = butt;
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
namespace TetrisRPS;
|
||||
|
||||
public class ZBlock : Block
|
||||
{
|
||||
private readonly Position[][] tiles = new Position[][]
|
||||
{
|
||||
new Position[] { new(0, 0), new(0, 1), new(1, 1), new(1, 2) },
|
||||
new Position[] { new(0, 1), new(1, 0), new(1, 1), new(2, 0) }
|
||||
};
|
||||
public override Position StartOffset => new(0, 3);
|
||||
public override int Id => 7;
|
||||
protected override Position[][] Tiles => tiles;
|
||||
}
|
Reference in New Issue
Block a user