PLEASE, PLEASE, PLEASE!!!! I have done searches, I have searched on Chegg for th
ID: 3667655 • Letter: P
Question
PLEASE, PLEASE, PLEASE!!!! I have done searches, I have searched on Chegg for the code, please DO NOT repost what is available already. I do not need graphics or any bells and whistles, I just need the code up to the point listed below. PLEASE!!!!! I will post my code so far after the instructions
*****You have already displayed the Tetris Bucket and started dropping the shapes. During this module, you stop the falling shape at the bottom of the Bucket. If there are shapes already stacked at the bottom of the bucket, then you stop the shape on top of other shapes. At the same time, you drop another shape from the top. Add plenty of narrative comments. Your program must be compilable and executable.
If you need guidance, you will find some detailed instruction (below) to assist you.
User input is provided using the arrow keys. By default (with no user input being entered), the shape falls one cell in each loop. The shape can also be made to fall more quickly by pressing the down-arrow key. Assuming that you are using a switch block to handle the user input, in each switch case, you will change the top left x, y and calling different functions to process that arrow input. Regardless of which of the two methods triggered the shape's downward movement, you will probably call the function processDownArrow(TetrisShape& tetrisShape). You will probably find that you need separate functions to handle different key inputs.
if (shapeArray[x][y] != ' '){
if (x,y dimensions are inside the bucket){
if bucket cell is not empty{
The shape is stuck
}
}
else if (x dimension reaches the bottom){
The shape is stuck, so stop the shape.
}
else{
/*Here the shape hits the side wall, but not stuck. The situation is, the shape is at the left wall, and user is pressing the left arrow. In this case, the input is ineffective. So, we will let the shape drop one cell here, and call the processDownArrow(*this). The processDownArrow function will return a bool value whether the shape is stuck or not.*/
if (the shape is stuck){
Stop the shape
}
else{
the shape is not stuck, let it fall
}
}
}
When the current shape stops at the bottom, the next shape starts to drop. In the main() function, before the while loop (game loop), you will create two Tetris objects at the same time: currentTetrisShape, and nextTetrisShape. Inside the game loop, when the current shape is stuck, you check whether a line is complete. Then, you call currentTetrisShape.setShape(nextTetrisShapeType). In the setShape() function, you assign the properties of the shape: shapeType, top left X and Y, shapeArray, etc. Then you call nextTetrisShape.setShape (use a new randomly generated int).
In each of these arrow-key input process functions, you need to check whether the shape is stuck and needs to stop. You need to go over the values of the shape array using two nested "for loops". Remember, the shape is not stuck if the cell is empty. Below is the logic to check when the shape is stuck:***
My Code So FAR! and it needs help, but just for your reference!
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstdlib>
#include <conio.h>
using namespace std;
//console width height
const int WIDTH = 400;
const int HEIGHT = 400;
//bucket row and column size
const int buckHeight = 25;
const int buckWidth = 12;
//bucket array
char bucket[buckHeight][buckWidth];
//bool variables
bool running = true;
bool shapeStuck = false;
//variables
int randNum;
int shapeTopLeftX = 0;
int shapeTopLeftY = 6;
int newShapeTopLeftX;
int newShapeTopLeftY;
int oldTopLeftX;
int oldTopLeftY;
int key;
char tempbucket;
enum arrowKeys { UP_ARROW = 72, DOWN_ARROW = 80, LEFT_ARROW = 75, RIGHT_ARROW = 77 };
//shape class
class TetrisShape {
public:
void createShape(int shapeType);
char shapeArray[4][4];
private:
enum Shapes { Box, Line, S, Z, L, J, T };
};
//function declarations
void setConsole();
void setCursorTo(int x, int y);
void initializeBucket();
void printBucket();
void getRandNum(int seed);
void drawShape();
void updateBucket(TetrisShape shape, int x, int y);
void removeTrail(int x, int y);
void getUserInput(TetrisShape temp);
void moveShape(TetrisShape shape, int arrowKeys);
void stuckCheck(TetrisShape shape);
void processDown();
void setShape();
void inline timeControl(int timer);
int wmain(int argc, _TCHAR* argv[])
{
srand((unsigned)time(NULL));
initializeBucket();
//creates a shape
TetrisShape shape;
TetrisShape temp;
TetrisShape currentTetrisShape;
TetrisShape nextTetrisShape;
getRandNum(6);
shape.createShape(randNum);
while (running) {
temp = shape;
printBucket();
processDown();
getUserInput(temp);
updateBucket(temp, shapeTopLeftX, shapeTopLeftY);
printBucket();
removeTrail(oldTopLeftX, oldTopLeftY);
timeControl(500);
}
system("Pause");
}
//functions
void setConsole() {
HWND console = GetConsoleWindow();
RECT ConsoleRect;
GetWindowRect(console, &ConsoleRect);
MoveWindow(console, ConsoleRect.left, ConsoleRect.top, WIDTH, HEIGHT, true);
}
void setCursorTo(int x, int y) {
HANDLE handle;
COORD position;
handle = GetStdHandle(STD_OUTPUT_HANDLE);
position.X = x;
position.Y = y;
SetConsoleCursorPosition(handle, position);
}
void initializeBucket() {
for (int i = 0; i < buckHeight; i++) {
for (int j = 0; j < buckWidth; j++) {
if (j == 0 || j == 11 || i == 24) {
bucket[i][j] = '#';
}
else if (bucket[i][j] == 'X') {
continue;
}
else {
bucket[i][j] = ' ';
}
}
}
}
void printBucket() {
setConsole();
setCursorTo(0, 1);
initializeBucket();
for (int i = 0; i < buckHeight; i++) {
for (int j = 0; j < buckWidth; j++) {
cout << bucket[i][j];
if (j == 11) cout << endl;
}
}
}
void getRandNum(int seed) {
randNum = (rand() % seed + 1);
}
void TetrisShape::createShape(int shapeType) {
switch (shapeType) {
case 1: //Box
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 2: //Line
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = 'X'; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 3: //S
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
shapeArray[0][1] = 'X'; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 4: // Z
shapeArray[0][0] = 'X'; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = 'X'; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 5: // L
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 6: //J
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = 'X'; shapeArray[1][2] = 'X'; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
case 7: //T
shapeArray[0][0] = 'X'; shapeArray[1][0] = 'X'; shapeArray[2][0] = 'X'; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = ' '; shapeArray[2][2] = ' '; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';
break;
default:
cout << "None" << endl;
break;
}
}
void updateBucket(TetrisShape shape, int x, int y) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
bucket[i + x][j + y] = shape.shapeArray[i][j];
}
}
oldTopLeftX = shapeTopLeftX;
oldTopLeftY = shapeTopLeftY;
}
void removeTrail(int x, int y) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
bucket[i + x][j + y] = ' ';
}
}
}
void getUserInput(TetrisShape temp) {
if (_kbhit()) {
key = _getch();
}
moveShape(temp, key);
}
void moveShape(TetrisShape shape, int arrowKeys) {
switch (arrowKeys) {
case UP_ARROW:
tempbucket = shape.shapeArray[0][0];
shape.shapeArray[0][0] = shape.shapeArray[0][3];
shape.shapeArray[0][3] = shape.shapeArray[3][3];
shape.shapeArray[3][3] = shape.shapeArray[3][0];
shape.shapeArray[3][0] = tempbucket;
tempbucket = shape.shapeArray[0][1];
shape.shapeArray[0][0] = shape.shapeArray[0][3];
shape.shapeArray[0][3] = shape.shapeArray[3][3];
shape.shapeArray[3][3] = shape.shapeArray[3][0];
shape.shapeArray[3][0] = tempbucket;
tempbucket = shape.shapeArray[0][2];
shape.shapeArray[0][2] = shape.shapeArray[2][3];
shape.shapeArray[2][3] = shape.shapeArray[3][1];
shape.shapeArray[3][1] = shape.shapeArray[1][0];
shape.shapeArray[1][0] = tempbucket;
tempbucket = shape.shapeArray[1][1];
shape.shapeArray[1][1] = shape.shapeArray[1][2];
shape.shapeArray[1][2] = shape.shapeArray[2][2];
shape.shapeArray[2][2] = shape.shapeArray[2][1];
shape.shapeArray[2][1] = tempbucket;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
bucket[i + shapeTopLeftX][j + shapeTopLeftY] = shape.shapeArray[i][j];
}
}
break;
case DOWN_ARROW:
shapeTopLeftX += 3;
processDown();
break;
case LEFT_ARROW:
stuckCheck(shape);
if (!shapeStuck) {
shapeTopLeftY--;
}
else processDown();
break;
case RIGHT_ARROW:
shapeTopLeftY++;
break;
default:
shapeTopLeftX++;
break;
}
}
void stuckCheck(TetrisShape shape) {
for (int i = 0; i < buckHeight; i++) {
for (int j = 0; j < buckWidth; j++) {
if (shape.shapeArray[i][j] != ' ') {
if (j > 0 && j < 24) {
shapeStuck = true;
}
}
}
}
}
void processDown() {
shapeTopLeftX++;
printBucket();
}
void inline timeControl(int timer)
{
Sleep(timer);
}
if (shapeArray[x][y] != ' '){
if (x,y dimensions are inside the bucket){
if bucket cell is not empty{
The shape is stuck
}
}
else if (x dimension reaches the bottom){
The shape is stuck, so stop the shape.
}
else{
/*Here the shape hits the side wall, but not stuck. The situation is, the shape is at the left wall, and user is pressing the left arrow. In this case, the input is ineffective. So, we will let the shape drop one cell here, and call the processDownArrow(*this). The processDownArrow function will return a bool value whether the shape is stuck or not.*/
if (the shape is stuck){
Stop the shape
}
else{
the shape is not stuck, let it fall
}
}
}
Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
#include <map>
#include <string>
#include <limits>
#include <vector>
#include <iomanip>
#include <windows.h>
#define TURBO
#define LOG
//#define CUSTOM
HWND hL2 = 0, hHxd = 0;
HWND hReplace = 0;
HWND err = 0;
HDC dc;
int curMsNum = -1;
int BEG = 600;
int END = 700;
char changedMultisellNum[] = "700022";
int MsLen;
clock_t time1, time2;
POINT pWhereNPC, pSectionButton, pWhereScanItem, pWhereCloseWindow, pLineEdit, pReplaceAll;
FILE * file;
FILE * file_custom;
FILE * file_out;
BOOL CALLBACK enumFunc(HWND hwnd, LPARAM p) {
static int c=0;
char str[BUFSIZ+1];
char str2[BUFSIZ+1];
::GetWindowTextA(hwnd,str,BUFSIZ);
::GetClassNameA(hwnd,str2,BUFSIZ);
if (strcmp(str,"")==0)
strcpy(str,"No name");
//printf("'%s' : %s ",str,str2);
if ((strstr(str,"Lineage")!=0) && (strstr(str,"Running")==0)) {
//puts(str);
//getchar();
hL2 = hwnd;
}
if (strstr(str,"HxD - ")!=0)
hHxd = hwnd;
if (strstr(str,"Replace")!=0)
hReplace = hwnd;
return TRUE;
}
void pressCtrlR() {
keybd_event( VK_CONTROL, 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( 'R', 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_CONTROL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
keybd_event( 'R', 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
#ifndef TURBO
Sleep(500);
#endif
}
void pressCtrlS() {
keybd_event( VK_CONTROL, 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( 'S', 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_CONTROL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
keybd_event( 'S', 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
#ifndef TURBO
Sleep(1000);
#endif
}
void pressEnter() {
keybd_event( VK_RETURN, 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_RETURN, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
#ifndef TURBO
Sleep(1000);
#endif
}
void pressLeft() {
keybd_event( VK_LEFT, 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_LEFT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
#ifndef TURBO
Sleep(1000);
#endif
}
void pressF5() {
keybd_event( VK_F5, 0x45, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_F5, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
#ifndef TURBO
Sleep(1000);
#endif
}
void doubleClick(POINT p) {
#ifndef TURBO
Sleep(200);
#endif
::SetCursorPos(p.x,p.y);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTDOWN, p.x, p.y, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTDOWN, p.x, p.y, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
Sleep(100);
}
void singleClick(POINT p) {
#ifndef TURBO
Sleep(200);
#endif
::SetCursorPos(p.x,p.y);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTDOWN, p.x, p.y, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
Sleep(100);
}
void readPointsFromFile() {
fscanf(file,"%d %d",&pWhereNPC.x,&pWhereNPC.y);
fscanf(file,"%d %d",&pSectionButton.x,&pSectionButton.y);
fscanf(file,"%d %d",&pWhereScanItem.x,&pWhereScanItem.y);
fscanf(file,"%d %d",&pWhereCloseWindow.x,&pWhereCloseWindow.y);
fscanf(file,"%d %d",&pLineEdit.x,&pLineEdit.y);
fscanf(file,"%d %d",&pReplaceAll.x,&pReplaceAll.y);
}
void emulatePrint(char * text) {
puts(text);
for (char * p = text; *p; p++) {
std::cout << ' ' << *p;
keybd_event( toupper(*p), 0, 0, 0 );
keybd_event( toupper(*p), 0, KEYEVENTF_KEYUP, 0 );
#ifndef TURBO
Sleep(100);
#endif
}
}
void setNewMs(int num) {
char buf[40] = "multisell ";
char tmp[20] = "";
sprintf(tmp,"%d",num);
strcat(buf, tmp);
while (strlen(buf) < (strlen("multisell ") + MsLen))
strcat(buf," ");
pressCtrlR();
Sleep(300);
singleClick(pLineEdit);
emulatePrint(buf);
#ifndef TURBO
Sleep(1000);
#endif
singleClick(pReplaceAll);
#ifndef TURBO
Sleep(4000);
#else
Sleep(3000);
#endif
pressEnter();
pressCtrlS();
Sleep(200);
pressLeft();
#ifndef TURBO
Sleep(500);
#endif
pressEnter();
}
bool hasItem() {
POINT p = pWhereScanItem;
p.y-=2;
COLORREF c;
for (int i=0; i<6; i++) {
GetCursorPos(&p);
c = GetPixel(dc, p.x, p.y);
int r, g, b;
r = GetRValue(c);
g = GetGValue(c);
b = GetBValue(c);
std::cout << r << ' ' << g << ' ' << b << ' ';
if ((r!= 16) || (g!= 16) || (b!= 16)) // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return true;
}
return false;
}
int main()
{
time_t t;
struct tm *t_m;
int found_counter = 0;
curMsNum = BEG;
dc = GetDC(0);
file = fopen("pos.txt","rt");
if (!file) {
puts("pos.txt not found");
getchar();
return 2;
}
file_custom = fopen("custom.txt","rt");
if (!file_custom) {
puts("custom.txt not found");
getchar();
return 2;
}
#ifdef CUSTOM
int custom[2000] = {0};
int n = 0;
while (1) {
fscanf(file_custom, "%d", &custom[n++]);
if (feof(file_custom)) {
custom[n] = -1;
break;
}
}
int c = 0;
#endif
readPointsFromFile();
fclose(file);
EnumWindows((WNDENUMPROC)enumFunc,0);
if (hHxd == 0 || hL2 == 0) {
puts("L2 or HxD not found");
getchar();
return 1;
}
MsLen = strlen(changedMultisellNum);
#ifndef TURBO
Sleep(1200);
#endif
#ifdef LOG
t = time(0);
t_m=localtime(&t);
file_out = fopen("out.txt","a+");
fprintf(file_out," --- session started at %d:%d:%d (%d/%d/%d) --- ", t_m->tm_hour, t_m->tm_min, t_m->tm_sec, t_m->tm_mday, t_m->tm_mon, t_m->tm_year);
fclose(file_out);
#endif
SetForegroundWindow(hL2);
ShowWindow(hL2,SW_SHOW);
Sleep(5000);
#ifdef LOG
time1 = clock();
#endif
while (1) {
#ifndef CUSTOM
if (curMsNum > END)
break;
#else
if (c >= n)
break;
curMsNum = custom[c++];
#endif
doubleClick(pWhereNPC);
Sleep(100);
SetForegroundWindow(hHxd);
pressF5();
#ifndef TURBO
Sleep(200);
#endif
//
setNewMs(curMsNum);
#ifndef TURBO
Sleep(2000);
#endif
//
SetForegroundWindow(hL2);
ShowWindow(hL2,SW_SHOW);
#ifndef TURBO
Sleep(2000);
#else
Sleep(1000);
#endif
singleClick(pSectionButton);
#ifndef TURBO
Sleep(1000);
#endif
singleClick(pWhereScanItem);
// !
if (hasItem()) {
found_counter++;
file_out = fopen("out.txt","a+");
fprintf(file_out,"%d ",curMsNum);
fclose(file_out);
}
#ifndef TURBO
Sleep(1500);
#else
;//Sleep(500);
#endif
//singleClick(pWhereCloseWindow);
//SetForegroundWindow(hReplace);
curMsNum++;
#ifndef TURBO
Sleep(300);
#endif
} // end while
#ifdef LOG
time2 = clock() - time1;
int total = END - BEG + 1;
t = time(0);
t_m=localtime(&t);
file_out = fopen("out.txt","a+");
#ifndef CUSTOM
fprintf(file_out," --- session ended at %d:%d:%d (%d/%d/%d) --- Checked: %d - %d (total %d) Success: %d Average time: %.2lf sec. ", t_m->tm_hour, t_m->tm_min, t_m->tm_sec,
t_m->tm_mday, t_m->tm_mon, t_m->tm_year, BEG, END, total, found_counter, (time2/CLOCKS_PER_SEC)/(double)total);
#else
fprintf(file_out," --- session ended at %d:%d:%d (%d/%d/%d) --- --- CUSTOM --- Average time: %.2lf sec. ", t_m->tm_hour, t_m->tm_min, t_m->tm_sec,
t_m->tm_mday, t_m->tm_mon, t_m->tm_year,(time2/(double)CLOCKS_PER_SEC)/(double)total);
fclose(file_custom);
#endif
fclose(file_out);
#endif
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.