회식 자리에서 간혹 이런 어처구니 없는 게임으로다가 본의 아니게 폭탄주를 마셔본적이 있어서...


폭탄 찾기 게임입니다. 폭탄을 발견한 사람이 다~~ 마시고 술값내고.... 대박.


버튼은 16개 빨간색 폭탄 버튼은 항상 4개 입니다.


Screenshot_2013-11-21-20-53-10.png


이하 풀 소스,...


package com.example.findbomb;


import java.util.Random;


import com.example.mygame001.R;


import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;


public class FindBomb extends Activity{

private Button startBtn;

private Button addBtn;

private TextView tvStatus;

private TextView tvTime;


@Override

protected void onCreate(Bundle savedInstanceState) {

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

super.onCreate(savedInstanceState);

LayoutParams lp;

lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

RelativeLayout gameView = new RelativeLayout(this);

gameView.setLayoutParams(lp);

setContentView(gameView);

LinearLayout cv = new LinearLayout(this);

cv.setLayoutParams(lp);

cv.setOrientation(LinearLayout.VERTICAL);

cv.setBackgroundColor(Color.rgb(255, 255, 255));

gameView.addView(cv);

lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

TextView tvLabel = new TextView(this);

tvLabel.setLayoutParams(lp);

tvLabel.setBackgroundColor(Color.BLACK);

tvLabel.setTextColor(Color.WHITE);

tvLabel.setTextSize(18f);

tvLabel.setGravity(Gravity.CENTER);

tvLabel.setText("지뢰를 발견한 사람이 벌칙 받는 게임입니다.");

cv.addView(tvLabel);

tvLabel = new TextView(this);

tvLabel.setLayoutParams(lp);

tvLabel.setBackgroundColor(Color.BLACK);

tvLabel.setTextColor(Color.RED);

tvLabel.setHeight(100);

tvLabel.setTextSize(18f);

tvLabel.setGravity(Gravity.CENTER);

tvLabel.setText("지뢰 버튼은 누르면 빨간색으로 바뀝니다.");

cv.addView(tvLabel);

startBtn = new Button(this);

startBtn.setLayoutParams(lp);

startBtn.setTextSize(32f);

startBtn.setText("START GAME");

startBtn.setOnClickListener(BtnClick);

cv.addView(startBtn);

gv = new RelativeLayout(this);

lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

gv.setLayoutParams(lp);

cv.addView(gv);

int btnId = 0;

int nb = 4;

for(int ny=0; ny<nb; ny++){

for(int nx=0; nx<nb; nx++){

gv.addView( CreateButton(ny, nx, String.valueOf(btnId++)) );

}

}

}

private RelativeLayout gv;

private Button CreateButton(int ny, int nx, String label){

int sw = getWindow().getWindowManager().getDefaultDisplay().getWidth();

int nb = 4;

int BtnWidth = sw / nb;

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(BtnWidth-4, BtnWidth-4);

lp.topMargin = 2+ny * BtnWidth;

lp.leftMargin = 2+nx * BtnWidth;

Button btn = new Button(this);

btn.setLayoutParams(lp);

btn.setTextColor(Color.WHITE);

btn.setBackgroundColor(Color.WHITE);

btn.setTextSize(52);

btn.setText(label);

btn.setOnClickListener(BtnClick);

btn.setEnabled(false);

return btn;

}

private int bombCount = 0;

private View.OnClickListener BtnClick = new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v == startBtn){

shuffleArray();

ResetButtons();

bombCount = 0;

startBtn.setVisibility(View.GONE);

}

else {

int btnId = Integer.valueOf( ((Button)v).getText().toString() );

int btnColor;

if(bombData[btnId]==1){

btnColor = Color.RED;

if( ++bombCount == 4) startBtn.setVisibility(View.VISIBLE);

}

else {

btnColor = Color.WHITE;

}

((Button)v).setBackgroundColor(btnColor);

((Button)v).setEnabled(false);

}

}

};

private void ResetButtons(){

for(int i=gv.getChildCount()-1; i>-1; --i){

Button btn = (Button)gv.getChildAt(i);

btn.setBackgroundColor(Color.BLUE);

btn.setEnabled(true);

}

}

private int[] bombData = {1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0};

private void shuffleArray(){

Random rnd = new Random();

for(int i=gv.getChildCount()-1; i>0; i--){

int index = rnd.nextInt(i+1);

int first = bombData[index];

int tmp = bombData[i];

bombData[index] = tmp;

bombData[i] = first;

}

}


}


간단하니 설명이 필요 없을 듯 합니다...