1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| import java.util.Scanner;
class chiz { String team; int level; String wd; int status; chiz(String steam, int slevel, String sface, int sstatus) { team = steam; level = slevel; wd = sface; status = sstatus; } boolean flipable() { return status == 0 ? true : false; } void flip() { status = 1; } }
class board { chiz[][] grd; int turn;
void init() { grd = new chiz[4][8]; turn = 1; grd[0][0] = new chiz("r", 0, "帥", 0); grd[0][1] = new chiz("r", 1, "仕", 0); grd[0][2] = new chiz("r", 1, "仕", 0); grd[0][3] = new chiz("r", 2, "相", 0); grd[0][4] = new chiz("r", 2, "相", 0); grd[0][5] = new chiz("r", 3, "俥", 0); grd[0][6] = new chiz("r", 3, "俥", 0); grd[0][7] = new chiz("r", 4, "傌", 0); grd[1][0] = new chiz("r", 4, "傌", 0); grd[1][1] = new chiz("r", 5, "炮", 0); grd[1][2] = new chiz("r", 5, "炮", 0); grd[1][3] = new chiz("r", 6, "兵", 0); grd[1][4] = new chiz("r", 6, "兵", 0); grd[1][5] = new chiz("r", 6, "兵", 0); grd[1][6] = new chiz("r", 6, "兵", 0); grd[1][7] = new chiz("r", 6, "兵", 0); grd[2][0] = new chiz("b", 0, "將", 0); grd[2][1] = new chiz("b", 1, "士", 0); grd[2][2] = new chiz("b", 1, "士", 0); grd[2][3] = new chiz("b", 0, "象", 0); grd[2][4] = new chiz("b", 0, "象", 0); grd[2][5] = new chiz("b", 0, "車", 0); grd[2][6] = new chiz("b", 0, "車", 0); grd[2][7] = new chiz("b", 0, "馬", 0); grd[3][0] = new chiz("b", 0, "馬", 0); grd[3][1] = new chiz("b", 0, "包", 0); grd[3][2] = new chiz("b", 0, "包", 0); grd[3][3] = new chiz("b", 0, "卒", 0); grd[3][4] = new chiz("b", 0, "卒", 0); grd[3][5] = new chiz("b", 0, "卒", 0); grd[3][6] = new chiz("b", 0, "卒", 0); grd[3][7] = new chiz("b", 0, "卒", 0); } void rnd() { for(int i=0 ; i<200 ; i++) { int x1 = (int) (Math.random() * 8); int y1 = (int) (Math.random() * 4); int x2 = (int) (Math.random() * 8); int y2 = (int) (Math.random() * 4); chiz tmp; tmp = grd[y1][x1]; grd[y1][x1] = grd[y2][x2]; grd[y2][x2] = tmp;
} } void show() { for(int i=0 ; i<4 ; i++) { for(int j=0 ; j<8 ; j++) if(grd[i][j] == null) System.out.print(" "); else if(grd[i][j].status == 1) System.out.print(grd[i][j].wd + " "); else if(grd[i][j].status == 0) System.out.print("口 "); System.out.println(); } } void move(int y1, int x1, int y2, int x2) { if(grd[y2][x2] != null) grd[y2][x2].status = 2; grd[y2][x2] = grd[y1][x1]; grd[y1][x1] = null; } boolean movable(int y1, int x1, int y2, int x2) { boolean movable = false; if(grd[y1][x1] != null && grd[y1][x1].status == 1) if(grd[y2][x2] == null) movable = true; else if(grd[y2][x2].status == 1 && !grd[y1][x1].team.equals(grd[y2][x2].team)) if(grd[y1][x1].level == 5) { int cnt = 0, f = 0, t = 0; if(x1 == x2) { if(y1 > y2) { f = y2; t = y1; } else { f = y1; t = y2; } for(int i=f+1 ; i<t ; i++) if(grd[i][x1] != null) cnt++; } else if (y1 == y2) { if(x1 > x2) { f = x2; t = x1; } else { f = x1; t = x2; } for(int i=f+1 ; i<t ; i++) if(grd[y1][i] != null) cnt++; } if(cnt == 1) movable = true; } else if(!(grd[y1][x1].level == 0 && grd[y2][x2].level == 0) && grd[y1][x1].level <= grd[y2][x2].level) movable = true; else if(grd[y1][x1].level == 6 && grd[y2][x2].level == 0) movable = true;
return movable; } } public class game { public static void main(String[] args) { board br = new board(); br.init(); br.rnd(); br.show(); Scanner sc = new Scanner(System.in); String cmd = ""; do { System.out.println(br.turn == 1 ? "P1:" : "P2:"); cmd = sc.nextLine(); String token[] = cmd.split(","); if(token.length == 2) { int y1 = Integer.parseInt(token[0]); int x1 = Integer.parseInt(token[1]); if(br.grd[y1][x1].flipable()) { br.grd[y1][x1].flip(); br.turn *= -1; } } else if(token.length == 4) { int y1 = Integer.parseInt(token[0]); int x1 = Integer.parseInt(token[1]); int y2 = Integer.parseInt(token[2]); int x2 = Integer.parseInt(token[3]); if(br.movable(y1, x1, y2, x2)) { br.move(y1, x1, y2, x2); br.turn *= -1; } } br.show(); } while (!cmd.equals("end")); sc.close(); } }
|