Java, 1886 caracteres
import java.io.*;import java.util.*;import java.util.regex.*;public class GuessGame {int L=1;int H=100;int G=5;int N;String HS="higher";String LS="lower";String CS="correct";public static void main(String[] args){if (args.length==2)new GuessGame(Integer.parseInt(args[0]),Integer.parseInt(args[1])).play();else if(args.length==0)new GuessGame(100,5).play();else System.out.println("usage GuessGame HighInteger NumberGuess");}GuessGame(int H,int G){this.H = H;this.G = G;}void play(){int pNum=getInt("Play As Player 1 or Player 2?","1|2");if(pNum==1)playP2();else playP1();System.out.println("The number was "+N);}int getInt(String pmpt,String val){BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));int i=0;Pattern p=Pattern.compile(val);boolean fnd=false;String ln="";try{while(!fnd){System.out.println(pmpt);ln=cin.readLine();Matcher m=p.matcher(ln);fnd=m.find();}i=Integer.parseInt(ln);} catch (Exception ex) {}return i;}String processGuess(int g){if(N>g)return HS;else if(N<g)return LS;else return CS;}void playP1(){N=new Random().nextInt(H);for(;G>0;G--){String rslt=processGuess(getInt("Player 2, enter your guess:","\\d?"));System.out.println(rslt);if(rslt.equals(CS)){System.out.println("Player 2 wins!");break;}}}void playP2() {N=getInt("Player 1, enter your number:", "\\d+");int max=H;int min=L;int nextGuess=min+(max-min)/2;for (;G>0;G--){System.out.println("Player 2, enter your guess:" + nextGuess);String rslt=processGuess(nextGuess);System.out.println(rslt);if(rslt.equals(HS)){min=nextGuess+1;nextGuess=fuzzify(nextGuess+(max-nextGuess)/2,min,max);}if (rslt.equals(LS)){max=nextGuess-1;nextGuess=fuzzify(nextGuess-(nextGuess-min)/2,min,max);}if(rslt.equals(CS)){System.out.println("Player 2 wins!");break;}}}int fuzzify(int i,int mn,int mx){int fz=new Random().nextInt(3);if(fz==1)return Math.max(mn,--i);if(fz==2)return Math.min(mx,++i);return i;}}
Versão sem golfe:
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class GuessGame {
int L = 1;
int H = 100;
int G = 5;
int N;
String HS = "higher";
String LS = "lower";
String CS = "correct";
public static void main(String[] args) {
if (args.length == 2)
new GuessGame(Integer.parseInt(args[0]), Integer.parseInt(args[1])).play();
else if (args.length == 0)
new GuessGame(100, 5).play();
else
System.out.println("usage GuessGame HighInteger NumberGuess");
}
GuessGame(int H, int G) {
this.H = H;
this.G = G;
}
void play() {
int pNum = getInt("Play As Player 1 or Player 2?","1|2");
if (pNum == 1)
playP2();
else
playP1();
System.out.println("The number was " + N);
}
int getInt(String pmpt, String val) {
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
int i = 0;
Pattern p = Pattern.compile(val);
boolean fnd = false;
String ln = "";
try {
while (!fnd) {
System.out.println(pmpt);
ln = cin.readLine();
Matcher m = p.matcher(ln);
fnd = m.find();
}
i = Integer.parseInt(ln);
} catch (Exception ex) {}
return i;
}
String processGuess(int g) {
if (N > g)
return HS;
else if (N < g)
return LS;
else
return CS;
}
void playP1() {
N = new Random().nextInt(H);
for (; G > 0; G--) {
String rslt = processGuess(getInt("Player 2, enter your guess:", "\\d?"));
System.out.println(rslt);
if (rslt.equals(CS)) {
System.out.println("Player 2 wins!");
break;
}
}
}
void playP2() {
N = getInt("Player 1, enter your number:", "\\d+");
int max = H;
int min = L;
int nextGuess = min + (max - min) / 2;
for (; G > 0; G--) {
System.out.println("Player 2, enter your guess:" + nextGuess);
String rslt = processGuess(nextGuess);
System.out.println(rslt);
if (rslt.equals(HS)) {
min = nextGuess + 1;
nextGuess = fuzzify(nextGuess + (max - nextGuess) / 2, min, max);
}
if (rslt.equals(LS)) {
max = nextGuess - 1;
nextGuess = fuzzify(nextGuess - (nextGuess - min) / 2, min, max);
}
if (rslt.equals(CS)) {
System.out.println("Player 2 wins!");
break;
}
}
}
int fuzzify(int i, int mn, int mx) {
int fz = new Random().nextInt(3);
if (fz == 1)
return Math.max(mn, --i);
if (fz == 2)
return Math.min(mx, ++i);
return i;
}
}