这是我们班温油的java老师给我们留的一道课后题,先来讲一下玩法吧
设计控制台版本的华容道游戏,用户输入人物和移动方向,能够判断可否移动,并且根据给定方向和人物,进行移动。请自行设计人物类,并实现相应的功能。
输入: 甲 右
输出:
输入: 赵云 下
输出:
输入: 曹操 下
输出:曹操不能向下走,请重新输入。
好啦,看一下小北同学是怎么做的叭:
首先是person类,用来记录每个名和字
package HuaRongDao;
public class Person {
private String name;
private int flag=0;//用来标记状态
Person(String name,int flag){
this.name=name;
this.flag=flag;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag){
this.flag=flag;
}
}
接下来是ChessBoard类,是对棋盘的show 和对象判断修改等操作
package HuaRongDao;
public class ChessBoard {
Person[][] board=new Person[5][4];
//空参构造,初始化二维数组
public ChessBoard(){
board[0][0]=new Person("马",1);
board[1][0]=new Person("超",1);
board[0][1]=new Person("曹",2);
board[0][2]=new Person("操",2);
board[1][1]=new Person("曹",2);
board[1][2]=new Person("操",2);
board[0][3]=new Person("张",0);
board[1][3]=new Person("飞",0);
board[2][0]=new Person("赵",5);
board[3][0]=new Person("云",5);
board[2][1]=new Person("关",6);
board[2][2]=new Person("羽",6);
board[3][1]=new Person("丙",7);
board[3][2]=new Person("丁",8);
board[2][3]=new Person("黄",9);
board[3][3]=new Person("忠",9);
board[4][0]=new Person("甲",10);
board[4][3]=new Person("乙",11);
board[4][1]=new Person("11", 0);
board[4][2]=new Person("11", 0);
}
//判断玩家输入的对象名字是否正确
public int judegname(String personname){
int temp=0;
switch(personname){
case "马超":temp=1;break;
case "曹操":temp=1;break;
case "张飞":temp=1;break;
case "赵云":temp=1;break;
case "关羽":temp=1;break;
case "黄忠":temp=1;break;
case "甲":temp=1;break;
case "乙":temp=1;break;
case "丙":temp=1;break;
case "丁":temp=1;break;
default:temp=0;break;
}
return temp;
}
//棋盘show
public void show(){
for (int i = 0; i 4||j<0||j>3||board[i][j-1].getName()!="11"){
return false;
}else
return true;
}
case "右":{
if(i<0||i>4||j<0||j>3||board[i][j+1].getName()!="11"){
return false;
}else {
return true;
}
}
case "上":{
if(i<0||i>4||j<0||j>3||board[i-1][j].getName()!="11"){
return false;
}else{
return true;
}
}
case "下":{
if(i<0||i>4||j<0||j>3||board[i+1][j].getName()!="11"){
return false;
}else{
return true;
}
}
}
return false;
}
boolean move(String temp,int i,int j)//判断是否能够移动
{
switch(temp){
case "左":if(judgemove(i,j,temp)){
board[i][j-1].setName(board[i][j].getName());
board[i][j].setName("11");
board[i][j-1].setFlag(board[i][j].getFlag());
board[i][j].setFlag(0);
return true;
}else{
return false;
}
case "右":if(judgemove(i,j,temp)){
board[i][j+1].setName(board[i][j].getName());
board[i][j].setName("11");
board[i][j+1].setFlag(board[i][j].getFlag());
board[i][j].setFlag(0);
return true;
}else{
return false;
}
case "上":if(judgemove(i,j,temp)){
board[i-1][j].setName(board[i][j].getName());
board[i][j].setName("11");
board[i-1][j].setFlag(board[i][j].getFlag());
board[i][j].setFlag(0);
return true;
}else{
return false;
}
case "下":if (judgemove(i,j,temp)){
board[i+1][j].setName(board[i][j].getName());
board[i][j].setName("11");
board[i+1][j].setFlag(board[i][j].getFlag());
board[i][j].setFlag(0);
return true;
}else {
return false;
}
default:return false;
}
}
//名字有两个字的对象移动情况
void findperson(String person1,String person2,String temp) {
A:for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j].getName().equals(person1))
{
for (int m = 0; m < 5; m++) {
for (int k = 0; k < 4; k++) {
if (board[m][k].getName().equals(person2)) {
if(board[i][j].getName()=="曹"&&board[i][j+1].getName()=="操")
{
CaoCao(temp,i,j);
break A;
}
else if ((board[i][j].getFlag()==board[m][k].getFlag())&&(board[i][j].getFlag()!=0&&board[m][k].getFlag()!=0))//属于同一个对象
{
if(temp.equals("上")||temp.equals("左"))
{
if (move(temp, i,j) && move(temp, m, k))
{
System.out.println("成功移动");
break A;
} else {
System.out.println("不能移动");
break A;
}
}else{
if (move(temp, m,k) && move(temp, i, j))
{
System.out.println("成功移动");
break A;
} else {
System.out.println("不能移动");
break A;
}
}
}
}
}
}
}
}
}
}
private void CaoCao(String temp, int i, int j) {
System.out.println(temp);
if(temp.equals("上")){
if(move(temp,i,j)&&move(temp,i,j+1)){
move(temp,i+1,j);
move(temp,i+1,j+1);
}
}else if(temp.equals("下")){
if(move(temp,i+1,j)&&move(temp,i+1,j+1)){
move(temp,i,j);
move(temp,i,j+1);
}
}else if(temp.equals("左")){
if(move(temp,i,j)&&move(temp,i+1,j)){
move(temp,i,j+1);
move(temp,i+1,j+1);
}
}else if(temp.equals("右")){
System.out.println("右");
if(move(temp,i,j+1)&&move(temp,i+1,j+1)){
move(temp,i,j);
move(temp,i+1,j);
}
}
}
//名字只有一个字的对象移动情况
void findperson1(String person1,String temp)//当名字只有一个字的时候移动的情况
{
B:for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
if (board[i][j].getName().equals(person1)) {
if(move(temp, i, j))
break B;
else
System.out.println("不能移动");
}
}
}
}
}
接下来就是主模块啦:
package HuaRongDao;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ChessBoard chessBoard=new ChessBoard();
chessBoard.show();
while (true) {
System.out.println("请输入你想要移动的人物名称:");
Scanner scanner = new Scanner(System.in);
String string = scanner.next();
if (chessBoard.judegname(string) == 0){
System.out.println("输入名称格式错误,请重新输入:");
} else {
if (string.length() == 2) {
String person1 = string.substring(0, 1);//截取字符串
String person2 = string.substring(1, 2);
System.out.println("请输入你要移动的方向:");
String fangxiang = scanner.next();
//此处判断一下是否越界
try {
chessBoard.findperson(person1, person2, fangxiang);
} catch (ArrayIndexOutOfBoundsException A) {
System.out.println("嘤嘤嘤,你玩坏我啦!");
}
} else {
String person1 = string.substring(0, 1);
System.out.println("请输入你要移动的方向:");
String fangxiang = scanner.next();
try {
chessBoard.findperson1(person1, fangxiang);
} catch (ArrayIndexOutOfBoundsException A) {
System.out.println("嘤嘤嘤,你一点都不可爱!");
}
}
chessBoard.show();
}
}
}
}
大概就是这样
在敲代码的过程中有几个需要注意的方面:
A:如果判断对象可以移动,在移动的过程中不仅要对name进行覆盖,而且flag 也要进行修改
B:曹操占的份数最大,要独立判断
C:容易出现多种异常,要注意异常处理