Write late, in the division of a Z City high School of a performance query system, the original system was written with VB, I have to admire the original writing system that the man really can live.
Tomorrow to set up the SVN will wait for the first project and then start projects, think of a salary, in my heart also for my withered purse small excited.
Gossip not much to say, today we analyze the work of this small game and the core code parsing:
Working principle:
"Main interface" and "Level interface" not much to say, these two are directly write the XML file,
Then, we build in the "Game Interface" is:
Java code dynamically generated this interface, in the interface through the service layer and the DAO layer method, the corresponding level of all the Idiom object, and then each of these objects are decomposed into 4 word objects, and then put into the interface, when clicked, Judging the four clicks of Word can not form an idiom (according to their level and the order they click to determine whether they can form an idiom). If you can form an idiom, pop up a box to show the explanation of the idiom, the source of information, and so on.
Let's look at the contents of the Cn.idiomlianliankan.dao package:
Gamedaoimpl.java:
Package cn.idiomlianliankan.dao.impl;
Import java.util.ArrayList;
Import java.util.List;
Import android.content.Context;
Import android.database.Cursor;
Import android.database.sqlite.SQLiteDatabase;
Import android.util.Log;
Import cn.idiomlianliankan.dao.GameDao;
Import cn.idiomlianliankan.domain.CheckPoint;
Import cn.idiomlianliankan.domain.Idiom;
Import cn.idiomlianliankan.domain.Word;
Public class GameDaoImpl implements GameDao {
Private SQLiteDatabase db;
Public GameDaoImpl(){}
/**
Construct a GameDaoImpl object, passing in the database output stream
* @param context passed in context object
* @param db database output stream
*/
Public GameDaoImpl(Context context,SQLiteDatabase db) {
This.db = db;
}
/**
* Add an idiom to bind the idiom object to be added to the level object
* @param checkpoint level object
* @param idiom idiom object
*/
@Override
Public void addIdiom(CheckPoint checkpoint,Idiom idiom) {
/ / Insert checkpoint table
/ / Determine whether there is this id in the checkpoint table, if there is no insertion, no insert
Cursor cursor = db.rawQuery("select * from checkpoint where checkId=?", new String[]{String.valueOf(checkpoint.getCheckId())});
If(!cursor.moveToNext()){
db.execSQL("insert into checkpoint(checkId) values(?)",
New Object[]{checkpoint.getCheckId()});
}
/ / Insert idiom table
db.execSQL("insert into idiom( idiomContent , idiomExplain , idiomProv , foreignCheckId) values(?,?,?,?)",
New Object[]{idiom.getIdiomContent(), idiom.getIdiomExplain(), idiom.getIdiomProv(), idiom.getCheckpoint().getCheckId()});
/ / Insert the word table
Idiom = getIdiom(idiom.getIdiomContent());
addWord(idiom);
Log.i("test", "4");
}
/**
* Decompose the idiom into four word objects and insert them into the word table
* @param idiom divides the idiom idiom into 4 word objects and inserts them into the word table.
*/
Public void addWord(Idiom idiom){
Log.i("test", "5");
String idiomContent = idiom.getIdiomContent();
For(int i =1;i<=4;i++){
Char wordContent = idiomContent.charAt(i-1);
db.execSQL("insert into word(wordContent,wordLevel,foreignIdiomId) values(?,?,?)",new Object[]{wordContent , i ,idiom.getIdiomId()});
}
}
/**
* Get the idiom object in the database according to idiomContent
* @param content idiom content
*/
Public Idiom getIdiom(String content){
Idiom idiom = new Idiom();
Cursor cursor = db.rawQuery("select * from idiom where idiomContent=?", new String[]{content});
If(cursor!=null && cursor.moveToFirst()){
Int idiomId = cursor.getInt(cursor.getColumnIndex("idiomId"));
String idiomContent = content;
String idiomExplain = cursor.getString(cursor.getColumnIndex("idiomExplain"));
String idiomProv = cursor.getString(cursor.getColumnIndex("idiomProv"));
idiom.setIdiomId(idiomId);
idiom.setIdiomContent(idiomContent);
idiom.setIdiomExplain(idiomExplain);
idiom.setIdiomProv(idiomProv);
}
Return idiom;
}
/**
* Get the idiom object in the database based on idiomId
* @param idiomId idiom id
*/
Public Idiom getIdiom(int idiomId){
Idiom idiom = new Idiom();
idiom.setIdiomId(idiomId);
Cursor cursor = db.rawQuery("select * from idiom where idiomId=?", new String[]{String.valueOf(idiomId)});
If(cursor!=null && cursor.moveToFirst()){
String idiomContent = cursor.getString(cursor.getColumnIndex("idiomContent"));
String idiomExplain = cursor.getString(cursor.getColumnIndex("idiomExplain"));
String idiomProv = cursor.getString(cursor.getColumnIndex("idiomProv"));
idiom.setIdiomContent(idiomContent);
idiom.setIdiomExplain(idiomExplain);
idiom.setIdiomProv(idiomProv);
}
Return idiom;
}
/**
* Return to the Idiom List collection of a level
* @param cp level object
*/
@Override
Public List<Idiom> getIdioms(CheckPoint cp) {
List<Idiom> idioms = new ArrayList<Idiom>();
Log.i("lujing", "dao layer");
Cursor cursor = db.rawQuery("select * from idiom where foreignCheckId=?", new String[]{String.valueOf(cp.getCheckId())});
While(cursor.moveToNext()){
Int idiomId = cursor.getInt(cursor.getColumnIndex("idiomId"));
String idiomContent = cursor.getString(cursor.getColumnIndex("idiomContent"));
String idiomExplain = cursor.getString(cursor.getColumnIndex("idiomExplain"));
String idiomProv = cursor.getString(cursor.getColumnIndex("idiomProv"));
Idiom idiom = new Idiom(idiomExplain, idiomProv, idiomContent, cp);
idiom.setIdiomId(idiomId);
Idioms.add(idiom);
}
Cursor.close();
Return idioms;
}
/**
* Get four Word objects from the database through the Idiom object
* @param idiom Get 4 word objects via idiom
*/
@Override
Public List<Word> getWordsByIdiom(Idiom idiom) {
List<Word> words = new ArrayList<Word>();
Cursor cursor = db.rawQuery("select * from word where foreignIdiomId=?", new String[]{String.valueOf(idiom.getIdiomId())});
While(cursor.moveToNext()){
Int wordId = cursor.getInt(cursor.getColumnIndex("wordId"));
Stri
Ng wordContent = cursor.getString(cursor.getColumnIndex("wordContent"));
Int wordLevel = cursor.getInt(cursor.getColumnIndex("wordLevel"));
Word word = new Word(wordLevel, w
then, let's look at the service layer code:
Package cn.idiomlianliankan.service.impl;
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.Iterator;
Import java.util.List;
Import android.content.*;
Import android.database.sqlite.SQLiteDatabase;
Import cn.idiomlianliankan.dao.impl.GameDaoImpl;
Import cn.idiomlianliankan.domain.CheckPoint;
Import cn.idiomlianliankan.domain.Idiom;
Import cn.idiomlianliankan.domain.Word;
Import cn.idiomlianliankan.initialize.GameConfigue;
Import cn.idiomlianliankan.service.GameService;
Public class GameServiceImpl implements GameService {
Private GameDaoImpl gamedaoImpl;
Private SQLiteDatabase db;
Public GameServiceImpl(Context context,SQLiteDatabase db){
gamedaoImpl = new GameDaoImpl(context,db);
This.db = db;
}
/**
* Returns a collection of word objects corresponding to all idioms
*/
@Override
Public List<Word> getWords(List<Idiom> idioms) {
List<Word> words = new ArrayList<Word>();
Idiom idiom = new Idiom();
Iterator idiomIterator = idioms.iterator();
While (idiomIterator.hasNext()) {
/ / Get the idiom object
Idiom = (Idiom) idiomIterator.next();
If(idiom != null){
/ / Define a word list collection, add 4 word objects obtained by an idiom into
List<Word> wordList = new ArrayList<Word>();
wordList = gamedaoImpl.getWordsByIdiom(idiom);
Word word = new Word();
Iterator wordIterator = wordList.iterator();
While(wordIterator.hasNext()){
Word = (Word) wordIterator.next();
Words.add(word);
}
}
}
Collections.shuffle(words);
Return words;
}
/**
* Determine if the blocks are connected
*/
@Override
Public boolean link(Word p1, Word p2, Word p3, Word p4) {
If((p1.getIdiom().getIdiomId() == p2.getIdiom().getIdiomId()) && (p3.getIdiom().getIdiomId() == p4.getIdiom().getIdiomId())){
If((p1.getWordLevel() == 1) && (p2.getWordLevel() == 2) && (p3.getWordLevel() == 3) && (p4.getWordLevel() == 4)){
Return true;
}
}
Return false;
}
}
Finally, the "Game Interface" code:
Package cn.idiomlianliankan.game;
Import java.util.ArrayList;
Import java.util.Iterator;
Import java.util.List;
Import cn.idiomlianliankan.dao.GameDao;
Import cn.idiomlianliankan.dao.impl.GameDaoImpl;
Import cn.idiomlianliankan.domain.CheckPoint;
Import cn.idiomlianliankan.domain.Idiom;
Import cn.idiomlianliankan.domain.Word;
Import cn.idiomlianliankan.initialize.GameConfigue;
Import cn.idiomlianliankan.service.GameService;
Import cn.idiomlianliankan.service.impl.GameServiceImpl;
Import android.app.Activity;
Import android.content.Intent;
Import android.database.sqlite.SQLiteDatabase;
Import android.graphics.Color;
Import android.os.Bundle;
Import android.util.Log;
Import android.view.View;
Import android.widget.Button;
Import android.widget.LinearLayout;
Import android.widget.TextView;
Import android.widget.Toast;
Public class GameViewActivity extends Activity {
/ / Define the database object
SQLiteDatabase db;
/ / Define service, dao layer object
Private GameService service;
Private GameDao dao;
Private textView textView;
/ / Define a word list collection
Private List<Word> ls = new ArrayList<Word>();
@Override
Protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/ / Initialize the database object, open the database
Db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+ ("/lianliankan.db"), null);
/ / Initialize the service layer object
Service = new GameServiceImpl(this,db);
/ / Initialize the dao layer object
Dao = new GameDaoImpl(this,db);
/ / Create a linear layout
LinearLayout gameView = new LinearLayout(this);
gameView.setOrientation(LinearLayout.HORIZONTAL);
/ / Create a game interface by the createView () function
gameView = createView(gameView);
gameView.setPadding(gameView.getLeft()+130, gameView.getTop()+130, gameView.getRight(), gameView.getBottom());
setContentView(gameView);
}
/**
* Create interface functions
* @param passes the LinearLayout object
* @return returns a LinearLayout object that creates a good interface
*/
Private LinearLayout createView(LinearLayout lay) {
/ / Game initial resource configuration
Int x = GameConfigue.getxSize();
Int y = GameConfigue.getySize();
Int height = GameConfigue.getPieceHeight();
Int width = GameConfigue.getPieceWidth();
//This is just setting the first level.
CheckPoint cp = new CheckPoint(1);
/ / Get the idioms object, and then get the words object
List<Idiom> idioms = dao.getIdioms(cp);
List<Word> words = service.getWords(idioms);
Iterator it = words.iterator();
Word singleword = new Word();
For(int i = 1; i< x+1;i++){
Final LinearLayout layView=new LinearLayout(this);
layView.setOrientation(LinearLayout.VERTICAL);
For(int j = 1;j<y+1; j++){
If(it.hasNext())
{
Singleword = (Word)it.next();
}
TextView mTextView = new TextView(this);
mTextView.setHeight(height);
mTextView.setWidth(width);
/ / The text here is the content of the word object in the database
mTextView.setText(singleword.getWordContent()+"");
//The id here is the id of the word object in the database
mTextView.setId(singleword.getWordId());
mTextView.setTextSize(35);
mTextView.setTextColor(Color.GREEN);
mTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.back));
/ / Set the click event listener
mTextView.setOnClickListener(new TextviewOnclickListener());
mTextView.setPadding(mTextView.getLeft()+18, mTextView.getTop()+7, mTextView.getRight(), mTextView.getBottom());
layView.addView(mTextView);
}
lay.addView(layView);
}
Return lay;
}
Private final class TextviewOnclickListener implements View.OnClickListener{
Iterator it = ls.iterator();
Boolean isLink = false;
/ / First judge the ls collection has several word objects
/ / If there are three objects, determine whether the click and the other three can link
/ / Less than three objects, then add
@Override
Public void onClick(View v) {
If(ls.size() == 3)
{
/ / To determine whether these four are connected
//If it is connected, it will be eliminated. Then call the IdiomDetailsActivity interface
/ / If not connected, cancel all background colors
Iterator it = ls.iterator();
Word w1 = new Word();
Word w2 = new Word();
Word w3 = new Word();
Word w4 = new Word();
Int id = v.getId();
W4 = dao.findWord(id);
While(it.hasNext()){
W1 = (Word) it.next();
W2 = (Word) it.next();
W3 = (Word) it.next();
}
isLink = service.link(w1, w2, w3, w4);
If(isLink){ // can form an idiom
Int idiomId = w1.getIdiom().getIdiomId();
setTextView(w1.getWordId());
textView.setVisibility(View.INVISIBLE);
setTextView(w2.getWordId());
textView.setVisibility(View.INVISIBLE);
setTextView(w3.getWordId());
textView.setVisibility(View.INVISIBLE);
setTextView(w4.getWordId());
textView.setVisibility(View.INVISIBLE);
//When the correct idiom is obtained, all the information of the idiom is obtained from the database through the id of the idiom, and then the interface is displayed.
Intent intent = new Intent();
intent.setClass(getApplicationContext(), IdiomDetialsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("id",idiomId+"");
intent.putExtra("bd", bundle);
startActivity(intent);
}else{
setTextView(w1.getWordId());
textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.back));
setTextView(w2.getWordId());
textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.back));
setTextView(w3
.getWordId());
textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.back));
}
Ls.clear();
}else{
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.backfu));
/ / According to the id of this box to get a word object
/ / Add the word object obtained by the query into ls
Int id = v.getId();
Word word = new Word();
Word = dao.findWord(id);
Ls.add(word);
}
}
}
Private void setTextView(int id){
textView = (TextView) this.findViewById(id);
}
}
The specific explanation is written in the code and can be viewed.
In fact, the working principle has been written, this program should be very good understanding. Writing is just a matter of speed.
Have not understood can leave a message ~ ~ ~
Android Development Series (16): "Android small game idiom repeatedly see" second article