北京邮电大学智能卡实验报告_第1页
北京邮电大学智能卡实验报告_第2页
北京邮电大学智能卡实验报告_第3页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、智能卡实验报告一. 需求分析1. 身份反馈:收到00 a0 00 00 00 Of时反馈由十位学号及6位的姓名组成的代码,所以我的 是:2013211209C1F5B2A9CEC52. 初始化:创立3X3棋盘存储空间(9位数组)默认存储为00,人下棋存储为01,电脑下 棋存储为02。3. 确定先后手:确定是人或机器先下。4. 下棋过程:人将存储空间中数据更改为 1,计算机将存储空间中数据更改为 2。5. 判断棋局情况:判断此时棋局状态为哪一方胜、和棋或者尚未完成。6. 纠错机制:已下过棋子的位置,不允许重复落子。7. 结束:当判断棋局状态为哪一方胜或者和棋时,游戏结束。二. 设计1. 系统结构

2、图Tic-Tac-Toe身份反馈Go!初始化对局博弈过程2. 子程序流程图开始*接收身枱反櫃命令是否垃0000 00 00 Of足返回结束*按收初蛉化酣令开曹冇储空间将打储空冋中的値初姑比为0读取先后丁信忌f人是再先手否进入机齬先干诲句是进入机器后手培旬结衷身份反馈流程图初始化流程图Jf址是台牯字检已有觀王足 报fH, 聪超ttU旳祖f否舒柿布储为改为I甬子jtHaTtt-WJTfflT*V 岳理 Afi *储出平JG<也*S養VJ&命臨 A是 * MiH幅OAr否鑒是杏Sb知<电irt岀4n囲罰信聽y丽 N人类先手流程图JTW悯如蘇牆f側人bW=貳施7慣L可橄宁足秋林.起

3、定他直已齐肝杏*t«>TS.-就出干局伍否ir旱 4勲屮沬完儔息否宝是書先手11 => *地t机劃±利信层杏融:皿(1利仁曳緖朿人类后手流程图3. APDU命令设计表1 APDU命令设计功能CLAINSP1P2LCDATALE返回值身份反馈00a00000一一1C1F5B2A9CEC5初始化00a10300/01一一0200 90 00 成功 电脑先手选 0009 90 00 成功 人先手63 01 初始化失败一03 9000成功落子未完01 9000成功落子人赢了下棋00a200-0800一一0202 9000成功落子电脑赢了00 9000成功落子和局一630

4、1落子失败打印棋盘00a30000一一080x 0x 0x 0x 0x 0x 0x 0x 0x棋盘情况三. 测试数据1. 选中/select 627570742e67616d652e31 选中2. 身份反馈/send 00a0000010身份反馈3. 初始化/send 00a1030002/send 00a10301024. 落字/send 00a2000002/send 00a2010002/send 00a2020002/send 00a2030002/send 00a2040002/send 00a2050002/send 00a2060002/send 00a2070002/send

5、00a20800025. 打印棋盘/send 00a3000008初始化棋盘并人先手 初始化棋盘并 AI 先手落字到左上角 落字到第一行中间 落字到右上角 落字到中间靠左 落字到中心 落字到中间靠右 落字到左下角 落字到最后一行中心 落字到右下角四 . 关键代码1. 游戏初始化模块:private void init(APDU apdu)byte buffer = apdu.getBuffer();/ 棋盘维数 报错 if (bufferISO7816.ISOException.OFFSET_P1 != ( byte )0x03) throwIt ( WRONG_P1);/ 先后手 报错 if

6、 (bufferISO7816.OFFSET_P2 != ( byte )0x01)if (bufferISO7816.OFFSET_P2 != ( byte )0x00)ISOException. throwIt ( WRONG_P2); / 将临时变量放进 RAMboard =JCSystem. order =JCSystem.CLEAR_ON_DESELECT );CLEAR_ON_DESELECT )0;makeTransientByteArraymakeTransientByteArray( short ) 9,JCSystem.( short )1,JCSystem.turn =J

