Flutter从头到尾设计一款简单的五子棋游戏(三) 具体代码设计_flutter 游戏源码(1)

abstract class ChessShape{  
  int? _shape;  

  int get shape => _shape!;  

  set shape(int value) {  
    _shape = value;  
  }  
}

ChessShape是一个抽象类,定义了其返回的形状类型。这里我们使用1代表圆,0代表方。具体的类设计如下:

class CircleShape extends ChessShape{  
  CircleShape(){  
    shape = 1;  
  }  
}

class RectShape extends ChessShape{  
  RectShape(){  
    shape = 2;  
  }  
}

玩家状态的切换——状态模式

游戏玩家状态的可以进行切换,如是否可以悔棋等,因此在这里使用了状态模式。

玩家类设计如下:

class UserContext {  

  late State _state;  

  State get state => _state;  

  UserContext(){  
    _state = StartState(this);  
  }  

  play() {  
    _state.play();  
  }  

  //悔棋只能悔棋三次  
  bool regretChess() {  
    return _state.regretChess();  
  }  

  // 认输 10步之内不能认输  
  bool surr
  • 30
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值