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,win,table)==1) then
- write (*,'(a)') 'Congrats! Player 1 won!'
- exit
- elseif (player_win(n,win,table)==2) then
- write (*,'(a)') 'Congrats! Player 2 won!'
- exit
- elseif (player_win(n,win,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, dr_1, dr_2
- character :: draw_table(draw_size,draw_size)
- !
- do dr_1 =1,draw_size
- if (dr_1 /= draw_size) then
- write (*,'(1x,i2$)') dr_1
- else
- write (*,'(1x,i2)') dr_1
- end if
- end do
- do dr_1 =1,draw_size
- do dr_2 =1,(draw_size-1)
- write (*,'(2x,a$)') draw_table(dr_1,dr_2)
- end do
- write (*,'(2x,a,1x,i2)') draw_table(dr_1,draw_size), dr_1
- end do
- end subroutine
- !
- integer function player_win(pw_win, pw_size, pw_table)
- integer :: pw_1, pw_2, pw_win, pw_size
- character :: pw_table(pw_size,pw_size), pw_win_table(pw_win,pw_win)
- !
- player_win = 3
- do pw_1 = 1, pw_size
- do pw_2 = 1, pw_size
- if ( pw_table(pw_1,pw_2) == '-' ) then
- player_win = 0
- end if
- end do
- end do
- !
- do pw_1 = 1, (pw_size-pw_win+1)
- do pw_2 = 1, (pw_size-pw_win+1)
- call equate(pw_win,pw_win_table,pw_size,pw_table,pw_1,pw_2)
- end do
- end do
- end function
- !
- subroutine equate(eq_win,eq_win_table,eq_size,eq_table, x, y)
- integer :: eq_win, eq_size, x, y, eq_x,eq_y
- character :: eq_win_table(eq_win,eq_win), eq_table(eq_size,eq_size)
- do eq_x =1,eq_win
- do eq_y =1,eq_win
- eq_win_table(eq_x,eq_y) = eq_table( (eq_x + x - 1),(eq_y + y - 1) )
- end do
- end do
- end subroutine
- !
- integer function check(ch_win,ch_table)
- integer :: ch_win, ch_1
- character :: ch_table(ch_win,ch_win)
- logical :: ch_0, ch_x
- !ch_0 = .true.
- !ch_x = .true.
- do ch_1 =1,ch_win
- if ( ch_table(ch_1,ch_1) == '-' ) then
- ch_0 = .false.
- ch_x = .false.
- check = 0
- exit
- elseif ( ch_table(1,1) == '0' ) then
- ch_0 = .true.
- ch_x = .false.
- else
- ch_x = .true.
- ch_0 = .false.
- end if
- end do
- ! ?
- do ch_1 =1,ch_win
- if ( ch_table(ch_1,ch_1) == '-' ) then
- ch_0 = .false.
- end if
- end do
- end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement