A-level 计算机/AQA/试卷 1/框架程序/2023
这是针对 AQA A Level 计算机科学规范的。
这里可以提出关于一些问题可能是什么以及我们如何解决它们的建议。
请保持尊重,不要破坏页面,因为这会影响学生备考!
请不要在此页面上讨论问题。请改用讨论页面。
涉及此框架程序的 A Level 考试已完成。祝所有等待成绩的人好运!
2023 年试卷 1 将包含 6 道题,每题 13 分。只要你对程序很熟悉,这将是小菜一碟。
以下是一组模拟题(适用于 Python 3)
7.1 指出 Dastan 类中公有方法的标识符名称 [1 分]
7.2 解释为什么此方法是公有的而不是私有的 [2 分]
8. 从队列中的第 1、2 和 3 个位置选择移动时,会损失多少分?[2 分]
9.1 假设您想显示一个 8x8 格的网格。您将在程序中修改什么?[1 分]
9.2 指出继承自另一个类的类的标识符名称 [1 分]
9.3 指出具有三个参数(包括对象参数)的私有方法的标识符名称。[1 分]
10.1 指出布尔变量的标识符名称 [1 分]
10.2 预发布中随机模块用于什么?[1 分]
11.1 指出内置函数的名称 [1 分]
11.2 Kotla 类中的 super 例程有什么作用?[1 分]
12. 从代码中给出一个对象实例化的示例?[1 分]
13. 给出代码中使用多态性的一个示例和原因?[3 分]
答案
7.1 指出 Dastan 类中公有方法的标识符名称 [1 分] PlayGame
7.2 解释为什么此方法是公有的而不是私有的 [2 分] 它允许主函数从类外部访问它,否则如果它是一个私有方法,这是不可能的。
8. 从队列中的第 1、2 和 3 个位置选择移动时,会损失多少分?[2 分] 1,4,7
9.1 假设您想显示一个 8x8 格的网格。您将在程序中修改什么?[1 分] 将此行:ThisGame = Dastan(6, 6, 4) 更改为 ThisGame = Dastan(8, 8, 4)
9.2 指出继承自另一个类的类的标识符名称 [1 分] Kotla
9.3 指出具有三个参数(包括对象参数)的私有方法的标识符名称。[1 分] 例如:__CreateMoveOption
10.1 指出布尔变量的标识符名称 [1 分] 例如:GameOver
10.2 预发布中随机模块用于什么?[1 分] 随机选择移动选项
11.1 指出内置函数的名称 [1 分] 例如:print、int、float、input、str
11.2 Kotla 类中的 super 例程有什么作用?[1 分] 即使被重写,也可以使用父类中的方法类
12. 从代码中给出一个对象实例化的示例?[1 分] Thisgame= Dastan(6,6,4)
框架程序的编程问题
- 2023 年试卷 1 包含 4 道题:一道 5 分题、一道 9 分题、一道 10 分题和一道 13 分题——这些分数包括屏幕截图,因此编码的可能分数会低 1-2 分。
- 2022 年试卷 1 包含 4 道题:一道 5 分题、一道 9 分题、一道 11 分题和一道 13 分题——这些分数包括屏幕截图,因此编码的可能分数会低 1-2 分。
- 2021 年试卷 1 包含 4 道题:一道 6 分题、一道 8 分题、一道 9 分题和一道 14 分题——这些分数包括屏幕截图,因此编码的可能分数会低 1-2 分。
- 2020 年试卷 1 包含 4 道题:一道 6 分题、一道 8 分题、一道 11 分题和一道 12 分题——这些分数包括屏幕截图,因此编码的可能分数会低 1-2 分。
- 2019 年试卷 1 包含 4 道题:一道 5 分题、一道 8 分题、一道 9 分题和一道 13 分题——这些分数包括屏幕截图,因此编码的分数会低 1-2 分。
- 2018 年试卷 1 包含 5 道题:一道 2 分题、一道 5 分题、两道 9 分题和一道 12 分题——这些分数包括屏幕截图。
- 2017 年试卷 1 包含 5 道题:一道 5 分题、三道 6 分题和一道 12 分题。
当前问题是本页面贡献者推测的。
问题描述:程序需要扩展一种名为“TibbleCross”的新型移动方式。此移动可以允许棋子从其起始位置沿任何方向(西北、东北、东南、西南)对角向上移动两格。
添加此移动,使其首先出现在两个玩家的移动队列中。
选择此移动并通过选择棋子“22”然后移动到行/列“44”进行测试。
C#
private void CreateMoveOptionOffer()
{
MoveOptionOffer.Add("tibblecross");
MoveOptionOffer.Add("jazair");
MoveOptionOffer.Add("chowkidar");
MoveOptionOffer.Add("cuirassier");
MoveOptionOffer.Add("ryott");
MoveOptionOffer.Add("faujdar");
}
private MoveOption CreateRyottMoveOption(int Direction)
{
MoveOption NewMoveOption = new MoveOption("ryott");
Move NewMove = new Move(0, 1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, -1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(1 * Direction, 0);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(-1 * Direction, 0);
NewMoveOption.AddToPossibleMoves(NewMove);
return NewMoveOption;
}
private MoveOption CreateFaujdarMoveOption(int Direction)
{
MoveOption NewMoveOption = new MoveOption("faujdar");
Move NewMove = new Move(0, -1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, 1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, 2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, -2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
return NewMoveOption;
}
private MoveOption CreateJazairMoveOption(int Direction)
{
MoveOption NewMoveOption = new MoveOption("jazair");
Move NewMove = new Move(2 * Direction, 0);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(2 * Direction, -2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(2 * Direction, 2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, 2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, -2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(-1 * Direction, -1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(-1 * Direction, 1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
return NewMoveOption;
}
private MoveOption CreateCuirassierMoveOption(int Direction)
{
MoveOption NewMoveOption = new MoveOption("cuirassier");
Move NewMove = new Move(1 * Direction, 0);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(2 * Direction, 0);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(1 * Direction, -2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(1 * Direction, 2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
return NewMoveOption;
}
private MoveOption CreateChowkidarMoveOption(int Direction)
{
MoveOption NewMoveOption = new MoveOption("chowkidar");
Move NewMove = new Move(1 * Direction, 1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(1 * Direction, -1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(-1 * Direction, 1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(-1 * Direction, -1 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, 2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(0, -2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
return NewMoveOption;
}
private MoveOption CreateTibbleCrossMoveOption(int Direction)
{
MoveOption NewMoveOption = new MoveOption("tibblecross");
Move NewMove = new Move(2 * Direction, 2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(2 * Direction, -2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(-2 * Direction, 2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
NewMove = new Move(-2 * Direction, -2 * Direction);
NewMoveOption.AddToPossibleMoves(NewMove);
return NewMoveOption;
}
private MoveOption CreateMoveOption(string Name, int Direction)
{
if (Name == "tibblecross")
{
return CreateTibbleCrossMoveOption(Direction);
}
else if (Name == "chowkidar")
{
return CreateChowkidarMoveOption(Direction);
}
else if (Name == "ryott")
{
return CreateRyottMoveOption(Direction);
}
else if (Name == "faujdar")
{
return CreateFaujdarMoveOption(Direction);
}
else if (Name == "jazair")
{
return CreateJazairMoveOption(Direction);
}
else
{
return CreateCuirassierMoveOption(Direction);
}
}
private void CreateMoveOptions()
{
Players[0].AddToMoveOptionQueue(CreateMoveOption("tibblecross", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("ryott", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("chowkidar", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("cuirassier", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("faujdar", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("jazair", 1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("tibblecross", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("ryott", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("chowkidar", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("jazair", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("faujdar", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("cuirassier", -1));
}
}
Delphi/Pascal
Java
private MoveOption createTibbleCrossMoveOption(int direction) {
MoveOption newMoveOption = new MoveOption("tibblecross");
Move newMove = new Move(2 * direction, 2 * direction);
newMoveOption.addToPossibleMoves(newMove);
newMove = new Move(-2 * direction, 2 * direction);
newMoveOption.addToPossibleMoves(newMove);
newMove = new Move(2 * direction, -2 * direction);
newMoveOption.addToPossibleMoves(newMove);
newMove = new Move(-2 * direction, -2 * direction);
newMoveOption.addToPossibleMoves(newMove);
return newMoveOption;
}
private MoveOption createMoveOption(String name, int direction) {
switch (name) {
case "tibblecross":
return createTibbleCrossMoveOption(direction);
case "chowkidar":
return createChowkidarMoveOption(direction);
case "ryott":
return createRyottMoveOption(direction);
case "faujdar":
return createFaujdarMoveOption(direction);
case "jazair":
return createJazairMoveOption(direction);
default:
return createCuirassierMoveOption(direction);
}
}
private void createMoveOptions(){
players.get(0).addToMoveOptionQueue(createMoveOption("tibblecross", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("ryott", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("chowkidar", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("cuirassier", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("faujdar", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("jazair", 1));
players.get(1).addToMoveOptionQueue(createMoveOption("tibblecross", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("ryott", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("chowkidar", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("jazair", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("faujdar", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("cuirassier", -1));
}
Python
def __CreateMoveOptionOffer(self):
self._MoveOptionOffer.append("tibblecross") #append the new option
self._MoveOptionOffer.append("jazair")
self._MoveOptionOffer.append("chowkidar")
self._MoveOptionOffer.append("cuirassier")
self._MoveOptionOffer.append("ryott")
self._MoveOptionOffer.append("faujdar")
def __CreateTibbleCrossMoveOption(self, Direction):
NewMoveOption = MoveOption("tibblecross")
NewMove = Move(2 * Direction,2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
NewMove = Move(-2 * Direction,2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
NewMove = Move(2 * Direction,-2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
NewMove = Move(-2 * Direction,-2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
return NewMoveOption
def __CreateMoveOption(self, Name, Direction):
if Name == "tibblecross":
return self.__CreateTibbleCrossMoveOption(Direction) ## add the move ##
elif Name == "chowkidar":
return self.__CreateChowkidarMoveOption(Direction)
elif Name == "ryott":
return self.__CreateRyottMoveOption(Direction)
elif Name == "faujdar":
return self.__CreateFaujdarMoveOption(Direction)
elif Name == "jazair":
return self.__CreateJazairMoveOption(Direction)
else:
return self.__CreateCuirassierMoveOption(Direction)
def __CreateMoveOptions(self):
self._Players[0].AddToMoveOptionQueue(self.__CreateMoveOption("tibblecross", 1)) ## add to player 1 ##
self._Players[0].AddToMoveOptionQueue(self.__CreateMoveOption("ryott", 1))
self._Players[0].AddToMoveOptionQueue(self.__CreateMoveOption("chowkidar", 1))
self._Players[0].AddToMoveOptionQueue(self.__CreateMoveOption("cuirassier", 1))
self._Players[0].AddToMoveOptionQueue(self.__CreateMoveOption("faujdar", 1))
self._Players[0].AddToMoveOptionQueue(self.__CreateMoveOption("jazair", 1))
self._Players[1].AddToMoveOptionQueue(self.__CreateMoveOption("tibblecross", -1)) ## add to player 2 ##
self._Players[1].AddToMoveOptionQueue(self.__CreateMoveOption("ryott", -1))
self._Players[1].AddToMoveOptionQueue(self.__CreateMoveOption("chowkidar", -1))
self._Players[1].AddToMoveOptionQueue(self.__CreateMoveOption("jazair", -1))
self._Players[1].AddToMoveOptionQueue(self.__CreateMoveOption("faujdar", -1))
self._Players[1].AddToMoveOptionQueue(self.__CreateMoveOption("cuirassier", -1))
#edit: please also remember to change the randint value in __UseMoveOptionOffer making it
#self.__MoveOptionOfferPosition = random.randint(0, 5)
- 感谢 ella 和 yani
VB.NET
Private Sub CreateMoveOptionOffer()
MoveOptionOffer.Add("jazair")
MoveOptionOffer.Add("chowkidar")
MoveOptionOffer.Add("cuirassier")
MoveOptionOffer.Add("ryott")
MoveOptionOffer.Add("faujdar")
MoveOptionOffer.Add("TibbleCross")
End Sub
Private Function CreateTibbleCrossMoveOption(ByVal Direction As Integer) As MoveOption
Dim NewMoveOption As MoveOption = New MoveOption("TibbleCross")
Dim NewMove As Move = New Move(2 * Direction, 2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
NewMove = New Move(-2 * Direction, 2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
NewMove = New Move(2 * Direction, -2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
NewMove = New Move(-2 * Direction, -2 * Direction)
NewMoveOption.AddToPossibleMoves(NewMove)
Return NewMoveOption
End Function
Private Function CreateMoveOption(ByVal Name As String, ByVal Direction As Integer) As MoveOption
If Name = "chowkidar" Then
Return CreateChowkidarMoveOption(Direction)
ElseIf Name = "ryott" Then
Return CreateRyottMoveOption(Direction)
ElseIf Name = "faujdar" Then
Return CreateFaujdarMoveOption(Direction)
ElseIf Name = "jazair" Then
Return CreateJazairMoveOption(Direction)
ElseIf Name = "cuirassier" Then
Return CreateCuirassierMoveOption(Direction)
Else
Return CreateTibbleCrossMoveOption(Direction)
End If
End Function
Private Sub CreateMoveOptions()
Players(0).AddToMoveOptionQueue(CreateMoveOption("TibbleCross", 1))
Players(0).AddToMoveOptionQueue(CreateMoveOption("ryott", 1))
Players(0).AddToMoveOptionQueue(CreateMoveOption("chowkidar", 1))
Players(0).AddToMoveOptionQueue(CreateMoveOption("cuirassier", 1))
Players(0).AddToMoveOptionQueue(CreateMoveOption("faujdar", 1))
Players(0).AddToMoveOptionQueue(CreateMoveOption("jazair", 1))
Players(1).AddToMoveOptionQueue(CreateMoveOption("TibbleCross", -1))
Players(1).AddToMoveOptionQueue(CreateMoveOption("ryott", -1))
Players(1).AddToMoveOptionQueue(CreateMoveOption("chowkidar", -1))
Players(1).AddToMoveOptionQueue(CreateMoveOption("jazair", -1))
Players(1).AddToMoveOptionQueue(CreateMoveOption("faujdar", -1))
Players(1).AddToMoveOptionQueue(CreateMoveOption("cuirassier", -1))
End Sub
问题描述:添加一个新的移动,该移动向前移动,直到棋子到达棋盘的末端或碰到另一个棋子。
C#
// Line 35 should be moved to Line 38, underneath `CreatePieces(NoOfPieces)`
// The line content is `CreateMoveOptions()`
// Line 304
private void CreateMoveOptionOffer() {
MoveOptionOffer.Add("jazair");
MoveOptionOffer.Add("chowkidar");
MoveOptionOffer.Add("cuirassier");
MoveOptionOffer.Add("ryott");
MoveOptionOffer.Add("faujdar");
MoveOptionOffer.Add("rook");
// ^^ THIS LINE WAS ADDED ^^
}
// Line 387
// START OF ADDED SECTION
private MoveOption CreateRookMoveOption(int Direction) {
MoveOption NewMoveOption = new MoveOption("rook");
for (int row = 1; row < NoOfRows; ++row)
{
int Index = Direction < 0 ? (row - 1) * 10 : (NoOfRows - row - 2) * 10;
if (Board[Index].GetPieceInSquare() != null)
{
break;
}
Move NewMove = new Move(row * Direction, 0);
NewMoveOption.AddToPossibleMoves(NewMove);
}
return NewMoveOption;
}
// END OF ADDED SECTION
// Line 410, within the `CreateMoveOption function`
...
else if (Name == "rook") {
return CreateRookMoveOption(Direction);
}
...
// Modified `CreateMoveOptions` at Line 417
private void CreateMoveOptions() {
Players[0].AddToMoveOptionQueue(CreateMoveOption("rook", 1));
// ^^^^^^^^^^^^^^^^^^ THIS LINE WAS ADDED ^^^^^^^^^^^^^^^^^^^^
Players[0].AddToMoveOptionQueue(CreateMoveOption("ryott", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("chowkidar", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("cuirassier", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("faujdar", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("jazair", 1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("rook", -1));
// ^^^^^^^^^^^^^^^^^^ THIS LINE WAS ADDED ^^^^^^^^^^^^^^^^^^^^
Players[1].AddToMoveOptionQueue(CreateMoveOption("ryott", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("chowkidar", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("jazair", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("faujdar", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("cuirassier", -1));
}
// This is not required, I just found it made the Illegal Moves a tad more
// obvious
// Line 264
if (MoveLegal) {
...
}
// START OF ADDED SECTION
else {
Console.WriteLine("\x1b[31;1mIllegal Move ({0}, {1})\x1b[0;0m", StartSquareReference, FinishSquareReference);
}
// END OF ADDED SECTION
Delphi/Pascal
Java
public Dastan(int r, int c, int noOfPieces ){
// change order of initialisation to enable createMoveOptions to work for rook
// board needs to be created first to check max movement going forward.
players.add(new Player("Player One", 1));
players.add(new Player("Player Two", -1));
noOfRows = r;
noOfColumns = c;
createBoard();
createMoveOptions();
moveOptionOfferPosition = 0;
createMoveOptionOffer();
createPieces(noOfPieces);
currentPlayer = players.get(0);
}
// changes made to createMoveOption
private MoveOption createMoveOption(String name, int direction) {
switch (name) {
case "chowkidar":
return createChowkidarMoveOption(direction);
case "ryott":
return createRyottMoveOption(direction);
case "faujdar":
return createFaujdarMoveOption(direction);
case "jazair":
return createJazairMoveOption(direction);
// added rook
case "rook":
return createRookMoveOption(direction);
default:
return createCuirassierMoveOption(direction);
}
}
// add new MoveOption method
private MoveOption createRookMoveOption (int Direction) {
MoveOption NewMoveOption = new MoveOption("rook");
for (int row = 1; row < this.noOfRows; row++) {
int Index = Direction < 0 ? (row - 1) * 10 : (noOfRows - row - 2) * 10;
if (board.get(index).getPieceInSquare() != null) {
break;
}
Move NewMove = new Move(row * Direction, 0);
NewMoveOption.addToPossibleMoves(NewMove);
}
return NewMoveOption;
}
private void createMoveOptions(){
// added new move rook
players.get(0).addToMoveOptionQueue(createMoveOption("rook", 1));
//
players.get(0).addToMoveOptionQueue(createMoveOption("ryott", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("chowkidar", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("cuirassier", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("faujdar", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("jazair", 1));
// added new move rook
players.get(1).addToMoveOptionQueue(createMoveOption("rook", -1));
//
players.get(1).addToMoveOptionQueue(createMoveOption("ryott", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("chowkidar", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("jazair", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("faujdar", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("cuirassier", -1));
}
}
Python
while not SquareIsValid:
StartSquareReference = self.__GetSquareReference("containing the piece to move")
SquareIsValid = self.__CheckSquareIsValid(StartSquareReference, True)
SquareIsValid = False
#this is what changed
while not SquareIsValid:
if self._CurrentPlayer.GetQueue(Choice) == "castle":
SquareIsValid = True
FinishSquareReference = StartSquareReference
while SquareIsValid:
FinishSquareReference =int(str((int(str(FinishSquareReference)[0])+(1)*self._CurrentPlayer.GetDirection()))+str(FinishSquareReference)[1])
SquareIsValid = self.__CheckSquareIsValid(FinishSquareReference, False)
if SquareIsValid == False:
FinishSquareReference =int(str((int(str(FinishSquareReference)[0])-(1)*self._CurrentPlayer.GetDirection()))+str(FinishSquareReference)[1])
elif ( self.__CalculatePieceCapturePoints(FinishSquareReference))!=0:
FinishSquareReference =int(str((int(str(FinishSquareReference)[0])-(1)*self._CurrentPlayer.GetDirection()))+str(FinishSquareReference)[1])
SquareIsValid = True
break
else:
FinishSquareReference = self.__GetSquareReference("to move to")
if SquareIsValid == False:
SquareIsValid = self.__CheckSquareIsValid(FinishSquareReference, False)
#this is the end of what is changed
MoveLegal = self._CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference)
if MoveLegal:
PointsForPieceCapture = self.__CalculatePieceCapturePoints(FinishSquareReference)
self._CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))))
self._CurrentPlayer.UpdateQueueAfterMove(Choice)
self.__UpdateBoard(StartSquareReference, FinishSquareReference)
self.__UpdatePlayerScore(PointsForPieceCapture)
print("New score: " + str(self._CurrentPlayer.GetScore()) + "\n")
if self._CurrentPlayer.SameAs(self._Players[0]):
self._CurrentPlayer = self._Players[1]
else:
self._CurrentPlayer = self._Players[0]
GameOver = self.__CheckIfGameOver()
self.__DisplayState()
self.__DisplayFinalResult()
def GetQueue(self,Pos):
Temp = self.__Queue.GetMoveOptionInPosition(Pos - 1).GetName()
return Temp
VB.NET
问题描述:选择棋子移动位置时,必须输入起始方格和结束方格。如果选择了有效的起始方格,但选择了无效的目标方格,程序将继续执行,而不会提示玩家重新输入。
修改代码,提示用户重新输入结束方格,直到给出有效的方格。
C#
// The changes for this are at the while loop at line 258 in the PlayGame method of the Dastan class
// Move the definition of MoveLegal in front of the while loop
bool MoveLegal = false;
// Make the while loop conditional on both the MoveLegal and SquareIsValid, could also be (!SquareIsValid || !MoveLegal)
while (!(SquareIsValid && MoveLegal))
{
FinishSquareReference = GetSquareReference("to move to");
SquareIsValid = CheckSquareIsValid(FinishSquareReference, false);
// Check if the move is legal in the loop
MoveLegal = CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference);
// Give some output if it is an illegal move
if (!MoveLegal)
Console.WriteLine("Cannot move to that square with this move");
if (!SquareIsValid)
Console.WriteLine("Invalid position");
}
//bool MoveLegal = CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference);
Delphi/Pascal
Java
public void playGame() {
boolean gameOver = false;
while (!gameOver) {
displayState();
boolean squareIsValid = false;
int choice;
do {
Console.write("Choose move option to use from queue (1 to 3) or 9 to take the offer: ");
choice = Integer.parseInt(Console.readLine());
if (choice == 9) {
useMoveOptionOffer();
displayState();
}
} while (choice < 1 || choice > 3);
int startSquareReference = 0;
while (!squareIsValid) {
startSquareReference = getSquareReference("containing the piece to move");
squareIsValid = checkSquareIsValid(startSquareReference, true);
}
int finishSquareReference = 0;
boolean moveLegal = false; //set up move legal here
while (!moveLegal) { //change the loop to look at moveLegal
finishSquareReference = getSquareReference("to move to");
squareIsValid = checkSquareIsValid(finishSquareReference, false);
if (squareIsValid){
//Calculate moveLegal
moveLegal = currentPlayer.checkPlayerMove(choice, startSquareReference, finishSquareReference);
}
}
//Can remove the chack to moveLegal here as it will be
int pointsForPieceCapture = calculatePieceCapturePoints(finishSquareReference);
currentPlayer.changeScore(-(choice + (2 * (choice - 1))));
//etc.
Python
class Dastan:
...
def PlayGame(self):
...
MoveLegal = False # change from SqureIsValid = False
while not MoveLegal: # change from while not SquareIsValid:
FinishSquareReference = self.__GetSquareReference("to move to")
SquareIsValid = self.__CheckSquareIsValid(FinishSquareReference, False)
if SquareIsValid: # Need to check that the square is both valid and legal first, otherwise can take own pieces
MoveLegal = self._CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference,FinishSquareReference) # bring MoveLegal check into while loop so that it asks until the Finish Square move is legal
# remove if MoveLegal: as it is now guaranteed to be True
PointsForPieceCapture = self.__CalculatePieceCapturePoints(FinishSquareReference)
self._CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))))
self._CurrentPlayer.UpdateQueueAfterMove(Choice)
self.__UpdateBoard(StartSquareReference, FinishSquareReference)
self.__UpdatePlayerScore(PointsForPieceCapture)
print("New score: " + str(self._CurrentPlayer.GetScore()) + "\n")
...
VB.NET
While Not SquareIsValid Or MoveLegal = False
FinishSquareReference = GetSquareReference("to move to")
SquareIsValid = CheckSquareIsValid(FinishSquareReference, False)
MoveLegal = CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference)
End While
问题描述:添加一个名为空间跳跃的新移动,将选择的棋子移动到棋盘上的随机位置。它不能落在你自己的棋子上,但其他任何地方都可以。玩家不能选择棋子最终将落在哪里。空间跳跃移动不会添加到移动队列中,而是每个玩家在游戏中可以使用一次,可以在他们选择时代替队列选择使用。通过允许玩家 1 从方格 22 使用此选项,然后玩家 2 从方格 55 使用此选项进行测试。玩家 1 在下一轮将不会显示此选项。
C#
public void PlayGame()
{
/* Only one time use is not implemented*/
/* SUMMARY - option of pressing 8 for a spacejump is implemented and the 'finishsquarereferance' is randomly generated instead of asking for user input*/
bool GameOver = false;
while (!GameOver)
{
DisplayState();
bool SquareIsValid = false;
int Choice;
do
{
Console.Write("Choose move option to use from queue (1 to 3) or 8 to make a space jump or 9 to take the offer: "); //added the option of pressubg 8 for a space jump
Choice = Convert.ToInt32(Console.ReadLine());
if (Choice == 9)
{
UseMoveOptionOffer();
DisplayState();
}
}
while (!(Choice != 1 || Choice != 2 || Choice != 3 || Choice != 8)); //adding 8 to the clause so that the loop can be escaped
int StartSquareReference = 0;
while (!SquareIsValid)
{
StartSquareReference = GetSquareReference("containing the piece to move");
SquareIsValid = CheckSquareIsValid(StartSquareReference, true);
}
int FinishSquareReference = 0;
if (Choice != 8) //if they choose 1, 2 or 3 then this asks the user to input their finish square referenace as normal
{
SquareIsValid = false;
bool MoveLegal = false; //moved the definition of movelegal so the issue of user picking a valid square that is not within the bounds of a move skips their turn
while (!SquareIsValid || !MoveLegal)
{
FinishSquareReference = GetSquareReference("to move to");
SquareIsValid = CheckSquareIsValid(FinishSquareReference, false);
MoveLegal = CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference);
}
//from here it updates the baord and calaculates points as normal
int PointsForPieceCapture = CalculatePieceCapturePoints(FinishSquareReference);
CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))));
CurrentPlayer.UpdateQueueAfterMove(Choice);
UpdateBoard(StartSquareReference, FinishSquareReference);
UpdatePlayerScore(PointsForPieceCapture);
Console.WriteLine("New score: " + CurrentPlayer.GetScore() + Environment.NewLine);
}
else //if their choice is 8 then it randomly generates a finish square referance
{
do
{ // do-while loop so that if the 'finishsquarereferance' does not equal the 'startsquarereferance'
FinishSquareReference = RGen.Next(1, NoOfRows) * 10 + RGen.Next(1, NoOfColumns); //1 to NoOfColumns & 1 to NoOfRows
SquareIsValid = CheckSquareIsValid(FinishSquareReference, false);
} while (FinishSquareReference == StartSquareReference && !SquareIsValid); //because lists are 0 based
//calcualtes points and updates the board
//removed line 'CurrentPlayer.UpdateQueueAfterMove(Choice);
int PointsForPieceCapture = CalculatePieceCapturePoints(FinishSquareReference);
CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))));
UpdateBoard(StartSquareReference, FinishSquareReference);
UpdatePlayerScore(PointsForPieceCapture);
Console.WriteLine("New score: " + CurrentPlayer.GetScore() + Environment.NewLine);
}
if (CurrentPlayer.SameAs(Players[0]))
{
CurrentPlayer = Players[1];
}
else
{
CurrentPlayer = Players[0];
}
GameOver = CheckIfGameOver();
}
DisplayState();
DisplayFinalResult();
}
Delphi/Pascal
Java
class Player {
private String name;
private int direction, score;
private MoveOptionQueue queue = new MoveOptionQueue();
private boolean spaceJumpUsed;
public Player(String n, int d) {
score = 100;
name = n;
direction = d;
spaceJumpUsed=false;
}
public boolean getspaceJumpUsed(){
return spaceJumpUsed;
}
public void setspaceJumpUsed(boolean newValue){
spaceJumpUsed=newValue;
}
//etc.
public void playGame() {
boolean gameOver = false;
while (!gameOver) {
displayState();
boolean squareIsValid = false;
char spaceJumpChoice = 'n';
if (!currentPlayer.getSpaceJumpUsed()) {
Console.println(" Do you want to space jump (y/n). "
+ "You can only do this once in a game: ");
spaceJumpChoice = Console.readLine().charAt(0);
}
if (spaceJumpChoice == 'n') {
//Continue with normal execution
int choice;
do {
Console.write("Choose move option to use from queue (1 to 3) or 9 to take the offer: ");
choice = Integer.parseInt(Console.readLine());
if (choice == 9) {
useMoveOptionOffer();
displayState();
}
} while (choice < 1 || choice > 3);
int startSquareReference = 0;
while (!squareIsValid) {
startSquareReference = getSquareReference("containing the piece to move");
squareIsValid = checkSquareIsValid(startSquareReference, true);
}
int finishSquareReference = 0;
squareIsValid = false;
while (!squareIsValid) {
finishSquareReference = getSquareReference("to move to");
squareIsValid = checkSquareIsValid(finishSquareReference, false);
}
boolean moveLegal = currentPlayer.checkPlayerMove(choice, startSquareReference, finishSquareReference);
if (moveLegal) {
int pointsForPieceCapture = calculatePieceCapturePoints(finishSquareReference);
currentPlayer.changeScore(-(choice + (2 * (choice - 1))));
currentPlayer.updateQueueAfterMove(choice);
updateboard(startSquareReference, finishSquareReference);
updatePlayerScore(pointsForPieceCapture);
Console.writeLine("New score: " + currentPlayer.getScore() + System.lineSeparator());
}
} else {
//do a spaceJump - use whatever can be used from normal execution above
currentPlayer.setSpaceJumpUsed(true);
int startSquareReference = 0;
while (!squareIsValid) {
startSquareReference = getSquareReference("containing the piece to move");
squareIsValid = checkSquareIsValid(startSquareReference, true);
}
int finishSquareReference = 0;
squareIsValid = false;
while (!squareIsValid) {
//Generate a finish square reference within the range 0-99 and then validate it
finishSquareReference=rGen.nextInt(100);
squareIsValid = checkSquareIsValid(finishSquareReference, false);
}
int pointsForPieceCapture = calculatePieceCapturePoints(finishSquareReference);
updateboard(startSquareReference, finishSquareReference);
updatePlayerScore(pointsForPieceCapture);
Console.writeLine("New score: " + currentPlayer.getScore() + System.lineSeparator());
}
if (currentPlayer.sameAs(players.get(0))) {
currentPlayer = players.get(1);
} else {
currentPlayer = players.get(0);
}
gameOver = checkIfGameOver();
}
displayState();
displayFinalResult();
}
Python
______________________________________________________________________________________________________________________________
# In Dastan class
def PlayGame(self):
GameOver = False
SpaceJump = False
while not GameOver:
self.__DisplayState()
SquareIsValid = False
Choice = 0
while (Choice < 1 or Choice > 3) and Choice != 8:
if self._CurrentPlayer.CanUseSpaceJump():
Choice = int(input("Choose move option to use from queue (1 to 3), 8 to use SpaceJump or 9 to take the offer: "))
else:
Choice = int(input("Choose move option to use from queue (1 to 3), or 9 to take the offer: "))
if Choice == 8:
if self._CurrentPlayer.CanUseSpaceJump():
SpaceJump = True
else:
Choice = 0 # Reset Choice to 0 so it asks again
print("You have already used SpaceJump this game")
elif Choice == 9:
self.__UseMoveOptionOffer()
self.__DisplayState()
while not SquareIsValid:
StartSquareReference = self.__GetSquareReference("containing the piece to move")
SquareIsValid = self.__CheckSquareIsValid(StartSquareReference, True)
SquareIsValid = False
while not SquareIsValid:
if SpaceJump:
FinishSquareReference = random.randint(1, self._NoOfRows) * 10 + random.randint(1, self._NoOfColumns)
else:
FinishSquareReference = self.__GetSquareReference("to move to")
# CheckSquareIsValid already prevents you from moving to your own piece so no additional check is needed
SquareIsValid = self.__CheckSquareIsValid(FinishSquareReference, False)
if SpaceJump:
MoveLegal = True
else:
MoveLegal = self._CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference)
if MoveLegal:
PointsForPieceCapture = self.__CalculatePieceCapturePoints(FinishSquareReference)
if SpaceJump:
# The question didn't specify a point cost for SpaceJump but I thought 22 was a bit much
# We also need a conditional here anyway as UpdateQueueAfterMove doesn't work when Choice=8
self._CurrentPlayer.ChangeScore(-10)
SpaceJump = False
self._CurrentPlayer.SetUsedSpaceJump(True)
else:
self._CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))))
self._CurrentPlayer.UpdateQueueAfterMove(Choice)
self.__UpdateBoard(StartSquareReference, FinishSquareReference)
self.__UpdatePlayerScore(PointsForPieceCapture)
print("New score: " + str(self._CurrentPlayer.GetScore()) + "\n")
if self._CurrentPlayer.SameAs(self._Players[0]):
self._CurrentPlayer = self._Players[1]
else:
self._CurrentPlayer = self._Players[0]
GameOver = self.__CheckIfGameOver()
self.__DisplayState()
self.__DisplayFinalResult()
______________________________________________________________________________________________________________________________
class Player:
def __init__(self, N, D):
self.__Score = 100
self.__Name = N
self.__Direction = D
self.__Queue = MoveOptionQueue()
self.__UsedSpaceJump = False # Added private variable
def CanUseSpaceJump(self):
return not self.__UsedSpaceJump
def SetUsedSpaceJump(self, Value):
self.__UsedSpaceJump = Value
# Other methods originally in Player are not edited
______________________________________________________________________________________________________________________________
VB.NET
#changes to PlayGame subroutine:
Loop Until (Choice >= 1 And Choice <= 3) Or (CurrentPlayer.GetSpaceJump = False And Choice = 10)
Dim StartSquareReference As Integer
While Not SquareIsValid
StartSquareReference = GetSquareReference("containing the piece to move")
SquareIsValid = CheckSquareIsValid(StartSquareReference, True)
End While
Dim FinishSquareReference As Integer
If Choice = 10 Then
FinishSquareReference = DoSpaceJump()
Else
SquareIsValid = False
Console.WriteLine("Movelegal false")
Dim MoveLegal As Boolean = False
While Not SquareIsValid Or MoveLegal = False
FinishSquareReference = GetSquareReference("to move to")
SquareIsValid = CheckSquareIsValid(FinishSquareReference, False)
MoveLegal = CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference)
End While
End If
#changes to Player class:
Private SpaceJump As Boolean = False
Public Function GetSpaceJump()
Return SpaceJump
End Function
Sub UseSpaceJump()
SpaceJump = True
End Sub
#Changes to Display Player State:
If CurrentPlayer.GetSpaceJump = False Then
Console.WriteLine("Spacejump available! Write 10 for spacejump")
End If
#New SpaceJump function(returns finish square reference):
Private Function DoSpaceJump() As Integer
Dim random As New Random
Dim ValidReference As Boolean = False
Dim Reference As Integer
While ValidReference = False
Reference = random.Next(11, 66)
If Board(GetIndexOfSquare(Reference)).GetPieceInSquare() IsNot Nothing Then
If Board(GetIndexOfSquare(Reference)).GetPieceInSquare().GetBelongsTo() IsNot CurrentPlayer Then
ValidReference = True
End If
End If
End While
CurrentPlayer.UseSpaceJump()
Return Reference
End Function
在游戏开始时,允许每个用户输入自己的姓名,以替换“玩家一”和“玩家二”。
C#
static void Main(string[] args)
{
//bercan
Console.WriteLine("\t --- Welcome to DASTAN! --- \t");
Console.WriteLine("\n");
Console.WriteLine("Player a name for player one"); // get name for player 1
string player1_name = Console.ReadLine();
Console.WriteLine("Player a name for player two"); // get name for player 2
string player2_name = Console.ReadLine();
Console.WriteLine("\n");
Dastan ThisGame = new Dastan(6, 6, 4, player1_name, player2_name); //enter the parameters
ThisGame.PlayGame();
Console.WriteLine("Goodbye!");
Console.ReadLine();
}
}
class Dastan
{
protected List<Square> Board;
protected int NoOfRows, NoOfColumns, MoveOptionOfferPosition;
protected List<Player> Players = new List<Player>();
protected List<string> MoveOptionOffer = new List<string>();
protected Player CurrentPlayer;
protected Random RGen = new Random();
public Dastan(int R, int C, int NoOfPieces , string player1_name , string player2_name) // parameters for the constructor
{
Players.Add(new Player(player1_name, 1)); // instead of "Player One" the parameter player1_name
Players.Add(new Player(player2_name, -1)); // instead of "Player the" the parameter player2_name
CreateMoveOptions();
NoOfRows = R;
NoOfColumns = C;
MoveOptionOfferPosition = 0;
CreateMoveOptionOffer();
CreateBoard();
CreatePieces(NoOfPieces);
CurrentPlayer = Players[0];
}
Delphi/Pascal
Java
//Class Dastan
public Dastan(int r, int c, int noOfPieces ){
Console.writeLine("Player 1, enter your name: ");
String player1Name = Console.readLine();
players.add(new Player(player1Name, 1));
Console.writeLine("Player 2, enter your name: ");
String player2Name = Console.readLine();
players.add(new Player(player2Name, -1));
createMoveOptions();
noOfRows = r;
noOfColumns = c;
moveOptionOfferPosition = 0;
createMoveOptionOffer();
createBoard();
createPieces(noOfPieces);
currentPlayer = players.get(0);
}
Python
class Dastan:
def __init__(self, R, C, NoOfPieces):
self._Board = []
self._Players = []
self._MoveOptionOffer = []
playerOneName = input("Please enter player 1's name: ")
self._Players.append(Player(playerOneName, 1))
playerTwoName = input("Please enter player 2's name: ")
self._Players.append(Player(playerTwoName, -1))
self.__CreateMoveOptions()
...
VB.NET
问题描述:目前,在选择移动时,玩家需要记住移动的适用方式。修改程序,以便在选择棋子后,在该棋子可以移动到的方格中显示“^”符号,基于所选的移动方式。 - 使用方法 A1 规范解决
C#
//Dastan class
private void DisplayBoardMoveOptions(Player CurrentPlayer, int Choice, int StartSquareReference)
{
//A slightly altered DisplayBoard() but checks if each square is a possible square to move to using CurrentPlayer.CheckPlayerMove() and places "^" if it is
//Adds Column axis labeling
Console.Write(Environment.NewLine + " ");
for (int Column = 1; Column <= NoOfColumns; Column++)
{
Console.Write(Column.ToString() + " ");
}
Console.Write(Environment.NewLine + " ");
for (int Count = 1; Count <= NoOfColumns; Count++)
{
Console.Write("---");
}
Console.WriteLine("-");
for (int Row = 1; Row <= NoOfRows; Row++)
{
//For each row add the row labelling at the start (number and spacing)
Console.Write(Row.ToString() + " ");
for (int Column = 1; Column <= NoOfColumns; Column++)
{
//Either places a "^" for a possible place to move to ,a " " for an empty sqaure or the symbol for the piece in the square
int Index = GetIndexOfSquare(Row * 10 + Column);
Console.Write("|" + Board[Index].GetSymbol());
Piece PieceInSquare = Board[Index].GetPieceInSquare();
if (CheckSquareIsValid(Row * 10 + Column, false) && CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, Row * 10 + Column))
{
Console.Write("^");
}
else if (PieceInSquare == null)
{
Console.Write(" ");
}
else
{
Console.Write(PieceInSquare.GetSymbol());
}
}
Console.WriteLine("|");
}
Console.Write(" -");
// Add spacing for the bottom of the board display
for (int Column = 1; Column <= NoOfColumns; Column++)
{
Console.Write("---");
}
Console.WriteLine();
Console.WriteLine();
}
public void PlayGame()
{
bool GameOver = false;
while (!GameOver)
{
DisplayState();
bool SquareIsValid = false;
int Choice;
do
{
Console.Write("Choose move option to use from queue (1 to 3) or 9 to take the offer: ");
Choice = Convert.ToInt32(Console.ReadLine());
if (Choice == 9)
{
UseMoveOptionOffer();
DisplayState();
}
}
while (Choice < 1 || Choice > 3);
int StartSquareReference = 0;
while (!SquareIsValid)
{
StartSquareReference = GetSquareReference("containing the piece to move");
SquareIsValid = CheckSquareIsValid(StartSquareReference, true);
}
int FinishSquareReference = 0;
SquareIsValid = false;
//Display possible move options using currentPlayer, the player choice number and the start point of the move
DisplayBoardMoveOptions(CurrentPlayer,Choice,StartSquareReference);
while (!SquareIsValid)
{
FinishSquareReference = GetSquareReference("to move to");
SquareIsValid = CheckSquareIsValid(FinishSquareReference, false);
}
bool MoveLegal = CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference);
if (MoveLegal)
{
int PointsForPieceCapture = CalculatePieceCapturePoints(FinishSquareReference);
CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))));
CurrentPlayer.UpdateQueueAfterMove(Choice);
UpdateBoard(StartSquareReference, FinishSquareReference);
UpdatePlayerScore(PointsForPieceCapture);
Console.WriteLine("New score: " + CurrentPlayer.GetScore() + Environment.NewLine);
}
if (CurrentPlayer.SameAs(Players[0]))
{
CurrentPlayer = Players[1];
}
else
{
CurrentPlayer = Players[0];
}
GameOver = CheckIfGameOver();
}
DisplayState();
DisplayFinalResult();
}
Delphi/Pascal
Java
//Class Dastan
// Now takes two parameters
// The index of the players choice in the Move Option Queue
// And the start square that the player has selected
private void displayBoard(int choice, int startSquareReference){
Console.write(System.lineSeparator() + " ");
for (int column = 1; column <= noOfColumns; column++) {
Console.write(column + " ");
}
Console.write(System.lineSeparator() + " ");
for (int count = 1; count <= noOfColumns; count++) {
Console.write("---");
}
Console.writeLine("-");
for (int row = 1; row <= noOfRows; row++) {
Console.write(row + " ");
for (int column = 1; column <= noOfColumns; column++) {
int index = getIndexOfSquare(row * 10 + column);
Console.write("|" + board.get(index).getSymbol());
Piece pieceInSquare = board.get(index).getPieceInSquare();
if (pieceInSquare == null) {
// START OF ADDED SECTION
// Doesn't display the possible moves if -1 is passed as the choice
if (choice != -1) {
//This is the row and column in the format that checkPlayerMove takes
int endSquareReference = Integer.parseInt(row+""+column);
// Displays a ^ if checkPlayerMove returns true with the arguments below
if (currentPlayer.checkPlayerMove(choice, startSquareReference, endSquareReference)) {
Console.write("^");
}
else {
Console.write(" ");
}
}
else {
Console.write(" ");
}
// END OF ADDED SECTION
} else {
Console.write(pieceInSquare.getSymbol());
}
}
Console.writeLine("|");
}
Console.write(" -");
for (int column = 1; column <= noOfColumns; column++) {
Console.write("---");
}
Console.writeLine();
Console.writeLine();
}
//Class Dastan
private void displayState(){
// Displays the board in the normal format is -1 is passed as the choice
//START OF ADDED SECTION
displayBoard(-1, -1);
//END OF ADDED SECTION
Console.writeLine("Move option offer: " + moveOptionOffer.get(moveOptionOfferPosition));
Console.writeLine();
Console.writeLine(currentPlayer.getPlayerStateAsString());
Console.writeLine("Turn: " + currentPlayer.getName());
Console.writeLine();
}
//Class Dastan
public void playGame() {
boolean gameOver = false;
while (!gameOver) {
displayState();
boolean squareIsValid = false;
int choice;
do {
Console.write("Choose move option to use from queue (1 to 3) or 9 to take the offer: ");
choice = Integer.parseInt(Console.readLine());
if (choice == 9) {
useMoveOptionOffer();
displayState();
}
} while (choice < 1 || choice > 3);
int startSquareReference = 0;
while (!squareIsValid) {
startSquareReference = getSquareReference("containing the piece to move");
squareIsValid = checkSquareIsValid(startSquareReference, true);
}
// Displays the board with ^ in the squares that are valid moves
// START OF ADDED SECTION
displayBoard(choice, startSquareReference);
// END OF ADDED SECTION
int finishSquareReference = 0;
squareIsValid = false;
while (!squareIsValid) {
finishSquareReference = getSquareReference("to move to");
squareIsValid = checkSquareIsValid(finishSquareReference, false);
}
boolean moveLegal = currentPlayer.checkPlayerMove(choice, startSquareReference, finishSquareReference);
if (moveLegal) {
int pointsForPieceCapture = calculatePieceCapturePoints(finishSquareReference);
currentPlayer.changeScore(-(choice + (2 * (choice - 1))));
currentPlayer.updateQueueAfterMove(choice);
updateboard(startSquareReference, finishSquareReference);
updatePlayerScore(pointsForPieceCapture);
Console.writeLine("New score: " + currentPlayer.getScore() + System.lineSeparator());
}
if (currentPlayer.sameAs(players.get(0))) {
currentPlayer = players.get(1);
} else {
currentPlayer = players.get(0);
}
gameOver = checkIfGameOver();
}
displayState();
displayFinalResult();
}
Python
def __DisplayMove(self, CurrentPlayer, Choice, StartSquareReference):
#set available squares
availableMove = Piece("potential",None,0,"^")
for Row in range(1, self._NoOfRows + 1):
for Column in range(1, self._NoOfColumns + 1):
Index = self.__GetIndexOfSquare(Row * 10 + Column)
if self.__CheckSquareIsValid(Row * 10 + Column, False) and CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, Row * 10 + Column):
self._Board[Index].SetPiece(availableMove)
##display board
self.__DisplayBoard()
#remove ^
for Row in range(1, self._NoOfRows + 1):
for Column in range(1, self._NoOfColumns + 1):
Index = self.__GetIndexOfSquare(Row * 10 + Column)
if self._Board[Index].GetPieceInSquare() is not None and self._Board[Index].GetPieceInSquare().GetSymbol() =="^":
self._Board[Index].SetPiece(None)
def PlayGame(self):
...
SquareIsValid = False
self.__DisplayMove(self._CurrentPlayer,Choice,StartSquareReference)
...
#~Victor
任何改进都将不胜感激。
VB.NET
Sub DisplayPotentialMoves(ByVal Choice As Integer, ByVal SquareReference As Integer, ByVal CurrentPlayer As Player) Console.Write(Environment.NewLine & " ") For Column = 1 To NoOfColumns Console.Write(Column.ToString() & " ") Next Console.Write(Environment.NewLine & " ") For Count = 1 To NoOfColumns Console.Write("---") Next Console.WriteLine("-")
For Row = 1 To NoOfRows Console.Write(Row.ToString() & " ") For Column = 1 To NoOfColumns Dim Index As Integer = GetIndexOfSquare(Row * 10 + Column) Console.Write("|" & Board(Index).GetSymbol()) Dim PieceInSquare As Piece = Board(Index).GetPieceInSquare()
If (CheckSquareIsValid(Row * 10 + Column, False) And CurrentPlayer.CheckPlayerMove(Choice, SquareReference, Row * 10 + Column)) Then Console.Write("^") ElseIf PieceInSquare Is Nothing Then Console.Write(" ") Else Console.Write(PieceInSquare.GetSymbol()) End If Next Console.WriteLine("|") Next Console.Write(" -") For Column = 1 To NoOfColumns Console.Write("---") Next Console.WriteLine() Console.WriteLine()End Sub
问题描述:在游戏开始时随机化玩家的移动队列,以便玩家不会在每局游戏开始时都使用相同的队列。
C#
// Dastan Class
private void CreateMoveOptions()
{
Players[0].AddToMoveOptionQueue(CreateMoveOption("ryott", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("chowkidar", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("cuirassier", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("faujdar", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("jazair", 1));
Players[0].AddToMoveOptionQueue(CreateMoveOption("crossMove", 1));
Players[0].Shuffle();
Players[1].AddToMoveOptionQueue(CreateMoveOption("ryott", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("chowkidar", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("jazair", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("faujdar", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("cuirassier", -1));
Players[1].AddToMoveOptionQueue(CreateMoveOption("crossMove", -1));
Players[1].Shuffle();
}
// Player Class
public void Shuffle()
{
Queue.Shuffle();
}
// MoveOptionQueue Class
public void Shuffle()
{
Random Rand = new Random();
int n;
MoveOption temp;
for (int i = Queue.Count-1; i > 1; i--)
{
n = Rand.Next(0, Queue.Count);
temp = Queue[n];
Queue[n] = Queue[i];
Queue[i] = temp;
}
}
Delphi/Pascal
Java
// Dastan Class
private void createMoveOptions(){
players.get(0).addToMoveOptionQueue(createMoveOption("ryott", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("chowkidar", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("cuirassier", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("faujdar", 1));
players.get(0).addToMoveOptionQueue(createMoveOption("jazair", 1));
players.get(1).addToMoveOptionQueue(createMoveOption("ryott", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("chowkidar", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("jazair", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("faujdar", -1));
players.get(1).addToMoveOptionQueue(createMoveOption("cuirassier", -1));
players.get(0).shuffleQueue();
players.get(1).shuffleQueue();
}
// Player Class
public void shuffleQueue() {
// Moves a random item from the queue to the back 1000 times to shuffle it
for (int i=0; i<1000; i++) {
Random rand = new Random();
int itemToMove = rand.nextInt(5);
// moveItemToBack is already a method in the MoveOptionQueue class
queue.moveItemToBack(itemToMove);
}
}
Python
def __CreateMoveOptions(self):
#creates a list of all possible moves
list = ["ryott","chowkidar","cuirassier","lazzad","jazair"]
#random.shufflle will randomize the list contents
random.shuffle(list)
#the for loop will iterate through each of the contents of thh list
for i in list:
#and will add the moves to the move options
self._Players[0].AddToMoveOptionQueue(self.__CreateMoveOption(i, 1))
self._Players[1].AddToMoveOptionQueue(self.__CreateMoveOption(i, -1))
#-zednin
VB.NET
Private Sub CreateMoveOptions()
Dim RandMove As New Random
For i = 0 To 1
Dim MoveList As New List(Of String)
MoveList.AddRange({"ryott", "chowkidar", "cuirassier", "faujdar", "jazair"})
Dim d As Integer
If i = 0 Then
d = 1
Else
d = -1
End If
While MoveList.Count > 0
Dim CurrentString As String = MoveList(RandMove.Next(0, MoveList.Count - 1))
Players(i).AddToMoveOptionQueue(CreateMoveOption(CurrentString, d))
MoveList.Remove(CurrentString)
End While
Next
End Sub
问题描述:在游戏开始时,允许玩家选择将他们的(Kotla 和 Mirza)一起放置在棋盘他们一侧的后面。例如:玩家一可以将他们的 Kotla 和 Mirza 放置在 (1,1) 到 (1,6) 之间,玩家二可以将他们的 Kotla 和 Mirza 放置在 (6,1) 和 (6,6) 之间。
C#
Delphi/Pascal
Java
class Dastan {
public Dastan(int r, int c, int noOfPieces ){
noOfRows = r;
noOfColumns = c;
players.add(new Player("Player One", 1, getPieceStartingColumn("Player One")));
players.add(new Player("Player Two", -1, getPieceStartingColumn("Player Two")));
createMoveOptions();
moveOptionOfferPosition = 0;
createMoveOptionOffer();
createBoard();
createPieces(noOfPieces);
currentPlayer = players.get(0);
}
private int getPieceStartingColumn(String playerName) {
boolean parsed = false;
int column = 0;
while (!parsed) {
try {
Console.writeLine(playerName + " which column (1-6) should your combined Kolta and Mirza start at?: ");
column = Integer.parseInt(Console.readLine());
Console.writeLine(noOfRows);
if (column >= 1 && column <= noOfRows) {
parsed = true;
} else {
Console.writeLine("Please enter a number between 1 and 6, inclusive");
}
}
catch (NumberFormatException e) {
Console.writeLine("Please enter a number");
}
}
return column;
}
private void createBoard() {
Square s;
board = new ArrayList<>();
for (int row = 1; row <= noOfRows; row++) {
for (int column = 1; column <= noOfColumns; column++) {
if (row == 1 && column == players.get(0).getKoltaColumn()) {
s = new Kotla(players.get(0), "K");
} else if (row == noOfRows && column == players.get(1).getKoltaColumn()) {
s = new Kotla(players.get(1), "k");
} else {
s = new Square();
}
board.add(s);
}
}
}
private void createPieces(int noOfPieces) {
Piece currentPiece;
for (int count = 1; count <= noOfPieces; count++) {
currentPiece = new Piece("piece", players.get(0), 1, "!");
board.get(getIndexOfSquare(2 * 10 + count + 1)).setPiece(currentPiece);
}
currentPiece = new Piece("mirza", players.get(0), 5, "1");
board.get(getIndexOfSquare(10 + players.get(0).getKoltaColumn())).setPiece(currentPiece);
for (int count = 1; count <= noOfPieces; count++) {
currentPiece = new Piece("piece", players.get(1), 1, "\"");
board.get(getIndexOfSquare((noOfRows - 1) * 10 + count + 1)).setPiece(currentPiece);
}
currentPiece = new Piece("mirza", players.get(1), 5, "2");
board.get(getIndexOfSquare(noOfRows * 10 + players.get(1).getKoltaColumn())).setPiece(currentPiece);
}
}
class Player {
private String name;
private int koltaColumn;
private int direction, score;
private MoveOptionQueue queue = new MoveOptionQueue();
public Player(String n, int d, int c) {
score = 100;
name = n;
direction = d;
koltaColumn = c;
}
public int getKoltaColumn() {
return koltaColumn;
}
}
Python
class Player:
def __init__(self, N, D):
self.__Score = 100
self.__Name = N
self.__Direction = D
self.__Queue = MoveOptionQueue()
self.Kotla = int(input(f"Which column should {self.__Name}'s Kotla be?: "))
class Dastan:
def __CreateBoard(self):
for Row in range(1, self._NoOfRows + 1):
for Column in range(1, self._NoOfColumns + 1):
if Row == 1 and Column == self._Players[0].Kotla:
S = Kotla(self._Players[0], "K")
elif Row == self._NoOfRows and Column == self._Players[1].Kotla:
S = Kotla(self._Players[1], "k")
else:
S = Square()
self._Board.append(S)
def __CreatePieces(self, NoOfPieces):
for Count in range(1, NoOfPieces + 1):
CurrentPiece = Piece("piece", self._Players[0], 1, "!")
self._Board[self.__GetIndexOfSquare(2 * 10 + Count + 1)].SetPiece(CurrentPiece)
CurrentPiece = Piece("mirza", self._Players[0], 5, "1")
self._Board[self.__GetIndexOfSquare(10 + self._Players[0].Kotla)].SetPiece(CurrentPiece)
for Count in range(1, NoOfPieces + 1):
CurrentPiece = Piece("piece", self._Players[1], 1, '"')
self._Board[self.__GetIndexOfSquare((self._NoOfRows - 1) * 10 + Count + 1)].SetPiece(CurrentPiece)
CurrentPiece = Piece("mirza", self._Players[1], 5, "2")
self._Board[self.__GetIndexOfSquare(self._NoOfRows * 10 + (self._Players[1].Kotla))].SetPiece(CurrentPiece)
VB.NET
Private Sub CreateBoard()
Dim S As Square
Board = New List(Of Square)
Dim PlayerColumns As New List(Of Integer)
For i = 0 To 1
Dim t As Integer
If i = 0 Then
t = 1
Else
t = 6
End If
Dim Choice As Integer = -1
While Choice < 0 Or Choice > 6
Console.WriteLine(Players(i).GetName & ", please enter column of where you would like like your kotla and mirza to be placed, it will be placed in row " & t)
Choice = Console.ReadLine
End While
PlayerColumns.Add(Choice)
Next
For Row = 1 To NoOfRows
For Column = 1 To NoOfColumns
If Row = 1 And Column = PlayerColumns(0) Then
S = New Kotla(Players(0), "K")
ElseIf Row = NoOfRows And Column = PlayerColumns(1) Then
S = New Kotla(Players(1), "k")
Else
S = New Square()
End If
Board.Add(S)
Next
Next
Dim CurrentPiece As Piece
CurrentPiece = New Piece("mirza", Players(0), 5, "1")
Board(GetIndexOfSquare(10 + PlayerColumns(0))).SetPiece(CurrentPiece)
CurrentPiece = New Piece("mirza", Players(1), 5, "2")
Board(GetIndexOfSquare(NoOfRows * 10 + PlayerColumns(1))).SetPiece(CurrentPiece)
End Sub
问题描述:目前,如果玩家接受移动选项(例如,玩家 1 接受了 jazair 选项并替换了 ryott),他们会再次获得完全相同的选项:从队列中选择要使用的移动选项 (1 到 3) 或 9 以接受选项:
。这就是初步资料中所说的“从选项中选择移动选项不计为移动”……仍然是该玩家的回合。但是,您的挑战是更改这一点,使其确实计为回合,如果他们选择 9,则轮到下一位玩家。
C#
Delphi/Pascal
Java
Python
def PlayGame(self):
GameOver = False
while not GameOver:
self.__DisplayState()
SquareIsValid = False
Choice = 0
while Choice < 1 or Choice > 3:
Choice = int(input("Choose move option to use from queue (1 to 3) or 9 to take the offer: "))
if Choice == 9:
self.__UseMoveOptionOffer()
#---------
#put it before to change player before board is displayed again
if self._CurrentPlayer.SameAs(self._Players[0]):
self._CurrentPlayer = self._Players[1]
else:
self._CurrentPlayer = self._Players[0]
#---------
self.__DisplayState()
while not SquareIsValid:
StartSquareReference = self.__GetSquareReference("containing the piece to move")
SquareIsValid = self.__CheckSquareIsValid(StartSquareReference, True)
SquareIsValid = False
while not SquareIsValid:
FinishSquareReference = self.__GetSquareReference("to move to")
SquareIsValid = self.__CheckSquareIsValid(FinishSquareReference, False)
MoveLegal = self._CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference)
if MoveLegal:
PointsForPieceCapture = self.__CalculatePieceCapturePoints(FinishSquareReference)
self._CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))))
self._CurrentPlayer.UpdateQueueAfterMove(Choice)
self.__UpdateBoard(StartSquareReference, FinishSquareReference)
self.__UpdatePlayerScore(PointsForPieceCapture)
print("New score: " + str(self._CurrentPlayer.GetScore()) + "\n")
if self._CurrentPlayer.SameAs(self._Players[0]):
self._CurrentPlayer = self._Players[1]
else:
self._CurrentPlayer = self._Players[0]
GameOver = self.__CheckIfGameOver()
self.__DisplayState()
self.__DisplayFinalResult()
VB.NET
问题描述:目前,当被要求选择“从队列中使用 (1 到 3) 或 9 以接受选项”时,可以通过输入非整数输入来崩溃游戏。
验证用户输入,强制其为整数,以选择 1、2、3 或 9,并且如果选择了 9,则验证替换的移动选项。一个好的方法是在 Dastan
类中编写一个方法 __ValidateUserInput(PromptString)
,并在 PlayGame
中调用此方法,在需要调用的两种情况下传递所需的提示字符串。该方法应返回用户的输入。目前,我不会验证它是否为合适的整数,只验证它实际上是否为整数。为此,您需要使用 try ... except ValueError
和 while
循环。
C#
Delphi/Pascal
Java
int choice = 0; // setting a default invalid value for the choice
do {
Console.write("Choose move option to use from queue (1 to 3) or 9 to take the offer: ");
String input = Console.readLine();
if (!input.matches("[0-9]")) { // loops again if the input isn't a single numeric digit
continue;
}
choice = Integer.parseInt(input);
if (choice == 9) {
useMoveOptionOffer();
displayState();
}
} while (choice < 1 || choice > 3);
Python
- 这修复了游戏在所有输入中因非整数输入而崩溃的问题。- 它还使得当您在“输入要移动到的方格(行号后跟列号):”中输入错误的数字时,可以再次输入,直到输入所选移动的正确输入。
______________________________________________________________________________________________________________________________
def __GetSquareReference(self, Description):
### ADDED ###
SelectedSquare = 0
try:
SelectedSquare = int(input("Enter the square " + Description + " (row number followed by column number): "))
except ValueError:
pass
### END ###
return SelectedSquare
______________________________________________________________________________________________________________________________
def __UseMoveOptionOffer(self):
### ADDED ###
val = True
while val:
try:
ReplaceChoice = int(input("Choose the move option from your queue to replace (1 to 5): "))
except ValueError:
continue
if 0 < ReplaceChoice < 6:
val = False
else:
continue
### END ###
______________________________________________________________________________________________________________________________
def PlayGame(self):
GameOver = False
while not GameOver:
self.__DisplayState()
SquareIsValid = False
Choice = 0
while Choice < 1 or Choice > 3:
### ADDED ###
try:
Choice = int(input("Choose move option to use from queue (1 to 3) or 9 to take the offer: "))
except ValueError:
continue
### END ###
______________________________________________________________________________________________________________________________
def PlayGame(self):
GameOver = False
while not GameOver:
[...]
while not SquareIsValid:
FinishSquareReference = self.__GetSquareReference("to move to")
SquareIsValid = self.__CheckSquareIsValid(FinishSquareReference, False)
### ADDED ###
MoveLegal = self._CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference)
if MoveLegal == False:
SquareIsValid = False
else:
SquareIsValid = True
### END ###
______________________________________________________________________________________________________________________________
- If you notice a mistake do fix it and if any improvements can be made, please do so :)
VB.NET
问题描述:您还应该看到 Kotla 类在 GetPointsForOccupancy
方法中有一些额外的行。子类可以添加额外的属性和方法,并且可以更改现有方法。
此代码中已有的示例可用于建模另一个子类。例如,“传送门”,它会自动将任何进入它的棋子移动到某个特定点。或者“加速器”方格,它会增加玩家的分数。我不确定夏季考试会要求您做什么,但我认为可能会出现某种基于 Kotla 模型的新类。
目前,尝试一个“传送门”类,它将放置在玩家 1 的 42 号方格和玩家 2 的 35 号方格上。任何玩家自己的棋子到达此方格,都应该神奇地移动到另一位玩家的 mirza 并赢得比赛。
C#
// Add containsPortal method in Square Class
public bool ContainsPortal()
{
if (Symbol.ToLowerInvariant() == "p")
{
return true;
}
else
{
return false;
}
}
// Create Portal class inheriting from Square
class Portal : Square
{
public Portal(Player p, string s)
{
BelongsTo = p;
Symbol = s;
}
}
// Change Updateboard in DASTAN CLASS
private void UpdateBoard(int startSquareReference, int finishSquareReference)
{
board[GetIndexOfSquare(finishSquareReference)].SetPiece(board[GetIndexOfSquare(startSquareReference)].RemovePiece());
// Get square instance as opposed to piece
Square finishSquare = board[GetIndexOfSquare(finishSquareReference)];
if (finishSquare.ContainsPortal() && finishSquare.GetBelongsTo().SameAs(currentPlayer))
{
// Iterate through each cell to find an opponent player 'mirza' piece
for (int row = 1; row <= noOfRows; row++)
{
for (int col = 1; col <= noOfColumns; col++)
{
Piece piece = board[GetIndexOfSquare(row * 10 + col)].GetPieceInSquare();
if (piece != null && piece.GetTypeOfPiece() == "mirza" && !piece.GetBelongsTo().SameAs(currentPlayer))
{
startSquareReference = finishSquareReference;
finishSquareReference = row * 10 + col;
// Move piece again, from portal to mirza square
board[GetIndexOfSquare(finishSquareReference)].SetPiece(board[GetIndexOfSquare(startSquareReference)].RemovePiece());
}
}
}
}
}
private void CreateBoard()
{
Square s;
board = new List<Square>();
for (int row = 1; row <= noOfRows; row++)
{
for (int column = 1; column <= noOfColumns; column++)
{
if (row == 1 && column == noOfColumns / 2)
{
s = new Kotla(players[0], "K");
}
else if (row == noOfRows && column == noOfColumns / 2 + 1)
{
s = new Kotla(players[1], "k");
// Create portals here
}
else if (row == 4 && column == 3)
{
s = new Portal(players[0], "P");
}
else if (row == 3 && column == 5)
{
s = new Portal(players[1], "P");
}
else
{
s = new Square();
}
board.Add(s);
}
}
}
Delphi/Pascal
Java
// Add containsPortal method in Square Class
public boolean containsPortal() {
if (symbol.toLowerCase(Locale.ROOT) == "p") {
return true;
} else {
return false;
}
}
// Create Portal class inheriting from Square
class Portal extends Square {
public Portal(Player p, String s) {
super();
belongsTo = p;
symbol = s;
}
}
// change Updateboard in DASTAN CLASS
private void updateboard(int startSquareReference, int finishSquareReference) {
board.get(getIndexOfSquare(finishSquareReference)).setPiece(board.get(getIndexOfSquare(startSquareReference)).removePiece());
// get square instance as opposed to piece
Square finishSquare = board.get(getIndexOfSquare(finishSquareReference));
if (finishSquare.containsPortal() && finishSquare.getBelongsTo().sameAs(currentPlayer)) {
// iterate through each cell to find a oppenent player 'mirza' piece
for (int row=1; row <= noOfRows; row++){
for (int col=1; col <= noOfColumns; col ++) {
Piece piece = board.get(getIndexOfSquare(row*10+ col)).getPieceInSquare();
if (piece != null && piece.getTypeOfPiece()== "mirza" && !piece.getBelongsTo().sameAs(currentPlayer)) {
startSquareReference = finishSquareReference;
finishSquareReference = row*10 + col;
// move piece again, from portal to mirza square
board.get(getIndexOfSquare(finishSquareReference)).setPiece(board.get(getIndexOfSquare(startSquareReference)).removePiece());
}
}
}
}
}
private void createBoard() {
Square s;
board = new ArrayList<>();
for (int row = 1; row <= noOfRows; row++) {
for (int column = 1; column <= noOfColumns; column++) {
if (row == 1 && column == noOfColumns / 2) {
s = new Kotla(players.get(0), "K");
} else if (row == noOfRows && column == noOfColumns / 2 + 1) {
s = new Kotla(players.get(1), "k");
// create portals here
} else if (row == 4 && column == 3) {
s = new Portal(players.get(0), "P");
} else if (row == 3 && column == 5) {
s = new Portal(players.get(1), "P");
} else {
s = new Square();
}
board.add(s);
}
}
}
Python
______________________________________________________________________________________________________________________________
# Added to Square class
def ContainsPortal(self):
if self._Symbol == 'P' or self._Symbol == 'p':
return True
else:
return False
______________________________________________________________________________________________________________________________
class Portal(Square):
def __init__(self, P, S):
super(Portal, self).__init__()
self._BelongsTo = P
self._Symbol = S
# No additional methods are needed as all definitions from Square will work here
______________________________________________________________________________________________________________________________
# In Dastan class
def __UpdateBoard(self, StartSquareReference, FinishSquareReference):
# Original movement code
self._Board[self.__GetIndexOfSquare(FinishSquareReference)].SetPiece(self._Board[self.__GetIndexOfSquare(StartSquareReference)].RemovePiece())
# Added to handle Portal
FinishSquare = self._Board[self.__GetIndexOfSquare(FinishSquareReference)]
if FinishSquare.ContainsPortal() and FinishSquare.GetBelongsTo().SameAs(self._CurrentPlayer):
for Row in range(1, self._NoOfRows + 1):
for Column in range(1, self._NoOfColumns + 1):
Piece = self._Board[self.__GetIndexOfSquare(Row*10 + Column)].GetPieceInSquare()
if Piece is not None and Piece.GetTypeOfPiece() == "mirza" and not Piece.GetBelongsTo().SameAs(self._CurrentPlayer):
StartSquareReference = FinishSquareReference
FinishSquareReference = Row * 10 + Column
# Change player score here as __UpdateBoard is called after checking points for piece capture in PlayGame,
# so points for capturing the Mirza wouldn't be awarded otherwise
# It's also possible for an opponent's piece to be on your portal, meaning you capture two pieces in one turn
# Having this check here means the points for both get added
self._CurrentPlayer.ChangeScore(self.__CalculatePieceCapturePoints(FinishSquareReference))
# This is also why we move twice, once at the start (moving to the portal) and once here (moving to the opponent's Mirza)
self._Board[self.__GetIndexOfSquare(FinishSquareReference)].SetPiece(self._Board[self.__GetIndexOfSquare(StartSquareReference)].RemovePiece())
______________________________________________________________________________________________________________________________
def __CreateBoard(self):
for Row in range(1, self._NoOfRows + 1):
for Column in range(1, self._NoOfColumns + 1):
if Row == 1 and Column == self._NoOfColumns // 2:
S = Kotla(self._Players[0], "K")
elif Row == self._NoOfRows and Column == self._NoOfColumns // 2 + 1:
S = Kotla(self._Players[1], "k")
# Create portals here
# I used a symbol of `P` for player 1's and `p` for player 2's
elif Row == 4 and Column == 2:
S = Portal(self._Players[0], 'P')
elif Row == 3 and Column == 5:
S = Portal(self._Players[1], 'p')
else:
S = Square()
self._Board.append(S)
______________________________________________________________________________________________________________________________
VB.NET
潜在问题
使棋盘更大(或允许用户设置棋盘大小)。
用户帮助/信息/教程页面。
新的类允许保存和加载游戏。
限制一个回合内可以接受选项的次数。
撤销移动选项。
在结束时显示分数。
不是生成一个选项,而是为用户生成三个随机选项供选择。
C#
.
Delphi/Pascal
.
Java
.
Python
def PlayGame(self):
GameOver = False
while not GameOver:
self.__DisplayState()
SquareIsValid = False
Choice = 0
if self._CurrentPlayer.GetScore()<=0: # Here it does the check if the score is below zero while playing the game
self.__DisplayFinalResult()
exit()
while Choice < 1 or Choice > 3:
if self._CurrentPlayer.GetScore() <=0: #Here it does the check while taking an offer
self.__DisplayFinalResult()
exit()
Choice = int(input("Choose move option to use from queue (1 to 3) or 9 to take the offer: "))
if Choice == 9:
self.__UseMoveOptionOffer()
self.__DisplayState()
while not SquareIsValid:
StartSquareReference = self.__GetSquareReference("containing the piece to move")
SquareIsValid = self.__CheckSquareIsValid(StartSquareReference, True)
SquareIsValid = False
while not SquareIsValid:
FinishSquareReference = self.__GetSquareReference("to move to")
SquareIsValid = self.__CheckSquareIsValid(FinishSquareReference, False)
MoveLegal = self._CurrentPlayer.CheckPlayerMove(Choice, StartSquareReference, FinishSquareReference)
if MoveLegal:
PointsForPieceCapture = self.__CalculatePieceCapturePoints(FinishSquareReference)
self._CurrentPlayer.ChangeScore(-(Choice + (2 * (Choice - 1))))
self._CurrentPlayer.UpdateQueueAfterMove(Choice)
self.__UpdateBoard(StartSquareReference, FinishSquareReference)
self.__UpdatePlayerScore(PointsForPieceCapture)
print("New score: " + str(self._CurrentPlayer.GetScore()) + "\n")
if self._CurrentPlayer.SameAs(self._Players[0]):
self._CurrentPlayer = self._Players[1]
else:
self._CurrentPlayer = self._Players[0]
GameOver = self.__CheckIfGameOver()
self.__DisplayState()
self.__DisplayFinalResult()
#Dimi
.
VB.NET