7、CSystem. makeTransientByteArray depth =JCSystem. makeTransientByteArray( short )1,JCSystem.( short )1,JCSystem.CLEAR_ON_DESELECT )0;CLEAR_ON_DESELECT )0;answer =JCSystem. makeTransientByteArray( short )1,JCSystem. CLEAR_ON_DESELECT )0;result =JCSystem.makeTra nsie ntByteArray(short )1,JCSystem. CLEA

8、R_ON_DESELECT )0;this.board 0=(byte )0x00;this.board1=(byte)0x00;this.board2=(byte)0x00;this.board3=(byte)0x00;this.board4=(byte)0x00;this.board5=(byte)0x00;this.board=(byte)0x00;this.board7=(byte)0x00;this.board8=(byte)0x00;this.depth=(byte)0x09;this.order=(byte)0x09;this.turn =(byte)0x09;this.answ

9、er=(byte )0x09;this.result=(byte )0x09;order =bufferISO7816.OFFSET_P2 ;return ;if ( order =0x01)turn =chessCom ;play(apdu);turn =chessHum ;shortle_= apdu.setOutgo in g();if(le < (byte)1)/ ISOExceptio n.throwlt(ISO7816.SW_WRONG_LENGTH);apdu.setOutgoingLength(byte )1);buffer0=( byte )0x09;apdu.se n

10、dBytes( short )0, ( short )1);2. 身份反馈模块/ 个人信息-OOaO 0000 0fprivate void my_in fo(APDU apdu)byte buffer = apdu.getBuffer();shortle_= apdu.setOutgo in g();if(le < (byte)15)/ ISOExceptio n.throwlt(ISO7816.SW_WRONG_LENGTH);/apdu.setOutgoingLength(byte )16);/学号,姓名buffer0= 0x32;buffer1= 0x30;buffer2= 0x

11、31;buffer3= 0x33;buffer4= 0x32;buffer5= 0x31;buffer6= 0x31;buffer7= 0x30;buffer8= 0x34;buffer9= 0x39;buffer10=(byte)0xc1;buffer11=(byte)0xf5;buffer12=(byte)0xb2;buffer13=(byte)0xa9;buffer14=(byte)0xce;buffer15=(byte)0xc5;apdu.se ndBytes(short )0, ( short )16);3. 对战入口模块private void play(APDU apdu)byt

12、e buffer = apdu.getBuffer();throwIt (ISO7816. SW_INS_NOT_Sif ( board bufferISO7816.OFFSET_P1 川=0)ISOException.UPPORTED); return ;if ( turn =chessHum ) / 该人下move( turn ,bufferISO7816.OFFSET_P1 );/ 第二个参数是人的下棋位置check();if ( result !=0x00)shortle_= apdu.setOutgo in g();if(le < (byte)2)/ ISOExceptio n

