Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Basic prisoner's dilemma? Date: Mon, 18 Apr 2011 16:33:41 -0400 Organization: albasani.net Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net u9Eu9DF6dHRPcv7pLyM44qXNce+iz5pbhM8urO8Jrdy1PjNRRZOTx888ou82IyiQCaRb2RhYQHpbp9dialTJsXDbopvJq6WBCyMlxyY9DSkw0m9BdiZVIqLRdapy2SG6 NNTP-Posting-Date: Mon, 18 Apr 2011 20:33:38 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="wuvNxVNQafHkPxxDjuasjbYHgEkNYgyL43yRNupyNcHeBzkm+xhJD/WVAQZZZybM6vVpVKDjQ9wD1k5vnJiq7NjYgm0CM1ZV3i2oEKOhy7YWUy4x8ziYYX5b5p69MEmi"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 In-Reply-To: Cancel-Lock: sha1:OHXfwpK2nKQw4gTdEy4jlxoWV80= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3116 Daniele Futtorovic wrote: > Lew allegedly wrote: >> Daniele Futtorovic wrote: >>> Basic tit-for-tat is as follows: >>> - if it's the first time you encounter that opponent (this implies you >>> keep a 'memory' of previous encounters), cooperate. >>> - otherwise, replicate that opponent's last move. >>> >>> In code (assuming oppHistory contains only the history with "this"): >>> >>> public String chooseAction( >>> ArrayList myHistory, ArrayList oppHistory) >>> { >>> return oppHistory.isEmpty() ? "COOPERATE" >>> : oppHistory.get( oppHistory.size() >> - 1 >>> ); >>> } >>> (or oppHistory.get(0), depending on whether it's used as a FIFO or a >>> LIFO). [snip] > Well spotted. Thanks, Lew. HTH. As a matter of good habit, I also suggest using interface-typed parameters to the method, but that is not germane to the point of your example. Inevitably my mind starts spinning coder fantasies even on these toy examples: - "Wouldn't an enum base type and return type be better than String?" - "What if we keep a rolling buffer of choices of size N rather than recording all history?" - "What if there were a third response possible (thus more reason to use an enum), say, 'PREVARICATE'?" - "What would that even mean?" - "What if the choices were more like Rock-Paper-Scissors?" - "What is the reward / cost for each combination of choices?" - "What if that reward / cost increased or decreased with k, the number of game rounds played so far?" - "What is the terminating condition of the game?" I'm firing these questions out without recently having reviewed the Prisoner's Dilemma, but the simple form you describe is only the launching point. -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg