软件保障与测试课程实践记录:贪吃蛇小程序
?
對象:貪吃蛇小程序?
?
(原代碼見?https://blog.csdn.net/leslie5205912/article/details/78980006)
?
測試過程:
程序改動部分:
1.bug修復
?
經過測試,發現了一個bug:豆子的生成并不是隨機產生的。
?
生成豆子部分代碼如下:
?
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] = '*';
}
?
此處原作者顯然是準備隨機生成豆子,然鵝沒有給與隨機數種子,可能是忘記了?
?
于是加上srand(time(NULL)); 語句根據時間生成隨機數種子
?
(此處原作者使用?do{ }while (snake[food_y][food_x] != 0||map[food_y][food_x]=='#');語句保證隨機生成的豆子不在蛇身上且沒有落在游戲邊框上)
?
經測試改動后無bug,種子能夠正常隨機生成了
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.實時顯示分數
?
玩家可能需要一個實時顯示分數的功能?
?
于程序運行主要循環代碼,也就是switch (ch) 部分添加語句如下
?
printf("\nScore:%d\n",score*10);
?
即可在每一次循環時輸出當時分數的值
?
改動后:
?
?
疑問部分:
?
想要實現按下指定按鍵即重新執行程序,按下除此外任意鍵退出
?
但是使用while語句后,的確可以重新回到主頁,但是不能正常繼續執行程序,Creatsnake(snake);Creatmap(map);Creatfood(map, snake);Run(map, snake);均無法運行,會直接跳轉到結束頁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;
}
}
轉載于:https://www.cnblogs.com/sakurazer0/p/10463329.html
總結
以上是生活随笔為你收集整理的软件保障与测试课程实践记录:贪吃蛇小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P4332 [SHOI2014]三叉神经
- 下一篇: Python字典和集合