Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program hello
- implicit none
- character, allocatable :: table(:,:)
- integer :: n,a,b,player
- logical :: process
- integer :: player_win
- !
- do while(.true.)
- write (*,'(a$)') 'Enter the size of the field:'
- read (*,*) n
- if ( (n >= 20) .or. (n <= 0) ) then
- write (*,'(a)') 'Too big or incorrect value, try again.'
- cycle
- else
- exit
- end if
- end do
- allocate(table(n,n))
- table(:,:) = '-'
- player = 1
- process = .true.
- !
- do while(process)
- if (player_win(n,table)==1) then
- write (*,'(a)') 'Congrats! Player 1 won!'
- exit
- elseif (player_win(n,table)==2) then
- write (*,'(a)') 'Congrats! Player 2 won!'
- exit
- elseif (player_win(n,table)==3) then
- write (*,'(a)') 'Noone won!'
- exit
- else
- do while (.true.)
- write (*,'(a,1x,i1,1x,a)') 'Player',player,'walks'
- write (*,'(a$)') 'Enter a row: '
- read (*,'(i5$)') a
- write (*,'(a$)') 'Enter a column: '
- read (*,'(i5$)') b
- if ( (.not.((a<=n).and.(1<=a))) .or. (.not.((b<=n).and.(1<=b))) ) then
- write (*,'(a)') 'Enter the correct value!'
- cycle
- else if ( (table(a,b) == '0') .or. (table(a,b) == 'x') ) then
- write (*,'(a)') 'Enter the correct value!'
- cycle
- else
- exit
- end if
- end do
- if (player == 1) then
- table(a,b) = 'x'
- player = 2
- else
- table(a,b) = '0'
- player = 1
- end if
- call draw(n,table)
- end if
- end do
- deallocate(table)
- end program
- subroutine draw(r,table_2)
- integer :: r, i, j, m
- character :: table_2(r,r)
- !
- do m =1,r
- if (m /= r) then
- write (*,'(1x,i2$)') m
- else
- write (*,'(1x,i2)') m
- end if
- end do
- do i =1,r
- do j =1,r-1
- write (*,'(2x,a$)') table_2(i,j)
- end do
- write (*,'(2x,a,1x,i2)') table_2(i,r), i
- end do
- end subroutine
- integer function player_win(siz, table_3)
- integer :: ch_1, ch_2, siz
- character :: table_3(siz,siz)
- logical :: full
- !
- full = .true.
- do ch_1 = 1, siz
- do ch_2 = 1, siz
- if ( table_3(ch_1,ch_2) == '-' ) then
- full = .false.
- end if
- end do
- end do
- if (full) then
- player_win = 3
- else
- player_win = 0
- end if
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement