2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)

规则介绍

棋类游戏由一个棋盘、一组棋子组成,双方交替在棋盘上走棋。本实验考虑在正方形棋盘上进行的棋类游戏,包括国际象棋和围棋。 一个尺寸nxn的棋盘:其中n表示每一行/每一列的格子数。例如:围棋盘由18x18个格子构成,国际象棋盘由8x8个格子构成。棋盘上共有nxn个格子和(n+1)x(n+1)个交叉点。 一组棋子:每个棋子属于一个特定的种类,不同的棋类游戏中包含的棋子种类不同,例如:围棋/五子棋中仅包含黑子和白子;国际象棋中包含king、queen、rock、bishop等类型,且双方棋手拥有同样的棋子(用颜色区分)。在一盘棋局中,不同种类的棋子的数量不同,例如国际象棋中每方有2个rock、2个bishop;围棋中的黑子和白子数量无限多。在游戏进行过程中,需要区分棋子是属于游戏中的哪一方玩家。
下棋过程中,棋手的动作包括:
⚫ 将一个棋子从棋盘外放到棋盘上的某个合法位置(围棋、五子棋等)
⚫ 将一个棋子从棋盘上的一个合法位置移动到另一个合法位置(中国象棋、
国际象棋等)
⚫ 将对方的一个或多个棋子从棋盘上移走

3.3 Playing Chess

3.3.1 ADT设计/实现方案

1.Position(mutable)

坐标类:定义一个点(棋子)的坐标位置,并且提供判重方法。
整体结构:
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//rep:
private int x;横纵坐标
private int x;
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//function:
public void setPosition(int x,int y)//修改横纵坐标
public int getPositionX()//获取横纵坐标
public int getPositionY()
public boolean equals(Position that) //判断两个Position相等

2. Piece(mutable)

整体结构:
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
棋子类:定义一个棋子,包括棋子的颜色(黑B/白W),棋子类型(将相车卒等),棋子所属的玩家,棋子在棋盘上的位置。
//Rep:
private final String owner;
private final String color;
private final String type;
private Position piecePosition=new Position(0, 0);
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//functions:都是一些getter and setter具体不赘述
public Position getPiecePosition();
public void setPiecePosition(int x,int y);
public String getOwner();
public String getColor();
public String getType();

3. Player(mutable)

整体结构:
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//functions:
getter//三个域的getter
1、public boolean addPiece(Piece newPiece)//添加一枚棋子,已经拥有返回false,否则添加并返回true
2、public boolean removePiece(Piece removePiece)//移除一枚棋子,不存在返回false,否则移除并返回true
3、public int countnum() //获取棋盘上属于自己的棋子数,即统计自己的棋子里pieceState为1的棋子数
4、public void addStep(String step) //向historyList域中添加一个走棋步骤

4. Board(mutable)

2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//functions:
1、getter //type和size和的getter
2、public Piece getPiece(int x,int y)//给出一对横纵坐标,获取该位置上的棋子,如果没有,返回的为空,因此需要在客户端进行判断
3、public boolean setPiece(Piece p,int x,int y)//在位置(x,y)上放置piece
4、public boolean removepiece(int x,int y)// 在位置(x,y)上移除piece
5、public boolean Placed(int x,int y)//获取(x,y)对应位置是否放置了棋子
6、public int countpieces()//统计棋盘上的棋子个数

5. Action(interface)

2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//functions:只给出spec
public static Action empty(String gameType)//根据输入的字符串判断以哪一个实例类作为默认的实现
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
public Player createPlayer1(String playerName1);//创建两个玩家
public Player createPlayer2(String playerName2);
public Board createboard();//创建一个新的空棋盘
public void init();
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
public boolean placePiece(Player player,Piece piece,int x,int y);
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
public boolean movePiece(Player player,int sx,int sy,int tx,int ty);
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
public boolean removePiece(Player player,int x,int y);
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
public boolean eatPiece(Player player,int sx,int sy,int tx,int ty);
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)

6. Action的两个实例类:ChessAction与 GoAction

(1)ChessAction
因为国际象棋中没有落子和提子的行为,所以客户端并不允许调用placePiece和removePiece,这两个接口在实现时返回值永远为false。
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//functions:
@override接口中的函数,具体实现方法按照规约操作即可。
(2)GoAction
因为围棋中没有移子吃子的行为,所以客户端并不允许调用movePiece和eatPiece,这两个接口在实现时返回值永远为false。
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//Rep:
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//functions:
@override接口中的函数,具体实现方法按照规约操作即可。

7. Game(immutable)

这是一个游戏类,用来创建一个棋盘游戏,并提供所有可能使用的功能。
整体结构:
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
//functions:
1.public int getBoardSize()1.get the size of board;
public String getPlayer1()2.get the names of two players;
public String getPlayer2()
2.public void printboard()//打印实时的棋盘的状态

3.achieve some kinds of behavior of board games, such as falling, lifting, moving and eating pieces.
public boolean placePiece(String playerName,int x,int y)
public boolean movePiece(String playerName,int sx,int sy,int tx,int ty)
public boolean removePiece(String playerName,int x,int y)
public boolean eatPiece(String playerName,int sx,int sy,int tx,int ty)

public boolean checkBoardPlaced(int x,int y)
4.given a position,check the state of this position on board,whether is occupied or not,if occupied,occupied by whose which pieces.

public int getNuberOfPiece(String playerName)
5.calculate the numbers of the two players’ pieces.

public String getHistorySteps(String playerName)
6.given a player’s name ,output the history of this game in this competition.

3.3.2 主程序MyChessAndGoGame设计/实现方案

2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)

(1)主程序入口

2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)

(2)主函数

2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)

(3)以围棋为例,进行游戏的函数:

2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)

(4)根据不同选项进行不同操作的函数:

2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)

(5)以国际象棋为例,程序运行截图如下:

1).初始化
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2).移动操作
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
3).吃子操作
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
4).查询操作
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
5).计算总棋子操作
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
6).跳步操作
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
7).结束操作
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
(6)各个ADT之间的调用关系(框图表示)
1.客户端与Game之间
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
2.Game与Action
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
3.Action的实现
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
4.Action与其他support类
2020年春季学期 计算机学院《软件构造》课程——Lab2总结(P3)
测试代码略采用等价类划分的方式进行