目录
1、初始化和输出
//棋盘的初始化
public void ErWei() {
int k = 0;
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
if (j == chess[i].length - 1) {
chess[i][j] = num[k];
} else {
chess[i][j] = line;
}
}
if (i == chess.length - 1) {
chess[i] = num;
}
k++;
}
}
//打印输出
public void display() {
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
System.out.print(chess[i][j]);
}
System.out.println();
}
}
2、黑白子下棋
//白子下棋
public void White_Chessman(int x, int y) {
WuZiQi wu = new WuZiQi();
chess[x][y] = white;
wu.display();
}
//黑子下棋
public void Black_Chessman(int x, int y) {
WuZiQi wu = new WuZiQi();
chess[x][y] = black;
wu.display();
}
3、判断棋子是否越界
//判断棋子是否越界
public boolean checkPoint(int x,int y){
if(x<0||x>13||y<0||y>13){
return false;
}
if(chess[x-1][y-1]!=line){
return false;
}
return true;
}
4、判断输赢
//判断输赢
public static int Judge(char[][] chess) {
int result = 0; //返回结果
//逐行扫描
for (int i = 0; i < chess.length; i++) {
int count = 1;
char value = chess[i][0];
for (int j = 1; j < 10; j++) {
if (chess[i][j] == value) {
count++;
}
if (chess[i][j] != value) {
count = 1;
value = chess[i][j];
}
if (count == 5 && value == black) { //5个1相连
result = 1;
}
if (count == 5 && value == white) { //5个2相连
result = 2;
}
}
}
//逐列扫描
for(int j=0;j<chess.length;j++) {
int count = 1;
char value = chess[j][0];
for(int i=1;i<10;i++) {
if(chess[i][j]== value) {
count++;
}
if(chess[i][j] != value) {
count = 1;
value = chess[i][j];
}
if(count==5 && value== black) { //5个1相连
result = 1;
}
if(count==5 && value== white) { //5个2相连
result = 2;
}
}
}
//斜线扫描
for(int i=0;i<chess.length-4;i++) {
int count = 1;
char value = chess[0][i];
int x = 0;
int y = i;
for(int j=1;j<chess.length-i;j++,x++,y++) {
if(chess[x+1][y+1]== value) {
count++;
}
if(chess[x+1][y+1]!= value) {
count = 1;
value = chess[x+1][y+1];
}
if(count==5 && value== black) { //5个1相连
result = 1;
}
if(count==5 && value== white) { //5个2相连
result = 2;
}
}
}
for(int i=1;i<chess.length-4;i++) {
int count = 1;
char value = chess[i][0];
int x = i;
int y = 0;
for(int j=1;j<chess.length-i;j++,x++,y++) {
if(chess[x+1][y+1]==value) {
count++;
}
if(chess[x+1][y+1]!=value) {
count = 1;
value = chess[x+1][y+1];
}
if(count==5 && value== black) { //5个1相连
result = 1;
}
if(count==5 && value== white) { //5个2相连
result = 2;
}
}
}
return result;
}
5、完整代码
import java.util.Scanner;
public class WuZiQi {
static char[] num = {'⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖'};
static char black = '★';
static char white = '☆';
static char line = '十';
static char[][] chess = new char[15][15];
public static void main(String[] args) {
System.out.println("五子棋");
boolean flag = true;
Scanner sc = new Scanner(System.in);
WuZiQi wu = new WuZiQi();
wu.ErWei();
wu.display();
int win;
while (true) {
if (flag) {
System.out.println("请黑子下棋:(请输入横纵坐标)");
int blackh = sc.nextInt();
int blackz = sc.nextInt();
if(wu.checkPoint(blackh,blackz)){
wu.Black_Chessman(blackh - 1,blackz - 1);
if(wu.Judge(chess) == 1){
System.out.println("黑棋胜");
break;
}
}else{
System.out.println("请重新坐标或此处已有棋子!!!");
continue;
}
flag = false;
} else {
System.out.println("请白子下棋:(请输入横纵坐标)");
int whiteh = sc.nextInt();
int whitez = sc.nextInt();
if(wu.checkPoint(whiteh,whitez) ){
wu.White_Chessman(whiteh - 1,whitez - 1);
if(wu.Judge(chess) == 2){
System.out.println("白棋胜");
break;
}
}else{
System.out.println("请重新坐标或此处已有棋子!!!");
continue;
}
flag = true;
}
}
}
//棋盘的初始化
public void ErWei() {
int k = 0;
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
if (j == chess[i].length - 1) {
chess[i][j] = num[k];
} else {
chess[i][j] = line;
}
}
if (i == chess.length - 1) {
chess[i] = num;
}
k++;
}
}
//打印输出
public void display() {
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
System.out.print(chess[i][j]);
}
System.out.println();
}
}
//白子下棋
public void White_Chessman(int x, int y) {
WuZiQi wu = new WuZiQi();
chess[x][y] = white;
wu.display();
}
//黑子下棋
public void Black_Chessman(int x, int y) {
WuZiQi wu = new WuZiQi();
chess[x][y] = black;
wu.display();
}
//判断棋子是否越界
public boolean checkPoint(int x,int y){
if(x<0||x>13||y<0||y>13){
return false;
}
if(chess[x-1][y-1]!=line){
return false;
}
return true;
}
//判断输赢
public static int Judge(char[][] chess) {
int result = 0; //返回结果
//逐行扫描
for (int i = 0; i < chess.length; i++) {
int count = 1;
char value = chess[i][0];
for (int j = 1; j < 10; j++) {
if (chess[i][j] == value) {
count++;
}
if (chess[i][j] != value) {
count = 1;
value = chess[i][j];
}
if (count == 5 && value == black) { //5个1相连
result = 1;
}
if (count == 5 && value == white) { //5个2相连
result = 2;
}
}
}
//逐列扫描
for(int j=0;j<chess.length;j++) {
int count = 1;
char value = chess[j][0];
for(int i=1;i<10;i++) {
if(chess[i][j]== value) {
count++;
}
if(chess[i][j] != value) {
count = 1;
value = chess[i][j];
}
if(count==5 && value== black) { //5个1相连
result = 1;
}
if(count==5 && value== white) { //5个2相连
result = 2;
}
}
}
//斜线扫描
for(int i=0;i<chess.length-4;i++) {
int count = 1;
char value = chess[0][i];
int x = 0;
int y = i;
for(int j=1;j<chess.length-i;j++,x++,y++) {
if(chess[x+1][y+1]== value) {
count++;
}
if(chess[x+1][y+1]!= value) {
count = 1;
value = chess[x+1][y+1];
}
if(count==5 && value== black) { //5个1相连
result = 1;
}
if(count==5 && value== white) { //5个2相连
result = 2;
}
}
}
for(int i=1;i<chess.length-4;i++) {
int count = 1;
char value = chess[i][0];
int x = i;
int y = 0;
for(int j=1;j<chess.length-i;j++,x++,y++) {
if(chess[x+1][y+1]==value) {
count++;
}
if(chess[x+1][y+1]!=value) {
count = 1;
value = chess[x+1][y+1];
}
if(count==5 && value== black) { //5个1相连
result = 1;
}
if(count==5 && value== white) { //5个2相连
result = 2;
}
}
}
return result;
}
}
6、示例(一部分)仅供参考
循环往复