Skip to main content
Commonmark migration
Source Link

#Java, 1886 chars

Java, 1886 chars

#Java, 1886 chars

Java, 1886 chars

edited body
Source Link
Timtech
  • 12.6k
  • 2
  • 45
  • 61

Java#Java, 1886 chars:

Java, 1886 chars:

#Java, 1886 chars

Removed all line feeds and accepted the unreadable textbox.
Source Link

Java, 19271886 chars:

import java.io.*; import*;import java.util.*; import*;import java.util.regex.*; public*;public class 
 GuessGame {int L=1;int H=100;int G=5;int N;String HS = "higher";String LS = "lower";
String CSHS="higher";String =LS="lower";String "correct";publicCS="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;thisH = H;this.G=G;
G = G;}void play() {int
  pNum=getInt("Play As Player 1 or Player 2?","1|2");if (pNum==1)playP2();else playP1();
System;System.out.println("The number was "+N);}int getInt(String pmpt, String val)
 {BufferedReader cin = newcin=new BufferedReader(new InputStreamReader(System.in));int i=0;
Patterni=0;Pattern p=Pattern.compile(val);boolean fnd=false;String ln="";try {while (!fnd)
 {System.out.println(pmpt);ln = cin;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;System.out.println(rslt);if (rslt.equals(CS)){System.out.
 println("Player 2 wins!");break;}}}void playP2() {N = getIntN=getInt("Player 1, enter your number:"
 , "\\d+");int max=H;int min=L;int nextGuess=min+(max-min)/2;for (; G > 0; G;G>0;G--)
 {System.out.println("Player 2, enter your guess:" + nextGuess);String rslt=
processGuessrslt=processGuess(nextGuess);System.out.println(rslt);if (rslt.equals(HS))
 {min=nextGuess+1;nextGuess = fuzzifymin=nextGuess+1;nextGuess=fuzzify(nextGuess+(max-nextGuess)/2,min, max);}if
  (rslt.equals(LS)) {max=nextGuess-1;nextGuess = fuzzify1;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;}}

Java, 1927 chars:

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;}}

Java, 1886 chars:

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;}}
Undid accidental string split from trying to prevent an unreadable single line text box control.
Source Link
Loading
Source Link
Loading