Ian
If you want to play within walking distance of train stations, and are not planning to go much further than St. Andrews, I would recommend the following:
1. Firstly, don't forget to look around the Birkdale area, which is full of great golf courses. I'm not sure what hours they are expecting you to work, but at Open time in that area the sun rises at 5am and sets at 9:30pm. Take whatever free time you have to vist and/or play places like Formby, Southport and Ainsdale, Hesketh, Lytham, Wallasey, Hoylake, etc.
2. Don't even think of getting off the train until you get to Edinburgh. While there are some good golf courses between Birkdale and there, they are not accessible by train.
3. When you get to Edinburgh, change trains to North Berwick and spend 2-3 days there. Play/see North Berwick, the Gullane courses, Kilspindie, Dunbar, Longniddry, Luffness, maybe even Muirfield if you are bold and/or lucky.
4. Next, take the train back to Edinburgh and then on past St. Andrews to Carnoustie. Spend 2-3 days there playing/seeing Carnoustie, Monifieth, Panmure and Montrose.
5. On the way back from Carnoustie to St. Andrews, play/see Scotscraig.
6. If you want to break the journey from North Berwick-Carnoustie, stop off along the way at Ladybank and/or one of the village courses along the way(Aberdour, Burntisland, Kinghorn, Thornton, Balbirnie).
7. Or, just stay in St. Andrews for another week!
Cheers
Rich