Skip to content

[Precogs Alert] undefined detected (undefined, Risk: undefined) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 67 additions & 42 deletions Project_Snake_Game/snake-game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,48 +49,73 @@ void Init() {

//Creating a map container where snake and fruit appears
void Map() {
system("cls");
SetConsoleTextAttribute(hConsole, 180);
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fy && j == fx)
cout << "X";
else
{
bool print = false;
for (int k = 0; k < nt; k++) {
if (tx[k] == j && ty[k] == i)
{
cout << "o";
print = true;
}

}
if (!print)
cout << " ";
}

if (j == width - 1)
cout << "#";
}
cout << endl;
}

for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
cout << "Score is " << score << endl;

// Clear the console screen securely without using system calls
#ifdef _WIN32
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };

if (hConsole == INVALID_HANDLE_VALUE) return;

// Get the number of cells in the current buffer
if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;

// Fill the entire buffer with spaces
if (!FillConsoleOutputCharacter(hConsole, (TCHAR)' ', cellCount, homeCoords, &count)) return;

// Fill the entire buffer with the current colors and attributes
if (!FillConsoleOutputAttribute(hConsole, csbi.wAttributes, cellCount, homeCoords, &count)) return;

// Move the cursor home
SetConsoleCursorPosition(hConsole, homeCoords);
#else
// For non-Windows systems, use ANSI escape codes
std::cout << "\033[2J\033[H";
#endif

SetConsoleTextAttribute(hConsole, 180);
for (int i = 0; i < width + 2; i++)
std::cout << "#";
std::cout << std::endl;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
std::cout << "#";
if (i == y && j == x)
std::cout << "O";
else if (i == fy && j == fx)
std::cout << "X";
else
{
bool print = false;
for (int k = 0; k < nt; k++) {
if (tx[k] == j && ty[k] == i)
{
std::cout << "o";
print = true;
}

}
if (!print)
std::cout << " ";
}

if (j == width - 1)
std::cout << "#";
}
std::cout << std::endl;
}

for (int i = 0; i < width + 2; i++)
std::cout << "#";
std::cout << std::endl;
std::cout << "Score is " << score << std::endl;
}
// managing input of keyboard to control snake
void Input() {
Expand Down