From ae57bbe34e97c55cc864d6e54df0cc04ef4ef38e Mon Sep 17 00:00:00 2001 From: Helen Kershaw Date: Fri, 15 Dec 2023 13:08:17 -0500 Subject: [PATCH] moved game simlations into a module --- Fortran/exercise2/Makefile | 2 +- Fortran/exercise2/league_sim.f90 | 60 ++------------------ Fortran/exercise2/league_sim_mod.f90 | 85 ++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 Fortran/exercise2/league_sim_mod.f90 diff --git a/Fortran/exercise2/Makefile b/Fortran/exercise2/Makefile index 4717dbc..a4f76c3 100644 --- a/Fortran/exercise2/Makefile +++ b/Fortran/exercise2/Makefile @@ -1,6 +1,6 @@ FC=gfortran #fortran compiler FFLAGS=-O2 -SRC=league_sim.f90 +SRC=league_sim_mod.f90 league_sim.f90 OBJ=${SRC:.f90=.o} #substitute .f90 with .o %.o: %.f90 #wildcard rule, creation of *.o depends on *.f90 diff --git a/Fortran/exercise2/league_sim.f90 b/Fortran/exercise2/league_sim.f90 index 5a0e76f..5a191c3 100644 --- a/Fortran/exercise2/league_sim.f90 +++ b/Fortran/exercise2/league_sim.f90 @@ -1,4 +1,7 @@ program football_league_simulator + + use league_simulator + implicit none integer, parameter :: num_teams = 5 @@ -19,65 +22,9 @@ program football_league_simulator ! Print results call print_results(team_names, points) - contains -subroutine simulate_match(team1, team2, points) - implicit none - integer, intent(in) :: team1, team2 - integer, intent(inout) :: points(:) - - integer :: goals1, goals2 - real :: g1, g2 - - call random_seed() - - ! Simulate goals for each team (max 5 goals) - call random_number(g1) - call random_number(g2) - goals1 = floor(6*g1) - goals2 = floor(6*g2) - - ! Update points based on match result - if (goals1 > goals2) then - points(team1) = points(team1) + 3 ! Team 1 wins - else if (goals2 > goals1) then - points(team2) = points(team2) + 3 ! Team 2 wins - else - points(team1) = points(team1) + 1 ! Draw, each team gets 1 point - points(team2) = points(team2) + 1 - end if - -end subroutine simulate_match - -subroutine sort_teams(team_names, points) - implicit none - character(len=20), intent(inout) :: team_names(:) - integer, intent(inout) :: points(:) - integer :: i, j - integer :: tempPoints - character(len=20) :: tempName - - do i = 1, size(team_names) - 1 - do j = 1, size(team_names) - i - if (points(j) < points(j + 1)) then - ! Swap points - tempPoints = points(j) - points(j) = points(j + 1) - points(j + 1) = tempPoints - - ! Swap team names accordingly - tempName = team_names(j) - team_names(j) = team_names(j + 1) - team_names(j + 1) = tempName - end if - end do - end do -end subroutine sort_teams - - - subroutine print_results(team_names, points) implicit none character(len=20), intent(in) :: team_names(:) @@ -92,4 +39,5 @@ subroutine print_results(team_names, points) end do end subroutine print_results + end program football_league_simulator diff --git a/Fortran/exercise2/league_sim_mod.f90 b/Fortran/exercise2/league_sim_mod.f90 new file mode 100644 index 0000000..0d15e23 --- /dev/null +++ b/Fortran/exercise2/league_sim_mod.f90 @@ -0,0 +1,85 @@ +! module for league simulation routines +module league_simulator + +implicit none + +integer :: max_goals = 5 + +private + +public :: simulate_match, sort_teams, max_goals + +contains + +! simulates a match between two teams +subroutine simulate_match(team1, team2, points) + implicit none + integer, intent(in) :: team1, team2 + integer, intent(inout) :: points(:) + + integer :: goals1, goals2 + real :: g1, g2 + + call random_seed() + + ! Simulate goals for each team (max 5 goals) + call random_number(g1) + call random_number(g2) + goals1 = floor(max_goals*g1) + goals2 = floor(max_goals*g2) + + ! Update points based on match result + if (goals1 > goals2) then + points(team1) = points(team1) + 3 ! Team 1 wins + else if (goals2 > goals1) then + points(team2) = points(team2) + 3 ! Team 2 wins + else + points(team1) = points(team1) + 1 ! Draw, each team gets 1 point + points(team2) = points(team2) + 1 + end if + +end subroutine simulate_match + +! sorts an array of teams by the points in array points +subroutine sort_teams(team_names, points) + implicit none + character(len=20), intent(inout) :: team_names(:) + integer, intent(inout) :: points(:) + integer :: i, j + integer :: tempPoints + character(len=20) :: tempName + + do i = 1, size(team_names) - 1 + do j = 1, size(team_names) - i + if (points(j) < points(j + 1)) then + ! Swap points + tempPoints = points(j) + points(j) = points(j + 1) + points(j + 1) = tempPoints + + ! Swap team names accordingly + tempName = team_names(j) + team_names(j) = team_names(j + 1) + team_names(j + 1) = tempName + end if + end do + end do +end subroutine sort_teams + +! prints out resuts from the league +subroutine print_results(team_names, points) + implicit none + character(len=20), intent(in) :: team_names(:) + integer, intent(in) :: points(:) + integer :: i + + +! prints out resuts from the league + print *, "Football League Results:" + print *, "------------------------" + do i = 1, size(team_names) + print *, team_names(i), ": ", points(i), " points" + end do +end subroutine print_results + +end module league_simulator