分享

Java学习—125.2048代码

 pythonjava学习 2021-04-06

本篇帖的是部分动作代码。

每个方法前都有注释说明本方法的功能。

//随机选取一个位置,并填入数字2.如果此位置中已有数字,那么换一个位置。

public void rule(){

x=ran.nextInt(4);

y=ran.nextInt(4);

//System.out.println(x+""+y);

String s=shuzi[x][y].getText();

if (s.equals(" ")) {

shuzi[x][y].setText("2");

count++;

} else {

rule();

}

}

//重新游戏。为游戏按钮中的每个按钮取消上面的数字,并重新调用rule()开始新一轮的游戏,并将几个变量设置为其初始值。

public void again(){

for(int i=0;i<4;i++)

for(int j=0;j<4;j++)

shuzi[i][j].setText(" ");

count=0;

iswin=false;

result.setVisible(false);

rule();

}

//判断游戏是否结束,是否胜利

public boolean win(String s){

//System.out.println(s);

int n=Integer.parseInt(s);

if(n==2048){

JOptionPane.showMessageDialog(this, "恭喜您,胜利了!");

iswin=true;

}

else if(n>2048)

{

JOptionPane.showMessageDialog(this, "数字大于2048,有误!!!游戏重新开始!");

iswin=true;

}

return iswin;

}

//向上合并的规则

public void uprule(){

if(iswin){

result.setVisible(true);

}

else{

int i=1,j=0; 

for(i=1;i<4;i++){

for(j=0;j<4;j++){

str = shuzi[i][j].getText();

st = shuzi[i-1][j].getText();

if (st.equals(" ")&&!str.equals(" ")) {

shuzi[i-1][j].setText(str);

shuzi[i][j].setText(" ");

//System.out.println(str);

if(win(str))

break;

else if(!st.equals(" ")&&!str.equals(" ")){

num = Integer.parseInt(st);

number=Integer.parseInt(str);

num*=number;

//System.out.println(num);

String s=String.valueOf(num);

shuzi[i-1][j].setText(s);

shuzi[i][j].setText(" ");

if(win(s))

break;

}

}

}

if(!iswin)

rule();

}

}

//向左合并的规则

public void leftrule(){

if(iswin){

result.setVisible(true);

}

else{

int i=0,j=1;

for(i=0;i<4;i++){

for(j=1;j<4;j++){

str = shuzi[i][j].getText();

st = shuzi[i][j-1].getText();

if (st.equals(" ")&&!str.equals(" ")) {

shuzi[i][j-1].setText(str);

shuzi[i][j].setText(" ");

if(win(str))

break;

//System.out.println(str);

} else if(!st.equals(" ")&&!str.equals(" ")){

num = Integer.parseInt(st);

number=Integer.parseInt(str);

num*=number;

//System.out.println(num);

String s=String.valueOf(num);

shuzi[i][j-1].setText(s);

shuzi[i][j].setText(" ");

if(win(s))

break;

}

}

}

if(!iswin)

rule();

}

}

//向下合并的规则

public void downrule(){

if(iswin){

result.setVisible(true);

}

else{

int i=0,j=0;

for(i=0;i<3;i++){

for(j=0;j<4;j++){

str = shuzi[i][j].getText();

st = shuzi[i+1][j].getText();

if (st.equals(" ")&&!str.equals(" ")) {

shuzi[i+1][j].setText(str);

shuzi[i][j].setText(" ");

if(win(str))

break;

//System.out.println(str);

} else if(!st.equals(" ")&&!str.equals(" ")){

num = Integer.parseInt(st);

number=Integer.parseInt(str);

number*=num;

//System.out.println(number);

String s=String.valueOf(number);

shuzi[i+1][j].setText(s);

shuzi[i][j].setText(" ");

if(win(s))

break;

}

}

}

if(!iswin)

rule();

}

}

//向右合并的规则

public void rightrule(){

if(iswin){

result.setVisible(true);

}

else{

int i=0,j=0;

for(i=0;i<4;i++){

for(j=0;j<3;j++){

str = shuzi[i][j].getText();

st = shuzi[i][j+1].getText();

if (st.equals(" ")&&!str.equals(" ")) {

shuzi[i][j+1].setText(str);

shuzi[i][j].setText(" ");

if(win(str))

break;

//System.out.println(str);

} else if(!st.equals(" ")&&!str.equals(" ")){

num = Integer.parseInt(st);

number=Integer.parseInt(str);

number*=num;

//System.out.println(number);

String s=String.valueOf(number);

shuzi[i][j+1].setText(s);

shuzi[i][j].setText(" ");

if(win(s))

break;

}

}

}

if(!iswin)

rule();

}

}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多