
目次
はじめに
MVCモデルとは
プロジェクト作成
要件定義
実装 Model編
ゲーム盤 GameBoardクラス実装
ゲーム全体の管理 TetrisGameクラス実装
まとめ
はじめに
引続きC#の学習の為にアプリを作成してみます。
今回はテトリスを作成してみたいと思います。
以前、テトリスを作成したことがあるのですが、その際は参考にしていたサイトが途中までしか更新されず
一人で続きを作成する知識もなかったので頓挫してしまいました。
Claudeを使用しつつ改めて最後まで作成しきる様子を執筆していきます。
MVCモデルとは
簡単にMVCモデルとは何かを説明します。
Model・・・データとビジネスロジックを管理
View・・・UIを担当
Controller・・・ModelとViewの仲介役
上記の頭文字をとってMVCと呼ばれるものです。
アプリの構造を3つに分離してそれぞれに役割を持たせることにより、コードが整理され理解しやすくなったり
各構造が独立しているため、機能の追加・修正がやりやすくなったりというメリットがあります。
プロジェクト作成
いつも通りVisual Studioにてプロジェクト作成から始めていきます。
今回はテトリスを作成するのでMVCモデルと呼ばれる形で作成していきます。
まずVisual Studioを起動して「新しいプロジェクトの作成」をクリック
テンプレートはASP.NET Core Webアプリにてプロジェクト作成から始めていきます。
今回はテトリスを作成するのでMVCモデルと呼ばれる形で作成していきます。
まずVisual Studioを起動して「新しいプロジェクトの作成」をクリック。
テンプレートはASP.NET Core Webアプリ(Model-View-Controller)を選択します。
プロジェクト名を入力して作成
作成するとソリューションエクスプローラーと呼ばれる場所にMVCでフォルダが生成されます。

