package prectice;
import java.util.Scanner;
public class ts2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] pc = new int[4][2];
//1-定义棋盘 +
String[][] qp = new String[10][10];
//2-画棋盘
for(int i = 0;i<qp.length;i++){
for(int j = 0;j<qp[i].length;j++){
qp[i][j] = "┼";
}
}
//3-输出棋盘
for(int i = 0;i<qp.length;i++){
for(int j = 0;j<qp[i].length;j++){
System.out.print(qp[i][j]);
}
System.out.println();
}
while(true){//死循环
//4-人机对战:人先下 ;放置棋子:坐标 0,0
//4.1-输入坐标
System.out.println("请输入坐标x:");
int x = sc.nextInt();
System.out.println("请输入坐标y:");
int y = sc.nextInt();
//4.2-根据坐标放置棋子
qp[x][y] = "●";
/*电脑放置棋子
人放置棋子后,获取该位置的上下左右四个点
找其中一个空位置(1-无棋子;2-有效的),
作为电脑的放置棋子的位置
*/
//计算出电脑可以放置棋子的4个位置
pc[0][0] = x-1;
pc[0][1] = y;
pc[1][0] = x;
pc[1][1] = y+1;
pc[2][0] = x+1;
pc[2][1] = y;
pc[3][0] = x;
pc[3][1] = y-1;
//判断一个有效位置放置棋子
for(int i = 0;i<pc.length;i++){//循环4个位置
if(pc[i][0]>=0 &&pc[i][0]<10 && pc[i][1]>=0 && pc[i][1]<10){//判断i行的位置是否是一个有效的位置
int x1 = pc[i][0];
int y1 = pc[i][1];
if(qp[x1][y1] == "┼"){//该位置是一个空位置
qp[x1][y1] = "○";
break;
}
}
}
//4.3-输出棋盘
for(int i = 0;i<qp.length;i++){
for(int j = 0;j<qp[i].length;j++){
System.out.print(qp[i][j]);
}
System.out.println();
}
//判断输赢
/*
*
*/
//zuo
int count = 1;
for(int temp_y = y-1;temp_y>=0;temp_y--){
if(qp[x][temp_y] == qp[x][y]){
count++;
}else{
break;
}
}
//you
for(int temp_y = y+1;temp_y<10;temp_y++){
if(qp[x][temp_y] == qp[x][y]){
count++;
}else{
break;
}
}
//向左方向判断输赢
for(int temp_x=x-1;temp_x>=0;temp_x--){
if(qp[temp_x][y]==qp[x][y]){
count++;
}else{
break;
}
}
//向右方向判断输赢
for(int temp_x=x+1;temp_x<10;temp_x++){
if(qp[temp_x][y]==qp[x][y]){
count++;
}else{
break;
}
}
//向斜左判断
for(int temp_L=x+1, temp_R=y+1;temp_L<10 && temp_R<10;){
if(qp[temp_L][temp_R]==qp[x][y]){
count++;
temp_L++;
temp_R++;
}
else{
break;
}
}
//向斜右判断
for(int temp_L=x-1, temp_R=y-1;temp_L>0 && temp_R>0;){
if(qp[temp_L][temp_R]==qp[x][y]){
count++;
temp_L--;
temp_R--;
}
else{
break;
}
}
//向测上判断
for(int temp_L=x-1, temp_R=y+1;temp_L>0 && temp_R<10;){
if(qp[temp_L][temp_R]==qp[x][y]){
count++;
temp_L--;
temp_R++;
}
else{
break;
}
}
for(int temp_L=x+1, temp_R=y-1;temp_L<10 && temp_R>0;){
if(qp[temp_L][temp_R]==qp[x][y]){
count++;
temp_L++;
temp_R--;
}
else{
break;
}
}
if(count>=5){
System.out.println("恭喜你,赢了");
break;
}
}
}
}
程序仅实现了五子棋的简单的一些逻辑
,还有待修改。
Java简单实现五子棋游戏
最新推荐文章于 2024-01-09 15:08:47 发布