1、a0102013211209C1F5B2A9CEC5初始化a10300/010200 90 00成功 电脑先手选0009 90 00成功 人先手63 01初始化失败下棋a200-0803 9000成功落子未完01 9000成功落子人赢了02 9000成功落子电脑赢了00 9000 成功落子和局 6301 落子失败打印棋盘a3080x 0x0x0x0x0x0x0x0x棋盘情况三. 测试数据1. 选中/select 627570742e67616d652e31 选中2. 身份反馈/send 00a0000010 身份反馈3. 初始化/send 00a1030002 初始化棋盘并人先手/send 0
2、0a1030102 初始化棋盘并AI先手4. 落字/send 00a2000002 落字到左上角/send 00a2010002 落字到第一行中间/send 00a2020002 落字到右上角/send 00a2030002 落字到中间靠左/send 00a2040002 落字到中心/send 00a2050002 落字到中间靠右/send 00a2060002 落字到左下角/send 00a2070002 落字到最后一行中心/send 00a2080002 落字到右下角5. 打印棋盘/send 00a3000008四. 关键代码1. 游戏初始化模块:privatevoidinit(APDU
3、apdu) byte buffer = apdu.getBuffer(); /棋盘维数报错 if(bufferISO7816.OFFSET_P1 != (byte)0x03) ISOException.throwIt(WRONG_P1); /先后手报错 if(bufferISO7816.OFFSET_P2 != (byte)0x01) if(bufferISO7816.OFFSET_P2 != (byte)0x00) ISOException.throwIt(WRONG_P2); /将临时变量放进RAM board=JCSystem.makeTransientByteArray(short)
4、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
5、)1,JCSystem.CLEAR_ON_DESELECT)0;result=JCSystem.makeTransientByteArray(short)1,JCSystem.CLEAR_ON_DESELECT)0; this.board0=(byte)0x00; this.board1=(byte)0x00; this.board2=(byte)0x00; this.board3=(byte)0x00; this.board4=(byte)0x00; this.board5=(byte)0x00; this.board6=(byte)0x00; this.board7=(byte)0x00;
6、 this.board8=(byte)0x00;this.depth=(byte)0x09;this.order=(byte)0x09;this.turn=(byte)0x09;this.answer=(byte)0x09;this.result=(byte)0x09; order=bufferISO7816.OFFSET_P2; if( order=0x01)turn=chessCom;play(apdu);return; turn=chessHum; shortle = apdu.setOutgoing(); /if(le (byte)1) / ISOException.throwIt(I
7、SO7816.SW_WRONG_LENGTH); / apdu.setOutgoingLength(byte)1); buffer0=(byte)0x09; apdu.sendBytes(short)0, (short)1); 2. 身份反馈模块/个人信息-00a0 0000 0f privatevoidmy_info(APDU apdu) (byte)15) apdu.setOutgoingLength(byte)16); /学号,姓名 buffer0= 0x32; buffer1= 0x30; buffer2= 0x31; buffer3= 0x33; buffer4= 0x32; buf
8、fer5= 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.sendBytes(short)0, (short)16);3. 对战入口模块privatevoidplay(APDU apdu) if(boardbufferISO78
9、16.OFFSET_P1!=0)ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); if (turn=chessHum)/该人下 move(turn,bufferISO7816.OFFSET_P1);/第二个参数是人的下棋位置 check(); if (result!=0x00) shortle = apdu.setOutgoing(); /if(le (byte)2) / ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); / apdu.setOutgoingLength(byte)2); buff
10、er0=result; buffer1=answer; apdu.sendBytes(short)0, (short)2); return; if(turn=chessCom)/电脑下 /*查表 /*查表 move(turn,answer);/刷新棋盘/检查状态 if(depth=8) shortle = apdu.setOutgoing(); /if(le / ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); / apdu.setOutgoingLength(byte)1); buffer0=answer; apdu.sendBytes(short
11、)0, (short)1); else 4. 检查棋盘状态模块/检查棋盘状态的入口 privatevoidcheck() if (win(chessHum)result=(byte)0x02; if (win(chessCom)result=(byte)0x01; if (draw()result=(byte)0x03; elseresult=(byte)0x00; return; /胜负 privatebooleanwin(byte turn) if (board0=turn &board1=turn &board2=turn) returntrue; if (board3=turn &bo
12、ard4=turn &board5=turn) if (board6=turn &board7=turn &board8=turn)board3=turn &board6=turn) if (board1=turn &board7=turn) if (board2=turn &board5=turn & elsereturnfalse; /平局privatebooleandraw() if(depth=(byte)0)/剩余步数为零 returntrue; returnfalse;/*检查棋局5. 打印棋盘模块/打印棋盘 privatevoidqipan_info(APDU apdu) (by
13、te)8) apdu.setOutgoingLength(byte)9); /棋盘信息 buffer0= board0; buffer1= board1; buffer2= board2; buffer3= board3; buffer4= board4; buffer5= board5; buffer6= board6; buffer7= board7; buffer8= board8; apdu.sendBytes(short)0, (short)9);五. 测试结果1. 身份反馈测试输入返回send 00a00000000f2. 人先手测试send 00a10301000109 90 0
14、0send 00a20400000200 08 90 00send 00a20100000200 07 90 00send 00a20500000202 06 90 003. 卡先手测试send 00a10300000106 90 0000 04 90 0000 00 90 0002 08 90 004. 报错测试send 00a10300020000016700send 00a1020000016A86send 00a3030000016D00卡片下载不足时6A84直接send 00a1030000016E00send 00a2030000026A88send 00a2010000016A8
15、9六. 团队协作与个人分工1. 团队通过小组讨论,确定通过一定的策略进行棋谱扫描。团队公用模板,具体算法实现,各自完成。2. 个人完成1) 自行设计策略算法先手优先抢角,后手优先占中。2) 所有代码由个人独自完成3) 团队的查表核心部分共633行由我撰写附录:中间省略的由我撰写查表核心代码/*查表 if(depth=9)/先手第一步 answer=(byte)0; if(depth=7)/先手第二步 if(board4!=chessHum) answer=(byte)4; else answer=(byte)2; if(depth=5)/先手第三步 if(board4=chessCom)/抢到
16、中 if(board8!=chessHum)/能赢就赢 answer=(byte)8; else/否则走一步最有利 if(board8=chessHum)&(board7=chessHum) answer=(byte)6; if(board8=chessHum)&(board5=chessHum) answer=(byte)2;(board6=chessHum) answer=(byte)7;(board2=chessHum) answer=(byte)5;(board3=chessHum)(board1=chessHum) answer=(byte)3; else/占了0和2角,人已占中 i
17、f(board1! answer=(byte)1; else/被堵 answer=(byte)7; if(depth=3)/先手第四步 if(board0=chessCom)&(board4=chessCom)&(board6=chessCom)/必赢 if(board2! answer=(byte)2; answer=(byte)3;(board2=chessCom)&(board4=chessCom)/必赢 if(board1! answer=(byte)1; answer=(byte)6;(board7=chessCom)&(board8=chessHum)& answer=(byte)
18、5;/最后搏一搏人不下3(board5=chessCom)&(board4=chessCom) if(board3!/最后搏一搏人不下7/最后搏一搏人不下6(board3=chessCom)& if(board5!(board1=chessHum)&(board4=chessHum)/机器抢完两角,人抢中堵后 if(board3=chessHum)/准备下8否则和棋 if(board5=chessHum)/准备下6否则和棋 if(board6=chessHum) if(board8=chessHum) if(depth=1)/先手第五步(board5=chessCom)/人未下3,赢了/人堵3,和棋 if(board0=chess
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1