<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
Hello,<div><br></div><div>I've been thinking about implementing some simple games, where one can program agents to play the game. I thought that the multiprocessing module would be perfect for it, organized with a main simulator engine spawning processes for each agent. However, I am having trouble wrapping my head around the organization of the code. I haven't found a lot of examples of using multiprocessing, and no game implementations.</div><div><br></div><div>There are two types of games that I would like to implement. In both cases, I'd like to be able to run something like:</div><div><br></div><div>results=Sim.run('agent1.py','agent2.py')</div><div><br></div><div>where the Sim object is initialized before with the board size, placement of pieces, etc... The files agent1.py and agent2.py would have the code to run the agent. The two types of games are different in terms of the organization of the agent code. In the first, which I think should be more straightforward, there will be one function defined in the file with syntax like:</div><div><br></div><div>def agent(board,my_player_number):</div><div> # stuff here for the agent</div><div> return move</div><div><br></div><div>Think "tic-tac-toe" here. The simulator would have to spawn processes, which load these files and handle the communication to the main simulator. The main simulator would loop through the agents, get moves, and then update the board with these moves. In the case of tic-tac-toe, then each agent is called in serial, and the update after each move. In the case of something like Civilization or Tron, the moves are all collected and then the board is updated.</div><div><br></div><div>The other type of game, which would work like Tron or Civilization, the agent file would not contain a function but a list of commands, like:</div><div><br></div><div>North()</div><div>North()</div><div>if Neighbor['North']:</div><div> East()</div><div># etc...</div><div><br></div><div>I am not sure what the best way to organize the code, in either case, for the main simulator or the best way to use multiprocessing here. I thought that maybe Pipe would be the best choice, and for each move there would be a send() and recv() between each agent and the main simulator, where the agent would receive updated information after each move. What happens if an agent has an infinite loop (ignores a signal from the simulator that it has lost)? Can the simulator catch this, and kill the process? Does that happen if either side closes the pipe? Is there an obvious way to structure this sort of game engine? Is there an implementation already out there that I am reinventing? What information would be best to send to the agent? I was finding myself using more global variables than I was comfortable, so I figured I was thinking about this incorrectly.</div><div><br></div><div><br></div><div>Any help would be great! Pointers to other examples of using multiprocessing, especially with games, would be fantastic.</div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>thanks,</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Brian Blais</div><div><br></div><div><br><div> <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Monaco; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="border-collapse: separate; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; color: rgb(0, 0, 0); font-family: Monaco; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; -webkit-text-decorations-in-effect: none; text-indent: 0px; -webkit-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div>-- </div><div>Brian Blais</div><div><a href="mailto:bblais@bryant.edu">bblais@bryant.edu</a></div><div><a href="http://web.bryant.edu/~bblais">http://web.bryant.edu/~bblais</a></div><div><a href="http://bblais.blogspot.com">http://bblais.blogspot.com</a>/</div><br class="Apple-interchange-newline"></span></span><br class="Apple-interchange-newline"> </div><br></div></body></html>