• 象棋小程序的基本框架——逻辑代码


    日期:2019.5.8 

    博客期:071

    星期三

      我在这里只写出基本的象棋构建代码模型,即基本的DOC窗口实现程度的算法,还有,对不起,没有棋局记录等功能、没有其他的界面就是简单的逻辑结构!

      (这个其实也可以算是每周总结的一部分啦!)

     1 package basic;
     2 
     3 public class Chess {
     4     //所属
     5     /*true为红方,false为黑方*/
     6     protected boolean aim;
     7     //名称
     8     protected ChessKind name;
     9     //位置
    10     protected TableSeat tableseat;
    11     //基本方法
    12     public boolean isAim() {
    13         return aim;
    14     }
    15     public void setAim(boolean aim) {
    16         this.aim = aim;
    17     }
    18     public ChessKind getName() {
    19         return name;
    20     }
    21     public void setName(ChessKind name) {
    22         this.name = name;
    23     }
    24     public TableSeat getTableseat() {
    25         return tableseat;
    26     }
    27     public void setTableseat(TableSeat tableseat) {
    28         this.tableseat = tableseat;
    29     }
    30     //构造
    31     public Chess(boolean aim, ChessKind name, TableSeat tableseat) {
    32         super();
    33         this.aim = aim;
    34         this.name = name;
    35         this.tableseat = new TableSeat(tableseat);
    36     }
    37     //其他方法
    38     /*改变所属方*/
    39     public void changeAim(){
    40         this.aim = !this.aim;
    41     }
    42     /*上下调换*/
    43     public void changeUpAndDown(){
    44         this.tableseat.y = 10-tableseat.y;
    45     }
    46     /*左右调换*/
    47     public void changeLeftAndRight(){
    48         this.tableseat.x = 11-tableseat.x;
    49     }
    50     public void display(){
    51         System.out.println("X:"+tableseat.x+"	Y:"+tableseat.y+"	K:"+(ChessTable.changeTo(name,aim))+"	M:"+(aim?"红方":"黑方"));
    52     }
    53 }
    Chess.java
     1 package basic;
     2 
     3 public enum ChessKind {
     4     King,        //将&帥
     5     Scholar,    //士&仕
     6     Elephant,    //象&相
     7     House,        //
     8     Car,        //
     9     Cannon,        //砲(炮)
    10     Army        //卒&兵
    11 }
    ChessKind.java
      1 package basic;
      2 
      3 import java.io.File;
      4 import java.io.FileNotFoundException;
      5 import java.util.ArrayList;
      6 import java.util.List;
      7 import java.util.Scanner;
      8 import static org.fusesource.jansi.Ansi.*;
      9 import static org.fusesource.jansi.Ansi.Color.*;
     10 
     11 @SuppressWarnings("unused")
     12 public class ChessTable {
     13     protected List<Chess> table;
     14     public static ChessKind changeTo(String read){
     15         if(read.compareTo("车")==0||read.compareTo("車")==0)
     16             return ChessKind.Car;
     17         else if(read.compareTo("兵")==0||read.compareTo("卒")==0)
     18             return ChessKind.Army;
     19         else if(read.compareTo("相")==0||read.compareTo("象")==0)
     20             return ChessKind.Elephant;
     21         else if(read.compareTo("仕")==0||read.compareTo("士")==0)
     22             return ChessKind.Scholar;
     23         else if(read.compareTo("炮")==0||read.compareTo("砲")==0)
     24             return ChessKind.Cannon;
     25         else if(read.compareTo("马")==0||read.compareTo("馬")==0)
     26             return ChessKind.House;
     27         else if(read.compareTo("帅")==0||read.compareTo("帥")==0||read.compareTo("将")==0)
     28             return ChessKind.King;
     29         else
     30             return null;
     31     }
     32     public static String changeTo(ChessKind read){
     33         return ChessTable.changeTo(read,true);
     34     }
     35     public static String changeTo(ChessKind read,boolean t){
     36         switch(read)
     37         {
     38             case King:return t?"帅":"将";
     39             case Army:return t?"兵":"卒";
     40             case Cannon:return t?"砲":"炮";
     41             case Scholar:return t?"仕":"士";
     42             case Car:return t?"車":"车";
     43             case Elephant:return t?"相":"象";
     44             case House:return t?"馬":"马";
     45             default:return null;
     46         }
     47     }
     48     public void Display(){
     49         System.out.print("	");
     50         for(int i=1;i<=9;++i)
     51             System.out.print(i+"	");
     52         System.out.println();
     53         for(int i=1;i<=10;++i)
     54         {
     55             System.out.print(i+"	");
     56             for(int j=1;j<=9;++j)
     57             {
     58                 if(isLocated(i,j))
     59                 {
     60                     int size = table.size();
     61                     for(int k=0;k<size;++k)
     62                     {
     63                         if(table.get(k).tableseat.x==i&&table.get(k).tableseat.y==j)
     64                         {
     65                             //ansi().eraseScreen().render("@|red Hello|@ @|green World|@") 
     66                             //System.out.print(ChessTable.changeTo(table.get(k).name,table.get(k).aim)+"	");
     67                             if(!table.get(k).aim)
     68                                 System.out.print(ChessTable.changeTo(table.get(k).name,table.get(k).aim));
     69                             else
     70                                 System.out.print(ChessTable.changeTo(table.get(k).name,table.get(k).aim));
     71                         }
     72                     }    
     73                 }
     74                 System.out.printf("	");
     75             }
     76             System.out.println();
     77             try {
     78                 Thread.sleep(20);
     79             } catch (InterruptedException e) {
     80                 // Do Nothing ...
     81             }
     82         }
     83     }
     84     public void display(){
     85         int size = table.size();
     86         for(int i=0;i<size;++i)
     87             table.get(i).display();
     88     }
     89     public List <Chess> getTable() {
     90         return table;
     91     }
     92     public void setTable(List<Chess> table) {
     93         this.table = table;
     94     }
     95     public void reset(){
     96         File f = new File("settings/reset.txt");
     97         try {
     98             Scanner sc = new Scanner(f);
     99             while(sc.hasNext())
    100             {
    101                 TableSeat ts = new TableSeat();
    102                 ts.x = sc.nextInt();
    103                 ts.y = sc.nextInt();
    104                 String str_chessKind = sc.next();
    105                 ChessKind chesskind = ChessTable.changeTo(str_chessKind);
    106                 boolean choice = sc.nextBoolean();
    107                 Chess c = new Chess(choice,chesskind,ts);
    108                 table.add(c);
    109             }
    110             sc.close();
    111         } catch (FileNotFoundException e) {
    112             System.out.println("爷爷!您的文件读取有问题!FileNotFoundException");
    113         }
    114     }
    115     public ChessTable() {
    116         super();
    117         this.table = new ArrayList<Chess>();
    118         reset();
    119     }
    120     public ChessTable(ChessTable ct) {
    121         super();
    122         this.table = new ArrayList<Chess>(ct.table);
    123         reset();
    124     }
    125     public ChessTable(List<Chess> table) {
    126         super();
    127         this.table = new ArrayList<Chess>(table);
    128     }
    129     public boolean isLocated(int X,int Y){
    130         int size = table.size();
    131         for(int i=0;i<size;++i)
    132         {
    133             TableSeat ts = table.get(i).tableseat;
    134             if(ts.x==X&&ts.y==Y)
    135                 return true;
    136         }
    137         return false;
    138     }
    139     public boolean isLocated(int X,int Y,boolean isAim){
    140         int size = table.size();
    141         for(int i=0;i<size;++i)
    142         {
    143             Chess c = table.get(i);
    144             if(c.tableseat.x==X&&c.tableseat.y==Y&&(c.aim==isAim))
    145                 return true;
    146         }
    147         return false;
    148     }
    149     public void Delete(int x,int y){
    150         int size = table.size();
    151         for(int i=0;i<size;++i)
    152         {
    153             Chess c = table.get(i);
    154             if(c.tableseat.x==x&&c.tableseat.y==y)
    155             {
    156                 table.remove(i);
    157                 break;
    158             }
    159         }
    160     }
    161     /*运行*/
    162     public void Runable(int seat,int x,int y){
    163         
    164         Chess ch = table.get(seat);
    165         if(ch.tableseat.x==x&&ch.tableseat.y==y)
    166             return;
    167         if(x<1||x>10||y<1||y>10)
    168             return;
    169         if(ch.name==ChessKind.King)//将帅
    170         {
    171             if(ch.aim&&(y<8||x<4||x>6))
    172                 return;
    173             else if(!ch.aim&&(y>3||x<4||x>6))
    174                 return;
    175             if(ch.tableseat.x==x)
    176             {
    177                 if(ch.tableseat.y==y-1||ch.tableseat.y==y+1)
    178                 {
    179                     if(isLocated(x,y,ch.aim))
    180                         return;
    181                     else
    182                     {
    183                         if(isLocated(x,y,!ch.aim))
    184                             Delete(x,y);
    185                         ch.tableseat.x = x;
    186                         ch.tableseat.y = y;
    187                     }
    188                 }
    189                 else
    190                     return;
    191             }
    192             else if(ch.tableseat.y==y)
    193             {
    194                 if(ch.tableseat.x==x-1||ch.tableseat.x==x+1)
    195                 {
    196                     if(isLocated(x,y,ch.aim))
    197                         return;
    198                     else
    199                     {
    200                         if(isLocated(x,y,!ch.aim))
    201                             Delete(x,y);
    202                         ch.tableseat.x = x;
    203                         ch.tableseat.y = y;
    204                     }
    205                 }
    206                 else
    207                     return;
    208             }
    209             table.set(seat, ch);
    210         }
    211         else if(ch.name==ChessKind.Army)//兵卒
    212         {
    213             int s2_x = (ch.tableseat.x-x)*(ch.tableseat.x-x);
    214             int s2_y = (ch.tableseat.y-y)*(ch.tableseat.y-y);
    215             if(s2_x+s2_y!=1)
    216                 return;
    217             if(ch.aim)
    218             {
    219                 if(ch.tableseat.x-x==-1)
    220                     return;
    221                 if(ch.tableseat.x-x==0&&(y>5))
    222                     return;
    223             }
    224             else
    225             {
    226                 if(ch.tableseat.x-x==1)
    227                     return;
    228                 if(ch.tableseat.x-x==0&&(y<6))
    229                     return;
    230             }
    231             if(isLocated(x,y,ch.aim))
    232                 return;
    233             else
    234             {
    235                 if(isLocated(x,y,!ch.aim))
    236                     Delete(x,y);
    237                 ch.tableseat.x = x;
    238                 ch.tableseat.y = y;
    239             }
    240             table.set(seat, ch);
    241         }
    242         else if(ch.name==ChessKind.Cannon)//
    243         {
    244             //去除在同一行、同一列的情况
    245             if(ch.tableseat.x-x!=0&&ch.tableseat.y-y!=0)
    246                 return;
    247             //移动到的点是自方阵营
    248             if(isLocated(x,y,ch.aim))
    249                 return;
    250             //p表示移动到的位置和原位置之间的棋数
    251             int p = 0;
    252             if(ch.tableseat.x-x==0)
    253             {
    254                 if(ch.tableseat.y>y)
    255                 {
    256                     for(int i=y+1;i<ch.tableseat.y;++i)
    257                     {
    258                         //如果有棋子
    259                         if(isLocated(x,i))
    260                             ++p;
    261                         if(p==2)
    262                             return;
    263                     }
    264                 }
    265                 else
    266                 {
    267                     for(int i=y-1;i>ch.tableseat.y;--i)
    268                     {
    269                         //如果有棋子
    270                         if(isLocated(x,i))
    271                             ++p;
    272                         if(p==2)
    273                             return;
    274                     }
    275                 }
    276             }
    277             else
    278             {
    279                 if(ch.tableseat.x>x)
    280                 {
    281                     for(int i=x+1;i<ch.tableseat.x;++i)
    282                     {
    283                         //如果有棋子
    284                         if(isLocated(i,y))
    285                             ++p;
    286                         if(p==2)
    287                             return;
    288                     }
    289                 }
    290                 else
    291                 {
    292                     for(int i=x-1;i>ch.tableseat.x;--i)
    293                     {
    294                         //如果有棋子
    295                         if(isLocated(i,y))
    296                             ++p;
    297                         if(p==2)
    298                             return;
    299                     }
    300                 }
    301             }
    302             if(isLocated(x,y)&&p==0)
    303                 return;
    304             else
    305             {
    306                 Delete(x,y);
    307                 ch.tableseat.x = x;
    308                 ch.tableseat.y = y;
    309             }
    310         }
    311         else if(ch.name==ChessKind.Car)//
    312         {
    313             if(ch.tableseat.x-x!=0&&ch.tableseat.y-y!=0)
    314                 return;
    315             if(ch.tableseat.x-x==0)
    316             {
    317                 if(ch.tableseat.y>y)
    318                 {
    319                     for(int i=y+1;i<ch.tableseat.y;++i)
    320                         if(isLocated(x,i))
    321                             return;
    322                 }
    323                 else
    324                 {
    325                     for(int i=y-1;i>ch.tableseat.y;--i)
    326                         if(isLocated(x,i))
    327                             return;
    328                 }
    329             }
    330             else
    331             {
    332                 if(ch.tableseat.x>x)
    333                 {
    334                     for(int i=x+1;i<ch.tableseat.x;++i)
    335                         if(isLocated(i,y))
    336                             return;
    337                 }
    338                 else
    339                 {
    340                     for(int i=x-1;i>ch.tableseat.x;--i)
    341                         if(isLocated(i,y))
    342                             return;
    343                 }
    344             }
    345             if(isLocated(x,y,ch.aim))
    346                 return;
    347             else
    348             {
    349                 if(isLocated(x,y,!ch.aim))
    350                     Delete(x,y);
    351                 ch.tableseat.x = x;
    352                 ch.tableseat.y = y;
    353             }
    354         }
    355         else if(ch.name==ChessKind.Elephant)//相象
    356         {
    357             int s2_x = (ch.tableseat.x-x)*(ch.tableseat.x-x);
    358             int s2_y = (ch.tableseat.y-y)*(ch.tableseat.y-y);
    359             if(s2_x+s2_y!=8)
    360                 return;
    361             if((ch.aim&&y<6)||(!ch.aim&&y>5))
    362                 return;
    363             if(isLocated((x+ch.tableseat.x)/2,(y+ch.tableseat.y)/2))
    364                 return;
    365             if(isLocated(x,y,ch.aim))
    366                 return;
    367             else
    368             {
    369                 if(isLocated(x,y,!ch.aim))
    370                     Delete(x,y);
    371                 ch.tableseat.x = x;
    372                 ch.tableseat.y = y;
    373             }
    374             table.set(seat, ch);
    375         }
    376         else if(ch.name==ChessKind.House)//
    377         {
    378             int s2_x = (ch.tableseat.x-x)*(ch.tableseat.x-x);
    379             int s2_y = (ch.tableseat.y-y)*(ch.tableseat.y-y);
    380             //马可跳跃到的位置
    381             if(s2_x+s2_y!=5)
    382                 return;
    383             //绊马腿
    384             if((s2_y==4&&isLocated(x,(y+ch.tableseat.y)/2))||(s2_x==4&&isLocated((x+ch.tableseat.x)/2,y)))
    385                 return;
    386             //位置存在本方棋子
    387             if(isLocated(x,y,ch.aim))
    388                 return;
    389             else
    390             {
    391                 if(isLocated(x,y,!ch.aim))
    392                     Delete(x,y);
    393                 ch.tableseat.x = x;
    394                 ch.tableseat.y = y;
    395             }
    396             table.set(seat, ch);
    397         }
    398         else if(ch.name==ChessKind.Scholar)//仕士
    399         {
    400             int s2_x = (ch.tableseat.x-x)*(ch.tableseat.x-x);
    401             int s2_y = (ch.tableseat.y-y)*(ch.tableseat.y-y);
    402             if(s2_x+s2_y!=2)
    403                 return;
    404             if((ch.aim&&(x<4||x>6||y>3))||((!ch.aim)&&(x<4||x>6||y>3)))
    405                 return;
    406             //位置存在本方棋子
    407             if(isLocated(x,y,ch.aim))
    408                 return;
    409             else
    410             {
    411                 if(isLocated(x,y,!ch.aim))
    412                     Delete(x,y);
    413                 ch.tableseat.x = x;
    414                 ch.tableseat.y = y;
    415             }
    416             table.set(seat, ch);
    417         }
    418         
    419     }
    420     //是否发生移动
    421     public boolean checkOut(Chess ch,int x,int y)
    422     {
    423         return !(ch.tableseat.x==x&&ch.tableseat.y==y);
    424     }
    425 }
     1 package basic;
     2 
     3 public class TableSeat {
     4     //水平坐标
     5     public int x;
     6     //竖直坐标
     7     public int y;
     8     public TableSeat() {
     9         super();
    10     }
    11     public TableSeat(TableSeat ts) {
    12         super();
    13         this.x = ts.x;
    14         this.y = ts.y;
    15     }
    16     public TableSeat(int x, int y) {
    17         super();
    18         this.x = x;
    19         this.y = y;
    20     }
    21     
    22 }
    TableSeat.java
  • 相关阅读:
    php 时间问题
    php语言
    高级查询
    数据库的查询详情
    数据库的创建和增删改查,外键和主键的创建
    数据库
    js的基本语句和语法
    JS的脚本语言
    样式、格式布局
    表单的元素和样式表
  • 原文地址:https://www.cnblogs.com/onepersonwholive/p/10872123.html
Copyright © 2020-2023  润新知