13、. throwlt(ISO7816.SW_WRONG_LENGTH);apdu.setOutgoingLength(byte )2);buffer0=result;buffer1=answer ;apdu.se ndBytes(short )0, ( short )2);return ;if (turn = chessCom ) / 电脑下/*/*查表查表move(turn , answer ); / 刷新棋盘check(); II检查状态if ( depth =8)short le = apdu.setOutgo in g();if(le < (byte)2)/ISOExceptio

14、n.throwlt(ISO7816.SW_WRONG_LENGTH);apdu.setOutgoingLength(byte )1);buffer0= answer ;apdu.se ndBytes( short )0, ( short )1);else shortle_= apdu.setOutgo in g();if(le < (byte)2)IIISOExceptio n.throwlt(ISO7816.SW_WRONG_LENGTH);IIapdu.setOutgoingLength(byte )2);buffer0=result;buffer1=answer ;apdu.se

15、ndBytes( short )0, ( short )2);4. 检查棋盘状态模块privatevoidcheck()if(win(chessHum )result=(byte)0x02;returnif(win(chessCom )result=(byte)0x01;returnif(draw()result=(byte)0x03;return;elseresult =( byte)0x00;returnJII检查棋盘状态的入口;II胜负privateboolea n win( bytetur n)if(board0=tur n &&board1=tur n &&a

16、mp;board2=turn)returntrue ;if(board3=tur n &&board4=tur n &&board5=turn)returntrue ;if(board6=tur n &&board7=tur n &&board8=turn)returntrue ;if(board0=tur n &&board3=tur n &&board6=turn)returntrue ;if(board1=tur n &&board4=tur n &&board

17、7=turn)returntrue ;if(boardreturn2=tur n &&true ;board5=tur n &&board8=turn)if(board0=tur n &&board4=tur n &&board8=turn)returntrue ;if(board2=tur n &&board4=tur n &&board6=turn)returntrue ;else return false ;/平局private boolea n draw()i f (depth =( byt

18、e )0) / 剩余步数为零 return true ;return false ;检查棋局/*5. 打印棋盘模块/打印棋盘private void qipa n_in fo(APDU apdu)byte buffer = apdu.getBuffer();short le_= apdu.setOutgo in g();/if(le < (byte)8)/ISOExceptio n.throwlt(ISO7816.SW_WRONG_LENGTH);/apdu.setOutgoingLength(byte )9);/棋盘信息bufferO=buffer1=buffer2=buffer3=b

19、uffer4=buffer5=buffer6=buffer7=buffer8=board 0;board 1;board 2;board 3;board 4;board 5;board 6;board 7;board 8;apdu.se ndBytes(五.测试结果short )0, ( short )9);1. 身份反馈测试返回输入send OOaOOOOOOOOf2013211209C1F5B2A9CEC52. 人先手测试输入返回send 00a10301000109 90 00send 00a20400000200 08 90 00send 00a20100000200 07 90 00

20、send 00a20500000202 06 90 003. 卡先手测试输入返回send 00a10300000106 90 00send 00a20400000200 04 90 00send 00a20100000200 00 90 00send 00a20500000202 08 90 004. 报错测试输入返回send 00a116700send 00a1020000016A86send 00a3030000016D00卡片下载不足时6A84直接 send 00a1030000016E00send 00a2030000026A88send 00a2010000016A89六.团队协作与

21、个人分工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= c

22、hessCom ) / 抢到中 if ( board 8!= chessHum ) / 能赢就赢 answer =( byte )8;else/否则走步最有利 if(board=chessHum)&&(board7=chessHum)an swer=(byte)6;if(board8=chessHum)&&(board5=chessHum)an swer=(byte)2;if(board8=chessHum)&&(boardQ=chessHum)answer =( byte )7;if(board 8=chessHum)&&(bo

23、ard2= chessHum)an swer =:(byte )5;if(board 8=chessHum)&&(board3= chessHum)an swer =:(byte )7;if(board 8=chessHum)&&(board1= chessHum)answer =( byte )3;else /占了 0和2角,人已占中 if ( board 1!= chessHum ) / 能赢就赢answer =( byte )1;else /被堵answer =( byte )7;if (depth =3) /先手第四步if ( board 0= ches

24、sCom )&&( board 4=chessCom )&&( board 6= chessCom ) / 必赢if ( board 2!= chessHum ) answer =( byte )2;elseanswer =( byte )3;if ( board 0=chessCom )&&( board 2=chessCom )&&( board4=chessCom ) / 必赢 if ( board 1!= chessHum ) answer =( byte )1;elseanswer =( byte )6; if ( bo

25、ard 0= chessCom )&&( board 4=chessCom )&&( board7=chessCom )&&( board8= chessHum )&&( board 6= chessHum ) if ( board 1!= chessHum ) / 能赢就赢 answer =( byte )1;elseanswer =( byte )5; / 最后搏一搏人不下 3 chessCom )if ( board 0= chessCom )&&( board 5= chessCom )&&

26、( board 4= if ( board 3!= chessHum ) / 能赢就赢 answer =( byte )3;elseanswer =( byte )1; / 最后搏一搏人不下 7 if ( board 0= chessCom )&&( board 4= chessCom )&&( board 7=chessCom )&&( board8= chessHum )&&( board 3= chessHum ) if ( board 1!= chessHum ) / 能赢就赢 answer =( byte )1;else

27、answer =( byte )2; / 最后搏一搏人不下 6 if ( board 0= chessCom )&&( board 3=chessCom )&&( board4=chessCom ) / 必赢 if ( board 5!= chessHum ) answer =( byte )5;elseanswer =( byte )6; if ( board 0= chessCom )&&( board 2=chessCom )&&( board7=chessCom )&&( board1= chessHum

28、)&&( 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=ch

29、essCom )&&( board 4= chessCom )&&( board 7=chessCom )&&( board5= chessCom )if ( board 3!= chessHum )answer =( byte )3; / 人未下 3,赢了 elseanswer =( byte )2; / 人堵 3,和棋 if ( board 0=chessCom )&&( board 4= chessCom )&&( board 1=chessCom )&&( board5= chessCom

30、)if ( board 7!= chessHum )answer =( byte )7; / 人未下 7,赢了 elseanswer =( byte )6; / 人堵 7 ,和棋if ( board 0=chessCom )&&( board 4= chessCom )&&( board 7=chessCom )&&( board2= chessCom )if ( board 匸 chessHum )answer =( byte )6; / 人未下 6,赢了 elseanswer =( byte )5; / 人堵 7 ,和棋if ( board

31、0= chessCom )&&( board 2= chessCom )&&( board 3=chessCom )&&( board7= chessCom )&&( board 5= chessHum )if ( board 匸 chessHum )answer =( byte )6; / 人未下 6,赢了 elseanswer =( byte )8; / 人堵 6,和棋if ( board 0= chessCom )&&( board 2= chessCom )&&( board 3=chess

32、Com )&&( board7= chessCom )&&( board 8= chessHum )if ( board 匸 chessHum )answer =( byte )6; / 人未下 6,赢了 elseanswer =( byte )5; / 人堵 6,和棋if ( board 0= chessCom )&&( board 2= chessCom )&&( board 5= chessCom )&&( board 7= c hessCom )&&( board 3= chessHum )

33、if ( board 8!= chessHum )answer =( byte )8; / 人未下 8,赢了elseanswer =( byte )6; / 人堵 8,和棋if ( board 0= chessCom )&&( board 2= chessCom )&&( board 5= chessCom )&&( board 7= chessCom )&&( board =chessHum )if ( board 8!=chessHum )answer =( byte )8; / 人未下 8,赢了elseanswer =( b

34、yte )3; / 人堵 8,和棋 if ( depth =8) / 后手第一步if ( board 4!= chessHum ) answer =( byte )4;else answer =( byte )0;if (depth =6) / 后手第二步if ( board 4= chessCom ) /人未下中心,机器抢中心后的第二步 /单角0角if ( board 0= chessHum answer =( byte )2;if ( board 0= chessHum answer =( byte )6;if ( board 0= chessHum answer =( byte )6;i

35、f ( board 0= chessHum)&&(board1=chessHum)&&(board3=chessHum)&&(board7=chessHum)&&(board5=chessHum) answer =( byte )2;2角if ( board 2=chessHum )&&( board1=chessHum )if ( board 2=chessHum)&&(board5= answer =( byte)8;if ( board 2=chessHum)&&(board3

36、= answer =( byte)0;if ( board 2=chessHum)&&(board7= answer =( byte)8;/6 角if ( board 6=chessHum)&&(board3= answer =( byte)0;if ( board 6=chessHum)&&(board7= answer =( byte)8;if ( board 6=chessHum)&&(board1= answer =( byte)0;if ( board 6=chessHum)&&(board5= answ

37、er =( byte)8;/8 角if ( board 8=chessHum)&&(board7= answer =( byte)6;if ( board 8=chessHum)&&(board5= answer =( byte)2;if ( board 8=chessHum)&&(board3= answer =( byte)6;if ( board 8=chessHum)&&(board1= answer =( byte)2;/ 双角if ( board 0=chessHum)&&(board2= answer

38、 =( byte)1;if ( board 0=chessHum)&&(board6= answer =( byte)3;if ( board 0=chessHum)&&(board8= answer =( byte)1;if ( board 2=chessHum)&&(board6= answer =( byte)3;if ( board 2=chessHum)&&(board8= answer =( byte)5;if ( board 6=chessHum)&&(board8= answer =( byte)7

39、;/ 人一个角都没抢if ( board 1=chessHum)&&(board3= answer =( byte)0; answer =( byte )0;chessHum )chessHum )chessHum )chessHum ) chessHum ) chessHum ) chessHum )chessHum ) chessHum ) chessHum ) chessHum )chessHum ) chessHum ) chessHum ) chessHum ) chessHum ) chessHum )chessHum )if ( board 1=chessHum)

40、&&(board5=chessHum) answer =( byte)2;if ( board 1=chessHum)&&(board7=chessHum) answer =( byte)0;if ( board 3=chessHum)&&(board5=chessHum) answer =( byte)0;if ( board 3=chessHum)&&(board7=chessHum) answer =( byte)6;if ( board 5=chessHum)&&(board7=chessHum) answ

41、er =( byte)8;/人第一步中心,机器抢0位后的第二步if ( board 4= chessHum )&&( board 0= chessCom ) if (board 1= chessHum )answer =( byte )7;if ( board 2= chessHum )answer =( byte )6;if ( board 3= chessHum )answer =( byte )5;if ( board 5= chessHum )answer =( byte )3;if ( board 6= chessHum )answer =( byte )2;if (

42、 board 7= chessHum )answer =( byte )1;if ( board 8= chessHum )answer =( byte )2;if (depth =4)/后手第三步/单角if ( board 0= chessHum )&&( board 1= chessHum )if ( board 匸chessHum ) / 能赢就赢answer =( byte )6;elseanswer =( byte )3; /堵住3,并且准备下5赢if ( board 0= chessHum )&&( board 3= chessHum )if ( b

43、oard 2!=chessHum ) / 能赢就赢answer =( byte )2;else answer =( byte )1; / 堵住 2 ,并且准备下 7赢if ( board 0= chessHum )&&( board 5= chessHum ) if ( board 6!= chessHum ) / 能赢就赢answer =( byte )6;elseanswer =( byte )3; / 赢不了,往 7或8 走逼和棋if ( board 0= chessHum )&&( board 7= chessHum ) if ( board 2!= c

44、hessHum ) / 能赢就赢answer =( byte )2;elseanswer =( byte )1; / 赢不了,往 5或8 走逼和棋if ( board 2= chessHum )&&( board 1= chessHum ) if ( board 8!= chessHum ) / 能赢就赢answer =( byte )8;elseanswer =( byte )5; / 堵住 8 ,并且准备下 3赢if ( board 2= chessHum )&&( board 3= chessHum ) if ( board 8!= chessHum )

45、/ 能赢就赢answer =( byte )8;elseanswer =( byte )5; / 赢不了,往 6或7 走逼和棋if ( board 2= chessHum )&&( board 5= chessHum ) if ( board 0!= chessHum ) / 能赢就赢answer =( byte )0;elseanswer =( byte )1; / 堵住 1 ,并且准备下 7赢if ( board 2= chessHum )&&( board 7= chessHum ) if ( board 0!= chessHum ) / 能赢就赢answ

46、er =( byte )0;elseanswer =( byte )1; / 赢不了,往 3或6 走逼和棋if ( board 6= chessHum )&&( board 1= chessHum ) if ( board 8!= chessHum ) / 能赢就赢answer =( byte )8;elseanswer =( byte )7; /2,5 逼和棋if ( board 6= chessHum )&&( board 3= chessHum ) if ( board 8!= chessHum ) / 能赢就赢answer =( byte )8;else

47、answer =( byte )7; /1 能赢if ( board 6= chessHum )&&( board 5= chessHum ) if ( board 0!= chessHum ) / 能赢就赢answer =( byte )0;elseanswer =( byte )3; /1,2 逼和棋if ( board 6= chessHum )&&( board 7= chessHum ) if ( board 0!= chessHum ) / 能赢就赢answer =( byte )0;elseanswer =( byte )3; /5 能赢if (

48、board 8= chessHum )&&( board 1= chessHum ) if ( board 6!= chessHum ) / 能赢就赢answer =( byte )6;elseanswer =( byte )7; /0,3 逼和棋if ( board 8= chessHum )&&( board 3= chessHum ) if ( board 2!= chessHum ) / 能赢就赢answer =( byte )2;elseanswer =( byte )5; / 赢不了,往 1或0 走逼和棋if ( board 8= chessHum )&&( board 5= chessHum ) if ( board 6!= chessHum ) / 能赢就赢answer =( byte )6;elseanswer =( byte )7; /1 能赢if ( board 8= chessHum )&&( board 7= chessHum ) if ( board 2!= chessH

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论