java控制檯版本 俄羅斯方塊

stevenice85發表於2015-06-17
package com;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;


//俄羅斯方塊 20*10
public class Tetris implements Runnable {
static String[][] Tetris = new String[20][10];
String p1 = "●";
String p2 = "o";
static List<String> list = new ArrayList<String>();
static Map<Integer,String> map = new HashMap<Integer,String>();
int score = 0;
static Thread t1 ;
static Thread t2 ;
public static void main(String[] args) {
list.add("0,5;1,5;2,5;3,5");
list.add("0,5;1,4;1,5;1,6");
list.add("0,5;0,6;1,5;1,6");
list.add("0,5;1,5;1,6;2,6");
list.add("0,6;1,6;1,5;2,5");
Tetris t = new Tetris();
Tetris tn = new Tetris();
Tetrisb tb = tn.new Tetrisb();
t1 = new Thread(t);
t2 = new Thread(tb);
t1.start();
t2.start();
}


public void run() {
while (true) {
try {
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in));
String str = stdin.readLine();


if("w".equals(str)){
change(Tetris);
}
if("a".equals(str)){
left(Tetris);
}
if("d".equals(str)){
right(Tetris);
}


} catch (Exception e) {
e.printStackTrace();
}
}
}


private void right(String[][] tetris) {
int canDo = 0;
for (int i = 0;i< tetris.length; i++) {
for (int j = tetris[i].length-1; j >-1; j--) {
if("●".equals(tetris[i][j])&&j<tetris[i].length-1){
if(!"o".equals(tetris[i][j+1])){
canDo++;
}
}
}
}
if(canDo==4){
for (int i = 0;i< tetris.length; i++) {
for (int j = tetris[i].length-1; j >-1; j--) {
if(j<tetris[i].length-1&&"●".equals(tetris[i][j])){
tetris[i][j]=null;
tetris[i][j+1]="●";
}
}
}
}
}


private void left(String[][] tetris) {
int canDo = 0;
for (int i = 0;i< tetris.length; i++) {
for (int j =  0; j <tetris[i].length; j++) {
if("●".equals(tetris[i][j])&&j>0){
if(!"o".equals(tetris[i][j-1])){
canDo++;
}
}
}
}
if(canDo==4){
for (int i = 0;i< tetris.length; i++) {
for (int j =  0; j <tetris[i].length; j++) {
if("●".equals(tetris[i][j])){
tetris[i][j]=null;
tetris[i][j-1]="●";
}
}
}
}
}


private void change(String[][] tetris) {
Set typeSet = map.keySet();
int type = 0;
for (Object object : typeSet) {
type = Integer.parseInt(String.valueOf(object));
}
System.out.println("type="+type);
if(type==0){//豎
for (int i = 0;i< tetris.length; i++) {
for (int j =  0; j <tetris[i].length; j++) {
if("●".equals(tetris[i][j])){
if(j<tetris[i].length-1&&"●".equals(tetris[i][j+1])){//橫
if(i<tetris.length-3&&!"o".equals(tetris[i+1][j])&&!"o".equals(tetris[i+2][j])&&!"o".equals(tetris[i+3][j])){
tetris[i][j+1]= null;
tetris[i][j+2]= null;
tetris[i][j+3]= null;
tetris[i+1][j]= "●";
tetris[i+2][j]= "●";
tetris[i+3][j]= "●";
return;
}
}else{//豎
if(j<tetris[i].length-3&&!"o".equals(tetris[i][j+1])&&!"o".equals(tetris[i][j+2])&&!"o".equals(tetris[i][j+3])){
tetris[i+1][j]= null;
tetris[i+2][j]= null;
tetris[i+3][j]= null;
tetris[i][j+1]= "●";
tetris[i][j+2]= "●";
tetris[i][j+3]= "●";
return;
}
}
}
}
} 
return;
}
if(type==1){//十字
for (int i = 0;i< tetris.length; i++) {
for (int j =  0; j <tetris[i].length; j++) {
if("●".equals(tetris[i][j])){
if(i<tetris.length-1&&j<tetris[i].length-1&&"●".equals(tetris[i+1][j])&&"●".equals(tetris[i+1][j-1])&&"●".equals(tetris[i+1][j+1])){//頭上
if(i<tetris.length-2&&!"o".equals(tetris[i+2][j])){
tetris[i+2][j]= "●";
tetris[i+1][j-1]= null;
return;
}
}
else if(i<tetris.length-2&&j<tetris[i].length-1&&"●".equals(tetris[i+1][j])&&"●".equals(tetris[i+2][j])&&"●".equals(tetris[i+1][j+1])){//頭右
if(i<tetris.length-2&&!"o".equals(tetris[i+1][j-1])){
tetris[i+1][j-1]= "●";
tetris[i][j]= null;
return;
}
}
else if(i<tetris.length-1&&j<tetris[i].length-2&&"●".equals(tetris[i+1][j+1])&&"●".equals(tetris[i][j+2])&&"●".equals(tetris[i][j+1])){//頭下
if(i<tetris.length-2&&!"o".equals(tetris[i-1][j+1])){
tetris[i][j+2]= null;
tetris[i-1][j+1]= "●";
return;
}
}else{//頭左
if(i<tetris.length-2&&!"o".equals(tetris[i+2][j])){
tetris[i+2][j]= null;
tetris[i+1][j+1]= "●";
return;
}
}
}
}
}
}
if(type==2){//方塊
return;
}
if(type==3){//反z
return;
}
if(type==4){//z
return;
}
}


