Knight in Fortran

Knight Move Game in Fortran 90
This is a brain game that you might enjoy. In the chess board you can move the knight as if you’re playing a regular game of chess, but you need to cover the whole board with the least possible moves, a good score would be 65 moves or less. This is a very good training for strategy games.

Written in fortran. Just for fun!! As always work in progress..


module modknight
 integer  :: table(0:63),tablein(0:63),board(1:64)
 integer  :: score,iwhere,oldiwhere,oldsquare
 logical  :: squares(0:63)
end module


subroutine init()
use modknight
integer  :: squarenum, curnum,i


squares(0:63) = .false.

curnum=0
squarenum=0
is=0
do j=0,7
  do i=0,7
    tablein(squarenum)=curnum
    table(squarenum)=squarenum
    
    if(mod(squarenum+is,2) .eq. 0) then
      board(squarenum+1)= 0
    else
      board(squarenum+1)= 1
    end if   
    curnum = curnum+1
    squarenum = squarenum +1
  end do
  curnum =curnum+ 20
  if(is .eq. 0) then
    is=1
  else  
    is=0
  end if   
end do

score=0
iwhere=0
oldiwhere=0
oldsquare=0

!call debugtest()



board(oldsquare+1)=2
end subroutine


subroutine debugtest
use modknight

board=3
squares(0:63)=.true.
board(10+1)=1
squares(10)=.false.
end 

subroutine moveto(square)
use modknight
logical stuffed
integer square

stuffed= .false.

iwhere=tablein(square)
!print *, "iwhere =",iwhere,square
if ((oldiwhere .eq. (iwhere-26) .or. oldiwhere .eq. (iwhere-30) .or. &
oldiwhere .eq. (iwhere-55) .or. oldiwhere .eq. (iwhere-57) .or. &
(oldiwhere-26) .eq. iwhere .or. (oldiwhere-30) .eq. iwhere .or. &
(oldiwhere-55) .eq. iwhere .or. (oldiwhere-57) .eq. iwhere)) then

  score =score+1
!  print *, 'o',oldiwhere,iwhere,square,score
  board(oldsquare+1)=3
  oldsquare=square
  oldiwhere=iwhere
  squares(square)=.true.
!  print *, 't',oldiwhere,iwhere,square,score  
  board(square+1) = 2
end if

do i=0,63
  if (.not. squares(i)) then
    stuffed=.true.
    exit
  end if  
end do

if (.not. stuffed) then
  print *, "You win in only ",score,"moves!"
  call init()
end if

end subroutine

subroutine show
use modknight
integer b
  b=1
    WRITE (*, '(A)') '     A   B   C   D   E   F   G   H' 
  DO i=1,8 
     WRITE (*, '(A)') '   +---+---+---+---+---+---+---+---+' 
     WRITE (*, '(A)', ADVANCE='NO') ACHAR(57-i) // '  |' 
    do j=1,8
        SELECT CASE (BOARD(B)) 
        CASE (3) 
           WRITE (*, '(A)', ADVANCE='NO') ' T |' 
        CASE (2) 
           WRITE (*, '(A)', ADVANCE='NO') ' K |' 
        CASE (1) 
           WRITE (*, '(A)', ADVANCE='NO') '   |' 
        CASE (0) 
           WRITE (*, '(A)', ADVANCE='NO') ' . |' 
        END SELECT 
        b=b+1
     end do  
        WRITE (*, '(A)', ADVANCE='NO') '  '//ACHAR(57-i)
        WRITE (*, *) '' 
     
  END DO 
  WRITE (*, '(A)') '   +---+---+---+---+---+---+---+---+' 
  WRITE (*, '(A)') '     A   B   C   D   E   F   G   H' 

end subroutine

function getval(mstr) 
 integer getval
 integer i,j
 character*2  mstr
 i=999
 j=999
 getval=999
! print *, mstr(1:1)
 select case(mstr(1:1))
 
 case('a','A')
    i=0
 case ('b','B')
    i=1
 case('c','C')
    i=2
 case ('d','D')
    i=3
 case('e','E')
    i=4
 case ('f','F')
    i=5
 case('g','G')
    i=6
 case ('h','H')
    i=7 
  case ('q','Q')
    getval=-111
    return   
 end select
  
 read(mstr(2:2),'(i1)',err=100) j
 
 if((j .ge. 1) .or. (j .le.8)) then
   j=8-j
   getval=(i)+j*8
 end if   
! print *, 'ee',i,j,getval
100 return
 

end function


program main
use modknight
 character*2 input
 integer getval
call init
do 
!   write(*,*) score,iwhere,oldiwhere
!   write(*,'(8i3)') (table(i),i=0,63)
!  write(*,'(8l3)')(squares(j),j=0,63)
!   write(*,'(8i3)')(board(j),j=1,64)
   call show
   read(*,'(a)') input
   ival=getval(input)
!   print *, ival
   if(ival .eq. -111) exit
   if((ival .ge. 0) .and. (ival .le. 63) ) then
    call moveto(ival)
   end if 
end do

end 



Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s