软件保障与测试课程实践记录:贪吃蛇小程序
?
對象:貪吃蛇小程序?
?
(原代碼見?https://blog.csdn.net/leslie5205912/article/details/78980006)
?
測試過程:
程序改動部分:
1.bug修復(fù)
?
經(jīng)過測試,發(fā)現(xiàn)了一個bug:豆子的生成并不是隨機(jī)產(chǎn)生的。
?
生成豆子部分代碼如下:
?
void Creatfood(char map[Row_max][Line_max], int snake[Row_max][Line_max]) {//生成豆子
do{
food_x = rand() % (Line_max - 2) + 1;
food_y = rand() % (Row_max - 2) + 1;
} while (snake[food_y][food_x] != 0||map[food_y][food_x]=='#');
map[food_y][food_x] = '*';
}
?
此處原作者顯然是準(zhǔn)備隨機(jī)生成豆子,然鵝沒有給與隨機(jī)數(shù)種子,可能是忘記了?
?
于是加上srand(time(NULL)); 語句根據(jù)時間生成隨機(jī)數(shù)種子
?
(此處原作者使用?do{ }while (snake[food_y][food_x] != 0||map[food_y][food_x]=='#');語句保證隨機(jī)生成的豆子不在蛇身上且沒有落在游戲邊框上)
?
經(jīng)測試改動后無bug,種子能夠正常隨機(jī)生成了
2.主頁面美化
?
原作者為了代碼簡潔,主頁面僅顯示一句"Press any key to start this game!"
?
還是美化一下吧orz
?
代碼如下:
?
cout << " ??*** ?????****" << endl;
cout << " ????**** ??* ?**Q ????*<--food" << endl;
cout << " ???????*****" << endl;
cout << "" << endl;
cout << "-------The Gluttonous Snake-------" << endl;
cout << "" << endl;
cout << " ?Use ??↑ ???????W" << endl;
cout << " ?????←↓→ or ?A S D to control" << endl;
cout << "" << endl;
cout << "" << endl;
cout << " Press any key to start this game!" << endl;
改動后:
?
?
3.實時顯示分?jǐn)?shù)
?
玩家可能需要一個實時顯示分?jǐn)?shù)的功能?
?
于程序運(yùn)行主要循環(huán)代碼,也就是switch (ch) 部分添加語句如下
?
printf("\nScore:%d\n",score*10);
?
即可在每一次循環(huán)時輸出當(dāng)時分?jǐn)?shù)的值
?
改動后:
?
?
疑問部分:
?
想要實現(xiàn)按下指定按鍵即重新執(zhí)行程序,按下除此外任意鍵退出
?
但是使用while語句后,的確可以重新回到主頁,但是不能正常繼續(xù)執(zhí)行程序,Creatsnake(snake);Creatmap(map);Creatfood(map, snake);Run(map, snake);均無法運(yùn)行,會直接跳轉(zhuǎn)到結(jié)束頁Result();
?
使用代碼如下:
?
int main() {
int k;
while(1){
system("cls");
cout << " ??*** ?????****" << endl;
cout << " ????**** ??* ?**Q ????*<--food" << endl;
cout << " ???????*****" << endl;
cout << "" << endl;
cout << "-------The Gluttonous Snake-------" << endl;
cout << "" << endl;
cout << " ?Use ??↑ ???????W" << endl;
cout << " ?????←↓→ or??A S D to control" << endl;
cout << "" << endl;
cout << "" << endl;
cout << " Press any key to start this game!" << endl;
getch();
Creatsnake(snake);
Creatmap(map);
Creatfood(map, snake);
Run(map, snake);
Result();
k=getch();
if(k!='Y')break;
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/sakurazer0/p/10463329.html
總結(jié)
以上是生活随笔為你收集整理的软件保障与测试课程实践记录:贪吃蛇小程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P4332 [SHOI2014]三叉神经
- 下一篇: scrapy two