Android Open Source - guess4num Guess Number Game

From Project

Back to project page guess4num.


The source code is released under:

GNU General Public License

If you think the Android project guess4num listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

 * Android Guess4num//from w  w  w .ja  v a 2  s .c om
 * by niasw<>
 * @author niasw(Sun Smallwhite)

import java.util.Random;

public class GuessNumberGame{
  int num; //The number
  int ges; //The guess
  int rpt=0; //The report
  Random randseed=new Random();

  public GuessNumberGame() {
  public GuessNumberGame(int setNum) {
    if (setNum<0) {setNum=-setNum;}
    if (setNum>9999) {setNum=setNum%10000;}
  public int getNum() {
    return num;
  public static String parse(int _ges, int _rep) {
     * generate reply message from integer return from Method "check".
     * _ges: guess number.
     * _rep: reply from check Method.
    String ret = new String("");
    ret=ret+String.format("%04d",_ges)+"  ";
    return ret;
  public int check(int _ges) {
    return check(_ges,num);
  public static int check(int _ges, int _num) {
     * check how many digits are in the right place, and how many digits are included.
     * _ges: guess number.
     * _num: target number.
    int include=0;
    int perfect=0;
    char cges[]=new char[4];
    char cnum[]=new char[4];
    for (int it=0;it<4;++it) {
    for (int it=0;it<4;it++) {
      if (cges[it]==cnum[it]) {
    for (int it2=0;it2<4;it2++) {
      for (int it1=0;it1<4;it1++) {
        if (cges[it1]==cnum[it2]&&cnum[it2]!='-') {
            assert(it1!=it2):"What? Leakage detected from Perfect Match Scanning!";
    return perfect*100+include;

Java Source Code List