华容道 ( java)
java老师布置这华容道的题,大概有两个多礼拜了,上机的时候,一班的同学做出来了,心里感觉很... 所以清明回家,就研究了研究。因为以前上过c++的选修课,所以对类的什么有那么一点点了解,对java也不熟悉,反正就这么用eclipse做出来了,哪里做的不好了,希望能指正。
华容道:
大体思路说一下:有个people类,和棋盘类,棋盘上将每个人名放好,然后根据移动规则来移动,当曹操移到下面就赢了。
people类:
package people;
import java.util.Scanner;
public class people {
String name;
int state;//状态,在移动时候将状态一样的一块移动
people(String c,int s) //构造函数
{
this.name=c;
state=s;
}
static void JudgeName(String name) //判断名字是否输入正确
{
int a=1;
while(a==1)
{
switch(name)
{
case"曹操":a=0;break;
case"张飞":a=0;break;
case"赵云":a=0;break;
case"黄忠":a=0;break;
case"关羽":a=0;break;
case"马超":a=0;break;
case"兵":a=0;break;
case"士":a=0;break;
case"卫":a=0;break;
case"卒":a=0;break;
default:
{
System.out.println("名字输入错误,请重新输入:");
Scanner reader= new Scanner(System.in);
name=reader.next();
JudgeName(name);
reader.close();
}
}
}
}
}
chessboard类:
package people;
public class Chessboard {
people[][] Board=new people[4][5]; //申请了一个people类型的棋盘
Chessboard()
{
Board[1][4]=new people("曹",0);
Board[2][4]=new people("操",0);
Board[0][4]=new people("赵",1);
Board[0][3]=new people("云",1);
Board[0][2]=new people("黄",2);
Board[0][1]=new people("忠",2);
Board[0][0]=new people("兵",3);
Board[1][2]=new people("马",4);
Board[2][2]=new people("超",4);
Board[1][1]=new people("卫",5);
Board[2][1]=new people("士",6);
Board[3][4]=new people("关",7);
Board[3][3]=new people("羽",7);
Board[3][2]=new people("张",8);
Board[3][1]=new people("飞",8);
Board[3][0]=new people("卒",9);
Board[1][3]=new people(" ",10);
Board[2][3]=new people(" ",11);
Board[1][0]=new people(" ",12);
Board[2][0]=new people(" ",13);
}
void print()//输出
{
for(int j=4;j>=0;j--)
{ for(int i=0;i<4;i++)
{ System.out.printf(" "+Board[i][j].name);
}
System.out.println();
}
System.out.println();
}
boolean win() //赢得条件
{
if(Board[1][1].name=="曹"&&Board[2][1].name=="操")
{
return true;
}
else return false;
}
boolean judgemove(int i,int j,String dir)//判断是否符合移动的条件
{
switch(dir){
case"左":
{ if(i<0||i>4||j<0||j>5||Board[i-1][j].name==" ")
{
return false;
}
else
return true;}
case"右":{ if(i<0||i>4||j<0||j>5||Board[i+1][j].name==" ")
{
return false;
}
else
return true;}
case"上":
{ if(i<0||i>4||j<0||j>5||Board[i][j+1].name==" ")
{
return false;
}
else
return true;}
case"下":
{ if(i<0||i>4||j<0||j>5||Board[i][j-1].name==" ")
{
return false;
}
else
return true;}
}
return false;
}
boolean move(String dir,int i,int j)//判断是否能移动
{
switch(dir)
{
case "左":if(judgemove(i,j,dir))
{
return false;
}
else
{
Board[i-1][j].name=Board[i][j].name;
Board[i][j].name=" ";
return true;
}
case "右":if(judgemove(i,j,dir))
{
return false;
}
else
{
Board[i+1][j].name=Board[i][j].name;
Board[i][j].name=" ";
return true;
}
case "上":if(judgemove(i,j,dir))
{
return false;
}
else
{ Board[i][j+1].name=Board[i][j].name;
Board[i][j].name=" ";
return true;
}
case "下":if(judgemove(i,j,dir))
{
return false;
}
else
{
Board[i][j-1].name=Board[i][j].name;
Board[i][j].name=" ";
return true;
}
default:return false;
}
}
void findname(String name1,String name2,String dir)//在棋盘里找出要移动的棋子且名字不是一个字
{
loop: for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
if(Board[i][j].name.equals(name1))
{
for(int m=0;m<4;m++)
{
for(int k=0;k<5;k++)
{
if(Board[m][k].name.equals(name2))
{
if(Board[i][j].state==Board[i][j-1].state&&Board[i][j].state!=Board[i+1][j-1].state)
{ if( move(dir,m,k)&& move(dir,i,j))
;
else System.out.println("不能移动");
}
if(Board[i][j].name=="曹"&&Board[i+1][j].name=="操")
{
if( move(dir,i,j-1)&&move(dir,i,j)&&move(dir,i+1,j-1)&&move(dir,i+1,j))
;
else System.out.println("不能移动");
}
else
{
if( move(dir,i,j)&& move(dir,m,k))
;
}
break loop;
}
}
}
}
}
}
}
void findname1(String name1,String dir)//当名字只有一个字的时候移动的情况
{
loop1: for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
if(Board[i][j].name.equals(name1))
{
move(dir,i,j);
break loop1;
}
}
}
}
}
主类:
package people;
import java.util.Scanner;
public class Mainpro {
public static void main(String[] args)
{
Chessboard Board=new Chessboard();
Board.print();
while(!Board.win())
{
System.out.println("请输入你要移动人物");
Scanner reader = new Scanner(System.in);
String name=reader.next();
people.JudgeName(name);
if(name.length()==2)
{ String name1=name.substring(0,1);
String name2=name.substring(1,2);
System.out.println("请输入你要移动方向");
String a=reader.next();
Board.findname(name1,name2,a);
}
else
{
String name1=name.substring(0,1);
System.out.println("请输入你要移动方向");
String a=reader.next();
Board.findname1(name1,a);
}
Board.print();
}
}
}
运行结果:
20150405
by:暖暖要坚持