五子棋小游戏:
今天写一个五子棋小游戏,大家都玩过,所以就不用介绍规则了,主要是用逻辑实现,2代表黑棋,1代表白棋。
要写这个程序呢,首先我们要有一个棋盘,还是老样子用二维数组实现,当你new一下后就在堆中开辟了一片空间,数组中的默认值都是0。
public static void main(String[] args) {
int[][] a = new int[10][10];
int color = 1;
Scanner scanner = new Scanner(System.in);
int x = 0;
int y = 0;
System.out.println("黑方先落子");
棋盘现在有了,要判断条件吧,横行竖列满5就会赢,这里我通过总数相加判断,连续的5个棋子为同一颜色时是否获胜。黑棋是2,当黑棋5个相连时,总数应该为10,当然了,白棋5个相连总数就是5。这里以行为例:
public static Boolean isHeng(int[][] a, int x, int y, int color){ //判断行是否获胜
int count = 1;
int tempy = y;
while (y > 0 && a[x][y - 1] == color){
count++;
y--;
}
y = tempy;
while (y < a[x].length - 1 && a[x][y + 1] == color){
count++;
y++;
}
return count >= 5;
}
然后用户要不断的落子,你不知道他什么时候能赢,所以这里我在main()里用了do-while()循环,现在要给循环一个判断条件吧,横行竖列任意一个方向连成5个就会跳出循环。判断条件代码为:
public static Boolean isSuccess(int[][] a, int x, int y, int color){ //判断获胜的条件
Boolean f = false;
if(isHeng(a, x, y, color) | isLie(a, x, y, color) | isZuoxie(a, x, y, color) | isYouxie(a, x, y, color)){
return true;
}else{
return f;
}
}
好了,大致的框子就搭好了,其实和关灯游戏差不多,只是繁琐了一点。以下是具体代码实现。
import java.sql.SQLOutput;
import java.util.Scanner;
public class 五子棋游戏 {
public static void main(String[] args) {
int[][] a = new int[10][10];
int color = 1;
Scanner scanner = new Scanner(System.in);
int x = 0;
int y = 0;
System.out.println("黑方先落子");
do {
show(a);
color = 3 - color;
System.out.println("请输入行:");
x = scanner.nextInt();
System.out.println("请输入列:");
y = scanner.nextInt();
if (a[x - 1][y - 1] == 0){
a[x - 1][y - 1] = color;
}else{
System.out.println("此处有子了,请重新落子");
System.out.println();
color = 3 - color;
continue;
}
System.out.println();
}while (!isSuccess(a, x - 1, y - 1, color));
System.out.println(color + "获胜");
}
public static void show(int[][] a){ //打印棋盘
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
public static Boolean isSuccess(int[][] a, int x, int y, int color){ //判断获胜的条件
Boolean f = false;
if(isHeng(a, x, y, color) | isLie(a, x, y, color) | isZuoxie(a, x, y, color) | isYouxie(a, x, y, color)){
return true;
}else{
return f;
}
}
public static Boolean isHeng(int[][] a, int x, int y, int color){ //判断行是否获胜
int count = 1;
int tempy = y;
while (y > 0 && a[x][y - 1] == color){
count++;
y--;
}
y = tempy;
while (y < a[x].length - 1 && a[x][y + 1] == color){
count++;
y++;
}
return count >= 5;
}
public static Boolean isLie(int[][] a, int x, int y, int color){ //判断列是否获胜
int count = 1;
int tempx = x;
while (x > 0 && a[x - 1][y] == color){
count++;
x--;
}
x = tempx;
while (x < a.length - 1 && a[x + 1][y] == color){
count++;
x++;
}
return count >= 5;
}
public static Boolean isZuoxie(int[][] a, int x, int y, int color){ //判断左斜是否获胜
int count = 1;
int tempx = x;
int tempy = y;
while (x > 0 && y > 0 && a[x - 1][y - 1] == color){
count++;
x--;
y--;
}
x = tempx;
y = tempy;
while (y < a.length - 1 && x < a.length - 1 && a[x + 1][y + 1] == color){
count++;
x++;
y++;
}
return count >= 5;
}
public static Boolean isYouxie(int[][] a, int x, int y, int color){ //判断右斜是否获胜
int count = 1;
int tempx = x;
int tempy = y;
while (x > 0 && y < a.length - 1 && a[x - 1][y + 1] == color){
count++;
x--;
y++;
}
x = tempx;
y = tempy;
while (y > 0 && x < a.length - 1 && a[x + 1][y - 1] == color){
count++;
x++;
y--;
}
return count >= 5;
}
}
执行结果为:
黑方先落子
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
请输入行:
其实这个程序不难的,用心一点就会完成的,加油~~~