看板 cksh75th09 關於我們 聯絡資訊
※ 引述《berserk (鬍子)》之銘言: : 我作業寫不出來啊 : 事關畢業麻煩會的人幫一下吧=_= 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總共走幾步嗎? 下面的程式不知道行不行ꄮ.... //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 direction; char room[10][20]={'#','#',.....}; //設定房間 起始點設'r' randomize(); while(!(cur_row==4 && cur_col==19)){ 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 04:01)