五子棋练习

编程入门 行业动态 更新时间:2024-10-24 14:17:54

<a href=https://www.elefans.com/category/jswz/34/1769950.html style=五子棋练习"/>

五子棋练习

import java.util.Arrays;
import java.util.Scanner;
public class lianxi2 {static String white = "☆";static String black = "★";static String[][] qp = new String[15][15];static String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};static String line = "十";static boolean flag=true;static Scanner ac=new Scanner(System.in);public static void main(String[] a) {lianxi2.chushi();lianxi2.dayin();lianxi2.start();}public static void chushi(){for(int q=0;q< qp.length;q++){for (int w=0;w< qp.length;w++){qp[q][w]=line;if(w==qp.length-1){qp[q][w]=num[q];}if(q== qp.length-1){qp[q][w]=num[w];}}}}public static void dayin(){for(int q=0;q< qp.length;q++){for (int w=0;w< qp.length;w++){System.out.print(qp[q][w]);}System.out.println();}}public static void start() {out:while (true) {if (flag==true) {System.out.println("请黑方输入行与列");int y = ac.nextInt() - 1;int x = ac.nextInt() - 1;boolean sd = lianxi2.check(x, y);if (sd == true) {qp[y][x] = black;lianxi2.dayin();boolean cs = lianxi2.iswin(y,x,black);if(cs==true){System.out.println("黑方胜利");break out;}else {flag = false;}}else {System.out.println("坐标有误,请重新输入");}} else {System.out.println("请白方输入行和列");int y = ac.nextInt() - 1;int x = ac.nextInt() - 1;boolean sd = lianxi2.check(x, y);if (sd==true) {qp[y][x] = white;lianxi2.dayin();if(lianxi2.iwin(y,x,white)){System.out.println("白方胜利");break out;}else {flag = true;}} else {System.out.println("坐标有误,请重新输入");}}}}
//判断public static boolean check ( int x, int y){if (x < 0 || x > qp.length - 1 || y < 0 || y > qp.length - 1) {return false;}if (!qp[y][x].equals(line)) {return false;}return true;}public static boolean iswin(int y,int x,String black) {int heng = 1,xie1=1,shu=1,xie2=1;for (int lf = x - 1; lf >= 0; lf--) {if (!qp[y][lf].equals(black)) break;else heng++;}for (int rg = x + 1; rg <qp.length-1; rg++) {if (!qp[y][rg].equals(black)) break;else heng++;}for(int sh=y-1;sh>=0;sh--){if (!qp[sh][x].equals(black)) break;else shu++;}for(int xi=y+1;xi<qp.length-1;xi++){if (!qp[xi][x].equals(black)) break;else shu++;}for(int xiq=y-1,xwq=x-1;xiq>=0&&xwq>=0;xiq--,xwq--){if (!qp[xiq][xwq].equals(black)) break;else xie1++;}for(int xid=y+1,xwd=x+1;xid<qp.length-1&&xwd< qp.length-1;xid++,xwd++){if (!qp[xid][xwd].equals(black)) break;else xie1++;}for(int xia=y+1,xwa=x-1;xia< qp.length-1&&xwa>=0;xia++,xwa--){if (!qp[xia][xwa].equals(black)) break;else xie2++;}for(int xie=y-1,xwe=x+1;xie>=0&&xwe<qp.length-1;xie--,xwe++){if (!qp[xie][xwe].equals(black)) break;else xie2++;}return heng >= 5 || xie1 >= 5 || xie2 >= 5 || shu >= 5;}public static boolean iwin(int y,int x,String white) {int heng = 1,xie1=1,shu=1,xie2=1;for (int lf = x - 1; lf >= 0; lf--) {if (!qp[y][lf].equals(white)) break;else heng++;}for (int rg = x + 1; rg < qp.length-1; rg++) {if (!qp[y][rg].equals(white)) break;else heng++;}for(int sh=y-1;sh>=0;sh--){if (!qp[sh][x].equals(white)) break;else shu++;}for(int xi=y+1;xi<qp.length-1;xi++){if (!qp[xi][x].equals(white)) break;else shu++;}for(int xiq=y-1,xwq=x-1;xiq>=0&&xwq>=0;xiq--,xwq--){if (!qp[xiq][xwq].equals(white)) break;else xie1++;}for(int xid=y+1,xwd=x+1;xid<qp.length-1&&xwd< qp.length-1;xid++,xwd++){if (!qp[xid][xwd].equals(white)) break;else xie1++;}for(int xia=y+1,xwa=x-1;xia< qp.length-1&&xwa>=0;xia++,xwa--){if (!qp[xia][xwa].equals(white)) break;else xie2++;}for(int xie=y-1,xwe=x+1;xie>=0&&xwe<qp.length-1;xie--,xwe++){if (!qp[xie][xwe].equals(white)) break;else xie2++;}if(heng>=5||xie1>=5||xie2>=5||shu>=5)return true;else return false;}
}

更多推荐

五子棋练习

本文发布于:2024-02-12 20:08:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1689239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:五子

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!