티스토리 뷰
package com.example.tjpangpang;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.*;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;
public class Game extends Activity {
SaveDb myHelper;
int value =0 ;
String[] temp=new String[100];
Handler mHandler;
BufferedReader in;
Resources myResoures;
InputStream is;
StringBuffer sb;
String str=null;
int flag1 = 0;
int cnt = 1, cntCorrect=0, cntFalse = 0; // 문제수,맞춘수,틀린수
int flag2=0, timebr=0;
int scoreInt,typePerMinInt=0,typePerSave=0;
Chronometer chrono;
float timeSum =0;
int accuracyFloat=0;
TextView LvTv,ExTv,Tv,score,typePerMin,accuracy,pan;
Button btn1;
EditText et;
String text,question;
ArrayList<String> words = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
myHelper = new SaveDb(this);
btn1=(Button)findViewById(R.id.button1);
LvTv=(TextView)findViewById(R.id.textView1);
ExTv=(TextView)findViewById(R.id.textView2);
et=(EditText)findViewById(R.id.editText1);
// pan=(Button)findViewById(R.id.pan);
score=(TextView)findViewById(R.id.Score);
typePerMin=(TextView)findViewById(R.id.typePerMin);
accuracy=(TextView)findViewById(R.id.accuracy);
chrono = (Chronometer)findViewById(R.id.chronometer1);
myResoures = getResources();
is = myResoures.openRawResource(R.raw.bbbb);//파일 불러와서
sb=new StringBuffer();//버퍼에 넣어놓기
try{
in=new BufferedReader(new InputStreamReader(is,"KSC5601"));
}
catch(Exception es){}
////////////////////////////////////////////////////////////////////
// 시작버튼 눌렸을때 이벤트 //
////////////////////////////////////////////////////////////////////
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
while((str=in.readLine())!=null){
words.add(str);
}
in.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
flag1=1;
flag2=1;
if(flag2 == 1)
chrono.start(); //시계시작
if(flag1 == 1){
if(words.size() != 0){
//for(int i=words.size();i>0;i--){
Random random = new Random();
cnt += 1;
int index = random.nextInt(words.size());
question = words.get(index);
text = question;
LvTv.setText(question);
words.remove(index);
}
}
}
});
////////////////////////////////////////////////////////////////////
//시작버튼 눌렸을때 이벤트 끝 //
////////////////////////////////////////////////////////////////////
et.setOnKeyListener(new OnKeyListener() {//키이벤트 엔터키 쳤을때 판별하는 이벤트
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN &&
keyCode == KeyEvent.KEYCODE_ENTER) {
//////////////////////////////////////////
// 09.24일 종료문구 확인 과 점수 표현 추가분, 10문제 맞출시 게임종료
// if(cntCorrect >= 10){ //이렇게 하니까 최고점수는 무조건 1000점이라 재미 없슴
if(text.contentEquals(et.getText()))
{
///////////이미지띄우기
Toast tv = new Toast(getApplicationContext());
ImageView img = new ImageView(getApplicationContext());
TextView textv = new TextView(getApplicationContext());
img.setImageResource(R.drawable.pang);
tv.setView(img);
tv.setDuration(Toast.LENGTH_SHORT);
tv.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
tv.show();
///////////
if(cntCorrect >= 9){
/*// (cntCorrect);//숫자나옴
//AlertDialog.setText.Builder dlg = new AlertDialog.Builder(Game.this);
dlg.setTitle("게임 클리어!!"); //알림창 제목
dlg.setMessage("최고 타수 : "+typePerSave+" 타!!"
+"\n 획득 점수 : "+scoreInt +" 점!!"
+"\n 랭킹등록하시려면 확인을누르세요"); //알림창 내용
dlg.setIcon(R.drawable.ic_launcher); //아이콘
dlg.setPositiveButton("확인", new DialogInterface.OnClickListener() { //확인 눌렀을때 액션
*/
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
AlertDialog.Builder alert = new AlertDialog.Builder(Game.this);
alert.setTitle("랭킹등록");
alert.setMessage("닉네임을 입력하세요(6글자이내)");
// Set an EditText view to get user input
final EditText input = new EditText(Game.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
if(value==null){
finish();
}else{
java.util.Date curDate = new java.util.Date();
String m = Integer.toString(1+curDate.getMonth());
String d = Integer.toString(curDate.getDate());
SQLiteDatabase db = myHelper.getWritableDatabase();
db.execSQL("INSERT INTO scoreDBa VALUES ('"+value+"',"+scoreInt+","+typePerSave+",'"+m+"-"+d+"');");
db.close();
// Do something with value!
finish();
}
}
});
alert.show();
myHelper.close();
}
});
dlg.setNegativeButton("취소", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
dlg.show();
}
/////////
//종료 문구 if 의 끝
//09.24 추가부분 종료구문
///////////////////////////////////////////////////
cnt += 1;
cntCorrect +=1; //맞춘문제수
ExTv.setText("정답");
//////////////////////////점수계산////////////////////////
accuracyFloat = ((cntCorrect/cnt)*100);
accuracy.setText(accuracyFloat+"%");
/////////////////////////////////////////////////////////////////////타수계산
float elapsedMills = SystemClock.elapsedRealtime() - chrono.getBase();
// 24일 추가 최고타수 추가 부분
timeSum += elapsedMills; //총시간 구할때 씀
typePerMinInt = (int)((question.length()/(elapsedMills/1000))*100);
if(typePerSave <= typePerMinInt) //최고타수 보다 방금 친타수가 높으면
typePerSave = typePerMinInt; //세이브 함수에 그 타수 저장 즉, 최대타수
// 최고타수 추가 부분 끝
typePerMin.setText((elapsedMills/1000)+"초!! "+typePerMinInt+"타!!");
chrono.setBase(SystemClock.elapsedRealtime());
scoreInt += (10*typePerMinInt);
score.setText(scoreInt+" 점!");
Toast.makeText(Game.this, scoreInt+"!!", 0).show();
//////////////////////점수계산 끝//////////////////////////////
if(words.size() != 0){
//for(int i=words.size();i>0;i--){
Random random2 = new Random();
int index2 = random2.nextInt(words.size());
String question = words.get(index2);
text = question;
LvTv.setText(question);
words.remove(index2);
et.setText("");
}
}
else
{
cnt += 1;
ExTv.setText("틀렸어요");
accuracyFloat = ((cntCorrect/cnt)*100);
accuracy.setText(accuracyFloat+"%");
Toast.makeText(Game.this, "다시~!다시!!", 0).show();
et.setText("");
}
return true;
}
return false;
}
});
}
}
'Computer > Android' 카테고리의 다른 글
16진수 색상표 #AARRGGBB (0) | 2013.12.25 |
---|---|
Android File 입출력 (0) | 2013.12.11 |
Android FrameLayout 내의 ChildView들의 위치제어하기 (0) | 2013.11.17 |
안드로이드/Android LayoutInflater 사용 방법 (0) | 2013.11.09 |
Custom Android TabWidget with Badges (0) | 2013.11.09 |