# 学习要点
1.数据结构
2.下标要认清
3.二维数组的遍历
# 维度
0维:点(单独的一个数据)
1维:由无数个点构成的一条线(由多个单独数据构成的一维数组)
2维:由无数条线构成的一个面(由多个一维数组构成的二维数组)
3维:由无数的面构成的立方体(由多个二维数组构成的三维数组)
4维:由无数个立方体构成的超立方体(由多个三维数组构成的四维数组)
5维:由多个超立方体构成的平行宇宙(由多个四维数组构成的五维数组)
总结:n维数组是由多个n-1维数组构成的(n>0)
# 二维数组名词属性
元素:一维数组
长度:一维数组的个数
二维数组元素的访问
- 访问二维数组里面某个一维数组 arr[下标]
- 访问二维数组里面最底层的元素 arr[x][y]
# 二维数组的创建
- 静态
int[][] arr = { {1,2,3}, {4,5,6},{7,8,9} }
- 动态
int[][] arr = new int[a][b];
a:代表二维数组中一维数组的个数
b:代表每一个一维数组元素的个数
可以理解为 a行b列
# 二维数组的遍历
例:
//遍历数组
System.out.print("+ ");
for (int i = 1; i <= col; i++) {
System.out.print(i+" ");
}
System.out.println();
for (int i = 0; i < cha.length; i++) {
System.out.print(i+1+" ");
for (int j = 0; j < cha[i].length; j++) {
cha[i][j]='O';
System.out.print(cha[i][j]+" ");
}
System.out.println();
}
用java写的打地鼠游戏:
package demo;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
int row=4;
int col=5;
char[][] cha=new char[row][col];
int xun=3;//游戏次数
int mark=0;//初始分数
System.out.println("*****************");
System.out.println("欢迎来到打地鼠");
System.out.println("*****************");
Random ran = new Random();
Scanner scan = new Scanner(System.in);
// 随机洞穴数量
// int a = ran.nextInt(8) + 2;
// System.out.println("------------------");
// System.out.println("洞穴数量【" + a + "】");
//遍历数组
System.out.print("+ ");
for (int i = 1; i <= col; i++) {
System.out.print(i+" ");
}
System.out.println();
for (int i = 0; i < cha.length; i++) {
System.out.print(i+1+" ");
for (int j = 0; j < cha[i].length; j++) {
cha[i][j]='O';
System.out.print(cha[i][j]+" ");
}
System.out.println();
}
//循环xun次
int ee=0;//打中次数
int miss=0;//没打中次数
for(int a=1;a<=xun;a++) {
System.out.println();
System.out.println("第【"+a+"】次游戏,当前分数为【"+mark+"】");
for (int i = 0; i < cha.length; i++) {
for (int j = 0; j < cha[i].length; j++) {
cha[i][j]='O';
}
}
int r=ran.nextInt(row);
int c=ran.nextInt(col);
cha[r][c] ='x';
System.out.println("按88-重新开始,按99-退出游戏");
System.out.println("请注意,地鼠已出现!");
System.out.println("-------------------");
//打印图表
System.out.print("+ ");
for (int i = 1; i <= col; i++) {
System.out.print(i+" ");
}
System.out.println();
for (int i = 0; i < cha.length; i++) {
System.out.print(i+1+" ");
for (int j = 0; j < cha[i].length; j++) {
System.out.print(cha[i][j]+" ");
}
System.out.println();
}
//判断是否打中
System.out.println("请输入行:");
int aa=scan.nextInt();
if(aa==88) {
a=0;
mark=0;
ee=0;
miss=0;
System.out.println("本次游戏结束,已重新开始!");
continue;
}else if(aa==99) {
System.out.println("游戏已退出!");
return;
}
System.out.println("请输入列:");
int bb=scan.nextInt();
if(aa==r+1 && bb==c+1) {
System.out.println("恭喜您成功打中地鼠!分数 +2");
mark=mark+2;
ee++;
}else if(aa==88 || bb==88) {
a=0;
mark=0;
ee=0;
miss=0;
System.out.println("本次游戏结束,已重新开始!");
continue;
}else if(aa==99 || bb==99) {
System.out.println("游戏已退出!");
return;
}else {
System.out.println("很遗憾没能打中!分数 -1");
mark=mark-1;
miss++;
}
}
System.out.println("游戏结束!您一共打中【"+ee+"】次,没打中【"+miss+"】次,总分为【"+mark+"】");
}
}
棋盘游戏1.0:
package demo;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
System.out.println("*********************");
System.out.println("欢迎使用棋盘程序1.0");
System.out.println("*********************");
System.out.println("初始棋盘=============》");
System.out.println();
Random ran = new Random();
Scanner scan = new Scanner(System.in);
int row = 4; // 棋盘行数
int col = 2; // 棋盘列数
char[][] cha = new char[row][col];
for (int i = 0; i < cha.length; i++) {
for (int j = 0; j < cha[i].length; j++) {
cha[i][j] = '-';
}
}
System.out.print("+ ");
for (int i = 1; i <= col; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < cha.length; i++) {
System.out.print(i + 1 + " ");
for (int j = 0; j < cha[i].length; j++) {
System.out.print(cha[i][j] + " ");
}
System.out.println();
System.out.println();
}
while (true) {
int total = 0;
System.out.println("输入行与列进行落子,【0-退出】");
System.out.println("请输入落子行号:");
int r = scan.nextInt();
// 结束程序
if (r == 0) {
System.out.println("游戏结束!");
break;
}
System.out.println("请输入落子列号:");
int c = scan.nextInt();
// 结束程序
if (c == 0) {
System.out.println("游戏结束!");
break;
}
System.out.println("玩家落子后==========》");
System.out.println("----------------------");
if (r > 0 && r <= row && c > 0 && c <= col) {
if (cha[r - 1][c - 1] == '-') {
cha[r - 1][c - 1] = 'o';
} else {
System.out.println("此处有子,无法落子!");
continue;
}
} else {
System.out.println("超出范围,无法落子!");
continue;
}
System.out.print("+ ");
for (int i = 1; i <= col; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < cha.length; i++) {
System.out.print(i + 1 + " ");
for (int j = 0; j < cha[i].length; j++) {
System.out.print(cha[i][j] + " ");
}
System.out.println();
System.out.println();
}
// 电脑落子
while (true) {
int a = ran.nextInt(row);
int b = ran.nextInt(col);
if (cha[a][b] == '-') {
cha[a][b] = 'x';
break;
}
}
System.out.println("----------------------");
System.out.println("电脑落子后==========》");
System.out.println("----------------------");
// 打印图表
System.out.print("+ ");
for (int i = 1; i <= col; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < cha.length; i++) {
System.out.print(i + 1 + " ");
for (int j = 0; j < cha[i].length; j++) {
System.out.print(cha[i][j] + " ");
}
System.out.println();
System.out.println();
}
// 填满结束程序
while (true) {
for (int i = 0; i < cha.length; i++) {
for (int j = 0; j < cha[i].length; j++) {
if (cha[i][j] == 'o' || cha[i][j] == 'x') {
total++;
}
}
}
if (total == row * col) {
System.out.println("棋盘填满,游戏结束!");
return;
} else if (total < row * col) {
break;
}
}
}
}
}
# 扫雷程序
- 利用二维数组构建游戏地图
int[][] map
- 展示地图
+ 1 2 3 4 5
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
5 0 0 0 0 0
- 布雷
随机指定数量的雷(10个雷)
难点:随机10个位置不能重复
- 排雷
输入行号,输入列号
判断如果此位置是雷,直接爆炸,程序结束
如果不是雷,计算这个位置的元素周围由几个雷
把雷的数量记录在当前格子中(注意考虑越界的情况)
- 重置地图
- 退出程序
遇到的问题:
如何判断棋盘落满并退出游戏
解决方法:在棋盘的while循环里面在嵌套一层while循环,并在第二层while循环里面遍历一遍数组,用if去判断,当遍历到的某个位置有落子时 落子总数total+1(total从0开始),当total==棋盘能落子的最大值时,用return退出main方法。