两天完成的代码
判断是否五字一线的循环300多行
总共500多行
初始棋盘
代码及使用说明: 五子棋.
.class t3
.super java/lang/Object
.field public static board [[Ljava/lang/String;
.field public static player I=0
.method public <init>()V
.limit locals 3
.limit stack 3
aload 0
invokespecial java/lang/Object/<init>()V
return
.end method
.method public static main([Ljava/lang/String;)V
.limit locals 20
.limit stack 20
bipush 15
bipush 15
multianewarray [[Ljava/lang/String; 2 ;定义棋盘数组
astore_1
aload_1
invokestatic t3/initBoard([[Ljava/lang/String;)V
aload_1
invokestatic t3/printBoard([[Ljava/lang/String;)V
aload_1
invokestatic t3/startGame([[Ljava/lang/String;)V
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc "author:harry c"
invokevirtual java/io/PrintStream.print(Ljava/lang/String;)V
return
.end method
;------------------初始化面板数组-------------------
.method static initBoard([[Ljava/lang/String;)V
.limit locals 20
.limit stack 20
iconst_0 ;i=0
istore_1 ;j=0
goto x ;55 i = 0; i < 15; i++
y: ;37
iconst_0
istore_2
goto y2 ;40
y3: ;41
aload_0
iload_1
aaload
iload_2
ldc "—"
aastore
iinc 2 1
y2: ;40
iload_2
bipush 15
if_icmplt y3 ;41
iinc 1 1
x: ;55
iload_1
bipush 15
if_icmplt y ;37
return
.end method
;-----------------添加棋子坐标---------------------
.method static playChess(Ljava/lang/String;[[Ljava/lang/String;)V
.limit locals 20
.limit stack 20
new java/util/Scanner
dup
getstatic java/lang/System.in Ljava/io/InputStream;
invokespecial java/util/Scanner/<init>(Ljava/io/InputStream;)V
astore_2
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc "输入棋子的坐标:"
invokevirtual java/io/PrintStream.print(Ljava/lang/String;)V
aload_2
invokevirtual java/util/Scanner.nextInt()I
iconst_1
isub
istore_3
aload_2
invokevirtual java/util/Scanner.nextInt()I
iconst_1
isub
istore 4
aload_1
iload_3
aaload
iload 4
aaload
ldc "—"
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq chongfu ;已有棋子
aload_1
iload_3
aaload
iload 4
aload_0
aastore
aload_1
invokestatic t3/printBoard([[Ljava/lang/String;)V
return
chongfu:
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc "已有棋子,重新输入"
invokevirtual java/io/PrintStream.println(Ljava/lang/String;)V
getstatic t3/player I
iconst_1
isub
putstatic t3/player I
return
.end method
;------------------------------------------------------
.method static printBoard([[Ljava/lang/String;)V
.limit locals 20
.limit stack 20
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc " "
invokevirtual java/io/PrintStream.print(Ljava/lang/String;)V
iconst_1
istore_1
goto i1 ;132
col: ;135
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc "%-3d"
iconst_1
anewarray java/lang/Object
dup
iconst_0
iload_1
invokestatic java/lang/Integer.valueOf(I)Ljava/lang/Integer;
aastore
invokevirtual java/io/PrintStream.printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
pop
iinc 1 1
i1: ;132
iload_1
bipush 15
if_icmple col ;列号 135
getstatic java/lang/System.out Ljava/io/PrintStream;
invokevirtual java/io/PrintStream.println()V
iconst_0
istore_1
goto row ;141-127 行号
row2: ;53
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc "%-3d"
iconst_1
anewarray java/lang/Object
dup
iconst_0
iload_1
iconst_1
iadd
invokestatic java/lang/Integer.valueOf(I)Ljava/lang/Integer;
aastore
invokevirtual java/io/PrintStream.printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
pop
iconst_0
istore_2
goto row3 ;112
row4: ;80
getstatic java/lang/System.out Ljava/io/PrintStream;
new java/lang/StringBuilder
dup
aload_0
iload_1
aaload
iload_2
aaload
invokestatic java/lang/String.valueOf(Ljava/lang/Object;)Ljava/lang/String;
invokespecial java/lang/StringBuilder/<init>(Ljava/lang/String;)V
ldc " "
invokevirtual java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
invokevirtual java/lang/StringBuilder.toString()Ljava/lang/String;
invokevirtual java/io/PrintStream.print(Ljava/lang/String;)V
iinc 2 1
row3: ;112
iload_2
bipush 15
if_icmplt row4 ;80
getstatic java/lang/System.out Ljava/io/PrintStream;
invokevirtual java/io/PrintStream.println()V
iinc 1 1
row: ;127
iload_1
bipush 15
if_icmplt row2 ;53
return
.end method
;-----------------开始游戏----------------------
.method static startGame([[Ljava/lang/String;)V
.limit locals 20
.limit stack 20
goto Judge ;50
play:
getstatic t3/player I
iconst_2
irem
ifne white ;白棋下27
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc "白方"
invokevirtual java/io/PrintStream.println(Ljava/lang/String;)V
ldc "●"
aload_0
invokestatic t3/playChess(Ljava/lang/String;[[Ljava/lang/String;)V
goto play2 ;42
white:
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc "黑方"
invokevirtual java/io/PrintStream.println(Ljava/lang/String;)V
ldc "○"
aload_0
invokestatic t3/playChess(Ljava/lang/String;[[Ljava/lang/String;)V
play2:
getstatic t3/player I
iconst_1
iadd
putstatic t3/player I
Judge:
aload_0
invokestatic t3/isOver([[Ljava/lang/String;)Z
ifeq play
getstatic java/lang/System.out Ljava/io/PrintStream;
ldc " 游戏结束"
invokevirtual java/io/PrintStream.println(Ljava/lang/String;)V
return
.end method
;-----------------判断是否结束----------------------
.method static isOver([[Ljava/lang/String;)Z
.limit locals 20
.limit stack 20
iconst_0
istore_1
goto x ;382
five:
iconst_0
istore_2
goto y ;7-373
ten:
aload_0
iload_1
aaload
iload_2
aaload
ldc "—"
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifne y1 ;22-370
iload_2
bipush 11
if_icmpge y2 ;28-103 y < 11
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
aaload
iload_2
iconst_1
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y2 ;y—1
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
aaload
iload_2
iconst_2
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y2 ;y—2 53-72
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
aaload
iload_2
iconst_3
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y2 ;y—3 94hang
aload_0
iload_1
aaload
iload_1
aaload
aload_0
iload_1
aaload
iload_2
iconst_4
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y2
iconst_1
ireturn
y2:
iload_1
bipush 11
if_icmpge y3 ;106-183
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_1
iadd
aaload
iload_2
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y3 ;x—1 ;-124-183
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_2
iadd
aaload
iload_2
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y3 ;x—2
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_3
iadd
aaload
iload_2
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y3 ;x—3
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_4
iadd
aaload
iload_2
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y3 ;x—4
iconst_1
ireturn
y3:
iload_1
bipush 11
if_icmpge y4 ;186-277 x < 11
iload_2
bipush 11
if_icmpge y4 ;x < 11 && y < 11 277
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_1
iadd
aaload
iload_2
iconst_1
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y4 ;x—1 y—1 250
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_2
iadd
aaload
iload_2
iconst_2
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y4 ;x—2 y—2 277
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_3
iadd
aaload
iload_2
iconst_3
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y4 ;x—3 y—3 298
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_4
iadd
aaload
iload_2
iconst_4
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y4 ;x—4 y—4 322
iconst_1
ireturn
y4:
iload_1
iconst_3
if_icmple y1 ;370 x > 3 && y < 11
iload_2
bipush 11
if_icmpge y1
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_1
isub
aaload
iload_2
iconst_1
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y1
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_2
isub
aaload
iload_2
iconst_2
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y1
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_3
isub
aaload
iload_2
iconst_3
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y1
aload_0
iload_1
aaload
iload_2
aaload
aload_0
iload_1
iconst_4
isub
aaload
iload_2
iconst_4
iadd
aaload
invokevirtual java/lang/String.equals(Ljava/lang/Object;)Z
ifeq y1
iconst_1
ireturn
y1: ;370
iinc 2 1
y:
iload_2 ;439
bipush 15
if_icmplt ten
iinc 1 1
x:
iload_1
bipush 15
if_icmplt five
iconst_0
ireturn
.end method