+ 5

C++ programming question ( 9 )

https://code.sololearn.com/cgKcmyccu4i4/?ref=app Hi everyone, I tried to make only one square but the square keeps repeating, could anyone help me? Thanks.

10th Mar 2018, 1:42 PM
Eros boulay
Eros boulay - avatar
11 Respuestas
+ 14
//only a small logical error 👍[corrected now] #include <iostream> #include <windows.h> using namespace std; bool gameOver; const int width=20; const int height=20; int x,y, fruitX, fruitY, score; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN}; eDirection 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+1;i++) cout<< "#"; cout<< endl; for(int i=0;i<height;i++) { for(int j=0;j<width;j++) { if(j==0) cout<< "#"; else cout<<" "; if(j== width-1) cout<<"#"; }cout<< endl; } for(int i=0;i<width+1;i++) cout<< "#"; cout<< endl; } void Input() { } void Logic() { } int main() { Setup(); if(!gameOver) //👈 { Draw(); Input(); Logic(); } return 0; }
10th Mar 2018, 1:46 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
You use the draw function in an neverending loop.
10th Mar 2018, 1:47 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 3
@ Gaurav @Aaron thanks, I got it
10th Mar 2018, 1:51 PM
Eros boulay
Eros boulay - avatar
+ 2
Anyway, after your advice the error is gone.
11th Mar 2018, 5:06 PM
Eros boulay
Eros boulay - avatar
+ 1
The Problem with the SL playground is, that you can't interact with your code while the program is running (except web). This means you have to specify the input before running which makes games here nearly impossible. But you could still run this code in a real console on a PC. The input doesn't work because SL waits for cin for input and doesn't accept your input with getch, so the '0' doesn't move. (And now you need your loop again, if you want to move more than once). Btw. if your really want to create a nice console game I'd recommend you the ncurses library (for Linux/UNIX) that helps you to handle the Output and input (even mouse interaction) in a comfortable way.
11th Mar 2018, 6:27 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Thank you @Aaron ,I'll try it
11th Mar 2018, 6:30 AM
Eros boulay
Eros boulay - avatar
+ 1
..\Playground\: In function 'void Logic()': ..\Playground\:83:22: error: 'in' was not declared in this scope switch(in) ^ void Input() { char in; cin>> in; { switch(in) { 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() { while(!gameOver) switch(in) { case LEFT : x--; break ; case RIGHT: x++; break ; case UP: y--; break ; case DOWN: y++; break ; default: break; How can we solve this problem?
11th Mar 2018, 7:22 AM
Eros boulay
Eros boulay - avatar
+ 1
First: the syntax of the while loop in void logic() is incomplete (and the loop wouldn't make sense Second: you want to use dir instead of in for the switch statement because Input() "translates" w/a/s/d into a direction (dir) that you can use in logic()
11th Mar 2018, 8:06 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
@Aaron Thank you so much , I just modified it with my cell phone , but I haven't tried running it with a computer so I don't know if it can move yet.
11th Mar 2018, 5:05 PM
Eros boulay
Eros boulay - avatar
0
Hi @ Gaurav I would like to make ' 0 ' move so I continued my code but ' 0 ' still don't move. I don't know why.
10th Mar 2018, 3:18 PM
Eros boulay
Eros boulay - avatar
0
Theoretically it’s a square, but actually it’s a rectangle 😂😂。
10th Mar 2018, 5:48 PM
liliboulay
liliboulay - avatar