diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/DBhelper.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/DBhelper.java new file mode 100644 index 00000000..a9eac5d5 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/DBhelper.java @@ -0,0 +1,38 @@ +package com.example.administrator.fingergame; + +import android.content.Context; +import android.database.DatabaseErrorHandler; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +/** + * Created by Administrator on 2016/6/18. + */ +public class DBhelper extends SQLiteOpenHelper { + public DBhelper(Context context) { + super(context, null, null, 1); + } + + public DBhelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { + super(context, name, factory, version, errorHandler); + } + + @Override + public void onCreate(SQLiteDatabase db) { + String sql = "create table IF NOT EXISTS guess(all_data text not null," + + "win_data text not null,fail_data text not null,ping_data text not null)"; + String sql1 = "insert into user values ('33','11','22','44')"; + String sql2 = "insert into user values ('11','22','44','55')"; + + db.execSQL(sql); +// db.execSQL(sql1); +// db.execSQL(sql2); + + + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/Game.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/Game.java new file mode 100644 index 00000000..afb50ccc --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/Game.java @@ -0,0 +1,140 @@ +package com.example.administrator.fingergame; + +import android.app.Activity; +import android.content.ContentValues; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; + +import java.util.Random; + +/** + * Created by Administrator on 2016/6/18. + */ +public class Game extends Activity { + public int dn=0; + public Random r; + Button bt1,bt2,bt3; + ImageView imageView; + TextView tv; + public static int win; + public static int fail; + public static int ping; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.game); + + bt1=(Button)findViewById(R.id.bt1); + bt2=(Button)findViewById(R.id.bt2); + bt3=(Button)findViewById(R.id.bt3); + imageView=(ImageView)findViewById(R.id.im1); + tv=(TextView)findViewById(R.id.tv1); + bt1.setOnClickListener(new ButtonListener()); + bt2.setOnClickListener(new ButtonListener()); + bt3.setOnClickListener(new ButtonListener()); + + + + + } + + @Override + public void onBackPressed() { + super.onBackPressed(); + int all=win+fail+ping; + DBhelper dBhelper=new DBhelper(Game.this); + SQLiteDatabase db=dBhelper.getWritableDatabase(); + ContentValues cv=new ContentValues(); + cv.put("all_data","1"); + cv.put("win_data","2"); + cv.put("fail_data", "3"); + cv.put("ping_data", "4"); + System.out.println(cv.toString()); + long id = db.insert("guess", null, cv); + System.out.println("longid:"+id); + + + } + + private class ButtonListener implements View.OnClickListener { + + @Override + public void onClick(View v) { + + switch (v.getId()){ + case R.id.bt1://0 + r= new Random(); + dn=r.nextInt(3); //0为石头。1为剪刀,2为布 + switch (dn){ + case 0: + imageView.setBackgroundResource(R.drawable.shitou); + tv.setText("平局"); + ping++; + break; + case 1: + imageView.setBackgroundResource(R.drawable.jiandao); + tv.setText("你赢了"); + win++; + break; + case 2: + imageView.setBackgroundResource(R.drawable.bu); + tv.setText("你输了"); + fail++; + break; + } + + + + break; + case R.id.bt2://1 + r= new Random(); + dn=r.nextInt(3); //0为石头。1为剪刀,2为布 + switch (dn) { + case 0: + imageView.setBackgroundResource(R.drawable.shitou); + tv.setText("你输了"); + fail++; + break; + case 1: + imageView.setBackgroundResource(R.drawable.jiandao); + tv.setText("平局"); + ping++; + break; + case 2: + imageView.setBackgroundResource(R.drawable.bu); + tv.setText("你赢了"); + win++; + break; + } + break; + case R.id.bt3://2 + r= new Random(); + dn=r.nextInt(3); //0为石头。1为剪刀,2为布 + switch (dn) { + case 0: + imageView.setBackgroundResource(R.drawable.shitou); + tv.setText("你赢了"); + win++; + break; + case 1: + imageView.setBackgroundResource(R.drawable.jiandao); + tv.setText("你输了"); + fail++; + break; + case 2: + imageView.setBackgroundResource(R.drawable.bu); + tv.setText("平局"); + ping++; + break; + } + break; + } + + + } + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/GameData.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/GameData.java new file mode 100644 index 00000000..0d373abb --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/GameData.java @@ -0,0 +1,46 @@ +package com.example.administrator.fingergame; + +import android.app.Activity; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.widget.TextView; + +/** + * Created by Administrator on 2016/6/18. + */ +public class GameData extends Activity { + String win; + String fail; + String ping; + String all; + String winlu; + TextView tv; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.data_layout); + + + DBhelper dbHelper = new DBhelper(GameData.this); + SQLiteDatabase readdb = dbHelper.getReadableDatabase(); + Cursor c = readdb.query("guess", null, null, null, null, null, null); + while (c.moveToNext()) { + all = c.getString(c.getColumnIndex("all_data")); + win = c.getString(c.getColumnIndex("win_data")); + fail = c.getString(c.getColumnIndex("fail_data")); + ping = c.getString(c.getColumnIndex("ping_data")); + +// System.out.println("摇一摇:"+String.format("win=%s,fail=%s,ping=%s",win,fail,ping)); + System.out.println("Game_data all:" + all + "win:" + win); + } + readdb.close(); + + } + +// tv.setText("胜率:"+ping+"负:"+fail); + + + + } + diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/MainActivity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/MainActivity.java new file mode 100644 index 00000000..7e83ba1f --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901229/MainActivity.java @@ -0,0 +1,43 @@ +package com.example.administrator.fingergame; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity); + Button bt1,bt2,bt3; + bt1=(Button)findViewById(R.id.bt1); + bt2=(Button)findViewById(R.id.bt2); + bt3=(Button)findViewById(R.id.bt3); + bt1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent i=new Intent(); + i.setClass(MainActivity.this,Game.class); + startActivity(i); + + } + }); + bt2.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent i2=new Intent(); + i2.setClass(MainActivity.this,GameData.class); + startActivity(i2); + } + }); + bt3.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + MainActivity.this.finish(); + } + }); + } +} diff --git a/app/src/main/res/layout/activity.xml b/app/src/main/res/layout/activity.xml new file mode 100644 index 00000000..69b7d6a2 --- /dev/null +++ b/app/src/main/res/layout/activity.xml @@ -0,0 +1,34 @@ + + +