// programmer: Tyler Harter // last modified: 2008-12-24 //what choices will be made for the first several rounds global multi opening; //how many rounds to play before we start using a strategy global number openingCount; //the choices made by the basicStrategy function which may or may not be used global multi basicChoices; proc init() { //Begin with a randomly chosen but predictable pattern to throw off the //opponent. The opponent will probably learn the pattern, but the //pattern won't be used for the rest of the game openingCount = 4; //this must be at least 3 number r = random number; choice c = random choice; if (r <= 20%) { for (number i = 1; i <= openingCount; inc i) { choice opening[i] = c; inc c; } } else if ( r <= 40%) { for (number i = 1; i <= openingCount; inc i) { choice opening[i] = c; dec c; } } else { for (number i = 1; i <= openingCount; inc i) { choice opening[i] = c; } } } choice basicStrategy { //see if the opponent would rather repeat or change each time //make sure enough rounds have been played to proceed if (rounds < 2) { say "basicStrategy can only be used if at least 2 rounds " + "have been played"; return random choice; } number repeats = 0; number changes = 0; //only base strategy on the last 20 rounds number start = rounds - 19; if (start < 2) { start = 2; } for (number i = start; i <= rounds; inc i) { if (you[i] = you[i - 1]) { inc repeats; } else { inc changes; } } choice c = you[rounds]; if (changes > repeats * 2) { //the opponent tends to change, so choose the safer of the //the two choices that could be good. For instance, the //opponent chose rock last time. Thus, this time they'll //probably play paper or scissors. Thus, we want to play //scissors or rock. Scissors is safer, as we have a 50% //of getting a tie and a 50% change of winning assuming they //do something different. If we choose rock, though, we have //a 50% chance of winning and a 50% chance of losing. dec c; } else { //the opponent tends to repeat, so do 1 better than their last //choice inc c; } return c; } choice choose() { choice c; if (rounds >= 2) { c = basicStrategy(); choice basicChoices[rounds + 1] = c; } if (rounds < openingCount) { //we're still playing the opening rounds return opening[rounds + 1]; } else { //we're in the mid game, so we'll consider using our basic //strategy. If the strategy has been doing well lately, we'll //use it; otherwise, we'll adjust it. if (basicChoices[rounds] = you[rounds] and basicChoices[rounds-1] = you[rounds-1]) { //the basicStrategy would have tied the last 2 rounds //(the odds of this are 1/9), so do something that's 1 //better inc c; } else if (basicChoices[rounds] < you[rounds] and basicChoices[rounds-1] < you[rounds-1]) { //the basicStrategy would have lost the last 2 rounds //(the odds of this are 1/9), so do something that's 2 //better inc c; inc c; } } return c; }