Tworze prostą grę w konsoli TETRIS, mam problem, program losuje mi caly czas ten sam klocek.
>>>>Klocki.h<<<<
#pragma once
#include <windows.h>
#include <Windows.h>
#include <time.h>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <iostream>
using namespace std;
char klocki[4][4][4][4] = {
{
{
{ ' ', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', }
},
{
{ '#', '#', '#', '#', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ ' ', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', }
},
{
{ '#', '#', '#', '#', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
}
},
{
{
{ ' ', '#', '#', ' ', },
{ ' ', '#', '#', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ ' ', '#', '#', ' ', },
{ ' ', '#', '#', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ ' ', '#', '#', ' ', },
{ ' ', '#', '#', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ ' ', '#', '#', ' ', },
{ ' ', '#', '#', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
}
},
{
{
{ ' ', ' ', ' ', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', '#', '#', ' ', }
},
{
{ ' ', ' ', ' ', '#', },
{ ' ', '#', '#', '#', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ ' ', '#', '#', ' ', },
{ ' ', ' ', '#', ' ', },
{ ' ', ' ', '#', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ '#', '#', '#', ' ', },
{ '#', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
}
},
{
{
{ ' ', '#', ' ', ' ', },
{ ' ', '#', '#', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ ' ', '#', ' ', ' ', },
{ '#', '#', '#', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ ' ', '#', ' ', ' ', },
{ '#', '#', ' ', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
},
{
{ '#', '#', '#', ' ', },
{ ' ', '#', ' ', ' ', },
{ ' ', ' ', ' ', ' ', },
{ ' ', ' ', ' ', ' ', }
}
}
};
>>>>Tetris.cpp<<<<<
// Tetris.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Klocki.h"
using namespace std;
char studnia[20][20] = {
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'x' },
{ 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' }
};
int losujKlocek()
{
return (rand()%4;
}
int losujObrot()
{
return (rand()%4;
}
void rysuj(char _studnia[20][20])
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
cout << _studnia[i][j];
}
cout << endl;
};
}
void wpiszKlocek(char _studnia[20][20], int _x, int _y, char _klocki[4][4][4][4], int _klocek, int _obrot)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (_klocki[_klocek][_obrot][i][j] == '#')
_studnia[i + _y][j + _x] = _klocki[_klocek][_obrot][i][j];
}
};
}
void kasujKlocek(char _studnia[20][20], int _x, int _y)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (_studnia[i + _y][j + _x] == '#')
{
_studnia[i + _y][j + _x] = ' ';
}
}
}
}
bool czyBetonuj(char _studnia[20][20], int _x, int _y, char _klocki[4][4][4][4], int _klocek, int _obrot)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if ((_studnia[(i + 1) + _y][j + _x] == 'o') || (_studnia[(i + 1) + _y][j + _x] == 'x'))
{
if (_klocki[_klocek][_obrot][i][j] == '#')
return true;
}
}
}
return false;
}
void betonuj(char _studnia[20][20], int _x, int _y, char _klocki[4][4][4][4], int _klocek, int _obrot)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (_klocki[_klocek][_obrot][i][j] == '#')
_studnia[i + _y][j + _x] = 'o';
}
};
}
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));
//opcje//
int start_x = 8, start_y = 0;
/////////
int x, y;
int klocek = losujKlocek();
int obrot = losujObrot();
y = start_y;
x = start_x;
do
{
if (czyBetonuj(studnia, x, y, klocki, klocek, obrot))
{
betonuj(studnia, x, y, klocki, klocek, obrot);
y = start_y;
x = start_x;
int klocek = losujKlocek();
int obrot = losujObrot();
}
else
{
kasujKlocek(studnia, x, y);
y++;
}
system("CLS");
wpiszKlocek(studnia, x, y, klocki, klocek, obrot);
rysuj(studnia);
kasujKlocek(studnia, x, y);
Sleep(500);
} while (1); //czy koniec
system("PAUSE");
return 0;
}