我正在處理 ASCII 影片,從 .txt 檔案列印影像后,我測驗了一個 6 個字符的塊,在 for 回圈中更改每個“幀”,效果很好,塊中的代碼注釋掉了部分。然而,在添加了一個演算法來制作一個看起來像爆炸錐的影片之后,函式中最外層的 for 回圈只迭代一次。
第一次生成最外層回圈的每次迭代都要生成的文本塊,但回圈在那里停止,然后在列印該文本塊后的 0.5 秒內,程式由于某種原因關閉,即使 animation_section( ) 函式很好地迭代了 for 回圈,并且程式沒有突然終止。
因為文本塊實際上是為每一行生成的,所以我不知道這個問題是如何產生的,因為這意味著新代碼中的兩個內部 for 回圈都可以正常運行,而最外面的 for 回圈使用在塊注釋掉部分中看到的輸出文本的更手動的方法運行良好。
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>
#include <limits>
#include <windows.h>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std::literals;
void gotoxy( int column, int line ){
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle( STD_OUTPUT_HANDLE ),
coord
);
}
void setPos(int col, int row){
gotoxy(col, row); // windows
//printf("\033[%d;%dH", row, col); // linux/terminal
//std::cout << "\033[F"; // for single line up, linux/terminal
}
std::string global_sequence1 = "!@#$%^&*()-_ =`~,.<>/';]}[{\\|";
std::string global_sequence2 = "qwertyuiopasdfghjklzxcvbnm";
std::string sequence_selection(std::string x){
std::string y = x;
random_shuffle(y.begin(), y.end());
std::string first_five = y.substr(0, 5);
return first_five;
}
void list_generation(std::vector<std::string>& list1, std::vector<std::string>& list2){
for (int i=0; i<30; i ){
list1.push_back(sequence_selection(global_sequence1));
list2.push_back(sequence_selection(global_sequence2));
}
}
void Animation_section(){
std::vector<std::string> s_list1; // random 5 chars from global_sequence1
std::vector<std::string> s_list2; // random 5 chars from global_sequence2
list_generation(s_list1, s_list2);
auto duration = 10000s;
auto duration_step = 50ms;
for (auto i = 0; i < (duration/duration_step); i) {
// new code start
int col_ref0 = 53;
for (int k = 11; k <= 33; k ){ // per line
setPos(col_ref0-floor(k*1.3), k); // higher floor coef moves blast cone left
int a = floor(1.1892*k - 13.614); // min line length, line eq = 1.1892*k - 13.614
int b = a floor(k/6);
int line_len = a (rand() % static_cast<int>(b - a 1));
// add a scrambler for list1 and list2 before iterating through each line to avoid repeititon
for (int j = 0; j <= line_len; j ){ // iterates through each col in the line length
if ( 0.80 > ((float) rand() / (RAND_MAX)) ){ // selects whether or not the coord displays something
if ( 0.60 > ((float) rand() / (RAND_MAX)) ){
std::cout << s_list1[j][i%s_list1[j].size()]; // print list1
}else{
std::cout << s_list2[j][i%s_list2[j].size()]; // print list2
}
}else{
std::cout << " ";
}
if (j == line_len){
std::cout << std::flush;
}
}
}
// new code end
/*
setPos(20,20);
std::cout << s_list1[0][i%s_list1[0].size()] << s_list1[1][i%s_list1[1].size()] <<
s_list1[2][i%s_list1[2].size()] << "\n" << std::flush;
setPos(20,21);
std::cout << s_list2[0][i%s_list2[0].size()] << s_list2[1][i%s_list2[1].size()] <<
s_list2[2][i%s_list2[2].size()] << std::flush;
setPos(20,20);
*/
std::this_thread::sleep_for(duration_step);
// add if statement for breaking animation to next screen for linux
if (GetAsyncKeyState(VK_RETURN)){
break;
}
}
}
int main(){
//welcome();
Animation_section();
//std::cin.get(); //press key to continue
std::system("cls");
loadingSymbol();
std::cin.get(); //press key to continue
}
這是在程式自行終止之前產生的
這是從舊塊注釋掉部分產生的,左側的 6 個字符塊正在按預期更改每個“幀”
uj5u.com熱心網友回復:
在最后一次迭代中,您最終得到:
a = 25
b = 30
int line_len = a (rand() % static_cast<int>(b - a 1))
b - a 1 == 6
25 rand % 6 has a maximum value of 30
30 is out of range on your s_list2 and s_list1 arrays
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/511110.html
標籤:C 循环
