问题描述
我正在尝试完成一项作业任务,该任务在屏幕周围显示一个弹跳的球".我有弹跳球,但作业还需要:
此外,程序将在遍历的数组的每个元素中保留字符大写字母"O"的副本.此外,程序还将计算球所经过的唯一数组元素的数量.当程序终止时,它将报告球所经过的数组的唯一元素的数量.
我的问题是我不知道从哪里开始使这种事情发生.有指针吗?下面,我包括了我当前的代码.
I am trying to complete a homework assignment that shows a bouncing "ball" around the screen. I have the bouncing ball working, but the assignment also requires:
In addition, the program will leave a copy of the character capital "O" in each element of the array which it has traversed. Also, the program will count the number of unique array elements which the ball has traversed. When the program terminates, it will report the number of unique elements of the array which the ball has traversed.
My problem is I can''t figure out where to start to get this to happen. Any pointers? Below I have included my current code.
#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
/*--------------------------- local variables ------------------------------*/
int ball[80][23];
void gotoxy(int, int);
int noOfCoords = 0;
int origX;
int origY;
char mov1;
char mov2;
int x;
int y;
/*----------------------------- user prompt --------------------------------*/
cout << "Enter the X coordinate for the ball (between 0 and 79): ";
cin >> origX;
cout << endl;
cout << "Enter the Y coordinate for the ball (between 0 and 22): ";
cin >> origY;
cout << endl;
cout << "Would you like the ball to move [u]p or [d]own? ";
cin >> mov1;
cout << endl;
cout << "would you like the ball to move [l]eft or [r]ight? ";
cin >> mov2;
cout << endl;
system("CLS"); //clear the screen
x = origX; //make x and y equal the original entries
y = origY;
ball[x][y]; //enter coordinate into ball array
gotoxy(x, y); //go to the coordinate
printf("O"); //print O at the coordinate
noOfCoords++; //add 1 to the counter
/*------------------------- move the ball once -----------------------------*/
if (mov1 == ''u'')
{
if (y > 0)
{
y--;
}//endif
else
{
mov1 = ''d'';
}//endif
}//endif
if (mov1 == ''d'')
{
if (y < 22)
{
y++;
}//endif
else
{
mov1 = ''u'';
}//endif
}//endif
if (mov2 == ''r'')
{
if (x < 79)
{
x++;
}//endif
else
{
mov2 = ''l'';
}//endif
}//endif
if (mov2 == ''l'')
{
if (x > 0)
{
x--;
}//endif
else
{
mov2 = ''r'';
}//endif
}//endif
ball[x][y]; //enter the coordinate into ball array
gotoxy(x, y); //go to coordinate
printf("O"); //print O at the coordinate
noOfCoords++; //add 1 to the counter
/*------------------------ ALGORITHM to move ball ---------------------------*/
while ((x != origX) || (y != origY))
{
if (mov1 == ''u'')
{
if (y > 0)
{
y--;
}//endif
else
{
mov1 = ''d'';
}//endif
}//endif
if (mov1 == ''d'')
{
if (y < 22)
{
y++;
}//endif
else
{
mov1 = ''u'';
}//endif
}//endif
if (mov2 == ''r'')
{
if (x < 79)
{
x++;
}//endif
else
{
mov2 = ''l'';
}//endif
}//endif
if (mov2 == ''l'')
{
if (x > 0)
{
x--;
}//endif
else
{
mov2 = ''r'';
}//endif
}//endif
ball[x][y];
gotoxy(x, y);
printf("O");
noOfCoords++;
system("CLS");
}//endwhile
//output the number of coordinates the O passed through
cout << noOfCoords;
cout << endl;
system("PAUSE");
return 0;
}//endmain
//gotoxy definition
void gotoxy(int eex, int eey)
{
COORD coord;
coord.X = eex;
coord.Y = eey;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
</iostream></stdio.h></windows.h></conio.h>
推荐答案
ball[x][y]; //enter the coordinate into ball array
这句话毫无意义.
在代码的开头,您应该将ball
数组的所有元素初始化为零(或相似的值).然后在gotoxy()
函数中,应检查数组元素ball[x][y]
,如果它为空,则将其设置为某个值(也许为1),并将1添加到唯一元素的计数中.在程序的末尾打印此计数.
This statement is meaningless.
At the beginning of the code you should initialise all elements of the ball
array to zero (or similar). Then in your gotoxy()
function you should check the array element ball[x][y]
, if it is empty then set it to some value (1 perhaps) and add 1 to the count of unique elements. At the end of the program print this count.
#include <conio.h>
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
/*--------------------------- local variables ------------------------------*/
int ball[80][23];
void gotoxy(int, int);
int noOfCoords = 0;
int origX;
int origY;
char mov1;
char mov2;
int x;
int y;
/*----------------------------- user prompt --------------------------------*/
cout << "Enter the X coordinate for the ball (between 0 and 79): ";
cin >> origX;
cout << endl;
cout << "Enter the Y coordinate for the ball (between 0 and 22): ";
cin >> origY;
cout << endl;
cout << "Would you like the ball to move [u]p or [d]own? ";
cin >> mov1;
cout << endl;
cout << "would you like the ball to move [l]eft or [r]ight? ";
cin >> mov2;
cout << endl;
system("CLS"); //clear the screen
x = origX; //make x and y equal the original entries
y = origY;
//initialize the ball array''s elements to 0
for(int row = 0; row < 79; row++)
{
for(int col = 0; col < 22; col++)
ball[row][col] = 0;
}
gotoxy(x, y); //go to the coordinate
printf("O"); //print O at the coordinate
//change the ball element to 1 if not 0 and increase the counter
if (ball[x][y] == 0)
{
ball[x][y] = 1;
noOfCoords++;
}//endif
/*------------------------- move the ball once -----------------------------*/
if (mov1 == ''u'')
{
if (y > 0)
{
y--;
}//endif
else
{
mov1 = ''d'';
}//endif
}//endif
if (mov1 == ''d'')
{
if (y < 22)
{
y++;
}//endif
else
{
mov1 = ''u'';
}//endif
}//endif
if (mov2 == ''r'')
{
if (x < 79)
{
x++;
}//endif
else
{
mov2 = ''l'';
}//endif
}//endif
if (mov2 == ''l'')
{
if (x > 0)
{
x--;
}//endif
else
{
mov2 = ''r'';
}//endif
}//endif
gotoxy(x, y); //go to coordinate
printf("O"); //print O at the coordinate
//change the ball element to 1 if not 0 and increase the counter
if (ball[x][y] == 0)
{
ball[x][y] = 1;
noOfCoords++;
}//endif
/*------------------------ ALGORITHM to move ball ---------------------------*/
//while the coordinates are not the same as entered
while ((x != origX) || (y != origY))
{
//move the ball in the correct direction
if (mov1 == ''u'')
{
if (y > 0)
{
y--;
}//endif
else
{
mov1 = ''d'';
}//endif
}//endif
if (mov1 == ''d'')
{
if (y < 22)
{
y++;
}//endif
else
{
mov1 = ''u'';
}//endif
}//endif
if (mov2 == ''r'')
{
if (x < 79)
{
x++;
}//endif
else
{
mov2 = ''l'';
}//endif
}//endif
if (mov2 == ''l'')
{
if (x > 0)
{
x--;
}//endif
else
{
mov2 = ''r'';
}//endif
}//endif
gotoxy(x, y);
printf("O");
if (ball[x][y] == 0)
{
ball[x][y] = 1;
noOfCoords++;
}//endif
system("CLS");
}//endwhile
//output the number of coordinates the O passed through
cout << endl;
cout << "The ball crossed " << noOfCoords << " unique coordinates.";
cout << endl;
system("PAUSE");
return 0;
}//endmain
//gotoxy definition
void gotoxy(int eex, int eey)
{
COORD coord;
coord.X = eex;
coord.Y = eey;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}//endgotoxy
这篇关于在C ++中弹跳O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!