Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wangrunyu committed Jun 27, 2016
1 parent 67cb712 commit 13170a9
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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) {

}
}
Original file line number Diff line number Diff line change
@@ -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;
}


}
}
}
Original file line number Diff line number Diff line change
@@ -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);



}

Original file line number Diff line number Diff line change
@@ -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();
}
});
}
}
34 changes: 34 additions & 0 deletions app/src/main/res/layout/activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:background="@drawable/bluebtn"
android:text="开始游戏"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/bt3"
android:layout_alignStart="@+id/bt3"
android:layout_marginTop="134dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt2"
android:background="@drawable/greenbtn"
android:text="游戏数据"
android:layout_below="@+id/bt1"
android:layout_alignLeft="@+id/bt3"
android:layout_alignStart="@+id/bt3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt3"
android:background="@drawable/yellowbtn"
android:text="退出游戏"
android:layout_below="@+id/bt2"
android:layout_centerHorizontal="true" />


</RelativeLayout>
Loading

0 comments on commit 13170a9

Please sign in to comment.