Facebook
From Ungracious Zebra, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 263
  1. TetrisFunctions.cpp
  2. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  3. //
  4.  
  5. // Tetris_2017_2018.cpp : Defines the entry point for the console application.
  6. //
  7.  
  8. #include "stdafx.h"
  9. #include "TetrisFunctions.h"
  10. #include <Windows.h>
  11. int main()
  12. {
  13.         TStudnia s = {
  14.                 { 10, 0,0,0,0,0,0,0,0,10 },
  15.                 { 10, 0,0,0,0,0,0,0,0,10 },
  16.                 { 10, 0,0,0,0,0,0,0,0,10 },
  17.                 { 10, 0,0,0,0,0,0,0,0,10 },
  18.                 { 10, 0,0,0,0,0,0,0,0,10 },
  19.                 { 10, 0,0,0,0,0,0,0,0,10 },
  20.                 { 10, 0,0,0,0,0,0,0,0,10 },
  21.                 { 10, 0,0,0,0,0,0,0,0,10 },
  22.                 { 10, 0,0,0,0,0,0,0,0,10 },
  23.                 { 10, 0,0,0,0,0,0,0,0,10 },
  24.                 { 10, 0,0,0,0,0,0,0,0,10 },
  25.                 { 10, 0,0,0,0,0,0,0,0,10 },
  26.                 { 10, 0,0,0,0,0,0,0,0,10 },
  27.                 { 10, 0,0,0,0,0,0,0,0,10 },
  28.                 { 10, 0,0,0,0,0,0,0,0,10 },
  29.                 { 10, 0,0,0,0,0,0,0,0,10 },
  30.                 { 10, 0,0,0,0,0,0,0,0,10 },
  31.                 { 10, 0,0,0,0,0,0,0,0,10 },
  32.                 { 10, 0,0,0,0,0,0,0,0,10 },
  33.                 { 10, 10,10,10,10,10,10,10,10,10 },
  34.         };
  35.         int klocek = 1, rot = 2;
  36.         int column = 1, row = 0;
  37.         while (true)
  38.         {
  39.                 WpiszKLocekDoStudni(s, row, column, klocek, rot);
  40.                 system("cls");
  41.                 DisplayStudnia(s);
  42.                 Sleep(500);
  43.                 Klawisze(s, row, column, rot);
  44.                 if (!SprawdzCzyMoznaWDol(s, row, column))
  45.                 {
  46.                         ZatrzymajKLocekWStudni(s, row, column, klocek, rot);
  47.                         row = 1;
  48.                 }
  49.                 else
  50.                 {
  51.                         ZmazKLocekWStudni(s, row, column, klocek, rot);
  52.                 }
  53.        
  54.  
  55.                 row++;
  56.         }
  57.  
  58.  
  59.         return 0;
  60. }
  61. ------------------------------------------
  62. TetrisFunctions.h
  63.  
  64. #ifndef TetrisFunctions_H_
  65. #define TetrisFunctions_H_
  66.  
  67. const int rowsCount = 20,
  68. colCount = 10;
  69. typedef int TStudnia[rowsCount][colCount];
  70.  
  71. void DisplayStudnia(TStudnia s);
  72. void WpiszKLocekDoStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka);
  73. void ZmazKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka);
  74. void ZatrzymajKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka);
  75. bool SprawdzCzyMoznaWDol(TStudnia s, int row, int column);
  76. bool SprawdzCzyMoznaWLewo(TStudnia s, int row, int column);
  77. bool SprawdzCzyMoznaWPrawo(TStudnia s, int row, int column);
  78. void Klawisze(TStudnia s,int&column, int&rot,int row);
  79. bool SprawdzCzyUsuwacLinie(TStudnia s, int row);
  80. void UsunLinie(TStudnia s, int row);
  81.  
  82. #endif // !TetrisFunctions_H_
  83. -------------------------------
  84. main
  85. #include "stdafx.h"
  86. #include "TetrisFunctions.h"
  87. #include <iostream>
  88. #include <conio.h>
  89.  
  90. using namespace std;
  91.  
  92.  
  93. typedef int UkladKlocka[4][4];
  94. typedef UkladKlocka Klocek[4];
  95. Klocek KlockiDef[7] = {
  96.         // klocek 0
  97.         {
  98.                 // uklad 0
  99.                 {
  100.                         { 0, 0,0,0 },
  101.                         { 0,0,0,0 },
  102.                         { 1,1,1,1 },
  103.                         { 0,0,0,0 },
  104.                 },
  105.                 // uklad 1
  106.                 {
  107.                         { 0, 1,0,0 },
  108.                         { 0,1,0,0 },
  109.                         { 0,1,0,0 },
  110.                         { 0,1,0,0 },
  111.                 },
  112.                 // uklad 2
  113.                 {
  114.                         { 0, 0,0,0 },
  115.                         { 0,0,0,0 },
  116.                         { 1,1,1,1 },
  117.                         { 0,0,0,0 },
  118.                 },
  119.                 // uklad 3
  120.                 {
  121.                         { 0, 1,0,0 },
  122.                         { 0,1,0,0 },
  123.                         { 0,1,0,0 },
  124.                         { 0,1,0,0 },
  125.                 }
  126.         },
  127.  
  128.  
  129.  
  130.  
  131.  
  132.         //klocek 1
  133.         {
  134.                 //uklad 0
  135.                 {
  136.                         { 0,0,0,0 },
  137.                         { 0,1,0,0 },
  138.                         { 1,1,1,0 },
  139.                         { 0,0,0,0 },
  140.                 },
  141.                 //uklad 1
  142.                 {
  143.                         { 0,0,0,0 },
  144.                         { 0,1,0,0 },
  145.                         { 1,1,0,0 },
  146.                         { 0,1,0,0 },
  147.                 },
  148.                 //uklad 2
  149.                 {
  150.                         { 0,0,0,0 },
  151.                         { 0,0,0,0 },
  152.                         { 1,1,1,0 },
  153.                         { 0,1,0,0 },
  154.                 },
  155.                 //uklad 3
  156.                 {
  157.                         { 0,0,0,0 },
  158.                         { 0,1,0,0 },
  159.                         { 0,1,1,0 },
  160.                         { 0,1,0,0 },
  161.                 },
  162.         },
  163.  
  164. };
  165.  
  166.  
  167.  
  168. void DisplayStudnia(TStudnia s)
  169. {
  170.         for (int i = 0; i < rowsCount; i++)
  171.         {
  172.                 for (int j = 0; j < colCount; j++)
  173.                 {
  174.  
  175.                         switch (s[i][j]) {
  176.                         case 10: cout << 'X'; break;
  177.                         case 5: cout << '$'; break;
  178.                         case 1: cout << '#'; break;
  179.                         case 0: cout << ' '; break;
  180.                         }
  181.                 }
  182.                 cout << endl;
  183.         }
  184. }
  185.  
  186. void WpiszKLocekDoStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka)
  187. {
  188.         for (int i = 0; i < 4; i++)
  189.         {
  190.                 for (int j = 0; j < 4; j++)
  191.                 {
  192.                         if (s[i + row][j + column] == 0)
  193.                                 s[i + row][j + column]
  194.                                 = KlockiDef[klocekNr][rotKlocka][i][j];
  195.                 }
  196.         }
  197. }
  198.  
  199. void ZmazKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka)
  200. {
  201.         for (int i = 0; i < 4; i++)
  202.         {
  203.                 for (int j = 0; j < 4; j++)
  204.                 {
  205.                         if (s[i + row][j + column] == 1)
  206.                                 s[i + row][j + column] = 0;
  207.  
  208.                 }
  209.         }
  210. }
  211.  
  212. bool SprawdzCzyMoznaWDol(TStudnia s, int row, int column)
  213. {
  214.         for (int i = 0; i < 4; i++)
  215.         {
  216.                 for (int j = 0; j < 4; j++)
  217.                 {
  218.                         if (s[i + row][j + column] == 1
  219.                                 &&
  220.                                 s[i + row + 1][j + column] > 1)
  221.                                 return false;
  222.  
  223.                 }
  224.         }
  225.         return true;
  226. }
  227.  
  228. void ZatrzymajKLocekWStudni(TStudnia s, int row, int column, int klocekNr, int rotKlocka)
  229. {
  230.         for (int i = 0; i < 4; i++)
  231.         {
  232.                 for (int j = 0; j < 4; j++)
  233.                 {
  234.                         if (s[i + row][j + column] == 1)
  235.                                 s[i + row][j + column] = 5;
  236.  
  237.                 }
  238.         }
  239. }
  240. void Klawisze(TStudnia s,int&column, int&rot,int row)
  241. {
  242.         while (_kbhit())
  243.         {
  244.                 char klawisz = _getch();
  245.  
  246.                 switch (klawisz)
  247.                 {
  248.                 case 'q':
  249.                         if(SprawdzCzyMoznaWLewo(s,row,column)) column--; break;
  250.                 case 'w':
  251.                 if(SprawdzCzyMoznaWPrawo(s,row,column))column++; break;
  252.                 case 'a':rot--; break;
  253.                 case 's':rot++; break;
  254.                 default:
  255.                         break;
  256.  
  257.                 }
  258.         }
  259. }
  260. bool SprawdzCzyMoznaWPrawo(TStudnia s, int row, int column)
  261. {
  262.         for (int i = 0; i < 4; i++)
  263.         {
  264.                 for (int j = 0; j < 4; j++)
  265.                 {
  266.                         if (s[i + row][j + column] == 1
  267.                                 &&
  268.                                 s[i + row][j + column+1] > 1)
  269.                                 return false;
  270.  
  271.                 }
  272.         }
  273.         return true;
  274. }
  275. bool SprawdzCzyMoznaWLewo(TStudnia s, int row, int column)
  276. {
  277.         for (int i = 0; i < 4; i++)
  278.         {
  279.                 for (int j = 0; j < 4; j++)
  280.                 {
  281.                         if (s[i + row][j + column] == 1
  282.                                 &&
  283.                                 s[i + row][j + column-1] > 1)
  284.                                 return false;
  285.  
  286.                 }
  287.         }
  288.         return true;
  289. }
  290. bool SprawdzCzyUsuwacLinie(TStudnia s, int row)
  291. {
  292.         for (int j = 1; j < colCount - 1; j++)
  293.         {
  294.                 if (s[row][j] != 5)
  295.                         return false;
  296.  
  297.         }
  298.         return true;
  299.  
  300.  
  301. }
  302. void UsunLinie(TStudnia s, int row)
  303. {
  304.         for (int j = 1; j < colCount - 1; j++)
  305.         {
  306.                 s[row][j] = 0;
  307.         }
  308. }
  309.