编写15*15的棋盘,其中第15行和第15列表示坐标。
详细代码实现如下:
import java.util.Scanner;
/*
* 五子棋
*/
public class WZQ {
static String white = "○";//表示白棋
static String black = "●";//表示黑棋
static String[][] qp = new String[15][15];//{{null,null}}
static String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
static String line = "十";
static boolean flag=true;//黑棋先下
public static void main(String[] args) {
//1.初始化棋盘(方法)
WZQ.init();
//2.打印棋盘(打印棋盘方法)
WZQ.print();
//3.开始游戏 内定黑棋或白棋先下tring[15][15];//{{null,null}}
WZQ.startGame();
//4.判断每次下子坐标是否越界,棋子是否重复
//5.判断输赢 每次下棋后判断输赢
}
public static void startGame(){
Scanner sc=new Scanner(System.in);
int x;
int y;
while(true){
if(flag){
System.out.println("请输入黑棋的坐标");
y= sc.nextInt()-1;//以左下为原点的直角坐标系,输入横坐标-1即为数组索引
x= sc.nextInt()-1;//输入纵坐标-1即为数组索引陈v
if(x<0||x> qp.length-2||y<0||y>qp.length-2){
System.out.println("坐标越界,请重新输入");
continue;
}else{
if(checkPoint(x,y)){
qp[x][y]=black;
print();
if(isWin(x,y,black)) {
System.out.println("黑棋胜利!");
break;
}
}else {
System.out.println("已有棋子,重新输入黑棋坐标");
continue;
}
}
flag = false;
}else{
System.out.println("请输入白棋的坐标");
y= sc.nextInt()-1;
x=sc.nextInt()-1;
if(x<0||x>qp.length-2||y<0||y>qp.length-2){
System.out.println("坐标越界,请重新输入");
continue;
}else{
if(checkPoint(x,y)){
qp[x][y]=white;
print();
if(isWin(x,y,white)) {
System.out.println("白棋胜利!");
break;
}
}else {
System.out.println("已有棋子,重新输入白棋坐标");
continue;
}
}
flag=true;
}
}
}
public static void init(){
for (int i = 0; i =0 ; i--) {
if(qp[i][y].equals(qz)) {
count++;
if (count == 5) return true;
}
else {
break;
}
}
//右边
for (int i = x+1; i < qp.length-1 ; i++) {
if(qp[i][y].equals(qz)){
count++;
if(count==5)return true;
}
else {
count=1;break;
}
}
//上面
for (int i = y-1; i >=0 ; i--) {
if(qp[x][i].equals(qz)){
count++;
if(count==5)return true;
}
else {
break;}
}
//下面
for (int i = y+1; i < qp.length-1 ; i++) {
if(qp[x][i].equals(qz)) {
count++;
if (count == 5) return true;
}
else {
count=1;break;}
}
//左下
for (int i = x-1,j = y+1; i >=0 &&j< qp.length-1; i--,j++) {
if(qp[i][j].equals(qz)){
count++;
if(count==5)return true;
}
else {
break;}
}
//右上
for (int i = x+1,j = y-1; i < qp.length-1 &&j>=0; i++,j--) {
if(qp[i][j].equals(qz)){
count++;
if(count==5)return true;
}
else {
count=1; break;
}
}
//右下
for (int i = x+1,j = y+1; i < qp.length-1 &&j<= qp.length-1; i++,j++) {
if(qp[i][j].equals(qz)){
count++;
if(count==5)return true;
}
else {
break;
}
}
//左上
for (int i = x-1,j = y-1; i >=0&&j>=0; i--,j--) {
if(qp[i][j].equals(qz)){
count++;
if(count==5)return true;
}
else {
count=1; break;
}
}
return false;
}
}
输入棋子越界或已有棋子:
赢棋: