2014-03-04 30 views
-1

好吧,所以我想做一个三角向左和/或向右移动,只有1个空格。请随身携带,因为我仍然在学习如何使用C++进行编码。如何在C++中移动ASCII艺术图画?

cout << setw(10) << "*" << endl; 
cout << setw(9) << "*" << setw(2) << "*" << endl; 
cout << setw(8) << "*" << setw(4) << "*" << endl; 
cout << setw(7) << "*" << setw(6) << "*" << endl; 
cout << setw(6) << "*" << setw(8) << "*" << endl; 
cout << setw(5) << "*" << setw(10) << "*" << endl; 
cout << setw(4) << "*" << setw(12) << "*" << endl; 
cout << setw(3) << "*" << setw(14) << "*" << endl; 
cout << setw(2) << "*" << setw(16) << "*" << endl; 
cout << setfill('*') << setw(19) << "*" << endl; 

while(ch = 'a','d'){ 
    cout << "Enter: 'a' move left, 'd' move right" << endl; 
    cin >> ch; 

    if(ch == 'a'){ 
    cout.setf(ios::left); 

上面的代码显示了三角形的样子。所以基本上我想让用户输入'a'[向左移动]和/或'b'[向右移动]。我如何编程它,因此它会移动整个三角形以移动1个空间,左侧或右侧,同时保持形状?

基于我的看法,使用cout.setf(ios :: left)将是最简单的,但我不知道该怎么做。

如果任何人都可以教我,我会非常感激。

在此先感谢。

+1

最大的可能是,通过'而(CH ='a','d')'你的意思是'while(ch =='a'|| ch =='d')'。 –

回答

0

对图形使用cout总是有点奇怪。它并非真正为绘画/动画设计。

你可以(使用某些功能,从您的while循环中重复打印三角形)尝试这样:

void print_triangle(int n) 
{ 
    cout << setw(n + 10) << "*" << endl; 
    cout << setw(n + 9) << "*" << setw(2) << "*" << endl; 
    cout << setw(n + 8) << "*" << setw(4) << "*" << endl; 
    cout << setw(n + 7) << "*" << setw(6) << "*" << endl; 
    cout << setw(n + 6) << "*" << setw(8) << "*" << endl; 
    cout << setw(n + 5) << "*" << setw(10) << "*" << endl; 
    cout << setw(n + 4) << "*" << setw(12) << "*" << endl; 
    cout << setw(n + 3) << "*" << setw(14) << "*" << endl; 
    cout << setw(n + 2) << "*" << setw(16) << "*" << endl; 
    cout << setfill('*') << setw(19) << "*" << endl; 
} 

void clear_screen() 
{ 
    cout << string(100, '\n'); 
} 

使用,如:

int n = 0; 
while(ch == 'a' || ch == 'd'){ 
    clear_screen(); 
    print_triangle(n); 
    cout << "Enter: 'a' move left, 'd' move right" << endl; 
    cin >> ch; 

    if(ch == 'a'){ 
     n++; 
    } else if(ch == 'd'){ 
     n--; 
    } 
}