就是五子棋,DOS版的,下子的时候输入横纵坐标。。。
没学GUI就是不爽啊。。
下面这个是我写的。成功运行
View Code JAVA
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 168 169 170 171 172 173 174 175 176 177 | package day6; /*五子棋*/ import java.util.*; public class FiveChessGame { public static int heng(int i, int j, char c, char a[][], int k) { int[] temp1 = new int[2]; int[] temp2 = new int[2]; for (int e = j - 1; e >= -1; e--) // 横着5个 { if (e == -1 || e == 16) { temp1[k] = e; break; } if (a[i][e] != c) { temp1[k] = e; break; } } for (int e = j + 1; e < = a[i].length; e++) { if (e == -1 || e == 16) { temp2[k] = e; break; } if (a[i][e] != c) { temp2[k] = e; break; } } if (temp2[k] - temp1[k] == 6 && k == 0) { return 2; } else if (temp2[k] - temp1[k] == 6 && k == 1) { return 1; } else return 0; } public static int shu(int i, int j, char c, char a[][], int k) { int[] temp1 = new int[2]; int[] temp2 = new int[2]; for (int e = i - 1; e >= -1; e--) // 竖着5个 { if (e == -1 || e == 16) { temp1[k] = e; break; } if (a[e][j] != c) { temp1[k] = e; break; } } for (int e = i + 1; e < = a[i].length; e++) { if (e == -1 || e == 16) { temp2[k] = e; break; } if (a[e][j] != c) { temp2[k] = e; break; } } if (temp2[k] - temp1[k] == 6 && k == 0) { return 2; } else if (temp2[k] - temp1[k] == 6 && k == 1) { return 1; } else return 0; } public static int xie(int i, int j, char c, char a[][], int k) { int[] temp1 = new int[2]; int[] temp2 = new int[2]; for (int e = j - 1; e >= -1; e--) // 斜着5个 { i--; if (e == -1 || e == 16 || i == -1 || i == 16) { temp1[k] = e; break; } if (a[i][e] != c) { temp1[k] = e; break; } } for (int e = j + 1; e < = a.length; e++) { i++; if (e == -1 || e == 16 || i == -1 || i == 16) { temp2[k] = e; break; } if (a[i][e] != c) { temp2[k] = e; break; } } if (temp2[k] - temp1[k] == 6 && k == 0) { return 2; } else if (temp2[k] - temp1[k] == 6 && k == 1) { return 1; } else return 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); char[][] a = new char[16][16]; char b[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; char c[] = { 'O', '@' }; int k = 0, m = 0; int h = 0, s = 0, x = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = '*'; } } while (true) { System.out.print(" "); for (int j = 0; j < a[0].length; j++) { System.out.print(b[j] + " "); } System.out.println(); for (int i = 0; i < a.length; i++) { System.out.print(b[i] + " "); for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } if (h == 1 || s == 1 || x == 1) { System.out.println("黑方获胜!"); break; } else if (h == 2 || s == 2 || x == 2) { System.out.println("白方获胜!"); break; } if (k == 0) { System.out.println("请黑方落子:"); k++; } else { System.out.println("请白方落子:"); k = 0; } String sd = sc.next(); char a1 = sd.charAt(0); char a2 = sd.charAt(1); for (int i = 0; i < a.length; i++) { if (a1 == b[i]) { for (int j = 0; j < a[i].length; j++) { if (a2 == b[j]) { if (a[i][j] == '*') { a[i][j] = c[k]; m = 1; h = heng(i, j, c[k], a, k); s = shu(i, j, c[k], a, k); x = xie(i, j, c[k], a, k); } else { System.out.println("此位置已经有棋子!请另选位置。"); k = k > 0 ? 0 : 1; m = 1; } } } } } if (m != 1) { System.out.println("输入有误,请重新输入!"); k = k > 0 ? 0 : 1; } m = 0; } } } |
下面这个呢,就是老师写的了。。比我写的好。。。
View Code JAVA
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 | package day05.fivechess; import java.util.Scanner; public class FiveChess { static char[][] chess=new char[16][16]; static boolean isBlack=true; public static void main(String[] args){ for(int i=0;i |
用java编个五子棋嘛,那样玩得比较方便~哈哈~