Advertisement
Oppaceted

x0_3

Feb 18th, 2023
365
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,win,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.     do while(.true.)
  20.         write (*,'(a$)') 'Enter the number of cells required to win:'
  21.         read (*,*) win
  22.         if ( (win > n) .or. (win <= 0) ) then
  23.             write (*,'(a)') 'Too big or incorrect value, try again.'
  24.             cycle
  25.         else
  26.             exit
  27.         end if
  28.     end do
  29.     allocate(table(n,n))
  30.     table(:,:) = '-'
  31.     player = 1
  32.     process = .true.
  33.     !
  34.     do while(process)
  35.         if (player_win(n,win,table)==1) then
  36.             write (*,'(a)') 'Congrats! Player 1 won!'
  37.             exit
  38.         elseif (player_win(n,win,table)==2) then
  39.             write (*,'(a)') 'Congrats! Player 2 won!'
  40.             exit
  41.         elseif (player_win(n,win,table)==3) then
  42.             write (*,'(a)') 'No one won!'
  43.             exit
  44.         else
  45.             do while (.true.)
  46.                 write (*,'(a,1x,i1,1x,a)') 'Player',player,'walks'
  47.                 write (*,'(a$)') 'Enter a row: '
  48.                 read (*,'(i5$)') a
  49.                 write (*,'(a$)') 'Enter a column: '
  50.                 read (*,'(i5$)') b
  51.                 if ( (.not.((a<=n).and.(1<=a))) .or. (.not.((b<=n).and.(1<=b))) ) then
  52.                     write (*,'(a)') 'Enter the correct value!'
  53.                     cycle
  54.                 else if  ( (table(a,b) == '0') .or. (table(a,b) == 'x') ) then
  55.                     write (*,'(a)') 'Enter the correct value!'
  56.                     cycle
  57.                 else
  58.                     exit
  59.                 end if
  60.             end do
  61.             if (player == 1) then
  62.                 table(a,b) = 'x'
  63.                 player = 2
  64.             else
  65.                 table(a,b) = '0'
  66.                 player = 1
  67.             end if
  68.             call draw(n,table)
  69.         end if
  70.     end do
  71.     deallocate(table)
  72. end program
  73. !
  74. subroutine draw(draw_size,draw_table)
  75.     integer :: draw_size, dr_1, dr_2
  76.     character :: draw_table(draw_size,draw_size)
  77.     !
  78.     do dr_1 =1,draw_size
  79.         if (dr_1 /= draw_size) then
  80.             write (*,'(1x,i2$)') dr_1
  81.         else
  82.             write (*,'(1x,i2)') dr_1
  83.         end if
  84.     end do
  85.     do dr_1 =1,draw_size
  86.         do dr_2 =1,(draw_size-1)
  87.             write (*,'(2x,a$)') draw_table(dr_1,dr_2)
  88.         end do
  89.         write (*,'(2x,a,1x,i2)') draw_table(dr_1,draw_size), dr_1
  90.     end do
  91. end subroutine
  92. !
  93. integer function player_win(pw_win, pw_size, pw_table)
  94.     integer :: pw_1, pw_2, pw_win, pw_size
  95.     character :: pw_table(pw_size,pw_size), pw_win_table(pw_win,pw_win)
  96.     !
  97.     player_win = 3
  98.     do pw_1 = 1, pw_size
  99.         do pw_2 = 1, pw_size
  100.             if ( pw_table(pw_1,pw_2) == '-' ) then
  101.                 player_win = 0
  102.             end if
  103.         end do
  104.     end do
  105.     !
  106.     do pw_1 = 1, (pw_size-pw_win+1)
  107.         do pw_2 = 1, (pw_size-pw_win+1)
  108.             call equate(pw_win,pw_win_table,pw_size,pw_table,pw_1,pw_2)
  109.         end do
  110.     end do
  111. end function
  112. !
  113. subroutine equate(eq_win,eq_win_table,eq_size,eq_table, x, y)
  114.     integer :: eq_win, eq_size, x, y, eq_x,eq_y
  115.     character :: eq_win_table(eq_win,eq_win), eq_table(eq_size,eq_size)
  116.     do eq_x =1,eq_win
  117.         do eq_y =1,eq_win
  118.             eq_win_table(eq_x,eq_y) = eq_table( (eq_x + x - 1),(eq_y + y - 1) )
  119.         end do
  120.     end do
  121. end subroutine
  122. !
  123. integer function check(ch_win,ch_table)
  124.     integer :: ch_win, ch_1
  125.     character :: ch_table(ch_win,ch_win)
  126.     logical :: ch_0, ch_x
  127.     !ch_0 = .true.
  128.     !ch_x = .true.
  129.     do ch_1 =1,ch_win
  130.         if ( ch_table(ch_1,ch_1) == '-' ) then
  131.             ch_0 = .false.
  132.             ch_x = .false.
  133.             check = 0
  134.             exit
  135.         elseif ( ch_table(1,1) == '0' ) then
  136.             ch_0 = .true.
  137.             ch_x = .false.
  138.         else
  139.             ch_x = .true.
  140.             ch_0 = .false.
  141.         end if
  142.     end do
  143.     ! ?
  144.     do ch_1 =1,ch_win
  145.         if ( ch_table(ch_1,ch_1) == '-' ) then
  146.             ch_0 = .false.
  147.         end if
  148.     end do
  149.  
  150. end function
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement