I'm making a snake game and having trouble with the tail movement. I understand the logic for this part, which is that each segment of the tail follows the previous segment, starting from the end of the tail. I am looking at someone else's code, and it looks like this
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailx[100], taily[100];
int nTail;
enum eDirecton { Stop, Left, Right, Up, Down } dir;
void Setup()
{
gameOver = false;
dir = Stop;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw()
{
system("cls");
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 == fruitY && j == fruitX)
cout << "F";
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailx[k] == j && taily[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:" << score << endl;
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir = Left;
break;
case 'd':
dir = Right;
break;
case 'w':
dir = Up;
break;
case 's':
dir = Down;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Logic()
{
for (int i = nTail - 1; i > 0; i--)
{
tailx[i] = tailx[i - 1];
taily[i] = taily[i - 1];
}
tailx[0] = x;
taily[0] = y;
switch (dir)
{
case Left:
x--;
break;
case Right:
x++;
break;
case Up:
y--;
break;
case Down:
y++;
break;
default:
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
for (int i = 0; i < nTail; i++)
if (tailx[i] == x && taily[i] == y)
gameOver = true;
if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(50);
}
return 0;
}
I understand the logic but I don't understand why it works. When we create an array, the value of each element is just a garbage value without initializing each element. So in the code above, when doing
tailx[i] = tailx[i-1];
taily[i] = taily[i-1];
what value is assigned to each element?
When displaying the snake, it has a for loop to go through every coordinate of the screen and inside it has another for loop to compare tailx[i] and taily[i] with each coordinate to find out the right position to print each segment of the tail. Since tailx and tialy are not storing the coordinates of the segments of the tail, how come this code works?
Thank you so much!!
Presumably, you're missing a line at the end that looks something like:
if(nTail < 100) { nTail++; }
Assuming that's the case, nTail is initialized to 0 and that this is all in a loop, the code probably looks something like (I'm using a size of 5 instead of 100 to make visualizing easier)
int tailx[5];
int taily[5];
int nTail = 0; //length
while(true) {
for(int i = nTail -1; i > 0; i--)
{
tailx[i] = tailx[i-1];
taily[i] = taily[i-1];
}
// Let's assume there's some logic here the fetches a new
// x and y. For the sake of debugging, let's assume the
// values will be {(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)}
tailx[0] = x; // x is the x-coordinate of the head
taily[0] = y; //y is the y-coordinate of the head
if(nTail < 5) { nTail++; }
}
Let's step through this!
Before we enter the loop, your tail arrays are going to look like: (I'm using NA here to mean "Garbage")
nTail = 0
+-------------------------+
|Name | 0 | 1 | 2 | 3 | 4 |
|-------------------------|
| X | NA| NA| NA| NA| NA|
+-------------------------+
| Y | NA| NA| NA| NA| NA|
+-------------------------+
We enter the loop, initialize i to nTail - 1 which is -1. This doesn't pass the check of i > 0, so we don't even enter the loop.
We'll now grab new x and y vals and assign them into the tails, along with incrementing nTail. So going into the next loop our variables will look like:
nTail = 1
+-------------------------+
|Name | 0 | 1 | 2 | 3 | 4 |
|-------------------------|
| X | 1 | NA| NA| NA| NA|
+-------------------------+
| Y | 1 | NA| NA| NA| NA|
+-------------------------+
We'll head on in, initialize i to nTail - 1 => 0. This DOESN'T pass the check of i > 0, so again we don't enter the loop (which sounds wrong to me...maybe you're initializing nTail to 1 instead of 0?).
We head on down, grab new x/y vals and increment nTail and restart the loop with:
nTail = 2
+-------------------------+
|Name | 0 | 1 | 2 | 3 | 4 |
|-------------------------|
| X | 2 | NA| NA| NA| NA|
+-------------------------+
| Y | 2 | NA| NA| NA| NA|
+-------------------------+
Initializing i to nTail - 1 => 1 means since i > 0 we'll finally enter the inner loop.
With i = 1, we update our tail arrays:
tailx[1] = tailx[0];
taily[1] = taily[0];
Then head down, grab new values and increment nTail. Our variables now look like:
nTail = 3
+-------------------------+
|Name | 0 | 1 | 2 | 3 | 4 |
|-------------------------|
| X | 3 | 2 | NA| NA| NA|
+-------------------------+
| Y | 3 | 2 | NA| NA| NA|
+-------------------------+
After the next loop, things will look like:
nTail = 4
+-------------------------+
|Name | 0 | 1 | 2 | 3 | 4 |
|-------------------------|
| X | 4 | 3 | 2 | NA| NA|
+-------------------------+
| Y | 4 | 3 | 2 | NA| NA|
+-------------------------+
I'll leave it to you to keep tracing if you so desire.
You're right; this code cannot work, and it has undefined behaviour.
nTail isn't even initialised to anything.
Are you sure it's a full program, and not just snippets glued together? Or a sort of "pseudocode" to show the logic without being actual valid C++? You'd need values for nTail and all the array elements.
Related
const int N = 200;
const string usr1 = "o", usr2 = "x";
void updateBoard(string a[N][N], int c, int n, string xo) {
int col = c - 1;
int row = n - 1;
for (int i = row; i >= 0; i--) {
if ((a[i][col] == usr1) || (a[i][col] == usr2)) {
a[i - 1][col] = xo;
}
if ((a[i][col] == " ")) {
a[i][col] = xo;
}
i = 0;
}
}
I don't know what's wrong, It stops at the second row, when i try to drop at third, it rewrites the value on the second...
This happens:
| x |
| x |
Want this:
| x |
| o |
| x |
I found the answer... Here's the fixed function:
bool updateBoard(string board[N][N], int col, int n, string xo) {
for (int i = n - 1; i >= 0; i--) {
if (board[i][col - 1] == "-") {
board[i][col - 1] = xo;
return true;
}
}
return false;
}
I want to print an X on screen like this:
* *
* *
* *
*
* *
* *
* *
I tried with this code:
int main(){
bool back = false;
for (int i = 0; i < 7; ++i) {
if (i == 4)
back = true;
if (!back){
for (int j = 0; j < i; ++j) {
cout << " ";
}
} else{
for (int j = 7-i-1; j > 0; --j) {
cout << " ";
}
}
cout << "*" << endl;
}
}
The result is missing the right half:
*
*
*
*
*
*
*
The problem is that I can't figure out how to print the spaces between the stars and the stars that follow them.
A more educational approach to solving this problem requires 2 loops.
The first for loop controls the height of the output, i.e. the number of lines printed. Each iteration prints a single line and ends it with a std::endl.
The second is a nested for loop, which controls the width and prints characters horizontally, i.e. it prints asterisk(s) and spaces for that line. Each iteration prints either a space or an asterisk.
This illustration might help to understand the values of the variables when x_size = 5:
(width)
0 1 2 3 4
(height) ---------------------
0 | * | | | | * | asterisk_pos = 0, end_pos = 4, inc = 1
---------------------
1 | | * | | * | | asterisk_pos = 1, end_pos = 3, inc = 1
---------------------
2 | | | * | | | asterisk_pos = 2, end_pos = 2, inc = 1
---------------------
3 | | * | | * | | asterisk_pos = 1, end_pos = 3, inc = -1
---------------------
4 | * | | | | * | asterisk_pos = 0, end_pos = 4, inc = -1
---------------------
Source code:
int main()
{
int x_size = 7; // size of the drawing
int asterisk_pos = 0; // initial position of the asterisk
int inc = 1; // amount of increment added to asterisk_pos after an entire line has been printed
// height is the line number
for (int height = 0; height < x_size; height++)
{
// width is the column position of the character that needs to be printed for a given line
for (int width = 0; width < x_size; width++)
{
int end_pos = (x_size - width) - 1; // the position of the 2nd asterisk on the line
if (asterisk_pos == width || asterisk_pos == end_pos)
cout << "*";
else
cout << " ";
}
// print a new line character
cout << std::endl;
/* when the middle of x_size is reached,
* it's time to decrease the position of the asterisk!
*/
asterisk_pos += inc;
if (asterisk_pos > (x_size/2)-1)
inc *= -1;
}
return 0;
}
Output with x_size = 7:
* *
* *
* *
*
* *
* *
* *
Output with x_size = 3:
* *
*
* *
Observe the sequence in each line. Look at the first part you have:
0 space, 1 *, 5 spaces, 1 *, 0 space
1 space, 1 *, 3 spaces, 1 *, 1 space
2 spaces, 1 *, 1 space, 1 *, 2 spaces
Then for line i: i spaces followed by 1 * followed by 5-2 i spaces, followed by 1 *, followed by i spaces
Then the following should work:
for (int line=0; line<3; line++) {
for (int n=0; n<line; n++) cout << ' ';
cout << '*';
for (int n=0; n<5-2*line; n++) cout << ' ';
cout << '*';
for (int n=0; n<line; n++) cout << ' ';
cout << endl;
}
The middle line 3 is obvious, and the following is the reverse of the first part.
Another way is to observe sequence of positions of *: (0,6) (1,5) (2,4) (3,3) (4,2) (5,1) (6,0), thus:
for (int line=0; line<7; line++) {
int pos1 = line;
int pos2 = 6-line;
for (int n=0; n<7; n++) {
if (n==pos1 || n==pos2) cout << '*';
else cout << ' ';
}
cout << endl;
}
You can then obviously remove pos1 and pos2...
spaces between in upper part are decreasing by 2 and start with line - 2
spaces between in down part are incensing by 2
here how I solve your problem
void printSpaces(int count)
{
for (int i = 0; i < count; ++i) {
cout << " ";
}
}
int main()
{
int lines = 7;
int spaceBefore = 0;
int spaceBetween = lines - 2;
bool backword = false;
for (int i = 0; i < lines; ++i)
{
printSpaces(spaceBefore);
cout << "*";
if (spaceBetween > 0)
{
printSpaces(spaceBetween);
cout << "*";
}
else
{
backword = true;
}
cout << "\n";
spaceBefore = backword ? spaceBefore-1 : spaceBefore+1;
spaceBetween = backword ? spaceBetween+2 : spaceBetween-2;
}
return 0;
}
The pattern consists of two equations: x = y and x + y = 4
Just loop through the axes and plot the points that fall on any of the lines.
( y )
0 1 2 3 4
( x ) ---------------------
0 | * | | | | * |
---------------------
1 | | * | | * | |
---------------------
2 | | | * | | |
---------------------
3 | | * | | * | |
---------------------
4 | * | | | | * |
---------------------
Two Equations
x = y
x + y = 4
#include <iostream>
int main() {
int num_lines = 7;
auto on_line1 = [](int x, int y) {
return x == y;
};
auto on_line2 = [num_lines](int x, int y) {
return (x + y) == (num_lines - 1);
};
for(int x = 0; x < num_lines; x++) { // Simple looping
for(int y = 0; y < num_lines; y++) { // through the axes
if(on_line1(x, y) or on_line2(x, y)) { // If on any of the line
std::cout << '*'; // Then plot it
} else {
std::cout << ' '; // Else leave it
}
}
std::cout << '\n';
}
return 0;
}
PS: I copied the ascii table from the other answer.
If you're not required to loop you can create a string and print it.
#include <iostream>
#include <string>
int main(int argc, char * argv[]){
std::string myX("* *\n * * \n * * \n * \n * * \n * * \n* *\n");
std::cout << myX;
return 0;
}
First of all, I'm pretty new to C++ so try not to be too harsh on me. I wrote this block of code:
int LargestProduct (string numStr, int groupSize) {
int numOfGroups = numStr.size() / groupSize;
int groupsRemaining = numStr.size() % groupSize;
int largestProduct = 0, thisProduct = 1;
for (int i = 1; i <= numOfGroups; i++) {
for (int j = i; j <= groupSize; j++)
thisProduct *= (numStr[j-1] - '0');
if (thisProduct > largestProduct)
largestProduct = thisProduct;
thisProduct = 1;
}
// .. A bit more irrelevant code here
return largestProduct;
}
The function call LargestProduct ("1234567890", 2) should yield 72, but it wrongly yields 6. So, for some reason, this code will work but not as expected (Note: this code I wrote should compute the largest product of groupsSize-adjacent numbers in a big, given number called numStr).
I did some debugging, and found a strange behaviour in the nested for-loop. I set up a breakpoint inside the second for-loop
thisProduct *= (numStr[j] - '0');
After some iterations (for example, 8 iterations), this is what I would expect i and j to be:
+--------+---------+
| i | j |
+--------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
| 4 | 1 |
| 4 | 2 |
+--------+---------+
This is what really happens:
+--------+---------+
| i | j |
+--------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
+--------+---------+
And suddenly the program spits out a wrong result (6, instead of 72)
But this seems counterintuitive, to say the least. The variable i goes from 0 to numOfGroups, which in the example above equals 5. On the other hand, j goes from i to groupSize, which happens to be 2.
There should be 5*2 = 10 iterations, but there are only 3 of them. Also, in the last iteration, j should be "re-initialized" to 0. This doesn't happen though.
Anyone please help this C++ newbie?
EDIT
The problem was that the j-for-loop ranged from a moving index (i) to a non-moving index(groupSize). This was causing that "shrinking" effect in the second for-loop, which is easily fixed by changing this line:
for (int j = i; j <= groupSize; j++)
To this other one:
for (int j = i; j <= i + groupSize - 1; j++)
And to make the full algorithm to work as expected, one should also replace these lines:
int numOfGroups = numStr.size() / groupSize;
int groupsRemaining = numStr.size() % groupSize;
with this single one:
int numOfGroups = numStr.size() - 1;
EDIT 2
Everything is OK now, thank you for your kindness guys! I appreciate it. The whole code is:
int LargestProduct (string numStr, int groupSize) {
int numOfGroups = numStr.size() - 1;
int largestProduct = 0, thisProduct = 1;
for (int i = 1; i <= numOfGroups; i++) {
for (int j = i; j <= i + groupSize - 1; j++)
thisProduct *= (numStr[j-1] - '0');
if (thisProduct > largestProduct)
largestProduct = thisProduct;
thisProduct = 1;
}
return largestProduct;
}
You said:
On the other hand, j goes from 0 to groupSize
But the code says:
for (int j = i; j <= groupSize; j++)
This means j is going from i to groupSize, not 0 to groupSize
My code is as follows. My confusion occurs during the 2nd and 3rd loop. Why does the result return 1*** then 12** then 123* then 1234.. I get the j loop is reset to 0 but doesn't it reenter the k loop whenever its true that j<=i?
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= i; j++)
cout << j;
for(int k = 4 - i; k >= 1; k--)
cout << "*";
cout << endl;
}
Some clarification first:
Firstly: j is never reset to 0, but to 1.
Secondly: This is imho no triple-nested for-loop, which was be (but is not needed to have your code working as you describe it):
for(...) {
for(...) {
for(...) {
}
}
}
To your confusion:
Pretty printing your code:
for(int i=1; i<=4; i++) {
// Write the digits 1..i (1, 12, 123, 1234)
for(int j=1; j<=i; j++) {
std::cout << j;
}
// Write the stars (***, **, *)
for(int k=(4-i); k>=1; k--) {
std::cout << "*";
}
std::cout << std::endl;
}
Imagine the following sequences:
// Iteration | i | j | k | String
// 1 | 1 | 1 | 3 | 1*
// 2 | 1 | 1 | 2 | 1**
// 3 | 1 | 1 | 1 | 1***\n
// 4 | 2 | 1 | - | 1
// 5 | 2 | 2 | - | 12
// 6 | 2 | 2 | 2 | 12*
// 7 | 2 | 2 | 1 | 12**\n
// 8 | 3 | 1 | - | 1
// 9 | 3 | 2 | - | 12
// 10 | 3 | 3 | - | 123
// 11 | 3 | 3 | 1 | 123*\n
// 12 | 4 | 1 | - | 1
// 13 | 4 | 2 | - | 12
// 14 | 4 | 3 | - | 123
// 15 | 4 | 4 | - | 1234\n
The k-loop is reentered, if the initial index:
// k:=(4-i) >= 1
So entering the k-Loop is exclusively dependent on the index i.
Mathematically:
// (4-i) >= 1
// <=> -i >= (1-3)
// <=> -i >= -3
// <=> i <= 3
So the k-loop is reentered, as long as i is <= 3.
In order to get the effect you want your code should be like this:
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= i; j++)
{
cout << j;
for(int k = 4 - i; k >= 1; k--)
cout << "*";
}
cout << endl;
}
if you dont have the {} the k loop is executed only after finishing the j loop
So I am writing a sudoku solver in C++ and have run into a little snag. Below is my solve board code. It works for the first 3 rows of the puzzle, but unrecurses when hitting the end of the 4th row. Looking at the code on gdb it hits the end of the 4th row, backtracks to 6th column, tries and then unrecurses out to the end.
A couple of other notes about the code is the matrix which holds the sudoku board begins at 1,1 not 0,0. So when solveBoard is initially called the parameters are (1, 1, 0). I have also attached the setCell and checkConflicts functions for more insight on there. I have three vectors rowConf,colConf and squConf to store the values that have already been placed in the respective row, column, or square. I have been at this for hours and cannot get it to go past the 3rd row. Any assistance is greatly appreicated. Thanks!
EDIT: Added clearCell()
bool board::solveBoard(int i, int j, int count)
{
if (j > 9)
{
j = 1;
i++;
printBoard();
if (isSolved())
{
printBoard();
cout <<"The Board has been solved!" <<endl
<<" The number of recursive calls was: " <<count <<endl;
return true;
}
}
if (isBlank(i, j))
{
for (int n = 1; n < 10; n++)
{
if (setCell(i, j, (char)n + '0'))
{
if (solveBoard(i, j + 1, count + 1))
{
return true;
}
}
}
}
else
{
return (solveBoard(i, j + 1, count + 1));
}
clearCell(i, j);
return false;
}
bool board::setCell(int i, int j, char val)
{
int intVal;
intVal = atoi(&val);
if (i >= 1 && i <= BoardSize && j >= 1 && j <= BoardSize &&
intVal >= 1 && intVal <= BoardSize)
{
if (!(checkConflicts(intVal, i, j, squareNumber(i, j))))
{
return false;
}
value[i][j] = intVal;
// Set flags of the conflicts
rowConf[i][intVal] = true;
colConf[j][intVal] = true;
squConf[squareNumber(i, j)][intVal] = true;
return true;
}
else
{
throw rangeError("bad value in setCell");
}
}
bool board::checkConflicts(int val, int i, int j, int k)
{
if (i < 1 && i > BoardSize && j < 1 && j > BoardSize &&
k < 1 && k > BoardSize && val < 1 && val > BoardSize)
{
throw rangeError("bad value in checkConflicts()");
}
if (rowConf[i][val] || colConf[j][val] || squConf[k][val])
{
return false;
}
else
{
return true;
}
}
Initial Board:
-----------------------------
| 3 | 8 | -----------------------------
| | 7 | 5 -----------------------------
| 1 | | -----------------------------
-----------------------------
| | | 3 6 -----------------------------
| 2 | 4 | -----------------------------
| 7 | | -----------------------------
-----------------------------
| | 6 | 1 3 -----------------------------
| 4 5 | 2 | -----------------------------
| | | 8 -----------------------------
-----------------------------
Final Output:
-----------------------------
| 3 2 4 | 1 8 5 | 6 7 9 -----------------------------
| 6 8 9 | 7 2 3 | 4 1 5 -----------------------------
| 1 5 7 | 4 9 6 | 2 8 3 -----------------------------
-----------------------------
| | | 3 6 -----------------------------
| 2 | 4 | -----------------------------
| 7 | | -----------------------------
-----------------------------
| | 6 | 1 3 -----------------------------
| 4 5 | 2 | -----------------------------
| | | 8 -----------------------------
-----------------------------
void board::clearCell(int i, int j)
{
int intVal;
if (i >= 1 && i <= BoardSize && j >= 1 && j <= BoardSize)
{
if (value[i][j] != -1)
{
intVal = value[i][j];
rowConf[i][intVal] = false;
colConf[j][intVal] = false;
squConf[squareNumber(i, j)][intVal] = false;
value[i][j] = -1;
}
}
else
{
throw rangeError("bad value in setCell");
}
}
Your problem is most likely here:
if (isBlank(i, j))
{
for (int n = 1; n < 10; n++)
{
if (setCell(i, j, (char)n + '0'))
{
if (solveBoard(i, j + 1, count + 1))
{
return true;
}
}
}
}
Somehow it is going through this section, which is why it isn't going through the else in the end, but since it hasn't returned before, it gets stuck.
This needs more debugging, but here is an idea that could lead to a solution:
if (isBlank(i, j))
{
for (int n = 1; n < 10; n++)
{
if (setCell(i, j, (char)n + '0'))
{
if (solveBoard(i, j + 1, count + 1))
{
return true;
} else {
echo 'Looks like it ended on the farthest-level..';
}
} else {
echo 'Looks like it ended on the second-farthest level.';
}
}
The atoi function expects a string as an argument, that is an array of chars terminated with character '\0', ASCII NUL. You give a parameter being a pointer to a character (equivalent to some arrray of chars) but do not guarantee it is zero-terminated. Please replace intVal = atoi(&val); with intVal = (int)val - '0';
And your checkConflicts should have || operators instead of && in the first if.
These are probably not reasons of the error but certainly need correction.