Skip to content

Commit

Permalink
Fix BlockSys
Browse files Browse the repository at this point in the history
Using UnionPointer
Made ModelPointer union
using referencing date
  • Loading branch information
ThermodySpring committed Jan 15, 2025
1 parent 8f6bb9e commit 0be6957
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 243 deletions.
260 changes: 38 additions & 222 deletions Block.cpp
Original file line number Diff line number Diff line change
@@ -1,221 +1,28 @@
#include "Block.h"
#include <iostream>

mat4x4 blockModel[28] =
{
/*
ㅁㅁㅁ */
{
{0,0,0,0},
{1,0,0,0},
{1,1,1,0},
{0,0,0,0}
},
{
{0,1,0,0},
{0,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,1,0},
{0,0,1,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{1,0,0,0},
{1,0,0,0}
},

/*
ㅁㅁㅁ */
{
{0,0,0,0},
{0,0,1,0},
{1,1,1,0},
{0,0,0,0}
},
{
{1,1,0,0},
{0,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,1,0},
{1,0,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,0,0,0},
{1,0,0,0},
{1,1,0,0}
},

/*
ㅁㅁㅁ */
{
{0,0,0,0},
{0,1,0,0},
{1,1,1,0},
{0,0,0,0}
},
{
{0,1,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,1,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,0,0,0},
{1,1,0,0},
{1,0,0,0}
},

/*
*/
{
{1,0,0,0},
{1,0,0,0},
{1,0,0,0},
{1,0,0,0}
},
{
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,0,0,0},
{1,0,0,0},
{1,0,0,0}
},
{
{0,0,0,0},
{1,1,1,1},
{0,0,0,0},
{0,0,0,0}
},

/* ㅁㅁ
ㅁㅁ */
{
{0,0,0,0},
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{1,1,0,0},
{0,0,0,0}
},

/* ㅁㅁ
ㅁㅁ */
{
{0,0,0,0},
{0,1,1,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{0,1,1,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
},

/* ㅁㅁ
ㅁㅁ */
{
{0,0,0,0},
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}
},
{
{0,1,0,0},
{1,1,0,0},
{1,0,0,0},
{0,0,0,0}
},
{
{0,0,0,0},
{1,1,0,0},
{0,1,1,0},
{0,0,0,0}
},
{
{0,1,0,0},
{1,1,0,0},
{1,0,0,0},
{0,0,0,0}
}
};

Block::Block(int posX = 0, int posY = 0):
x(posX), y(posY){
memset(block,0,sizeof(block));
}

void Block::Initalize() {
int SetRandNum(int size) {

std::random_device rd;
std::mt19937 gen(rd());

int totalShapes = sizeof(blockModel) / sizeof(mat4x4);
std::uniform_int_distribution<> distr(0, size - 1);

std::uniform_int_distribution<> distr(0,totalShapes - 1);

int randomNumber = distr(gen);
return distr(gen);
}

memcpy(block,blockModel[randomNumber],sizeof(blockModel[randomNumber]));
memcpy(prevShape,blockModel[randomNumber],sizeof(blockModel[randomNumber]));
void Block::Initalize() {
type = static_cast<ShapeType>(SetRandNum(SHAPE_COUNT));
rotation = static_cast<RotationState>(SetRandNum(ROTATION_COUNT));

currentShape = &modelPointers[type][0];
}


void Block::Update()
{
y++;
Expand Down Expand Up @@ -245,41 +52,50 @@ void Block::MoveDown()
}





void Block::Rotate() {

memcpy(prevShape,block,sizeof(prevShape));

mat4x4 temp = {0}; // 임시 배열 초기화

// 90도 시계 방향 회전 로직
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
temp[j][3 - i] = block[i][j];
}
}

// 회전 결과를 block에 복사
memcpy(block,temp,sizeof(temp));
rotation = static_cast<RotationState>((rotation + 1) % ROTATION_COUNT);
}

void Block::rollback()
{
memcpy(block,prevShape,sizeof(prevShape));
rotation = prevRotate;
x = prevX;
y = prevY;
}

void Block::UpdatePos() {

prevRotate = rotation;
prevX = x;
prevY = y;
memcpy(prevShape,block,sizeof(prevShape)); // 현재 블록 상태 저장
}

const mat4x4& Block::GetShape() const
const ModelPointer* Block::GetShapeMatrix() const {

return currentShape;
}

int Block::GetMatrixSize() const
{
return block;
if(type == SHAPE_O)
return 2;
else if(type == SHAPE_I)
return 4;
return 3;
}

char Block::GetValue(int i,int j)
{
currentShape[rotation].mat2[i][j];
if(type == SHAPE_O) {
return (*currentShape[rotation].mat2)[i][j]; // mat2x2 접근
} else if(type == SHAPE_I) {
return (*currentShape[rotation].mat4)[i][j]; // mat4x4 접근
} else {
return (*currentShape[rotation].mat3)[i][j]; // mat3x3 접근
}
}



26 changes: 17 additions & 9 deletions Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

#include <random>
#include "InputManager.h"


typedef char mat4x4[4][4];
#include "ModelPointer.h"

extern mat4x4 blockModel[28];


class Block {
mat4x4 prevShape;
mat4x4 block;


ShapeType type;
RotationState rotation;
RotationState prevRotate;


const ModelPointer* currentShape;

int prevX,prevY;
int x,y;
Expand All @@ -33,11 +35,17 @@ class Block {
void MoveDown();



void Rotate();
void rollback();

void UpdatePos();

const mat4x4& GetShape() const;
};

const ModelPointer* GetShapeMatrix() const;
int GetMatrixSize() const;
char GetValue(int i,int j);

};

int SetRandNum();

6 changes: 6 additions & 0 deletions Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ void Engine::LoadGameMode(GameMode * newGameMode)
gameMode = newGameMode;
}

void Engine::Initailize()
{
InitializeModelPointers();

}

void Engine::Run()
{
// 고해상도 카운터
Expand Down
2 changes: 2 additions & 0 deletions Engine.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "InputManager.h"
#include "ConsoleRenderer.h"
#include "ModelPointer.h"

class GameMode;
class Engine
Expand All @@ -11,6 +12,7 @@ class Engine

void LoadGameMode(GameMode* newGameMode);

void Initailize();
void Run();

private:
Expand Down
1 change: 1 addition & 0 deletions HongLabTetris.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int main()
{
ConsoleRenderer renderer(60, 40, 240.0f); // 가로, 세로, 주사율
Engine engine(renderer);
engine.Initailize();
engine.Run();

return 0;
Expand Down
Loading

0 comments on commit 0be6957

Please sign in to comment.