class Tetrisb implements Runnable {
public void run() {
while (true) {
try {
Thread.sleep(500);
if(!exsist(Tetris,"●")){
addTetris(Tetris,list);
}else{
movedown(Tetris);
}
cut(Tetris);
draw(Tetris);
} catch (Exception e) {
e.printStackTrace();
}
}
}


private void cut(String[][] tetris) {
int cut = 0;
for (int i = 0;i< tetris.length; i++) {
cut=0;
for (int j =  0; j <tetris[i].length; j++) {
if("o".equals(tetris[i][j])){
cut++;
}
if(cut==10){
for (int k =  0; k <tetris[i].length; k++) {
tetris[i][k] = null;
}
cut2(tetris,i);
score++;
}
}
}
}


private void cut2(String[][] tetris, int ii) {
ii--;
for (int i = ii;i>-1; i--) {
for (int j =  0; j <tetris[i].length; j++) {
tetris[i+1][j]=tetris[i][j];
tetris[i][j]=null;
}
}
}


private void draw(String[][] tetris) {
for (int i = 0;i< tetris.length; i++) {
for (int j =  0; j <tetris[i].length; j++) {
if(tetris[i][j]==null){
System.out.print("_");
}else{
System.out.print(tetris[i][j]);
}
System.out.print("|");
}
System.out.println();
}
System.out.println("------------you score : "+score+"-----------------------------");
}


private void movedown(String[][] tetris) {
int turn =0;
for (int i =  tetris.length-1; i > -1; i--) {
for (int j =  tetris[i].length-1; j >-1; j--) {
if(i==19&&"●".equals(tetris[i][j])){
beWhite(tetris);
return;
}
if("●".equals(tetris[i][j])&&!"o".equals(tetris[i+1][j])){
turn++;
}if("●".equals(tetris[i][j])&&"o".equals(tetris[i+1][j])){
beWhite(tetris);
}
}
}
if(turn==4){
for (int i =  tetris.length-1; i > -1; i--) {
for (int j =  tetris[i].length-1; j >-1; j--) {
if("●".equals(tetris[i][j])){
tetris[i][j]=null;
tetris[i+1][j]="●";
}
}
}
}
}


private void beWhite(String[][] tetris) {
for (int i =  tetris.length-1; i > -1; i--) {
for (int j =  tetris[i].length-1; j >-1; j--) {
if("●".equals(tetris[i][j])){
tetris[i][j] = "o";
}

}
}
}


private void addTetris(String[][] tetris, List<String> list) {
Random random = new Random();
int index = random.nextInt(3);
String str = list.get(index);
map.clear();
map.put(index,str);
String[] ary1 = str.split(";");
for (String string : ary1) {
String[] ary2 = string.split(",");
int x = Integer.parseInt(ary2[0]);
int y =  Integer.parseInt(ary2[1]);
if(tetris[x][y]!=null){
System.out.println("you Lost");
return;
}else{
tetris[x][y]= "●";
}
}

}


private boolean exsist(String[][] tetris, String string) {
for (String[] strings : tetris) {
for (String string2 : strings) {
if(string.equals(string2)){
return true;
}
}
}
return false;
}
}
}

相關文章