Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program hello
- implicit none
- character, allocatable :: table(:,:)
- integer :: n,win,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
- do while(.true.)
- write (*,'(a$)') 'Enter the number of cells required to win:'
- read (*,*) win
- if ( (win > n) .or. (win <= 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)') 'No one 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(draw_size,draw_table)
- integer :: draw_size, i, j, m
- character :: draw_table(draw_size,draw_size)
- !
- do m =1,draw_size
- if (m /= draw_size) then
- write (*,'(1x,i2$)') m
- else
- write (*,'(1x,i2)') m
- end if
- end do
- do i =1,draw_size
- do j =1,(draw_size-1)
- write (*,'(2x,a$)') draw_table(i,j)
- end do
- write (*,'(2x,a,1x,i2)') draw_table(i,draw_size), i
- end do
- end subroutine
- integer function player_win(siz, table_3)
- integer :: ch_1, ch_2, siz
- character :: table_3(siz,siz)
- !
- player_win = 3
- do ch_1 = 1, siz
- do ch_2 = 1, siz
- if ( table_3(ch_1,ch_2) == '-' ) then
- player_win = 0
- end if
- end do
- end do
- end function
- !
- integer function check(check_win,check_table)
- integer :: check_win
- character :: check_table(check_win,check_win)
- end function
- !
- integer function truncation(tr_win, tr_size, tr_table)
- integer :: tr_win, tr_size
- character :: tr_table(tr_size,tr_size)
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement