由于还没有学到图像什么的,黑白棋只能用1 -1来表示 空棋盘用0表示
import java.util.Scanner; public class DEMO5 {//下棋算法 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("输入你要走的步数"); int step = in.nextInt(); System.out.println("输出第一个走的棋子的颜色 -1 或 1 "); int first = in.nextInt(); int second; if (first == 1) second = -1; else second = 1; int[][] chess_board = new int[10][10]; for (int i = 0; i < 10; i++) { chess_board[0][i] = 2; chess_board[9][i] = 2; chess_board[i][0] = 2; chess_board[i][9] = 2; } System.out.println("输入你要走的点的坐标 按数组坐标输入"); for (int i = 1; i < 9; i++) { for (int j = 1; j < 9; j++) { chess_board[i][j] = in.nextInt(); } }//initialize the board for (int i = 1; i <= step; i++) { if (i % 2 == 1) { int X = in.nextInt(); int Y = in.nextInt(); chess_board = check_valid(first, second, X + 1, Y + 1, chess_board); if(chess_board[9][9]==10)break; } if (i % 2 == 0) { int X = in.nextInt(); int Y = in.nextInt(); chess_board = check_valid(second, first, X + 1, Y + 1, chess_board); if(chess_board[9][9]==10)break; } } for (int i = 1; i < 9; i++) { for (int j = 1; j < 9; j++) { System.out.printf("%3d", chess_board[i][j]); } System.out.println(""); }//print output } public static int[][] check_valid(int mine, int opponent, int x, int y, int[][] arrays) { //right check int loop_count = 1; int check = 0; arrays [9][9]=2; while (arrays[x][y + loop_count] == opponent) { loop_count++; } if (arrays[x][y + loop_count] == mine && loop_count != 1) { check = 1; }//valid test if (check == 1) { for (int counter = 0; counter < loop_count; counter++) { arrays[x][y + counter] = mine; } } else arrays[9][9]++; //left check int loop_count1 = 1; int check1 = 0; while (arrays[x][y - loop_count1] == opponent) { loop_count1++; } if (arrays[x][y - loop_count1] == mine && loop_count1 != 1) { check1 = 1; }//valid test if (check1 == 1) { for (int counter = 0; counter < loop_count1; counter++) { arrays[x][y - counter] = mine; } }else arrays[9][9]++; //up check int loop_count2 = 1; int check2 = 0; while (arrays[x + loop_count2][y] == opponent) { loop_count2++; } if (arrays[x + loop_count2][y] == mine && loop_count2 != 1) { check2 = 1; }//valid test if (check2 == 1) { for (int counter = 0; counter < loop_count2; counter++) { arrays[x + counter][y] = mine; } }else arrays[9][9]++; // down check int loop_count3 = 1; int check3 = 0; while (arrays[x - loop_count3][y] == opponent) { loop_count3++; } if (arrays[x - loop_count3][y] == mine && loop_count3 != 1) { check3 = 1; }//valid test if (check3 == 1) { for (int counter = 0; counter < loop_count3; counter++) { arrays[x - counter][y] = mine; } }else arrays[9][9]++; //right down check int loop_count5 = 1; int check5 = 0; while (arrays[x + loop_count5][y + loop_count5] == opponent) { loop_count5++; } if (arrays[x + loop_count5][y + loop_count5] == mine && loop_count5 != 1) { check5 = 1; }//valid test if (check5 == 1) { for (int counter = 0; counter < loop_count5; counter++) { arrays[x + counter][y + counter] = mine; } }else arrays[9][9]++; //left down check int loop_count6 = 1; int check6 = 0; while (arrays[x + loop_count6][y - loop_count6] == opponent) { loop_count6++; } if (arrays[x + loop_count6][y - loop_count6] == mine && loop_count6 != 1) { check6 = 1; }//valid test if (check6 == 1) { for (int counter = 0; counter < loop_count6; counter++) { arrays[x + counter][y - counter] = mine; } }else arrays[9][9]++; //left up check int loop_count7 = 1; int check7 = 0; while (arrays[x - loop_count7][y - loop_count7] == opponent) { loop_count7++; } if (arrays[x - loop_count7][y - loop_count7] == mine && loop_count7 != 1) { check7 = 1; }//valid test if (check7 == 1) { for (int counter = 0; counter < loop_count7; counter++) { arrays[x - counter][y - counter] = mine; } }else arrays[9][9]++; //right up check int loop_count8 = 1; int check8 = 0; while (arrays[x - loop_count8][y + loop_count8] == opponent) { loop_count8++; } if (arrays[x - loop_count8][y + loop_count8] == mine && loop_count8 != 1) { check8 = 1; }//valid test if (check8 == 1) { for (int counter = 0; counter < loop_count8; counter++) { arrays[x - counter][y + counter] = mine; } }else arrays[9][9]++; return arrays; } }
输入和输出的样例
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)