java - Stuck on the prisoner dilema -
i have classic prisoner's dilema java code.
here instructions:
table:
ask user input going do: stay silent or betray. must use joptionpane drop down lists.
look , use java random number generator determine friend going do.
print message showing results. results include each of did (stayed silent or betrayed) , sentence each person. message should sentence or more – no codes s or b actual words.
use joptionpane input , output
this have far:
import javax.swing.*; public class rich04 { public static void main (string[] args) { string[] ddlist = {"betray", "stay silent"}; object selectedvalue; int code; selectedvalue = joptionpane.showinputdialog( null, "choose one", "prisoner's dilema", joptionpane.question_message, null, ddlist, ddlist[1]); (int i=0; i<ddlist.length; i++) if (ddlist[i].equals(selectedvalue)) joptionpane.showmessagedialog( null, "the user selected " + ddlist[i], "decision", joptionpane.information_message); } }
i know it's not lot i'm @ loss next
note payoff table 2x2 array. store result of game in each of cells. retrieve user's choice , friend's choice 0 (silent) or 1 (betray). these indexes 2x2 array. finally, use indexes result:
import java.util.random; import javax.swing.*; public class rich04 { public static void main (string[] args) { string[] ddlist = {"silent", "betray"}; string[][] sentencematrix = { {"both players chose remain silent. both 1 year", "you stayed silent. friend betrayed. 5 you; 0 friend"}, {"you betrayed. friend stayed silent. 0 you; 5 friend", "both players betrayed. both 3 years"} }; //get user's choice object selectedvalue = joptionpane.showinputdialog( null, "choose one", "prisoner's dilema", joptionpane.question_message, null, ddlist, ddlist[1]); int mychoicenum = 0; if (((string)selectedvalue).equalsignorecase("silent")) mychoicenum = 0; else mychoicenum = 1; //get friend's choice random rand = new random(); int friendchoicenum = rand.nextint(2); string sentence = sentencematrix[mychoicenum][friendchoicenum]; //output result joptionpane.showmessagedialog( null, sentence, "decision", joptionpane.information_message); } }
Comments
Post a Comment