programação i engenharia de computação prof.ª claudia boeres e prof. estefhan dazzi wandekoken...
TRANSCRIPT
![Page 1: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/1.jpg)
Programação IEngenharia de Computação
Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken
2010/1
![Page 2: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/2.jpg)
RESOLVENDO PROBLEMAS – Os movimentos do cavalo no xadrez
Considere o jogo de xadrez, onde peças são movimentadas em um tabuleiro dividido em 8 linhas e oito colunas. Considere ainda os movimentos do cavalo, a partir de uma dada posição, conforme diagrama a seguir, onde cada possível movimento é designado por mi. No esquema, o cavalo localizado na posição (5, 4) pode fazer oito movimentos, onde o primeiro deles, m1, levaria o cavalo para a posição (7,5).
![Page 3: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/3.jpg)
Os movimentos do cavalo
8
7
6 m3 m2
5 m4 m1
4 C
3 m5 m8
2 m6 m7
1
1 2 3 4 5 6 7 8
![Page 4: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/4.jpg)
PROBLEMA 1
Escreva uma função que determina se, a partir de uma dada posição, o cavalo pode ou não realizar o primeiro movimento. Vamos chamá-la de pmov, e denominar seu parâmetro (a posição corrente), de (x, y).
![Page 5: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/5.jpg)
Instâncias de pmov
INSTÂNCIA RESULTADO
pmov (5, 4) True
pmov (8, 1) False
pmov (1, 1) True
pmov (1, 8) False
![Page 6: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/6.jpg)
Solução
pmov (x, y) = (x + 2 <= 8 ) && (y + 1 <= 8)
![Page 7: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/7.jpg)
Testando a solução
8
7
6
5
4
3
2
1
1 2 3 4 5 6 7 8
![Page 8: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/8.jpg)
Estendendo o problema
pmov (x, y) = (x + 2 <= 8 ) && (y + 1 <= 8)
smov (x, y) = (x + 1 <= 8 ) && (y + 2 <= 8)
tmov (x,y) = (x - 1 >= 1 ) && (y + 2 <= 8)
qmov (x,y) = (x - 2 >= 1 ) && (y + 1 <= 8)
qtmov (x,y) = (x - 2 >=1 ) && (y - 1 >= 1)
sxmov (x,y) = (x - 1 >= 1 ) && (y - 2 >= 1)
stmov (x,y) = (x + 1 <= 8 ) && (y - 2 >= 1)
omov (x,y) = (x + 2 <= 8 ) && (y - 1 >= 1)
![Page 9: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/9.jpg)
Identificando abstrações
pmov (x,y) = f (x + 2 ) && f( y + 1)
smov (x,y) = f (x + 1) && f (y + 2)
tmov (x,y) = g (x - 1) && f (y + 2)
qmov (x,y) = g (x - 2) && f (y + 1)
qtmov (x,y) = g (x - 2) && g (y – 1)
sxmov (x,y) = g (x - 1) && g (y – 2)
stmov (x,y) = f (x + 1 ) && g (y – 2)
omov (x,y) = f (x + 2) && g (y – 1)
f w = w <= 8
g w = w >= 1
![Page 10: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/10.jpg)
Análise da solução
Clareza - Na medida em que agora está explicitado, que todas as oito funções para verificar os movimentos possuem estrutura semelhante e que todas estão usando funções para verificar a ultrapassagem das bordas;
Manutenção - Se nosso tabuleiro mudasse, ou seja, passasse a ter 9 linhas por nove colunas, bastaria alterar a função f e tudo estaria modificado, ao invés de termos que alterar as oito definições.
Reuso - As duas funções que testam as bordas poderiam ser usadas para construir funções para avaliar o movimento de outras peças do jogo de xadrez.
![Page 11: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/11.jpg)
PROBLEMA 2
Sabemos que para cada posição alguns movimentos podem ser realizados e outros não. Como ordenamos os movimentos no sentido anti-horário, gostaríamos de obter, para uma dada posição, dos movimentos que podem ser realizados, aquele que possui o menor índice.
![Page 12: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/12.jpg)
Qual o menor índice de movimento possível?
8 m4 m1 C1
7 C3 m5
6 m5 m8 m6
5 m6 m7
4
3 m2 m3
2 m1 m4
1 C4 C2
1 2 3 4 5 6 7 8
![Page 13: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/13.jpg)
Instâncias de qualmov
Instância resultado
qualmov 8 1 3
qualmov 8 8 5
qualmov 3 7 1
qualmov 1 1 1
![Page 14: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/14.jpg)
Solução
![Page 15: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/15.jpg)
Codificando a solução
qualmov (x, y) =
if pmov x y then 1 else if smov x y then 2 else if tmov x y then 3 else if qmov x y then 4 else if qtmov x y then 5 else if sxmov x y then 6 else if stmov x y then 7 else if omov x y then 8 else 0
![Page 16: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/16.jpg)
Análise da solução
qualmov (x, y) =
if not (pert x 1 8) || not (pert y 1 8) then 0else if pmov x y then 1 else if smov x y then 2 else if tmov x y then 3 else if qmov x y then 4 else if qtmov x y then 5 else if sxmov x y then 6 else 7wherepert x a b = a <= b && x >= a && x <= b
![Page 17: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/17.jpg)
REVISITANDO O PROBLEMA 1
Observando a solução encontrada para o problema 1, constatamos que embora a noção de movimento do cavalo seja única, quem precisar saber se um dado movimento é válido, precisará conhecer o nome das oito funções. Embora seja cedo para falarmos de interface homem-máquina, já dá para dizer que estamos sobrecarregando nosso usuário ao darmos oito nomes para coisas tão parecidas. Será que temos como construir uma só função para tratar o problema?
![Page 18: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/18.jpg)
Melhorando a interface
pmov x y
smov x y
tmov x y
qmov x y
qtmov x y
sxmov x y
stmov x y
omov x y
mov (m, x, y)
![Page 19: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/19.jpg)
Exemplo de avaliação da função mov
? mov 7 3 4True?
![Page 20: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/20.jpg)
Codificação da Solução
mov (m, x, y) = if not (pert m 1 8) || not (pert x 1 8) || not (pert y 1 8) then False else if m == 1 then pmov else if m == 2 then smov else if m == 3 then tmov else if m == 4 then qmov else if m == 5 then qtmov else if m == 6 then sxmov else if m == 7 then stmov else omov where pmov = ... smov = ... tmov = ... ...
![Page 21: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/21.jpg)
Análise da solução nova solução
<1 1 2 3 4 5 6 7 8 >8
<1 1 2 3 4 5 6 7 8 >8
<1 1 2 3 4 5 6 7 8 >8
![Page 22: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/22.jpg)
Codificação da solução
mov (m,x,y) = if not (pert m 1 8)|| not(pert x 1 8) || not(pert y 1 8) then False else if m <= 4 then if m<= 2 then if m== 1 then pmov else smov else if m==3 then tmov else qmov else if m<= 6 then if m==5 then qtmov else sxmov else if m == 7 then stmov else omov where pmov = ... smov = ... tmov = ... ...
![Page 23: Programação I Engenharia de Computação Prof.ª Claudia Boeres e Prof. Estefhan Dazzi Wandekoken 2010/1](https://reader035.vdocuments.pub/reader035/viewer/2022062623/552fc0f8497959413d8b50eb/html5/thumbnails/23.jpg)
Número de comparações
número de valores
esquema linear (número médio)
esquema binário(número máximo)
8 4 4
100 50 7
1000 500 10
1000000 500000 20