Translate

четверг, 24 мая 2012 г.

Android programming for beginners. "Hangman" the word game developing for android. About program drawing and java - logic

 It's last article about intellectual game development for android. We must realize next things:

-output game information (word and lives)

-checkLetter() method

-indication of wrong and right letters via program drawing

-output the toast about win or fail

-new game method





  Method for new game begin:

Fields in custom component:
String word;//word
String[] words={"ОДИН","ДВА","ТРИ","ЧЕТЫРЕ"};//Wordbase
String right_letters;
String wrong_letters;
int word_length;/
int lives;

static final int MAX_LETTERS_ON_THE_WORD=1000;
boolean[] bool=new boolean[MAX_LETTERS_ON_THE_WORD];//known/unknown letter indicator. dor example, if word is "kn??", bool is {true, true, false, false}
private void newWord() {
   lives=10;//Set 10 lives for user
   word=words[(int) (System.currentTimeMillis()%words.length)];//determine the word via getting random number 
   invalidate();//call OnDraw()

   //hide all letter       
   for (int i=0;i<MAX_LETTERS_ON_THE_WORD;i++){
             bool[i]=false;        
   }            
    
  //obviously))        
   word_length=word.length();        
   right_letters="";        
   wrong_letters="";            
}  

Show the word and lives in onDraw() method:
  
double word_length_in_pixels=word_length*letter_size*k*c;//k and c is- special fields of class for style. k is relative size of word letter to usually letters in table.  k is relative of with height of letter to width (k=1.4  c=0.6)
      
String new_word="";//Begin forming output word
      
paint.setTextSize((float) (letter_size*k));//Set size
      
//Forming new_word literally. If letter is unknown, add "?". Else - add letter from word
 for (int i=0;i<word_length;i++){
            if(bool[i]==false){
                new_word+="?";
            }
            else{
                new_word+=word.charAt(i)+"";
            }
 }
      
 //If all letters are known, it's win
if(new_word.indexOf("?")==-1){
            YouWin();
            newWord();
 }
      
//Draw new word and lives number
canvas.drawText(new_word, (float)(width/2-word_length_in_pixels/2), (float)(off_const*height/2), paint);
canvas.drawText("Lives:  "+lives, (float)(width/2-word_length_in_pixels/2), (float)(off_const*height/1.2), paint);


"You have won" and "You have lost" methods
private void YouWin() {
        Toast.makeText(context, "You have lost.  Word is "+word, Toast.LENGTH_LONG).show();
}

private void YouWin() {
        Toast.makeText(context, "You have won.  Word is "+word, Toast.LENGTH_LONG).show();
}
  
Time for write checkLetter() method:

private void checkLetter(String letter) {
        boolean good_try;//is it right letter?
        if(word.indexOf(letter)!=-1){//if word contains letter
            good_try=true;
            
            //loop on the word. is letter is datum, change the bool flag
            for (int i=0;i<word_length;i++){
                if(word.charAt(i)==letter.charAt(0)){
                    bool[i]=true;
                }
            }
        }
        else{
            good_try=false;
        }
      
        //Push letter in right or wrong list
        if(!good_try){
            if(wrong_letters.indexOf(letter)==-1){
              lives--;
              wrong_letters+=letter;
            }
        }
        else{
            right_letters+=letter;
        }
      

        if(lives==0){
            YouLose();
            newWord();
        }
      
        //onDraw() call
        invalidate();
}
  
///

 Last method is program drawing wrong and right letter marks on the gamefield (it's part of onDraw() - in the end)
        //Borders of mark
        double x1;
        double y1;
        double x2;
        double y2;
      
        for (int i=0;i<wrong_letters.length();i++){//loop on all wrong letters
            int order=0;          
            //determining order of letter on the alphabet
            for (int j=0;j<alphabet.length();j++){
                if(alphabet.charAt(j)==wrong_letters.charAt(i)){
                    order=j;
                }
            }
          
            int x;
            int y;

            //find line and row of cell
            x=order%rows;
            y=(int) Math.floor((double)(order)/(double)(rows));
            x1= (double)(x)*width/(double)(rows);
            y1= (double)(y)*red_height/(double)(columns)+off_const*height;
            x2= (double)(x+1)*width/(double)(rows);
            y2= (double)(y+1)*red_height/(double)(columns)+off_const*height;
          
            //Set parameters for cross
            paint.setColor(wrong_color);//color (class field)
            paint.setStrokeWidth(mark_wigth);//thickness (class field)
          
            canvas.drawLine((float)(x1),  (float)(y1),  (float)(x2),  (float)(y2),  paint);
            canvas.drawLine((float)(x1),  (float)(y2),  (float)(x2),  (float)(y1),  paint);
        }
Analogical with right letter string

Скачать исходник игры

Комментариев нет:

Отправить комментарий

Related Posts Plugin for WordPress, Blogger...