看板 cksh75th09 關於我們 聯絡資訊
※ 引述《quaker (山貓)》之銘言: : ※ 引述《mmx (瑞)》之銘言: : : There is a possibility that this may take quite some time, so the : : pargram will prompt for the maximum number of steps the user wants : : the robot to take. : : 這一段話是啥意思?是說走完要告訴user總共走幾步嗎? : 是說robot可能永遠走不出來...或走很久... : 所以要設定步數的最大值 : (由使用者設定) : 超過此值則程式自動中斷 嗯嗯嗯...了解那我改一下下..... 在下面.. : : //Random Walk Of A Robot : : #include <iostream.h> : : #include <stdlib.h> : : int cur_row=7; //robot位置記錄用,初始位置為(7,0),出口在(4,19) : : int cur_col=0; : : int steps=0; //記錄總共走了幾步 : : void move_up(char a[][20]); : : void move_down(char a[][20]); : : void move_left(char a[][20]); : : void move_right(char a[][20]); : : void main(){ int maximum_steps; : : int direction; : : char room[10][20]={'#','#',.....}; //設定房間 起始點設'r' : : randomize(); cout<<"\n鬍子主人,請設定最大容忍步數\n"; cin>>maximum_steps; : : while(!(cur_row==4 && cur_col==19)){ if(maximum_steps<=steps){ cout<<"鬍子主人,我走不出來啦..>_<"; return; } : : direction=rand()%4; : : switch(direction){ : : case 0 : move_up(room);break; : : case 1 : move_down(room);break; : : case 2 : move_left(room);break; : : case 3 : move_right(room);break; : : } : : } : : cout<< "\n 親愛的鬍子主人,我走出來了,共走了"<<steps<<"步 \n"; : : for(int i=0;i<10;i++){ : : cout<<"\n"; : : for(int j=0;j<20;j++) : : cout<<room[i][j]; : : } : : } : : void move_up(char a[][20]){ : : if(a[cur_row-1][cur_col]!='#'){ : : a[cur_row][cur_col]='*'; : : cur_row--; : : a[cur_row][cur_col]='r'; : : steps++; : : } : : } : : void move_down(char a[][20]){ : : if(a[cur_row+1][cur_col]!='#'){ : : a[cur_row][cur_col]='*'; : : cur_row++; : : a[cur_row][cur_col]='r'; : : steps++; : : } : : } : : void move_left(char a[][20]){ : : if(a[cur_row][cur_col-1]!='#' && cur_col-1 !=0){ : : a[cur_row][cur_col]='*'; : : cur_col--; : : a[cur_row][cur_col]='r'; : : steps++; : : } : : } : : void move_right(char a[][20]){ : : if(a[cur_row][cur_col+1]!='#'){ : : a[cur_row][cur_col]='*'; : : cur_col++; : : a[cur_row][cur_col]='r'; : : steps++; : : } : : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.216.84 ※ 編輯: mmx 來自: 140.115.216.84 (05/19 11:48)