五子棋代码实现
package day4;
import java.util.Scanner;
/*
1.
初始化棋盘 抽取到一个方法中
打印棋盘 抽取到一个方法中
2.
黑白双方交替下棋
打印棋盘
3.
判断是否越界
判断是否重复
判断输赢
*/
public class WZQ {
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];//棋盘
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
static boolean flag = true;//黑棋先下
public static void main(String[] args) {
init();
print();
start();
}
//启动五子棋游戏
public static void start() {
Scanner scan = new Scanner(System.in);
while (true) {
if (flag == true) {
System.out.println("请黑子下子");
System.out.println("请输入下子的x坐标");
int x = scan.nextInt() - 1;
System.out.println("请输入下子的y坐标");
int y = scan.nextInt() - 1;
boolean result = panduan(x, y);
if (result == false) {
System.out.println("您的输入有误");
continue;
}
qp[x][y] = black;
boolean yn = win(x, y);
if (yn == true) {
print();
System.out.println("恭喜黑方获胜");
break;
}
flag = false;
} else {
System.out.println("请白子下子");
System.out.println("请输入下子的x坐标");
int x = scan.nextInt() - 1;
System.out.println("请输入下子的y坐标");
int y = scan.nextInt() - 1;
boolean result = panduan(x, y);
if (result == false) {
System.out.println("您的输入有误");
continue;
}
qp[x][y] = white;
boolean yn = win(x, y);
if (yn == true) {
print();
System.out.println("恭喜白方获胜");
break;
}
flag = true;
}
print();
}
}
//初始化棋盘
public static void init() {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp.length; j++) {
qp[i][j] = line;
if (j == qp[i].length - 1) {
qp[i][j] = num[i];
}
if (i == qp.length - 1) {
qp[i][j] = num[j];
}
}
}
}
//打印棋盘
public static void print() {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp.length; j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
//判断是否越界
//判断是否重复
public static boolean panduan(int x, int y) {
if (x >= 14 || x < 0 || y >= 14 || y < 0) {
return false;
} else if (qp[x][y] != line) {
return false;
} else
return true;
}
//判断输赢
public static boolean win(int x, int y) {
//判断水平输赢
int count = 0;
for (int i = 0; i < qp.length - 1; i++) {
if (qp[x][i] != line) {
if (qp[x][i] == qp[x][i + 1]) {
count++;
} else
break;
}
}
//判断竖直输赢
for (int j = 0; j < qp.length-1; j++) {
if (qp[j][y] != line) {
if (qp[j][y] == qp[j+1][y]) {
count++;
} else
break;
}
}
//判断左上输赢
int j=y;
for(int i=x;i>0;i--) {
if (i == 0 || j == 0) {
break;
}
if (qp[i][j] != line) {
if (qp[i][j] == qp[i - 1][j - 1]) {
count++;
} else
break;
}
j--;
}
//判断左下输赢
j=y;
for(int i=x;i<qp.length-1;i++) {
if (i == qp.length-1|| j == 0) {
break;
}
if (qp[i][j] != line) {
if (qp[i][j] == qp[i + 1][j - 1]) {
count++;
} else
break;
}
j--;
}
//判断右上输赢
j=y;
for(int i=x;i>0;i--){
if(i==0||j==qp.length-1){
break;
}
if(qp[i][j]!=line){
if(qp[i][j]==qp[i-1][j+1]){
count++;
}else
break;
}
j--;
}
//判断右下输赢
j=y;
for(int i=x;i<qp.length-1;i++){
if(i==qp.length-1||j==qp.length-1){
break;
}
if(qp[i][j]!=line){
if(qp[i][j]==qp[i+1][j+1]){
count++;
}else
break;
}
j++;
}
//判断是否五子连成
if (count >= 4) {
return true;
} else
count = 0;
return false;
}
}
杨氏三角代码实现
public class triangle {
public static void main(String[] args) {
/* Scanner scanner=new Scanner(System.in);
System.out.println("请输入整数");
int n=scanner.nextInt();
int [][]a=new int[n][];
for(int i=0;i<n;i++){
a[i]=new int[i+1];//创建一个长度为i+1的整型数组,并将其赋值给a数组的第i个元素
for(int j=0;j<i;j++){
if(j==0||j==i){
a[i][j]=1;
}
else{
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
System.out.print(a[i][j]+" ");
}
System.out.println();
}*/
int [][]a=new int[8][8*2+1];
a[0][8]=1;
for(int i=1;i<a.length;i++){
for(int j=1;j<a[i].length-1;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j+1];
}
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
int n=a[i][j];
if(n==0){
System.out.print("\t");
}else{
System.out.print(n+"\t");
}
}
System.out.println();
}
}
}
数组的创建与遍历
import java.util.Arrays;
public class 二维数组与遍历 {
public static void main(String[] args) {
/*
二维数组:
数组中的每一个元素又是一个数组;
数据类型 [][] 数组名;
数据类型 数组名[][];
new int[3][4];
3--二维数组长度是3
4--标识二维数组中每一个一维数组长度是4
指定长度后创建,会自动对数组进行初始化
{{0,0,0,0},{0,0,0,0},{0,0,0,0}}
二维数组创建:
*/
//int [][]a=new int[3][4];
//System.out.println(a[2][1]);//0
//3--二维数组长度是3
//每一个一维数组并没有被创建
//{null,null,null}
/*int [][] a=new int[3][];
a[0]=new int[4];
a[1]=new int[10];
a[2]=new int[8];
System.out.println(Arrays.toString(a));*/
/*int[][] a={{1,2,3},{4,5,6},{7,8,9}};
System.out.println(a[1][2]);//6*/
// 二维数组遍历:
int[][] a={{1,2,3,3},{4,5,6,4},{7,8,9,5}};
//循环二维数组 从二位数组中每次取出一个一维数组
for(int i=0;i<a.length;i++){
//循环遍历一维数组
for(int j=0;j<a[i].length;j++) {
System.out.println(a[i][j]);
}
}
}
}