Java练习——五子棋游戏


package day4;
import java.util.Scanner;public class game {
//声明static String white = "☆";static String black = "★";static String[][] qp = new String[15][15];static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};static String line = "十";public static void start() {init();//初始化print();//打印xiaqi();//下棋过程}public static void init() {for (int i = 0; i < 14; i++) {for (int j = 0; j < 14; j++) {qp[i][j] = line;}}for (int i = 0; i < 15; i++) {qp[14][i] = num[i];qp[i][14] = num[i];}}public static void print() {for (int i = 0; i < 15; i++) {for (int j = 0; j < 15; j++) {System.out.print(qp[i][j]);}System.out.println();}}public static void xiaqi() {Scanner scan = new Scanner(System.in);int x;int y;int count = 0;do {System.out.println("请输入下棋位置:");if (count % 2 == 0) {System.out.println("黑子下棋");} else {System.out.println("白子下棋");}x = scan.nextInt()-1;y = scan.nextInt()-1;if (((x+1< 15) &&( x+1 > 0))&& ((y+1 < 15) && (y+1 > 0))) {if (count % 2 == 0) {//如果为奇数就是黑棋否则为白if (qp[x][y].equals("十")) {//判断这个地方是否下过了qp[x][y] = black;count++;} else {agin();}} else {if (qp[x][y] == "十") {qp[x][y] = white;count++;} else {agin();}}print();win();} else {agin();}} while (true);}public static void agin() {System.out.println("输入有误,是否继续");Scanner s = new Scanner(System.in);boolean choose = s.nextBoolean();//判断是否要继续下棋if (choose == true) {print();xiaqi();} else {System.out.println("游戏结束");init();System.exit(0);}}public static void win(){for(int i=0;i<14;i++){for(int j=0;j<14;j++){if(qp[i][j].equals(black)&&qp[i][j+1].equals(black)&&qp[i][j+2].equals(black)&&qp[i][j+3].equals(black)&&qp[i][j+4].equals(black)){System.out.println("黑棋胜利");System.exit(0);}//横着5个else if(qp[i][j].equals(black)&&qp[i+1][j].equals(black)&&qp[i+2][j].equals(black)&&qp[i+3][j].equals(black)&&qp[i+4][j].equals(black)){System.out.println("黑棋胜利");System.exit(0);}//竖着五个else if(qp[i][j].equals(black)&&qp[i+1][j+1].equals(black)&&qp[i+2][j+2].equals(black)&&qp[i+3][j+3].equals(black)&&qp[i+4][j+4].equals(black)){System.out.println("黑棋胜利");System.exit(0);}//斜着五个else if(qp[i][j].equals(black)&&qp[i][j+1].equals(black)&&qp[i][j+2].equals(black)&&qp[i][j+3].equals(black)&&qp[i][j+4].equals(black)){System.out.println("白棋胜利");System.exit(0);}else if(qp[i][j].equals(black)&&qp[i+1][j].equals(black)&&qp[i+2][j].equals(black)&&qp[i+3][j].equals(black)&&qp[i+4][j].equals(black)){System.out.println("白棋胜利");System.exit(0);}else if(qp[i][j].equals(black)&&qp[i+1][j+1].equals(black)&&qp[i+2][j+2].equals(black)&&qp[i+3][j+3].equals(black)&&qp[i+4][j+4].equals(black)){System.out.println("白棋胜利");System.exit(0);}}}}}​​

Java练习——五子棋游戏


package day4;
import java.util.Scanner;public class game {
//声明static String white = "☆";static String black = "★";static String[][] qp = new String[15][15];static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};static String line = "十";public static void start() {init();//初始化print();//打印xiaqi();//下棋过程}public static void init() {for (int i = 0; i < 14; i++) {for (int j = 0; j < 14; j++) {qp[i][j] = line;}}for (int i = 0; i < 15; i++) {qp[14][i] = num[i];qp[i][14] = num[i];}}public static void print() {for (int i = 0; i < 15; i++) {for (int j = 0; j < 15; j++) {System.out.print(qp[i][j]);}System.out.println();}}public static void xiaqi() {Scanner scan = new Scanner(System.in);int x;int y;int count = 0;do {System.out.println("请输入下棋位置:");if (count % 2 == 0) {System.out.println("黑子下棋");} else {System.out.println("白子下棋");}x = scan.nextInt()-1;y = scan.nextInt()-1;if (((x+1< 15) &&( x+1 > 0))&& ((y+1 < 15) && (y+1 > 0))) {if (count % 2 == 0) {//如果为奇数就是黑棋否则为白if (qp[x][y].equals("十")) {//判断这个地方是否下过了qp[x][y] = black;count++;} else {agin();}} else {if (qp[x][y] == "十") {qp[x][y] = white;count++;} else {agin();}}print();win();} else {agin();}} while (true);}public static void agin() {System.out.println("输入有误,是否继续");Scanner s = new Scanner(System.in);boolean choose = s.nextBoolean();//判断是否要继续下棋if (choose == true) {print();xiaqi();} else {System.out.println("游戏结束");init();System.exit(0);}}public static void win(){for(int i=0;i<14;i++){for(int j=0;j<14;j++){if(qp[i][j].equals(black)&&qp[i][j+1].equals(black)&&qp[i][j+2].equals(black)&&qp[i][j+3].equals(black)&&qp[i][j+4].equals(black)){System.out.println("黑棋胜利");System.exit(0);}//横着5个else if(qp[i][j].equals(black)&&qp[i+1][j].equals(black)&&qp[i+2][j].equals(black)&&qp[i+3][j].equals(black)&&qp[i+4][j].equals(black)){System.out.println("黑棋胜利");System.exit(0);}//竖着五个else if(qp[i][j].equals(black)&&qp[i+1][j+1].equals(black)&&qp[i+2][j+2].equals(black)&&qp[i+3][j+3].equals(black)&&qp[i+4][j+4].equals(black)){System.out.println("黑棋胜利");System.exit(0);}//斜着五个else if(qp[i][j].equals(black)&&qp[i][j+1].equals(black)&&qp[i][j+2].equals(black)&&qp[i][j+3].equals(black)&&qp[i][j+4].equals(black)){System.out.println("白棋胜利");System.exit(0);}else if(qp[i][j].equals(black)&&qp[i+1][j].equals(black)&&qp[i+2][j].equals(black)&&qp[i+3][j].equals(black)&&qp[i+4][j].equals(black)){System.out.println("白棋胜利");System.exit(0);}else if(qp[i][j].equals(black)&&qp[i+1][j+1].equals(black)&&qp[i+2][j+2].equals(black)&&qp[i+3][j+3].equals(black)&&qp[i+4][j+4].equals(black)){System.out.println("白棋胜利");System.exit(0);}}}}}​​