北京邮电大学智能卡实验报告.docx
- 文档编号:5362945
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:33
- 大小:146.64KB
北京邮电大学智能卡实验报告.docx
《北京邮电大学智能卡实验报告.docx》由会员分享,可在线阅读,更多相关《北京邮电大学智能卡实验报告.docx(33页珍藏版)》请在冰豆网上搜索。
北京邮电大学智能卡实验报告
智能卡实验报告
一.需求分析
1.身份反馈:
收到00a00000000f时反馈由十位学号及6位的姓名组成的代码,所以我的是:
2013211209C1F5B2A9CEC5。
2.初始化:
创立3X3棋盘存储空间(9位数组)默认存储为00,人下棋存储为01,电脑下棋存储为02。
3.确定先后手:
确定是人或机器先下。
4.下棋过程:
人将存储空间中数据更改为1,计算机将存储空间中数据更改为2。
5.判断棋局情况:
判断此时棋局状态为哪一方胜、和棋或者尚未完成。
6.纠错机制:
已下过棋子的位置,不允许重复落子。
7.结束:
当判断棋局状态为哪一方胜或者和棋时,游戏结束。
二.设计
1.系统结构图
2.子程序流程图
身份反馈流程图初始化流程图
人类先手流程图人类后手流程图
3.APDU命令设计
表1APDU命令设计
功能
CLA
INS
P1
P2
LC
DATA
LE
返回值
身份反馈
00
a0
00
00
—
—
10
2013211209C1F5B2A9CEC5
初始化
00
a1
03
00/01
—
—
02
009000—成功电脑先手选00
099000—成功人先手
6301—初始化失败
下棋
00
a2
00-08
00
—
—
02
039000成功落子未完
019000成功落子人赢了
—
029000成功落子电脑赢了
009000成功落子和局
—
6301落子失败
打印棋盘
00
a3
00
00
—
—
08
0x0x0x0x0x0x0x0x0x
棋盘情况
三.测试数据
1.选中
/select627570742e67616d652e31选中
2.身份反馈
/send00a0000010身份反馈
3.初始化
/send00a1030002初始化棋盘并人先手
/send00a1030102初始化棋盘并AI先手
4.落字
/send00a2000002落字到左上角
/send00a2010002落字到第一行中间
/send00a2020002落字到右上角
/send00a2030002落字到中间靠左
/send00a2040002落字到中心
/send00a2050002落字到中间靠右
/send00a2060002落字到左下角
/send00a2070002落字到最后一行中心
/send00a2080002落字到右下角
5.打印棋盘
/send00a3000008
四.关键代码
1.游戏初始化模块:
privatevoidinit(APDUapdu){
byte[]buffer=apdu.getBuffer();
//棋盘维数——报错
if(buffer[ISO7816.OFFSET_P1]!
=(byte)0x03){
ISOException.throwIt(WRONG_P1);
}
//先后手——报错
if(buffer[ISO7816.OFFSET_P2]!
=(byte)0x01){
if(buffer[ISO7816.OFFSET_P2]!
=(byte)0x00)
ISOException.throwIt(WRONG_P2);
}
//将临时变量放进RAM
board=JCSystem.makeTransientByteArray((short)9,JCSystem.CLEAR_ON_DESELECT);
order=JCSystem.makeTransientByteArray((short)1,JCSystem.CLEAR_ON_DESELECT)[0];
turn=JCSystem.makeTransientByteArray((short)1,JCSystem.CLEAR_ON_DESELECT)[0];
depth=JCSystem.makeTransientByteArray((short)1,JCSystem.CLEAR_ON_DESELECT)[0];
answer=JCSystem.makeTransientByteArray((short)1,JCSystem.CLEAR_ON_DESELECT)[0];
result=JCSystem.makeTransientByteArray((short)1,JCSystem.CLEAR_ON_DESELECT)[0];
this.board[0]=(byte)0x00;
this.board[1]=(byte)0x00;
this.board[2]=(byte)0x00;
this.board[3]=(byte)0x00;
this.board[4]=(byte)0x00;
this.board[5]=(byte)0x00;
this.board[6]=(byte)0x00;
this.board[7]=(byte)0x00;
this.board[8]=(byte)0x00;
this.depth=(byte)0x09;
this.order=(byte)0x09;
this.turn=(byte)0x09;
this.answer=(byte)0x09;
this.result=(byte)0x09;
order=buffer[ISO7816.OFFSET_P2];
if(order==0x01){turn=chessCom;play(apdu);return;}
turn=chessHum;
shortle=apdu.setOutgoing();
//if(le<(byte)1){
//ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
//}
apdu.setOutgoingLength((byte)1);
buffer[0]=(byte)0x09;
apdu.sendBytes((short)0,(short)1);
}
2.身份反馈模块
//个人信息--00a000000f
privatevoidmy_info(APDUapdu){
byte[]buffer=apdu.getBuffer();
shortle=apdu.setOutgoing();
//if(le<(byte)15){
//ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
//}
apdu.setOutgoingLength((byte)16);
//学号,姓名
buffer[0]=0x32;
buffer[1]=0x30;
buffer[2]=0x31;
buffer[3]=0x33;
buffer[4]=0x32;
buffer[5]=0x31;
buffer[6]=0x31;
buffer[7]=0x30;
buffer[8]=0x34;
buffer[9]=0x39;
buffer[10]=(byte)0xc1;
buffer[11]=(byte)0xf5;
buffer[12]=(byte)0xb2;
buffer[13]=(byte)0xa9;
buffer[14]=(byte)0xce;
buffer[15]=(byte)0xc5;
apdu.sendBytes((short)0,(short)16);
}
3.对战入口模块
privatevoidplay(APDUapdu){
byte[]buffer=apdu.getBuffer();
if(board[buffer[ISO7816.OFFSET_P1]]!
=0){ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);return;}
if(turn==chessHum)//该人下
{
move(turn,buffer[ISO7816.OFFSET_P1]);//第二个参数是人的下棋位置
check();
if(result!
=0x00){
shortle=apdu.setOutgoing();
//if(le<(byte)2){
//ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
//}
apdu.setOutgoingLength((byte)2);
buffer[0]=result;
buffer[1]=answer;
apdu.sendBytes((short)0,(short)2);
return;}}
if(turn==chessCom){//电脑下
//*********************************************************查表
//*********************************************************查表
}
move(turn,answer);//刷新棋盘
check();//检查状态
if(depth==8){
shortle=apdu.setOutgoing();
//if(le<(byte)2){
//ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
//}
apdu.setOutgoingLength((byte)1);
buffer[0]=answer;
apdu.sendBytes((short)0,(short)1);}
else
{
shortle=apdu.setOutgoing();
//if(le<(byte)2){
//ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
//}
apdu.setOutgoingLength((byte)2);
buffer[0]=result;
buffer[1]=answer;
apdu.sendBytes((short)0,(short)2);}
}
4.检查棋盘状态模块
//检查棋盘状态的入口
privatevoidcheck(){
if(win(chessHum)){result=(byte)0x02;return;}
if(win(chessCom)){result=(byte)0x01;return;}
if(draw()){result=(byte)0x03;return;}
elseresult=(byte)0x00;return;
}
//胜负
privatebooleanwin(byteturn){
if(board[0]==turn&&board[1]==turn&&board[2]==turn)
returntrue;
if(board[3]==turn&&board[4]==turn&&board[5]==turn)
returntrue;
if(board[6]==turn&&board[7]==turn&&board[8]==turn)
returntrue;
if(board[0]==turn&&board[3]==turn&&board[6]==turn)
returntrue;
if(board[1]==turn&&board[4]==turn&&board[7]==turn)
returntrue;
if(board[2]==turn&&board[5]==turn&&board[8]==turn)
returntrue;
if(board[0]==turn&&board[4]==turn&&board[8]==turn)
returntrue;
if(board[2]==turn&&board[4]==turn&&board[6]==turn)
returntrue;
elsereturnfalse;
}
//平局
privatebooleandraw(){
if(depth==(byte)0)//剩余步数为零
returntrue;
returnfalse;}
//**************************************************************检查棋局
5.打印棋盘模块
//打印棋盘
privatevoidqipan_info(APDUapdu){
byte[]buffer=apdu.getBuffer();
shortle=apdu.setOutgoing();
//if(le<(byte)8){
//ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
//}
apdu.setOutgoingLength((byte)9);
//棋盘信息
buffer[0]=board[0];
buffer[1]=board[1];
buffer[2]=board[2];
buffer[3]=board[3];
buffer[4]=board[4];
buffer[5]=board[5];
buffer[6]=board[6];
buffer[7]=board[7];
buffer[8]=board[8];
apdu.sendBytes((short)0,(short)9);
}
五.测试结果
1.身份反馈测试
输入
返回
send00a00000000f
2013211209C1F5B2A9CEC5
2.人先手测试
输入
返回
send00a103010001
099000
send00a204000002
00089000
send00a201000002
00079000
send00a205000002
02069000
3.卡先手测试
输入
返回
send00a103000001
069000
send00a204000002
00049000
send00a201000002
00009000
send00a205000002
02089000
4.报错测试
输入
返回
send00a1030002000001
6700
send00a102000001
6A86
send00a303000001
6D00
卡片下载不足时
6A84
直接send00a103000001
6E00
send00a203000002
6A88
send00a201000001
6A89
六.团队协作与个人分工
1.团队
通过小组讨论,确定通过一定的策略进行棋谱扫描。
团队公用模板,具体算法实现,各自完成。
2.个人完成
1)自行设计策略算法
先手优先抢角,后手优先占中。
2)所有代码由个人独自完成
3)团队的查表核心部分共633行由我撰写
附录:
中间省略的由我撰写查表核心代码
//*********************************************************查表
if(depth==9){//先手第一步
answer=(byte)0;}
if(depth==7){//先手第二步
if(board[4]!
=chessHum)
{answer=(byte)4;}
else
{answer=(byte)2;}
}
if(depth==5){//先手第三步
if(board[4]==chessCom)//抢到中
{if(board[8]!
=chessHum)//能赢就赢
answer=(byte)8;
else//否则走一步最有利
{if((board[8]==chessHum)&&(board[7]==chessHum))
answer=(byte)6;
if((board[8]==chessHum)&&(board[5]==chessHum))
answer=(byte)2;
if((board[8]==chessHum)&&(board[6]==chessHum))
answer=(byte)7;
if((board[8]==chessHum)&&(board[2]==chessHum))
answer=(byte)5;
if((board[8]==chessHum)&&(board[3]==chessHum))
answer=(byte)7;
if((board[8]==chessHum)&&(board[1]==chessHum))
answer=(byte)3;
}
}
else//占了0和2角,人已占中
{if(board[1]!
=chessHum)//能赢就赢
answer=(byte)1;
else//被堵
answer=(byte)7;
}
}
if(depth==3){//先手第四步
if((board[0]==chessCom)&&(board[4]==chessCom)&&(board[6]==chessCom))//必赢
{if(board[2]!
=chessHum)
answer=(byte)2;
else
answer=(byte)3;
}
if((board[0]==chessCom)&&(board[2]==chessCom)&&(board[4]==chessCom))//必赢
{if(board[1]!
=chessHum)
answer=(byte)1;
else
answer=(byte)6;
}
if((board[0]==chessCom)&&(board[4]==chessCom)&&(board[7]==chessCom)&&(board[8]==chessHum)&&(board[6]==chessHum))
{if(board[1]!
=chessHum)//能赢就赢
answer=(byte)1;
else
answer=(byte)5;//最后搏一搏人不下3
}
if((board[0]==chessCom)&&(board[5]==chessCom)&&(board[4]==chessCom))
{if(board[3]!
=chessHum)//能赢就赢
answer=(byte)3;
else
answer=(byte)1;//最后搏一搏人不下7
}
if((board[0]==chessCom)&&(board[4]==chessCom)&&(board[7]==chessCom)&&(board[8]==chessHum)&&(board[3]==chessHum))
{if(board[1]!
=chessHum)//能赢就赢
answer=(byte)1;
else
answer=(byte)2;//最后搏一搏人不下6
}
if((board[0]==chessCom)&&(board[3]==chessCom)&&(board[4]==chessCom))//必赢
{if(board[5]!
=chessHum)
answer=(byte)5;
else
answer=(byte)6;
}
if((board[0]==chessCom)&&(board[2]==chessCom)&&(board[7]==chessCom)&&(board[1]==chessHum)&&(board[4]==chessHum))//机器抢完两角,人抢中堵后
{if(board[3]==chessHum)
answer=(byte)5;//准备下8否则和棋
if(board[5]==chessHum)
answer=(byte)3;//准备下6否则和棋
if(board[6]==chessHum)
answer=(byte)5;//准备下8否则和棋
if(board[8]==chessHum)
answer=(byte)3;//准备下6否则和棋
}
}
if(depth==1){//先手第五步
if((board[0]==chessCom)&&(board[4]==chessCom)&&(board[7]==chessCom)&&(board[5]==chessCom))
{if(board[3]!
=chessHum)
answer=(byte)3;//人未下3,赢了
else
answer=(byte)2;//人堵3,和棋
}
if((board[0]==chess
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 北京邮电 大学 智能卡 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)