i'm studying following client/server code called knockknockserver , knockknockclient. has helper class called knockknockprotcol, responsible order of knockknock jokes.
i want modify can start program @ specific joke. right now, have start first joke.
this tried knockknockprotocol:
public class knockknockprotocol { int ourstep; public knockknockprotocol (int step) { this.ourstep = step; } private static final int waiting = 0; private static final int sentknockknock = 1; private static final int sentclue = 2; private static final int = 3; private static final int numjokes = 5; private int state = waiting; int currentjoke = ourstep; //we initialize step here private string[] clues = { "turnip", "little old lady", "atch", "who", "who" }; private string[] answers = { "turnip heat, it's cold in here!", "i didn't know yodel!", "bless you!", "is there owl in here?", "is there echo in here?" }; public string processinput(string theinput) { string theoutput = null; if (state == waiting) { theoutput = "knock! knock!"; state = sentknockknock; } else if (state == sentknockknock) { if (theinput.equalsignorecase("who's there?")) { theoutput = clues[currentjoke]; state = sentclue; } else { theoutput = "you're supposed \"who's there?\"! " + "try again. knock! knock!"; } } else if (state == sentclue) { if (theinput.equalsignorecase(clues[currentjoke] + " who?")) { theoutput = answers[currentjoke] + " want another? (y/n)"; state = another; } else { theoutput = "you're supposed \"" + clues[currentjoke] + " who?\"" + "! try again. knock! knock!"; state = sentknockknock; } } else if (state == another) { if (theinput.equalsignorecase("y")) { theoutput = "knock! knock!"; if (currentjoke == (numjokes - 1)) currentjoke = 0; else currentjoke++; state = sentknockknock; } else { theoutput = "bye."; state = waiting; } } return theoutput; } } and in server class, call knockknockprotocol :
/* omitting boilerplate code */ string inputline, outputline; // initiate conversation client knockknockprotocol kkp = new knockknockprotocol(2); outputline = kkp.processinput(null); out.println(outputline); while ((inputline = in.readline()) != null) { outputline = kkp.processinput(inputline); out.println(outputline); if (outputline.equals("bye.")) break; the problem , when run code starting "turnip" joke. how make can start @ arbitrary joke within list of jokes? can see joke controlled clues array, after i'm not seeing it. thanks
if @ input knockknockprotocol constructor, notice directly affects currentjoke counter via this.ourstep used reference clues array.
therefore start @ different locations can pass in different number in constructor @ beginning of program.
hope helps!
Comments
Post a Comment