I am trying to test 8 different game algorithms. These algorithms can play against each other in different games that follow the game interface.
Thus, they have to play against each other in 100 games. I have done this part and everything works fine. Now I'm trying to do this multi-threaded in order to use a friend's 8-core computer.
I have very little experience with threads. So, what changes should I make to make my code multithreaded?
Here is the code for my single threaded version.
EDIT: The solution I was thinking about (with my basic knowledge) is to create a Match class that accepts two players and the games they want to play. This class will implement Runnable, and I could create a thread for each of the games. My question now would be, how would I notify the results after the run () method completes?
thanks
for (int p1 = 0; p1 < allPlayers.length; p1++) { for (int p2 = p1 + 1; p2 < allPlayers.length; p2++) { for (int t = 0; t < trials; t++) { int player1 = t % 2 == 0 ? p1 : p2; int player2 = t % 2 == 0 ? p2 : p1; Player[] players = new Player[] { allPlayers[player1], allPlayers[player2] }; game.newGame(); while (!game.isFinished()) game.playNthMove(players[game.currentPlayer()].move(game)); data[p1][p2][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 0 : 1]); data[p2][p1][t] = getCharValue(game.getOutcome()[t % 2 == 0 ? 1 : 0]); } } }
David robles
source share