Friday, January 23, 2015

This simple game is base on a mind game concept, the flow of this basic program is simply input a set of numbers to be guess, and enter a second set of number and check if there are match in every index of two(2) sets of numbers. If there is a number matched in the-same index it will count as one correct and it can sum-up the matched number, like for example, if the user input 123, and the second user input 125, obviously 1 and 2 are matched, and they are the same index in a sets, 1 found on index 0, and 2 found on index 1, and after getting the correct answer, it will sum it up and the answer is 3, obviously 1 + 2; now just take a look on the code below, its a little bit long solution, but i think it will be lessen, the basic way is to create a function to eliminate those redundant data and process.

package teamUndefined;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ahemAw {

    public static void main(String[] args)throws IOException {

  BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
       
        String[] _myNum, _guess;
        String _myNum2 = null, _guess2 = null, _sub = "A",  _onenull, _two = null;       
        int  _minus = 0, _index = 0, _correct = 0, _sum = 0, _length = 0, _b = 0;               
           
            System.out.print("Enter Number to Guess:");
            _myNum2  = buff.readLine();
            System.out.print("Enter Guess Number:");
            _guess2 = buff.readLine();       
           
                        if (_myNum2.length() == _guess2.length()) {
                           
                            _guess = new String[_guess2.length()];
                            _myNum = new String[_myNum2.length()];
                           
                            for (int i = 0; i < _guess2.length(); i++) {
                                _guess[i] = _guess2.substring(i , i + 1);
                                _myNum[i] = _myNum2.substring(i, i + 1);
                            }
                           
                            for (int i = 0; i < _guess.length; i++) {
                                _one = _myNum[i];
                                _two = _guess[i];                       
                                if (_one.equals(_two)) {
                                    _correct++;
                                    _sum += Integer.parseInt(_two);
                                }
                            }                           
                        }
                        else{
                            if (_myNum2.length() > _guess2.length()) {
                                _length =_myNum2.length();           
                                _minus =  _myNum2.length() - _guess2.length();   
                                _b =  1;
                            }
                            else if (_myNum2.length() < _guess2.length()){
                                _length = _guess2.length();
                                _minus = _guess2.length() - _myNum2.length();
                                _b = 2;
                            }
                               
                            _guess = new String[_length];
                            _myNum = new String[_length];
                           
                                    for (int i = 0; i < _length; i++) {                       
                                        if (i < _minus){
                                            if (_b == 1) {
                                                _guess[i] = _sub;   
                                            }
                                            else if(_b == 2){
                                                _myNum[i] = _sub;   
                                            }                                                                           
                                        }
                                        else{
                                            if (_b == 1) {
                                                if (_guess2.length() < (_index + 1)) {
                                                    break;
                                                }
                                                else{
                                          _guess[i] = _guess2.substring(_index, _index + 1);   
                                                    _index++;   
                                                }
                                                for (int k = 0; k < _myNum2.length(); k++) {
                                                    _myNum[k] = _myNum2.substring(k, k + 1);
                                                }
                                            }   
                                            else if(_b == 2){
                                                if (_myNum2.length() < (_index + 1)) {
                                                    break;
                                                }
                                                else{
                                 _myNum[i] = _myNum2.substring(_index, _index + 1);   
                                                    _index++;   
                                                }
                                                for (int k = 0; k < _guess2.length(); k++) {
                                                    _guess[k] = _guess2.substring(k, k + 1);
                                                }
                                            }
                                        }                               
                                    }                   
                                       
                                    if (_b == 1) {
                                        for (int i = 0; i < _guess.length; i++) {
                                            _one = _guess[i];
                                            _two = _myNum[i];
                                            if (_one.equals(_two)) {
                                                _correct++;
                                                _sum += Integer.parseInt(_one);
                                            }
                                        }                       
                                    }
                                    else if (_b == 2){
                                        for (int i = 0; i < _guess.length; i++) {
                                            _one = _myNum[i];
                                            _two = _guess[i];                       
                                            if (_one.equals(_two)) {
                                                _correct++;
                                                _sum += Integer.parseInt(_two);
                                            }
                                        }                       
                                    }   
                               
                            }   
                       
                   
                        System.out.println("Correct: " + _correct + " Sum: " + _sum);
       
    }

}

Note: if the user inputted different length of set numbers,
e.g

input 1: 123
input 2: 23

so, 23 become A23, to match the length of input 1 and shift it to the right.

0 comments:

Post a Comment