Advertisement
Oppaceted

x0

Feb 11th, 2023
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. program hello
  3.     implicit none
  4.     character, allocatable :: table(:,:)
  5.     integer :: n,a,b,player
  6.     logical :: process
  7.     integer :: player_win
  8.     !
  9.     do while(.true.)
  10.         write (*,'(a$)') 'Enter the size of the field:'
  11.         read (*,*) n
  12.         if ( (n >= 20) .or. (n <= 0) ) then
  13.             write (*,'(a)') 'Too big or incorrect value, try again.'
  14.             cycle
  15.         else
  16.             exit
  17.         end if
  18.     end do
  19.     allocate(table(n,n))
  20.     table(:,:) = '-'
  21.     player = 1
  22.     process = .true.
  23.     !
  24.     do while(process)
  25.         if (player_win(n,table)==1) then
  26.             write (*,'(a)') 'Congrats! Player 1 won!'
  27.             exit
  28.         elseif (player_win(n,table)==2) then
  29.             write (*,'(a)') 'Congrats! Player 2 won!'
  30.             exit
  31.         elseif (player_win(n,table)==3) then
  32.             write (*,'(a)') 'Noone won!'
  33.             exit
  34.         else
  35.             do while (.true.)
  36.                 write (*,'(a,1x,i1,1x,a)') 'Player',player,'walks'
  37.                 write (*,'(a$)') 'Enter a row: '
  38.                 read (*,'(i5$)') a
  39.                 write (*,'(a$)') 'Enter a column: '
  40.                 read (*,'(i5$)') b
  41.                 if ( (.not.((a<=n).and.(1<=a))) .or. (.not.((b<=n).and.(1<=b))) ) then
  42.                     write (*,'(a)') 'Enter the correct value!'
  43.                     cycle
  44.                 else if  ( (table(a,b) == '0') .or. (table(a,b) == 'x') ) then
  45.                     write (*,'(a)') 'Enter the correct value!'
  46.                     cycle
  47.                 else
  48.                     exit
  49.                 end if
  50.             end do
  51.             if (player == 1) then
  52.                 table(a,b) = 'x'
  53.                 player = 2
  54.             else
  55.                 table(a,b) = '0'
  56.                 player = 1
  57.             end if
  58.             call draw(n,table)
  59.         end if
  60.     end do
  61.     deallocate(table)
  62. end program
  63. subroutine draw(r,table_2)
  64.     integer :: r, i, j, m
  65.     character :: table_2(r,r)
  66.     !
  67.     do m =1,r
  68.         if (m /= r) then
  69.             write (*,'(1x,i2$)') m
  70.         else
  71.             write (*,'(1x,i2)') m
  72.         end if
  73.     end do
  74.     do i =1,r
  75.         do j =1,r-1
  76.             write (*,'(2x,a$)') table_2(i,j)
  77.         end do
  78.         write (*,'(2x,a,1x,i2)') table_2(i,r), i
  79.     end do
  80. end subroutine
  81. integer function player_win(siz, table_3)
  82.     integer :: ch_1, ch_2, siz
  83.     character :: table_3(siz,siz)
  84.     logical :: full
  85.     !
  86.     full = .true.
  87.     do ch_1 = 1, siz
  88.         do ch_2 = 1, siz
  89.             if ( table_3(ch_1,ch_2) == '-' ) then
  90.                 full = .false.
  91.             end if
  92.         end do
  93.     end do
  94.     if (full) then
  95.         player_win = 3
  96.     else
  97.         player_win = 0
  98.     end if
  99. end function
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement