Making a snake game and i made a function for creating an apple position which has lots of errors

function NewAppleLocation(boardSize, player)
    use :: cfunctions
    type Vec2_t
        integer :: x, y
    end type
    type(Vec2_t) :: NewAppleLocation
    type Player_t
        type(Vec2_t)                            :: pos
        integer                                 :: direction
        type(Vec2_t), dimension(:), allocatable :: tail
    end type
    type(Player_t) :: player
    type(Vec2_t)   :: test
    integer        :: i ! iterator
    integer        :: j ! iterator

    logical :: created = .false.
    logical :: valid

    ! generate an array of valid apple positions
    logical, dimension(boardSize.y, boardSize.x) :: available
    integer                                      :: numberAvailable
    numberAvailable = boardSize%y * boardSize%x
    do i = 1, boardSize%y
        do j = 1, boardSize%x
            available(i, j) = .true.
        end do
    end do
    do i = 1, size(player%tail) ! remove taken places
        available(player%tail(i)%y, player%tail(i)%x) = .false.
        numberAvailable                               = numberAvailable - 1
    end do
    type(Vec2_t), dimension(numberAvailable) availablePositions
    type(Vec2_t) :: add
    integer      :: posIndex = 1
    do i = 1, boardSize%y
        do j = 1, boardSize%x
            if (available(i, j)) then
                add%x                        = j
                add%y                        = i
                availablePositions(posIndex) = add
                posIndex                     = posIndex + 1
            end if
        end do
    end do

    NewAppleLocation = availablePositions(RandomRange(1, numberAvailable))
end function NewAppleLocation
src/main.f90:61:1:

   61 | function NewAppleLocation(boardSize, player)
      | 1
Error: Unclassifiable statement at (1)
src/main.f90:62:21:

   55 | type Player_t
      |             2        
......
   62 |     use :: cfunctions
      |                     1
Error: USE statement at (1) cannot follow derived type declaration statement at (2)
src/main.f90:75:33:

   75 |     numberAvailable = boardSize%y * boardSize%x
      |                                 1
Error: Symbol ‘boardsize’ at (1) has no IMPLICIT type
src/main.f90:76:33:

   76 |     allocate(available(boardSize.y, boardSize.x))
      |                                 1
Error: Invalid form of array reference at (1)
src/main.f90:77:25:

   77 |     do i = 1, boardSize%y
      |                         1
Error: Symbol ‘boardsize’ at (1) has no IMPLICIT type
src/main.f90:78:29:

   78 |         do j = 1, boardSize%x
      |                             1
Error: Symbol ‘boardsize’ at (1) has no IMPLICIT type
src/main.f90:80:11:

   80 |         end do
      |           1
Error: Expecting END PROGRAM statement at (1)
src/main.f90:81:7:

   81 |     end do
      |       1
Error: Expecting END PROGRAM statement at (1)
src/main.f90:86:18:

   86 |     type(Vec2_t), dimension(numberAvailable) availablePositions
      |                  1
Error: Invalid character in name at (1)
src/main.f90:87:23:

   87 |     type(Vec2_t) :: add
      |                       1
Error: Unexpected data declaration statement at (1)
src/main.f90:88:32:

   88 |     integer      :: posIndex = 1
      |                                1
Error: Unexpected data declaration statement at (1)
src/main.f90:89:25:

   89 |     do i = 1, boardSize%y
      |                         1
Error: Symbol ‘boardsize’ at (1) has no IMPLICIT type
src/main.f90:90:29:

   90 |         do j = 1, boardSize%x
      |                             1
Error: Symbol ‘boardsize’ at (1) has no IMPLICIT type
src/main.f90:92:21:

   92 |                 add%x                        = j
      |                     1
Error: Symbol ‘add’ at (1) has no IMPLICIT type
src/main.f90:93:21:

   93 |                 add%y                        = i
      |                     1
Error: Symbol ‘add’ at (1) has no IMPLICIT type
src/main.f90:97:11:

   97 |         end do
      |           1
Error: Expecting END PROGRAM statement at (1)
src/main.f90:98:7:

   98 |     end do
      |       1
Error: Expecting END PROGRAM statement at (1)
src/main.f90:102:3:

  102 | end function NewAppleLocation
      |   1

i feel like it might have something to do with other parts of my code so here is all of main.f90 module cfunctions use, intrinsic :: iso_c_binding, only : c_int interfac - Pastebin.com

1 Like

fixed some stupid mistakes

function NewAppleLocation(boardSize, player)
    use :: cfunctions
    type Vec2_t
        integer :: x, y
    end type
    type(Vec2_t) :: NewAppleLocation
    type Player_t
        type(Vec2_t)                            :: pos
        integer                                 :: direction
        type(Vec2_t), dimension(:), allocatable :: tail
    end type
    type(Player_t) :: player
    type(Vec2_t)   :: boardSize
    type(Vec2_t)   :: test
    integer        :: i ! iterator
    integer        :: j ! iterator

    logical :: created = .false.
    logical :: valid

    ! generate an array of valid apple positions
    logical, dimension(boardSize.y, boardSize.x) :: available
    integer                                      :: numberAvailable
    numberAvailable = boardSize%y * boardSize%x
    do i = 1, boardSize%y
        do j = 1, boardSize%x
            available(i, j) = .true.
        end do
    end do
    do i = 1, size(player%tail) ! remove taken places
        available(player%tail(i)%y, player%tail(i)%x) = .false.
        numberAvailable                               = numberAvailable - 1
    end do
    type(Vec2_t), dimension(numberAvailable) :: availablePositions
    type(Vec2_t) :: add
    integer      :: posIndex = 1
    do i = 1, boardSize%y
        do j = 1, boardSize%x
            if (available(i, j)) then
                add%x                        = j
                add%y                        = i
                availablePositions(posIndex) = add
                posIndex                     = posIndex + 1
            end if
        end do
    end do

    NewAppleLocation = availablePositions(RandomRange(1, numberAvailable))
end function NewAppleLocation
src/main.f90:73:33:

   73 |     logical, dimension(boardSize.y, boardSize.x) :: available
      |                                 1
Error: Expected another dimension in array declaration at (1)
src/main.f90:85:66:

   85 |     type(Vec2_t), dimension(numberAvailable) :: availablePositions
      |                                                                  1
Error: Unexpected data declaration statement at (1)
src/main.f90:86:23:

   86 |     type(Vec2_t) :: add
      |                       1
Error: Unexpected data declaration statement at (1)
src/main.f90:87:32:

   87 |     integer      :: posIndex = 1
      |                                1
Error: Unexpected data declaration statement at (1)
src/main.f90:91:21:

   91 |                 add%x                        = j
      |                     1
Error: Symbol ‘add’ at (1) has no IMPLICIT type
src/main.f90:92:21:

   92 |                 add%y                        = i
      |                     1
Error: Symbol ‘add’ at (1) has no IMPLICIT type
src/main.f90:78:36:

   78 |             available(i, j) = .true.
      |                                    1
Error: Fortran 2008: Pointer procedure assignment at (1)
src/main.f90:78:12:

   78 |             available(i, j) = .true.
      |            1
Error: The function result on the lhs of the assignment at (1) must have the pointer attribute.
src/main.f90:82:63:

   82 |         available(player%tail(i)%y, player%tail(i)%x) = .false.
      |                                                               1
Error: Fortran 2008: Pointer procedure assignment at (1)
src/main.f90:82:8:

   82 |         available(player%tail(i)%y, player%tail(i)%x) = .false.
      |        1
Error: The function result on the lhs of the assignment at (1) must have the pointer attribute.
src/main.f90:90:16:

   90 |             if (available(i, j)) then
      |                1
Error: IF clause at (1) requires a scalar LOGICAL expression
src/main.f90:93:50:

   93 |                 availablePositions(posIndex) = add
      |                                                  1
Error: Fortran 2008: Pointer procedure assignment at (1)
src/main.f90:93:16:

   93 |                 availablePositions(posIndex) = add
      |                1
Error: The function result on the lhs of the assignment at (1) must have the pointer attribute.
src/main.f90:99:53:

   99 |     NewAppleLocation = availablePositions(RandomRange(1, numberAvailable))
      |                                                     1
Error: Return type mismatch of function ‘randomrange’ at (1) (REAL(4)/INTEGER(4))
src/main.f90:99:23:

   99 |     NewAppleLocation = availablePositions(RandomRange(1, numberAvailable))
      |                       1
Error: Cannot convert REAL(4) to TYPE(vec2_t) at (1)

i put a . instead of %

1 Like

Perhaps of interest here: my graphical snake game in Fortran 2003 for X11, using the EGGX/ProCALL library.

snake

1 Like