- TetrisFunctions.cpp
- // ConsoleApplication1.cpp : Defines the entry point for the console application.
- //
- // Tetris_2017_2018.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "TetrisFunctions.h"
- #include <Windows.h>
- int main()
- {
- TStudnia s = {
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 0,0,0,0,0,0,0,0,10 },
- { 10, 10,10,10,10,10,10,10,10,10 },
- };
- int klocek = 1, rot = 2;
- int column = 1, row = 0;
- while (true)
- {
- WpiszKLocekDoStudni(s, row, column, klocek, rot);
- system("cls");
- DisplayStudnia(s);
- Sleep(500);
- Klawisze(s, row, column, rot);
- if (!SprawdzCzyMoznaWDol(s, row, column))
- {
- ZatrzymajKLocekWStudni(s, row, column, klocek, rot);
- row = 1;
- }
- else
- {
- ZmazKLocekWStudni(s, row, column, klocek, rot);
- }
- row++;
- }
- return 0;
- }
- ------------------------------------------
- TetrisFunctions.h
- #ifndef TetrisFunctions_H_
- #define TetrisFunctions_H_
- const int rowsCount = 20,
- colCount = 10;
- typedef int TStudnia[rowsCount][colCount];
- void DisplayStudnia(TStudnia s);
- void WpiszKLocekDoStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka);
- void ZmazKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka);
- void ZatrzymajKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka);
- bool SprawdzCzyMoznaWDol(TStudnia s, int row, int column);
- bool SprawdzCzyMoznaWLewo(TStudnia s, int row, int column);
- bool SprawdzCzyMoznaWPrawo(TStudnia s, int row, int column);
- void Klawisze(TStudnia s,int&column, int&rot,int row);
- bool SprawdzCzyUsuwacLinie(TStudnia s, int row);
- void UsunLinie(TStudnia s, int row);
- #endif // !TetrisFunctions_H_
- -------------------------------
- main
- #include "stdafx.h"
- #include "TetrisFunctions.h"
- #include <iostream>
- #include <conio.h>
- using namespace std;
- typedef int UkladKlocka[4][4];
- typedef UkladKlocka Klocek[4];
- Klocek KlockiDef[7] = {
- // klocek 0
- {
- // uklad 0
- {
- { 0, 0,0,0 },
- { 0,0,0,0 },
- { 1,1,1,1 },
- { 0,0,0,0 },
- },
- // uklad 1
- {
- { 0, 1,0,0 },
- { 0,1,0,0 },
- { 0,1,0,0 },
- { 0,1,0,0 },
- },
- // uklad 2
- {
- { 0, 0,0,0 },
- { 0,0,0,0 },
- { 1,1,1,1 },
- { 0,0,0,0 },
- },
- // uklad 3
- {
- { 0, 1,0,0 },
- { 0,1,0,0 },
- { 0,1,0,0 },
- { 0,1,0,0 },
- }
- },
- //klocek 1
- {
- //uklad 0
- {
- { 0,0,0,0 },
- { 0,1,0,0 },
- { 1,1,1,0 },
- { 0,0,0,0 },
- },
- //uklad 1
- {
- { 0,0,0,0 },
- { 0,1,0,0 },
- { 1,1,0,0 },
- { 0,1,0,0 },
- },
- //uklad 2
- {
- { 0,0,0,0 },
- { 0,0,0,0 },
- { 1,1,1,0 },
- { 0,1,0,0 },
- },
- //uklad 3
- {
- { 0,0,0,0 },
- { 0,1,0,0 },
- { 0,1,1,0 },
- { 0,1,0,0 },
- },
- },
- };
- void DisplayStudnia(TStudnia s)
- {
- for (int i = 0; i < rowsCount; i++)
- {
- for (int j = 0; j < colCount; j++)
- {
- switch (s[i][j]) {
- case 10: cout << 'X'; break;
- case 5: cout << '$'; break;
- case 1: cout << '#'; break;
- case 0: cout << ' '; break;
- }
- }
- cout << endl;
- }
- }
- void WpiszKLocekDoStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- if (s[i + row][j + column] == 0)
- s[i + row][j + column]
- = KlockiDef[klocekNr][rotKlocka][i][j];
- }
- }
- }
- void ZmazKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- if (s[i + row][j + column] == 1)
- s[i + row][j + column] = 0;
- }
- }
- }
- bool SprawdzCzyMoznaWDol(TStudnia s, int row, int column)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- if (s[i + row][j + column] == 1
- &&
- s[i + row + 1][j + column] > 1)
- return false;
- }
- }
- return true;
- }
- void ZatrzymajKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- if (s[i + row][j + column] == 1)
- s[i + row][j + column] = 5;
- }
- }
- }
- void Klawisze(TStudnia s,int&column, int&rot,int row)
- {
- while (_kbhit())
- {
- char klawisz = _getch();
- switch (klawisz)
- {
- case 'q':
- if(SprawdzCzyMoznaWLewo(s,row,column)) column--; break;
- case 'w':
- if(SprawdzCzyMoznaWPrawo(s,row,column))column++; break;
- case 'a':rot--; break;
- case 's':rot++; break;
- default:
- break;
- }
- }
- }
- bool SprawdzCzyMoznaWPrawo(TStudnia s, int row, int column)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- if (s[i + row][j + column] == 1
- &&
- s[i + row][j + column+1] > 1)
- return false;
- }
- }
- return true;
- }
- bool SprawdzCzyMoznaWLewo(TStudnia s, int row, int column)
- {
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- if (s[i + row][j + column] == 1
- &&
- s[i + row][j + column-1] > 1)
- return false;
- }
- }
- return true;
- }
- bool SprawdzCzyUsuwacLinie(TStudnia s, int row)
- {
- for (int j = 1; j < colCount - 1; j++)
- {
- if (s[row][j] != 5)
- return false;
- }
- return true;
- }
- void UsunLinie(TStudnia s, int row)
- {
- for (int j = 1; j < colCount - 1; j++)
- {
- s[row][j] = 0;
- }
- }