



Elabore um programa que crie tabuleiro 5×5 do jogo roboWarriors.
Inicialmente o sistema deverá solicitar a posição inicial (x,y) de um robô
no tabuleiro e também solicitar a orientação do robô (“cim”, “bax”, “dir” ou “esq”).
Depois desta etapa, o programa deverá ficar aguardando comandos do usuário para a
movimentação do robô.
Os comandos aceitos são:
L //faz o robô girar 90 graus para a esquerda (Left)
R //faz o robô girar 90 graus para a direita (Right)
F //faz o robô andar uma casa para a frente (Front)
S //termina o programa
Os seguintes caracteres para determinar a orientação do robô:
“^” para “cim”,
“V” para “bax”,
“>” para “dir” e
“<" para "esq".
Após cada comando, o programa deverá exibir a posição atualizada do robô.
O robô não poderá sair do tabuleiro.
Sugestão de resposta em Pascal, Lazarus, Delphi (Console)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | { Desenvolvido por Giovani Da Cruz - showdelphi.com.br } program Robo; {$mode objfpc}{$H+} uses {$IFDEF UNIX} {$IFDEF UseCThreads} cthreads, {$ENDIF} {$ENDIF} Classes, SysUtils, Console, windows, CustApp { you can add units after this }; type TDirection = (drTop, drBotton, drLeft, drRight); { TRobo } TRobo = class(TCustomApplication) private FXPosition: integer; FYPosition: integer; FDirection: TDirection; function GetCharDirection: char; protected procedure DoRun; override; public property Direction: char read GetCharDirection; property XPosition: integer read FXPosition; property YPosition: integer read FYPosition; constructor Create(AOwner: TComponent); override; overload; constructor Create(AOwner: TComponent; X, Y : Integer; aDirection: TDirection); overload; procedure DrawScreen; procedure SetDirection(aDirection: TDirection); procedure MovRobo(); procedure GiraRobo(Lado: char); end; const TAM_LARGURA = 20; TAM_AUTURA = 16; { TMyApplication } function TRobo.GetCharDirection: char; begin case FDirection of drBotton: Result := 'V'; drTop: Result := '^'; drLeft: Result := '<'; drRight: Result := '>'; end; end; procedure TRobo.DoRun; var ErrorMsg: string; begin { futuramente colocar a parte inicial aqui } Terminate; end; constructor TRobo.Create(AOwner: TComponent); begin inherited Create(AOwner); Self.FXPosition := 3; Self.FYPosition := 8; SetDirection(drBotton); end; constructor TRobo.Create(AOwner: TComponent; X, Y: Integer; aDirection: TDirection); begin Self.Create(AOwner); FYPosition:= y; FXPosition:= x; SetDirection(aDirection); end; procedure TRobo.DrawScreen; var I, L: integer; begin ClrScr; WriteLn('Bem vindo ao jogo do roboWarriors, desenvolvido por Giovani Da Cruz'); WriteLn('Comandos:'); WriteLn('R ->faz o robo girar 90 graus para a direita'); WriteLn('L ->faz o robo girar 90 graus para a esquerda'); WriteLn('F ->faz o robo andar para frente'); WriteLn('S ->Termina o programa'); { Montando o cenario } for I := 0 to TAM_LARGURA do begin Write('_'); end; for L := 1 to 5 do begin WriteLn(''); for I := 0 to TAM_LARGURA do begin if I mod 4 = 0 then Write('|') else Write(' '); end; WriteLn(''); for I := 0 to TAM_LARGURA do begin if I mod 4 = 0 then Write('|') else Write('_'); end; end; { Desenhando o robo } GotoXY(Self.XPosition, Self.YPosition); Write(Self.Direction); GotoXY(1, 19); Write('Esperando o proximo comando: '); end; procedure TRobo.SetDirection(aDirection: TDirection); begin FDirection := aDirection; end; procedure TRobo.MovRobo; var AuxX, AuxY : Integer; begin AuxX := FXPosition; AuxY := FYPosition; case FDirection of drBotton: AuxY := AuxY + 2; drTop: AuxY := AuxY - 2; drLeft: AuxX := AuxX - 4; drRight: AuxX := AuxX + 4; end; if (AuxY > TAM_AUTURA) or (AuxX > TAM_LARGURA) or (AuxY < 8) or (AuxX < 3) then begin { Robo batendo na parede } Beep(850, 500); end else begin FXPosition := AuxX; FYPosition := AuxY; end; end; procedure TRobo.GiraRobo(Lado: char); begin if Lado = 'L' then begin case FDirection of drBotton: SetDirection(drRight); drTop: SetDirection(drLeft); drLeft: SetDirection(drBotton); drRight: SetDirection(drTop); end; end; if Lado = 'R' then begin case FDirection of drBotton: SetDirection(drLeft); drTop: SetDirection(drRight); drLeft: SetDirection(drTop); drRight: SetDirection(drBotton); end; end; end; var Application: TRobo; Command: char; x, y : Integer; posicaoIni : string; aDirection: TDirection; begin { Lendo X } repeat Write('Informe a posicao X [1..5]: '); Readln(x); if ((x < 1) or (x > 5)) then begin WriteLn('X e invalido!'); Write('Enter para informar novamente!'); ReadLn; end; until ((x >= 1) and (x <= 5)); x := 3 + (x-1)*4; { Lendo Y } repeat Write('Informe a posicao Y [1..5]: '); Readln(y); if ((y < 1) or (y > 5)) then begin WriteLn('Y e invalido!'); Write('Enter para informar novamente!'); ReadLn; end; until ((y >= 1) and (y <= 5)); y := 8 + (y-1)*2; { Lendo a posicao inicial do robo } repeat Write('Informe a posicao inicial do robo ("cim", "bax", "dir" ou "esq"): '); ReadLn(posicaoIni); posicaoIni:= LowerCase(posicaoIni); if ((posicaoIni <> 'cim') and (posicaoIni <> 'bax') and (posicaoIni <> 'dir') and (posicaoIni <> 'esq')) then begin WriteLn('Posicao e invalida!'); Write('Enter para informar novamente!'); ReadLn; end; until (posicaoIni = 'cim') or (posicaoIni = 'bax') or (posicaoIni = 'dir') or (posicaoIni = 'esq'); if (posicaoIni = 'cim') then aDirection:= drTop; if (posicaoIni = 'bax') then aDirection:= drBotton; if (posicaoIni = 'dir') then aDirection:= drRight; if (posicaoIni = 'esq') then aDirection:= drLeft; { Iniciando o jogo } Application := TRobo.Create(nil, x, y, aDirection); Application.Title := 'Meu Robo - showdelphi.com.br'; Application.Run; Application.DrawScreen; repeat Command := UpCase(ReadKey); case Command of 'L' : Application.GiraRobo(Command); 'R' : Application.GiraRobo(Command); 'F' : Application.MovRobo(); else begin { Verificar a necessidade de informar comando invalido } end; end; Application.DrawScreen; until Command = 'S'; Application.Free; end. |