FORTRAN

FORTRAN
REVIEW
¡  4 Data types: INTEGER, REAL, CHARACTER, LOGICAL
¡  Arithmetic Operators: + = / * ** // .AND. .OR. .NOT.
¡  Relational Operators: < > <= >= == /= .EQV. .NEQV.
¡  Format Specifiers: A , I, F, E, EN, ES, L
¡  SUBROUTINES, FUNCTIONS
¡  INTENT(IN), INTENT(OUT), INTENT(INOUT)
¡  DO start, stop, increment
¡  DO WHILE (logical expression)
¡  DO ... EXIT
¡  IF (logical expression) THEN ... ELSE ... END IF
¡  SELECT CASE(var) ... CASE(1:4) ... CASE(5:8) ... END SELECT
¡  RECURSIVE SUBROUTINES & FUNCTIONS
¡  Implied DO Loops: (expr, i=start, stop, incr)
§  WRITE(*,*) (i**2, i=1,10)
ARRAY REVIEW
¡  Fortran arrays can be up to 7 dimensional
INTEGER, DIMENSION(4, 5, 6, 7) :: my_array
¡  Arrays can have arbitrar y lower and upper bounds
INTEGER, DIMENSION(-1:4,2:5) :: my_array
¡  Arrays can be sectioned using start:stop:incr notation
my_array(-1:4:2,:)
¡  Arrays can also be sectioned using another array of indices
though this creates a temporar y copy in memor y which cannot be
written to.
my_array((/-1 ,2,4/),:)
¡  Pointers can point to arrays, or scalars, and can be reassigned.
When a pointer to an array is passed to a subroutine, the lower
bounds are preser verd.
¡  /public/jcarrol5/ex7.f90
MODULES
¡  Derived Data Types
§  Create your own data type built up from combinations of existing data
types. Similar to 'classes' in c.
¡  Modules
§  Convenient way to organize code into logical units – usually centered
around a particular data type – or function type.
§  Similar to Programs, though there is no execution section. Just
variable declarations and subroutines/functions.
§  /public/jcarrol5/fortran/ex8.f90
§  /public/jcarrol5/fortran/planets.f90
TIC-TAC-TOE
¡  We are going to build a tic tac toe game.
X
O
X
O
X
X
O
X
O
¡  If there's time we'll build an AI player using the minimax
algorithm.
MINIMAX ALGORITHM
¡  Basically looks ahead at all of the possible outcomes and tries
to make the best choice assuming that the opponent will
always make there best choice.
http://en.wikipedia.org/wiki/File:Minimax.svg
MINIMAX PSEUDOCODE
function minimax(node, depth, maximizingPlayer)
if depth = 0 or node is a terminal node
return the heuristic value of node
if maximizingPlayer
bestValue := -∞
for each child of node
val := minimax(child, depth - 1, FALSE)
bestValue := max(bestValue, val)
return bestValue
else
bestValue := +∞
for each child of node
val := minimax(child, depth - 1, TRUE)
bestValue := min(bestValue, val)
return bestValue
(* Initial call for maximizing player *)
minimax(origin, depth, TRUE)
http://en.wikipedia.org/wiki/Minimax