就是五子棋,DOS版的,下子的时候输入横纵坐标。。。

没学GUI就是不爽啊。。
下面这个是我写的。成功运行 :|

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;
		}
	}
}

下面这个呢,就是老师写的了。。比我写的好。。。 :arrow:

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<chess .length;i++){
			for(int j=0;j<chess[i].length;j++){
				chess[i][j]='*';
			}
		}
		Scanner s=new Scanner(System.in);
		printChess();
		while(true){
			System.out.println("请"+(isBlack?"黑":"白")+"方落子:");
			String str=s.next();
			int r=fromCharToInt(str.charAt(0));
			int c=fromCharToInt(str.charAt(1));
			if(chess[r][c]!='*'){
				System.out.println("该位置已经有棋子,请重新输入!");
				continue;
			}else{
				chess[r][c]=isBlack?'@':'O';
				printChess();
				if(wasWin(r,c)){
					System.out.println((isBlack?"黑":"白")+"方获胜!");
					break;
				}
			}
 
			isBlack=!isBlack;
		}
	}
 
	public static void printChess(){
		for(int i=0;i<chess.length;i++){
			if(i<10){
				System.out.print("  "+i);
			}else{
				System.out.print("  "+(char)('a'+i-10));
			}
		}
		System.out.println();
		for(int i=0;i<chess.length;i++){
			if(i<10){
				System.out.print(i+" ");
			}else{
				System.out.print((char)('a'+i-10)+" ");
			}
 
			for(int j=0;j<chess[i].length;j++){
				System.out.print(chess[i][j]+"  ");
			}
			System.out.println();
		}
	}
 
	public static int fromCharToInt(char c){
		if(c>='0' && c< ='9'){
			return c-'0';
		}else{
			return c-'a'+10;
		}
	}
 
 
	public static boolean wasWin(int r,int c){
		return wasWinAtV(r,c)||wasWinAtH(r,c)||wasWinAtLD(r,c)||wasWinAtRD(r,c);
	}
 
	public static boolean wasWinAtV(int r,int c){
		char ch=isBlack?'@':'O';
		int i=c;
		while(i>=0 && chess[r][i]==ch){//向左找到第一个不是ch的字符
			i--;
		}
		int num=0;
		i++;
		while(i</chess><chess .length && chess[r][i]==ch){
			num++;
			i++;
		}
		return num>=5;
 
	}
 
	public static boolean wasWinAtH(int r,int c){
		char ch=isBlack?'@':'O';
		int i=r;
		while(i>=0 && chess[i][c]==ch){//向左找到第一个不是ch的字符
			i--;
		}
		int num=0;
		i++;
		while(i</chess><chess .length && chess[i][c]==ch){
			num++;
			i++;
		}
		return num>=5;
	}
 
	public static boolean wasWinAtLD(int r,int c){
		char ch=isBlack?'@':'O';
		int i=r;
		int j=c;
		while(i>=0 && j</chess><chess .length && chess[i][j]==ch){//向左找到第一个不是ch的字符
			i--;
			j++;
		}
		int num=0;
		i++;
		j--;
		while(i<chess.length && j>=0 && chess[i][j]==ch){
			num++;
			i++;
			j--;
		}
		return num>=5;
	}
 
	public static boolean wasWinAtRD(int r,int c){
		char ch=isBlack?'@':'O';
		int i=r;
		int j=c;
		while(i>=0 && j>=0 && chess[i][j]==ch){//向左找到第一个不是ch的字符
			i--;
			j--;
		}
		int num=0;
		i++;
		j++;
		while(i</chess><chess .length && j<chess.length && chess[i][j]==ch){
			num++;
			i++;
			j++;
		}
		return num>=5;
	}
 
 
}
</chess>

>> 若为原创,转载请注明: 转载自Laycher's Blog

>> 本文链接地址: 五子棋DOS版。。。

>> 订阅本站: http://feed.feedsky.com/laycher



无觅相关文章插件,快速提升流量