Applied Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Applied Algorithms

Description:

Bool rook(Board b, Color c, int i, int j) { return ( check(c,i 1,j,north,b) ... if (c==White && piece=='r') return(rook (b,White,i,j) ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 19
Provided by: she46
Learn more at: http://web.cecs.pdx.edu
Category:

less

Transcript and Presenter's Notes

Title: Applied Algorithms


1
Applied Algorithms
  • Lecture 4
  • Strings
  • Recursion

2
Homework Grading Notes Again
  • You must turn in your homework in class each
    Friday.
  • Give me a paper copy in class. Email is not
    convenient for me.
  • You must turn in the report from the judge.
  • Homework without a Judge report can get a maximum
    of 85.
  • Always turn in a page or two of output. Try and
    make the input for the output you turn in be
    non-standard. Test the gotchas!
  • Include some sort of high level description of
    the strategy your program uses to solve the
    problem.

3
Quiz
  • We have a 10 minute quiz today on the reading
    Chapter 3.
  • We will continue to have quizzes until the class
    gets an average score high enough to convince me
    that people are doing the reading.

4
Check the Check
  • This problem has some very elegant solutions,
    which are easy to understand

Every piece has a number of squares that it
threatens, many squares have a successor, unless
blocked.
5
Checking for check
  • A piece checks a king if the king is in the path
    that the piece threatens.
  • Checking for check on a path
  • Check for check on first square on path
  • Check for check on rest of the path
  • A recursive procedure?
  • How do we terminate the recursion?

6
The key to check the check
  • // check if the position "(i,j)" on board "b"
  • // checks the king with color "c", or if any
  • // square in the direction pointed to by "next"
    does
  • Bool check(Color c, int i, int j,
  • void next(int,int), Board b)
  • if (ilt1 jlt1 igt8 jgt8) return False
  • if (cBlack bij'k') return True
  • if (cWhite bij'K') return True
  • if (bij'.')
  • next(i,j) return check(c,i,j,next,b)
  • return False

Out of bounds terminates recursion
Only a non-blocking empty square causes
recursion, in the direction computed by next.
7
Directions and paths
  • // Directions are indicated by functions that
    return
  • // the next square in that direction from the
    given
  • // position "(i,j)".
  • void north(int i, int j) i i 1
  • void south(int i, int j) i i - 1
  • void east (int i, int j) j j 1
  • void west (int i, int j) j j - 1
  • void nw (int i, int j) north(i,j)
    west(i,j)
  • void ne (int i, int j) north(i,j)
    east(i,j)
  • void sw (int i, int j) south(i,j)
    west(i,j)
  • void se (int i, int j) south(i,j)
    east(i,j)
  • // Return a position guaranteed to be out of
    bounds
  • void never (int i, int j) i 0 j 0

8
How does a piece check for check?
Vertical and horizontal paths
  • Bool rook(Board b, Color c, int i, int j)
  • return ( check(c,i1,j,north,b)
  • check(c,i-1,j,south,b)
  • check(c,i,j1,east ,b)
  • check(c,i,j-1,west ,b) )
  • Bool bishop(Board b, Color c, int i, int j)
  • return ( check(c,i1,j1,ne,b)
  • check(c,i1,j-1,nw,b)
  • check(c,i-1,j1,se,b)
  • check(c,i-1,j-1,sw,b) )
  • Bool queen(Board b, Color c, int i, int j)
  • return ( rook(b,c,i,j) bishop(b,c,i,j) )

Diagonal paths
Reuse galore!
9
Pawns and Knights are irregular
  • Bool pawn(Board b, Color c, int i, int j)
  • if (cBlack)
  • return(check(c,i1,j1,never,b)
  • check(c,i1,j-1,never,b))
  • if (cWhite)
  • return(check(c,i-1,j1,never,b)
  • check(c,i-1,j-1,never,b))
  • Bool knight(Board b, Color c,int i,int j)
  • return(check(c,i2,j1,never,b)
  • check(c,i2,j-1,never,b)
  • check(c,i-2,j1,never,b)
  • check(c,i-2,j-1,never,b)
  • check(c,i1,j2,never,b)
  • check(c,i-1,j2,never,b)
  • check(c,i1,j-2,never,b)
  • check(c,i-1,j-2,never,b) )

Even though they are irregular, we reuse the same
mechanism by designing a path, never that
immediately goes out of bounds and stops the
recursion
10
Dispatch
  • Bool checkPiece(Board b,Color c,int i, int j,
    char piece)
  • if (cWhite piece'k') return(king
    (b,White,i,j))
  • if (cBlack piece'K') return(king
    (b,Black,i,j))
  • if (cWhite piece'q') return(queen
    (b,White,i,j))
  • if (cBlack piece'Q') return(queen
    (b,Black,i,j))
  • if (cWhite piece'b') return(bishop(b,Whit
    e,i,j))
  • if (cBlack piece'B') return(bishop(b,Blac
    k,i,j))
  • if (cWhite piece'r') return(rook
    (b,White,i,j))
  • if (cBlack piece'R') return(rook
    (b,Black,i,j))
  • if (cWhite piece'p') return(pawn
    (b,White,i,j))
  • if (cBlack piece'P') return(pawn
    (b,Black,i,j))
  • if (cWhite piece'n') return(knight(b,Whit
    e,i,j))
  • if (cBlack piece'N') return(knight(b,Blac
    k,i,j))
  • return(False)

Note the use of alignment to emphasize
similarities and differences.
11
Testing the board
  • Bool test(Board b,Color c)
  • int i int j Bool bool
  • for (i1 ilt8 i)
  • for (j1 jlt8 j)
  • bool checkPiece(b,c,i,j,bij)
  • if (bool) return True
  • return False

Short circuit the loop if we find a check!
12
The Main Loop
readBoard, emptyBoard, showBoard, and test break
up program into logical units
  • int main()
  • Board b
  • int n char nl20 Bool ans
  • readBoard(b)
  • n 1
  • while (!(emptyBoard(b)))
  • // showBoard(b)
  • gets(nl) // skip past the blank line
  • if (test(b,White))
  • printf("Game ld white king is in
    check.\n",n)
  • if (test(b,Black))
  • printf("Game ld black king is in
    check.\n",n)
  • readBoard(b)
  • n

Shades of testing strategy
13
Jolly Jumpers in Haskell
  • import IO(stdin,hIsEOF,hGetLine,openFile,IOMode(..
    ),hClose)
  • import Input(Parser,natural,runParser)
  • import List(sort)
  • -- A sorted sequence covers an interval low..high
  • -- if the first element is low, and the tail of
    the
  • -- sequence covers (low1..high). Note a sequnce
    of
  • -- length 1, must match both low and high
  • jolly low high x
  • xlow lowhigh True
  • jolly low high (xxs)
  • lowx jolly (low1) high xs
  • jolly _ _ _ False

14
Testing for Jolly
  • -- Sequences of length 1 are always Jolly Jumpers
  • -- otherwise compute a sorted list of
    differences,
  • -- then use "jolly" for testing
  • testJumper n x return "Jolly"
  • testJumper n xs return(if jolly 1 (n-1) diffs
  • then "Jolly"
  • else "Not jolly")
  • where
  • diffs sort(zipWith test xs (tail xs))
  • test x y abs(x - y)

15
What does this mean?
  • diffs sort(zipWith test xs (tail xs))
  • test x y abs(x - y)
  • xs 1,4,2,3
  • tail xs 4,2,3
  • zipWith test xs (tail xs)
  • (test 1 4),(test 4 2),(test 2 3) 3,2,1
  • sort(zipWith test xs (tail xs)) 1,2,3

16
Main loop
  • oneline line
  • do let (n,xs) fst(runParser parseInput
    line)
  • message lt- testJumper n xs
  • putStr (message"\n") -- " "
  • show n" " show xs"\n")
  • main do h lt- openFile "JollyJumper.dat"
    ReadMode
  • loop h hClose h return () where
  • loop h
  • do b lt- hIsEOF h
  • if b then return ()
  • else do line lt- hGetLine h
  • oneline line
  • loop h

17
In Class Problems
  • WERTYU 3.8.1
  • Page 66 of the text
  • We will write this together as a class
  • Wheres Waldorf 3.8.2
  • Page 67 of the text
  • Keeping in mind todays lecture.
  • Pairs of two

18
Todays Assignments
  • Read for next time
  • Chapter 4 of the text. pp 78-101
  • Be prepared to answer questions in class next
    Friday from the reading.
  • Programming assignment
  • 3.8.4 Crypt Kicker II
  • Page 70
  • Write a solution
  • Submit your solution (until you get it right)
  • Hand in both your program, and the judge output.
  • Those who volunteer to discuss their program get
    class participation points. Email me solutions
    before noon on Friday, April 29.
Write a Comment
User Comments (0)
About PowerShow.com