Can someone compile this code program for me, since, I keep getting error on fro
ID: 3770327 • Letter: C
Question
Can someone compile this code program for me, since, I keep getting error on from my compiler... I just to check if it is executable program or not... THANKS
#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include <ctime>
using namespace std;
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];
/*
Tetris-
1.user input-start game
2.launch bucket
3.randomize shape, drop in bucket
4.accept user input of shape
5.take user input and display with shape
6.repeat 3-5 till drop
7.drop shape
8.score
9. repeat 2-8 until game over
*/
void initializeBucket();
void displayBucket();
void setCursorTo(int x, int y);
void userInput();
void moveShape();
void shapeDrop();
void clearRow();
void score();
void beginGame();
void endGame();
int main()
{
int score = 0;
char playAgain;
//this is my working game loop, keeps replaying until n is entered
cout << " Welcome to Tetris!!! Would you like to play? (y/n): ";
cin >> playAgain;
while ((playAgain != 'y') && (playAgain != 'n'))
{
cout << " Please enter y or n: ";
cin >> playAgain;
}
if (playAgain == 'y')
{
//rand for shape
srand(static_cast<unsigned int>(time(0)));
int shapeType = rand() % 7;
while (playAgain == 'y')
{
displayBucket();
setCursorTo(0, 0);
beginGame();
cout << " Would you like to play again? (y/n): ";
cin >> playAgain;
while ((playAgain != 'y') && (playAgain != 'n'))
{
cout << " Please enter y or n: ";
cin >> playAgain;
}
}
}
else
system("pause");
return 0;
}
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 beginGame()
{
//this function calls the gameplay functions
//each function here is what keeps the shapes moving and user playing
userInput();
moveShape();
shapeDrop();
clearRow();
endGame();
}
void displayBucket()
{
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 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] = ' ';
}
}
}
}
class TetrisShape {
public:
int shapeTopLeftX;
int shapeTopLeftY;
TetrisShape() {
shapeTopLeftX = 6;
shapeTopLeftY = 0;
}
char shapeArray[4][4];
void populateShapeArray(int shapeType) {
switch (shapeType) {
case 1:
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 2:
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:
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 4:
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 5:
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 6:
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;
}
}
};
void userInput(char shapeType)
{
char shapeArray[4][4];
char mvDir; // Move Direction
//user presses keys to flip/move shape
cout << " User moves shape" << endl;
cout << "Please enter move direction: L = left, R = right, U = Up, D = Down ";
cin >> mvDir;
switch (mvDir) {
case '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 'R':
shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';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] = 'X';shapeArray[2][2] = 'X';shapeArray[3][2] = ' ';
shapeArray[0][3] = ' ';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
break;
case 'U':
shapeArray[0][0] = ' ';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] = 'X';shapeArray[2][2] = ' ';shapeArray[3][2] = ' ';
shapeArray[0][3] = ' ';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
break;
case 'D':
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] = ' ';shapeArray[2][3] = 'X';shapeArray[3][3] = ' ';
break;
} // end of switch
}
void moveShape(int shapeType)
{
char shapeArray[4][4];
//shape reacts to user input
cout << " Computer moves shape according to user request" << endl;
int mvDir; // Move Direction
//user presses keys to flip/move shape
cout << "Please enter move direction: 1 = Up & left, 2 = Up & right, 3 = Down & Left, 4 = Down & Right";
cin >> mvDir;
switch (mvDir) {
case '1':
shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';shapeArray[2][0] = ' ';shapeArray[3][0] = 'X';
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 '2':
shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';shapeArray[2][0] = 'X ';shapeArray[3][0] = ' ';
shapeArray[0][1] = ' ';shapeArray[1][1] = 'X';shapeArray[2][1] = ' ';shapeArray[3][1] = 'X';
shapeArray[0][2] = ' ';shapeArray[1][2] = 'X';shapeArray[2][2] = ' ';shapeArray[3][2] = ' ';
shapeArray[0][3] = ' ';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
break;
case '3':
shapeArray[0][0] = ' ';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] = 'X';shapeArray[1][3] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
break;
case '4':
shapeArray[0][0] = ' ';shapeArray[1][0] = 'X';shapeArray[2][0] = ' ';shapeArray[3][0] = 'X';
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] = ' ';shapeArray[2][3] = ' ';shapeArray[3][3] = ' ';
break;
} // end of switch
}
void shapeDrop()
{
char shapeArray[4][4];
//when shape drops on bottom and stops
cout << " Shape drops to bottom" << endl;
shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';shapeArray[2][0] = ' ';shapeArray[3][0] = ' ';
shapeArray[0][1] = ' ';shapeArray[1][1] = ' ';shapeArray[2][1] = ' ';shapeArray[3][1] = ' ';
shapeArray[0][2] = ' ';shapeArray[1][2] = ' ';shapeArray[2][2] = ' ';shapeArray[3][2] = ' ';
shapeArray[0][3] = 'X';shapeArray[1][3] = 'X';shapeArray[2][3] = 'X';shapeArray[3][3] = 'X';
}
void clearRow()
{
char shapeArray[4][4];
//when a row is filled, will clear and score
cout << " A row is filled, it clears" << endl;
shapeArray[0][0] = ' ';shapeArray[1][0] = ' ';shapeArray[2][0] = ' ';shapeArray[3][0] = ' ';
shapeArray[0][1] = ' ';shapeArray[1][1] = ' ';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] = ' ';
score();
}
void score()
{
//calculates score
int score;
cout << " Add to score" << endl;
score++;
}
void endGame()
{
//when shape hits top, clear screen and display/save score
cout << " Clear screen and save score" << endl;
clearRow();
cout << " Score = " << score;
}
Explanation / Answer
Here I got the Error " fatal error: 'stdafx.h' file not found"
stdafx.h is the precompiled header used by visual studio.
tdafx.h is a file, generated by Microsoft Visual Studio IDE wizards, that describes both standard system and project specific include files that are used frequently but hardly ever change.
Compatible compilers (for example, Visual C++ 6.0 and newer) will precompile this file to reduce overall compile times. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
The AFX in stdafx.h stands for Application Framework eXtensions. AFX was the original abbreviation for the Microsoft Foundation Classes (MFC). While the name stdafx.h is used by default, projects may specify an alternative name and handle all includes files containing in project file.
When you #include a header file, it's sort of like copy/pasting that header file into the cpp file. The compiler will walk through, examine, and compile that header whenever you compile the cpp file.
The compiler will do this process for every cpp file that includes that header. So if you have 100 cpp files in your project and they all include the same header, that header is being compiled 100 times.
If the header is very large, this might cause the computer to take a long time to compile your program. So compilers give you an option to "precompile" a header so that it compiles only once. Then each cpp file that #includes it does not have to re-compile it every time, they can just use the precompiled version. It can speed up compile time.