要件定義
少し順番は前後してしまいましたが要件定義を簡単にしておこうと思います。
・盤面サイズ:横10マス×縦20マス
・テトリミノ:7種類(I、O、T、S、Z、J、L)
・ソフトドロップ・ハードドロップ
・スコアシステム
・レベルアップシステム
以上の機能を簡単に要件定義として設定します。
実装 Model編
では早速コードを書き始めていきたいと思います。
まずはModelsフォルダに、テトリミノ(落ちてくるブロック)のクラスを作っていきます。
フォルダ内で新しくクラスを作成します。
クラス名はTetrominoとし、下記のコードを書きます。
<code>using System.Text.Json.Serialization;
namespace TetrisGame.Models
{
/// <summary>
/// テトロミノ(テトリスのブロック)を表すクラス
/// </summary>
public class Tetromino
{
/// <summary>
/// テトロミノの種類
/// </summary>
public enum TetrominoType
{
I, // 直線型
O, // 正方型
T, // T字型
S, // S字型
Z, // Z字型
J, // J字型
L // L字型
}
/// <summary>
/// テトロミノの種類
/// </summary>
public TetrominoType Type { get; set; }
/// <summary>
/// 現在のX座標(ゲーム盤上の位置)
/// </summary>
public int X { get; set; }
/// <summary>
/// 現在のY座標(ゲーム盤上の位置)
/// </summary>
public int Y { get; set; }
/// <summary>
/// 回転状態(0-3: 0度、90度、180度、270度)
/// </summary>
public int Rotation { get; set; }
/// <summary>
/// テトロミノの形状データ(4x4のジャグ配列、JSONシリアライズ対応)
/// </summary>
public bool[][] Shape { get; set; }
/// <summary>
/// デフォルトコンストラクタ(JSONデシリアライズ用)
/// </summary>
public Tetromino()
{
Type = TetrominoType.I;
X = 3; // 中央寄りに修正
Y = 0;
Rotation = 0;
Shape = GetInitialShape(TetrominoType.I);
}
/// <summary>
/// 通常のコンストラクタ
/// </summary>
public Tetromino(TetrominoType type)
{
Type = type;
X = 3; // スポーン位置を調整(より安全な位置)
Y = 0;
Rotation = 0;
Shape = GetInitialShape(type);
}
/// <summary>
/// JSON用のコンストラクタ
/// </summary>
[JsonConstructor]
public Tetromino(TetrominoType type, int x, int y, int rotation, bool[][] shape)
{
Type = type;
X = x;
Y = y;
Rotation = rotation % 4; // 回転状態を正規化
Shape = shape ?? GetInitialShape(type);
}
/// <summary>
/// テトロミノの種類に応じた初期形状を取得
/// 形状の位置を最適化
/// </summary>
private bool[][] GetInitialShape(TetrominoType type)
{
// 4x4のジャグ配列を初期化
bool[][] shape = new bool[4][];
for (int i = 0; i < 4; i++)
{
shape[i] = new bool[4];
}
switch (type)
{
case TetrominoType.I: // 直線型 ████
shape[1][0] = shape[1][1] = shape[1][2] = shape[1][3] = true;
break;
case TetrominoType.O: // 正方型 ██
shape[0][1] = shape[0][2] = shape[1][1] = shape[1][2] = true; // ██
break;
case TetrominoType.T: // T字型 ███
shape[0][1] = true; // █
shape[1][0] = shape[1][1] = shape[1][2] = true; // ███
break;
case TetrominoType.S: // S字型 ██
shape[0][1] = shape[0][2] = true; // ██
shape[1][0] = shape[1][1] = true; // ██
break;
case TetrominoType.Z: // Z字型 ██
shape[0][0] = shape[0][1] = true; // ██
shape[1][1] = shape[1][2] = true; // ██
break;
case TetrominoType.J: // J字型 █
shape[0][0] = true; // █
shape[1][0] = shape[1][1] = shape[1][2] = true; // ███
break;
case TetrominoType.L: // L字型 █
shape[0][2] = true; // █
shape[1][0] = shape[1][1] = shape[1][2] = true; // ███
break;
}
return shape;
}
/// <summary>
/// テトロミノを右に90度回転
/// </summary>
public void RotateClockwise()
{
// O型は回転しても形状が変わらないのでスキップ
if (Type == TetrominoType.O)
return;
Rotation = (Rotation + 1) % 4;
Shape = RotateShapeClockwise(Shape);
}
/// <summary>
/// 4x4配列を時計回りに90度回転
/// </summary>
private bool[][] RotateShapeClockwise(bool[][] original)
{
bool[][] rotated = new bool[4][];
for (int i = 0; i < 4; i++)
{
rotated[i] = new bool[4];
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
rotated[j][3 - i] = original[i][j];
}
}
return rotated;
}
/// <summary>
/// テトロミノを左に移動
/// </summary>
public void MoveLeft()
{
X--;
}
/// <summary>
/// テトロミノを右に移動
/// </summary>
public void MoveRight()
{
X++;
}
/// <summary>
/// テトロミノを下に移動
/// </summary>
public void MoveDown()
{
Y++;
}
/// <summary>
/// テトロミノの実際の境界を取得(デバッグ用)
/// </summary>
public (int minX, int maxX, int minY, int maxY) GetBounds()
{
int minX = 4, maxX = -1, minY = 4, maxY = -1;
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
if (Shape[row][col])
{
minX = Math.Min(minX, col);
maxX = Math.Max(maxX, col);
minY = Math.Min(minY, row);
maxY = Math.Max(maxY, row);
}
}
}
return (minX, maxX, minY, maxY);
}
/// <summary>
/// テトロミノのクローンを作成
/// </summary>
public Tetromino Clone()
{
var clonedShape = new bool[4][];
for (int i = 0; i < 4; i++)
{
clonedShape[i] = new bool[4];
for (int j = 0; j < 4; j++)
{
clonedShape[i][j] = Shape[i][j];
}
}
return new Tetromino(Type, X, Y, Rotation, clonedShape);
}
}
}</code>
ゲーム盤 GameBoardクラス実装
続けてゲーム盤の実装をします。
新しくGameBoardクラスを作成し、以下のコードを書きます。
こちらはゲーム盤(プレイエリア)での設定部分を実装しています。
<code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace TetrisGame.Models
{
/// <summary>
/// テトリスのゲーム盤を管理するクラス
/// </summary>
public class GameBoard
{
/// <summary>
/// ゲーム盤の幅(マス数)
/// </summary>
public const int Width = 10;
/// <summary>
/// ゲーム盤の高さ(マス数)
/// </summary>
public const int Height = 20;
/// <summary>
/// ゲーム盤の状態(0=空、1以上=ブロックあり)
/// JSON シリアライズ用に int[][] (ジャグ配列) を使用
/// </summary>
public int[][] Board { get; set; }
/// <summary>
/// デフォルトコンストラクタ
/// </summary>
public GameBoard()
{
InitializeBoard();
}
/// <summary>
/// JSON デシリアライズ用コンストラクタ
/// </summary>
[JsonConstructor]
public GameBoard(int[][] board)
{
if (board == null || board.Length != Height)
{
InitializeBoard();
}
else
{
Board = board;
// 各行の長さをチェック・修正
for (int i = 0; i < Height; i++)
{
if (Board[i] == null || Board[i].Length != Width)
{
Board[i] = new int[Width];
}
}
}
}
/// <summary>
/// ゲーム盤を初期化
/// </summary>
private void InitializeBoard()
{
Board = new int[Height][];
for (int i = 0; i < Height; i++)
{
Board[i] = new int[Width];
}
ClearBoard();
}
/// <summary>
/// ゲーム盤をクリア(全て0にする)
/// </summary>
public void ClearBoard()
{
for (int row = 0; row < Height; row++)
{
for (int col = 0; col < Width; col++)
{
Board[row][col] = 0;
}
}
}
/// <summary>
/// 指定位置の値を取得(安全アクセス)
/// </summary>
public int GetCell(int row, int col)
{
if (row < 0 || row >= Height || col < 0 || col >= Width)
return -1; // 範囲外
return Board[row][col];
}
/// <summary>
/// 指定位置に値を設定(安全アクセス)
/// </summary>
public void SetCell(int row, int col, int value)
{
if (row >= 0 && row < Height && col >= 0 && col < Width)
{
Board[row][col] = value;
}
}
/// <summary>
/// テトロミノが指定位置に配置可能かチェック
/// </summary>
public bool CanPlaceTetromino(Tetromino tetromino, int x, int y)
{
if (tetromino?.Shape == null) return false;
for (int row = 0; row < 4; row++)
{
if (tetromino.Shape.Length <= row || tetromino.Shape[row] == null)
continue;
for (int col = 0; col < 4; col++)
{
if (tetromino.Shape[row].Length <= col)
continue;
// テトロミノのこの位置にブロックがない場合はスキップ
if (!tetromino.Shape[row][col])
continue;
// ゲーム盤上の実際の座標を計算
int boardX = x + col;
int boardY = y + row;
// 範囲外チェック
if (boardX < 0 || boardX >= Width || boardY >= Height)
return false;
// 上端は許可(テトロミノが画面上から入ってくる)
if (boardY < 0)
continue;
// 既存のブロックとの衝突チェック
if (GetCell(boardY, boardX) != 0)
return false;
}
}
return true;
}
/// <summary>
/// テトロミノをゲーム盤に固定する
/// </summary>
public void PlaceTetromino(Tetromino tetromino)
{
if (tetromino?.Shape == null) return;
for (int row = 0; row < 4; row++)
{
if (tetromino.Shape.Length <= row || tetromino.Shape[row] == null)
continue;
for (int col = 0; col < 4; col++)
{
if (tetromino.Shape[row].Length <= col)
continue;
if (tetromino.Shape[row][col])
{
int boardX = tetromino.X + col;
int boardY = tetromino.Y + row;
// 範囲内の場合のみ配置
if (boardX >= 0 && boardX < Width && boardY >= 0 && boardY < Height)
{
// テトロミノの種類+1を保存(0は空きマス用)
SetCell(boardY, boardX, (int)tetromino.Type + 1);
}
}
}
}
}
/// <summary>
/// 完成した行を見つけて削除し、削除行数を返す
/// </summary>
public int ClearCompletedLines()
{
List<int> completedLines = new List<int>();
// 完成した行を探す
for (int row = 0; row < Height; row++)
{
bool isLineComplete = true;
for (int col = 0; col < Width; col++)
{
if (GetCell(row, col) == 0)
{
isLineComplete = false;
break;
}
}
if (isLineComplete)
{
completedLines.Add(row);
}
}
// 完成した行を削除(下から上へ)
foreach (int lineIndex in completedLines.OrderByDescending(x => x))
{
RemoveLine(lineIndex);
}
return completedLines.Count;
}
/// <summary>
/// 指定した行を削除し、上の行を下に移動
/// </summary>
private void RemoveLine(int lineIndex)
{
// 削除する行より上の行を1つずつ下に移動
for (int row = lineIndex; row > 0; row--)
{
for (int col = 0; col < Width; col++)
{
SetCell(row, col, GetCell(row - 1, col));
}
}
// 一番上の行をクリア
for (int col = 0; col < Width; col++)
{
SetCell(0, col, 0);
}
}
/// <summary>
/// ゲームオーバー判定
/// より堅牢な判定ロジックに修正
/// </summary>
public bool IsGameOver()
{
// 上から数行をチェック(スポーン領域)
for (int row = 0; row < 2; row++)
{
for (int col = 0; col < Width; col++)
{
if (GetCell(row, col) != 0)
return true;
}
}
return false;
}
/// <summary>
/// デバッグ用:ボードの状態を文字列で表示
/// </summary>
public string GetDebugString()
{
var lines = new List<string>();
for (int row = 0; row < Height; row++)
{
var line = "";
for (int col = 0; col < Width; col++)
{
line += GetCell(row, col) == 0 ? "." : "#";
}
lines.Add($"{row:D2}|{line}|");
}
return string.Join("\n", lines);
}
}
}</code>
ゲーム全体の管理 TetrisGameクラス実装
次にゲーム全体の管理を行うTetrisGameクラスを実装します。
新しくTetrisGameクラスを作成し、以下のコードを書きます。
<code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace TetrisGame.Models
{
/// <summary>
/// テトリスゲーム全体を管理するクラス(自動落下対応版)
/// </summary>
public class TetrisGame
{
/// <summary>
/// ゲーム盤
/// </summary>
public GameBoard Board { get; set; }
/// <summary>
/// 現在落下中のテトロミノ
/// </summary>
public Tetromino? CurrentPiece { get; set; }
/// <summary>
/// 次に出現するテトロミノ
/// </summary>
public Tetromino? NextPiece { get; set; }
/// <summary>
/// 現在のスコア
/// </summary>
public int Score { get; set; }
/// <summary>
/// 消去した行数
/// </summary>
public int Lines { get; set; }
/// <summary>
/// 現在のレベル
/// </summary>
public int Level { get; set; }
/// <summary>
/// ゲーム終了フラグ
/// </summary>
public bool IsGameOver { get; set; }
/// <summary>
/// ゲーム一時停止フラグ
/// </summary>
public bool IsPaused { get; set; }
/// <summary>
/// 最後にテトロミノが自動落下した時刻
/// </summary>
public DateTime LastDropTime { get; set; }
/// <summary>
/// 落下間隔(ミリ秒)
/// レベルに応じて速度を調整
/// </summary>
[JsonIgnore]
public int DropInterval => Math.Max(50, 800 - Level * 50);
/// <summary>
/// テトロミノ生成用の乱数(シリアライズ対象外)
/// </summary>
[JsonIgnore]
private Random _random;
/// <summary>
/// デフォルトコンストラクタ
/// </summary>
public TetrisGame()
{
InitializeGame();
}
/// <summary>
/// JSON デシリアライズ用コンストラクタ
/// </summary>
[JsonConstructor]
public TetrisGame(GameBoard board, Tetromino? currentPiece, Tetromino? nextPiece,
int score, int lines, int level, bool isGameOver, bool isPaused,
DateTime lastDropTime)
{
Board = board ?? new GameBoard();
CurrentPiece = currentPiece;
NextPiece = nextPiece;
Score = Math.Max(0, score); // 負の値を防ぐ
Lines = Math.Max(0, lines);
Level = Math.Max(1, level); // レベルは最低1
IsGameOver = isGameOver;
IsPaused = isPaused;
LastDropTime = lastDropTime == default ? DateTime.Now : lastDropTime;
_random = new Random();
// NextPiece が null の場合は生成
if (NextPiece == null)
{
NextPiece = GenerateRandomTetromino();
}
// 復元後の状態チェック
ValidateGameState();
}
/// <summary>
/// ゲーム初期化
/// </summary>
private void InitializeGame()
{
Board = new GameBoard();
Score = 0;
Lines = 0;
Level = 1;
IsGameOver = false;
IsPaused = false;
_random = new Random();
LastDropTime = DateTime.Now;
// 最初のテトロミノを生成
NextPiece = GenerateRandomTetromino();
SpawnNextPiece();
Console.WriteLine("[DEBUG] TetrisGame初期化完了");
}
/// <summary>
/// ゲーム状態の妥当性をチェック
/// </summary>
private void ValidateGameState()
{
if (CurrentPiece != null && !Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X, CurrentPiece.Y))
{
Console.WriteLine("[DEBUG] 無効な CurrentPiece 状態を検出 - ゲームオーバーに設定");
IsGameOver = true;
}
}
/// <summary>
/// ランダムなテトロミノを生成
/// </summary>
private Tetromino GenerateRandomTetromino()
{
var types = Enum.GetValues<Tetromino.TetrominoType>();
var randomType = types[_random.Next(types.Length)];
return new Tetromino(randomType);
}
/// <summary>
/// 次のテトロミノをゲーム盤に出現させる
/// </summary>
private void SpawnNextPiece()
{
CurrentPiece = NextPiece;
if (CurrentPiece != null)
{
// スポーン位置をリセット
CurrentPiece.X = 3;
CurrentPiece.Y = 0;
}
NextPiece = GenerateRandomTetromino();
Console.WriteLine($"[DEBUG] 新しいピース出現: {CurrentPiece?.Type} at ({CurrentPiece?.X}, {CurrentPiece?.Y})");
// 新しいピースが配置できない場合はゲームオーバー
if (CurrentPiece != null && !Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X, CurrentPiece.Y))
{
Console.WriteLine("[DEBUG] 新しいピースが配置できません - ゲームオーバー");
IsGameOver = true;
}
}
/// <summary>
/// テトロミノを左に移動
/// </summary>
public bool MoveLeft()
{
if (CurrentPiece == null || IsGameOver || IsPaused)
{
Console.WriteLine("[DEBUG] MoveLeft: 移動不可能な状態");
return false;
}
if (Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X - 1, CurrentPiece.Y))
{
CurrentPiece.MoveLeft();
Console.WriteLine($"[DEBUG] 左移動成功: 新しい位置 ({CurrentPiece.X}, {CurrentPiece.Y})");
return true;
}
Console.WriteLine("[DEBUG] 左移動失敗: 衝突または範囲外");
return false;
}
/// <summary>
/// テトロミノを右に移動
/// </summary>
public bool MoveRight()
{
if (CurrentPiece == null || IsGameOver || IsPaused)
{
Console.WriteLine("[DEBUG] MoveRight: 移動不可能な状態");
return false;
}
if (Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X + 1, CurrentPiece.Y))
{
CurrentPiece.MoveRight();
Console.WriteLine($"[DEBUG] 右移動成功: 新しい位置 ({CurrentPiece.X}, {CurrentPiece.Y})");
return true;
}
Console.WriteLine("[DEBUG] 右移動失敗: 衝突または範囲外");
return false;
}
/// <summary>
/// テトロミノを下に移動(ソフトドロップ)
/// </summary>
public bool MoveDown()
{
if (CurrentPiece == null || IsGameOver || IsPaused)
{
Console.WriteLine("[DEBUG] MoveDown: 移動不可能な状態");
return false;
}
Console.WriteLine($"[DEBUG] MoveDown開始: Current piece at ({CurrentPiece.X}, {CurrentPiece.Y})");
if (Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X, CurrentPiece.Y + 1))
{
CurrentPiece.MoveDown();
LastDropTime = DateTime.Now; // 落下タイマーをリセット
Console.WriteLine($"[DEBUG] MoveDown成功: 新しい位置 ({CurrentPiece.X}, {CurrentPiece.Y})");
return true;
}
else
{
// 下に移動できない場合はピースを固定
Console.WriteLine("[DEBUG] MoveDown失敗: ピースを固定します");
PlaceCurrentPiece();
return false;
}
}
/// <summary>
/// テトロミノを回転
/// ウォールキック機能付き
/// </summary>
public bool RotatePiece()
{
if (CurrentPiece == null || IsGameOver || IsPaused)
{
Console.WriteLine("[DEBUG] RotatePiece: 回転不可能な状態");
return false;
}
// O型は回転しても同じなのでスキップ
if (CurrentPiece.Type == Tetromino.TetrominoType.O)
{
return true;
}
// 現在の状態を保存
var originalPiece = CurrentPiece.Clone();
// 回転を試行
CurrentPiece.RotateClockwise();
// 通常位置で配置可能かチェック
if (Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X, CurrentPiece.Y))
{
Console.WriteLine($"[DEBUG] 回転成功: 新しい回転状態 {CurrentPiece.Rotation}");
return true;
}
// ウォールキックを試行(左右に1マスずつ)
int[] wallKickOffsets = { -1, 1, -2, 2 };
foreach (int offset in wallKickOffsets)
{
if (Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X + offset, CurrentPiece.Y))
{
CurrentPiece.X += offset;
Console.WriteLine($"[DEBUG] ウォールキック成功: オフセット {offset}, 新しい位置 ({CurrentPiece.X}, {CurrentPiece.Y})");
return true;
}
}
// 回転できない場合は元に戻す
CurrentPiece.Type = originalPiece.Type;
CurrentPiece.X = originalPiece.X;
CurrentPiece.Y = originalPiece.Y;
CurrentPiece.Rotation = originalPiece.Rotation;
CurrentPiece.Shape = originalPiece.Shape;
Console.WriteLine("[DEBUG] 回転失敗: 元の状態に戻しました");
return false;
}
/// <summary>
/// ハードドロップ(一気に底まで落とす)
/// </summary>
public int HardDrop()
{
if (CurrentPiece == null || IsGameOver || IsPaused)
{
Console.WriteLine("[DEBUG] HardDrop: 実行不可能な状態");
return 0;
}
int dropDistance = 0;
while (Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X, CurrentPiece.Y + 1))
{
CurrentPiece.MoveDown();
dropDistance++;
}
Console.WriteLine($"[DEBUG] ハードドロップ: {dropDistance}マス落下");
// ピースを固定
PlaceCurrentPiece();
// ハードドロップのボーナス点
Score += dropDistance * 2;
return dropDistance;
}
/// <summary>
/// 自動落下処理(時間経過でテトロミノを下に移動)
/// </summary>
public void Update()
{
if (IsGameOver || IsPaused || CurrentPiece == null)
return;
// 落下間隔をチェック
var now = DateTime.Now;
var timeSinceLastDrop = now.Subtract(LastDropTime);
if (timeSinceLastDrop.TotalMilliseconds >= DropInterval)
{
Console.WriteLine("[DEBUG] 自動落下実行");
MoveDown();
}
}
/// <summary>
/// 現在のテトロミノをゲーム盤に固定
/// </summary>
private void PlaceCurrentPiece()
{
if (CurrentPiece == null)
{
Console.WriteLine("[DEBUG] PlaceCurrentPiece: CurrentPiece が null");
return;
}
Console.WriteLine($"[DEBUG] ピース固定開始: {CurrentPiece.Type} at ({CurrentPiece.X}, {CurrentPiece.Y})");
// ピースをボードに配置
Board.PlaceTetromino(CurrentPiece);
// 完成した行をクリア
int clearedLines = Board.ClearCompletedLines();
if (clearedLines > 0)
{
Console.WriteLine($"[DEBUG] {clearedLines}行消去");
// スコア計算(一度に多く消すほど高得点)
int lineScore = clearedLines switch
{
1 => 100 * Level, // シングル
2 => 300 * Level, // ダブル
3 => 500 * Level, // トリプル
4 => 800 * Level, // テトリス!
_ => 100 * Level * clearedLines
};
Score += lineScore;
Lines += clearedLines;
// レベルアップ判定(10行消去で1レベルアップ)
int newLevel = Lines / 10 + 1;
if (newLevel > Level)
{
Level = newLevel;
Console.WriteLine($"[DEBUG] レベルアップ!新しいレベル: {Level}");
}
Console.WriteLine($"[DEBUG] スコア更新: +{lineScore} (合計: {Score})");
}
// ソフトドロップのボーナス
Score += 1;
// ゲームオーバー判定
if (Board.IsGameOver())
{
Console.WriteLine("[DEBUG] ゲームオーバー判定: true");
IsGameOver = true;
return;
}
// 次のピースを出現
Console.WriteLine("[DEBUG] 次のピースを出現させます");
SpawnNextPiece();
}
/// <summary>
/// ゲームをリセット
/// </summary>
public void Reset()
{
Console.WriteLine("[DEBUG] ゲームリセット開始");
InitializeGame();
Console.WriteLine("[DEBUG] ゲームリセット完了");
}
/// <summary>
/// ゲームの一時停止/再開
/// </summary>
public void TogglePause()
{
if (!IsGameOver)
{
IsPaused = !IsPaused;
if (!IsPaused)
{
// 再開時は落下タイマーをリセット
LastDropTime = DateTime.Now;
}
Console.WriteLine($"[DEBUG] ゲーム {(IsPaused ? "一時停止" : "再開")}");
}
}
/// <summary>
/// ゲームの状態を文字列で取得(デバッグ用)
/// </summary>
public override string ToString()
{
return $"Score: {Score}, Lines: {Lines}, Level: {Level}, " +
$"GameOver: {IsGameOver}, Paused: {IsPaused}, " +
$"CurrentPiece: {CurrentPiece?.Type} at ({CurrentPiece?.X},{CurrentPiece?.Y})";
}
/// <summary>
/// 次のピースが配置可能な座標を予測(ゴーストピース用)
/// </summary>
public int GetGhostPieceY()
{
if (CurrentPiece == null) return -1;
int ghostY = CurrentPiece.Y;
while (Board.CanPlaceTetromino(CurrentPiece, CurrentPiece.X, ghostY + 1))
{
ghostY++;
}
return ghostY;
}
}
}</code>
まとめ
今回はここまでとします。
現状では下記の3クラスを作成しました。
1,Tetrimino.cs テトリミノ(落ちてくるブロック)
2,GameBoard.cs ゲーム盤の管理
3,TetrisGame.cs ゲーム全体の制御
次回はController部分を作成します。
では、また次回お会いしましょう!!
