<>使用二维数组,实现五子棋功能
注意:我们使用的是纯面向过程的思想!!!
我们先来看一下棋盘长什么样
知道了模样,我们还需要知道程序需要哪些功能,根据这些功能写出相应的方法.那既然用到方法,我们需要确定有什么全局变量
static String white = "☆";//白棋子 static String black = "★";//黑棋子 static String[]
[] qp = new String[15][15];//棋盘数组 static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌"
, "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};//棋盘格线标识 static String line
= "十";//可下子位置
<>1.棋盘的初始化,init
首先棋盘肯定是要进行初始化,也就是在这个二维数组中存储数据,这个当然要需要用到二维数组的遍历来赋值,也就是二重循环.我们给这个方法命名为init.那需不需要初始化后直接打印.很明显不行,因为每下一个棋子都需要打印.所以还需要一个打印的方法
public static void init() {//棋盘初始化 for (int i = 0; i < qp.length; i++) { for (
int j = 0; j < qp[i].length; j++) { qp[i][j] = line; } }//此时得到的全是格子线 for (int j
= 0; j < qp.length; j++) { for (int k = 0; k < qp[j].length; k++) { if (k == qp[
j].length - 1) { qp[j][k] = num[j]; } if (j == qp.length - 1) { qp[j][k] = num[k
]; } } }//将格子线边界变成数字 }
<>2.打印棋盘,print
这个就比较简单,直接遍历输出即可
public static void print() {//打印棋盘 for (int i = 0; i < qp.length; i++) { for (
int j = 0; j < qp[i].length; j++) { System.out.print(qp[i][j]); } System.out.
println(); } }
<>3.开始游戏,startGame
这里其实就可以说是主函数了,我们打印出棋盘以后,肯定要进行下子,首先肯定要注意的是下子的位置是否正确如果不正确那么就继续重新下.很明显是先执行再判断
<>判断下子位置是否合法
public static boolean checkPoint(int x, int y) {//判断棋子是否可以落子 if (x<15 && y<15
&& qp[x - 1][y - 1] == line ){//不能超出棋盘并且这个位置是"+" return true; }else return false
; }
<>每下一次就需要判断输赢
这个比较麻烦,纯面向过程的话需要考虑三个方向
public static boolean isWin(int x, int y) {//判断输赢 boolean a1 = Homework.ord(x,y
); boolean a2 = Homework.abs(x,y); boolean a3 = Homework.sla(x,y); if(a1==true||
a2==true||a3==true){//斜向横向纵向只要有一个成立就赢 return true; }else{ return false; } }
判断三个方向也很简单,思路是一样的.我们拿横向来说
首先下子.我们判断横向有没有五子连棋.只需要创造一个计数器.计数器的值初始为一(因为已经下了一个字).之后左边利用for循环减一,有相同的计数器就加一,没有就用break结束此循环.右边同理.只要循环下来计数器的值为5.那么就赢了.
public static boolean ord(int x,int y){//判断纵向有没有五子连棋 int num = 1; for (int i =
1; x-1+i < qp.length; i++) { if(qp[x-1+i][y-1]==qp[x-1][y-1]){ num += 1; }else{
break; } } for (int i = 1; x-1-i >= 0; i++){ if(qp[x-i-1][y-1]==qp[x-1][y-1]){
num+= 1; }else{ break; } } if(num==5){ return true; }else{ return false; } }
public static boolean abs(int x,int y){//判断横向有没有五子连棋 int num = 1; for (int i = 1
; y-1+i < qp.length; i++) { if(qp[x-1][y-1+i]==qp[x-1][y-1]){ num ++; }else{
break; } } for (int i = 1; y-1-i >= 0; i++){ if(qp[x-1][y-1-i]==qp[x-1][y-1]){
num++; }else{ break; } } if(num==5){ return true; }else{ return false; } }
public static boolean sla(int x,int y){//判断斜向有没有五子连棋 int num = 1; for (int i = 1
; x-1+i< qp.length && y-1-i>=0; i++) { if(qp[x-1+i][y-1-i]==qp[x-1][y-1]){ num
++; }else{ break; } } for (int i = 1; y-1+i <qp[i].length && x-1-i>=0; i++){ if(
qp[x-1-i][y-1+i]==qp[x-1][y-1]){ num ++; }else{ break; } } if(num==5){ return
true; }else{ num = 1; for (int i = 1; x-1+i < qp.length && y-1+i<qp[i].length; i
++) { if(qp[x-1+i][y-1+i]==qp[x-1][y-1]){ num ++; }else{ break; } } for (int i =
1; x-1-i >=0 && y-1-i>=0; i++) { if(qp[x-1-i][y-1-i]==qp[x-1][y-1]){ num ++; } }
if(num==5){ return true; }else{ return false; } } }
!!!
!!!
!!!
这里当然还有一些细节,比如怎么让两人重复下子.或者说有人赢了怎么结束,这些细节方法都很简单,就不做过多强调.程序整体参下
import java.util.Scanner; public class Homework { static String white = "☆";
static String black = "★"; static String[][] qp = new String[15][15]; static
String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔",
"⒕", "⒖"}; static String line = "十"; static boolean check = true; public static
void main(String[] args) { Homework.init(); Homework.print(); Homework.startGame
(); } public static void startGame() {//开始游戏 int x=0,y=0; Scanner scanner = new
Scanner(System.in); a:while (true) { do { System.out.println("请黑棋下子"); System.
out.println("请输入行"); x = scanner.nextInt(); System.out.println("请输入列"); y =
scanner.nextInt(); check = Homework.checkPoint(x, y); } while (check == false);
qp[x-1][y-1] = black; Homework.print(); boolean win1 = Homework.isWin(x,y); if(
win1==true){ System.out.println("黑棋胜利"); break a; } do{ System.out.println(
"请白棋下子"); System.out.println("请输入行"); x = scanner.nextInt(); System.out.println(
"请输入列"); y = scanner.nextInt(); check = Homework.checkPoint(x, y); }while (check
== false); qp[x-1][y-1] = white; Homework.print(); boolean win2 = Homework.isWin
(x,y); if(win2==true){ System.out.println("白棋胜利"); break a; } } } public static
boolean checkPoint(int x, int y) {//判断棋子是否可以落子 if (x<15 && y<15 && qp[x - 1][y -
1] == line ){ return true; }else return false; } public static boolean isWin(int
x, int y) {//判断输赢 boolean a1 = Homework.ord(x,y); boolean a2 = Homework.abs(x,y
); boolean a3 = Homework.sla(x,y); if(a1==true||a2==true||a3==true){ return true
; }else{ return false; } } public static boolean ord(int x,int y){//判断纵向有没有五子连棋
int num = 1; for (int i = 1; x-1+i < qp.length; i++) { if(qp[x-1+i][y-1]==qp[x-1
][y-1]){ num += 1; }else{ break; } } for (int i = 1; x-1-i >= 0; i++){ if(qp[x-i
-1][y-1]==qp[x-1][y-1]){ num += 1; }else{ break; } } if(num==5){ return true; }
else{ return false; } } public static boolean abs(int x,int y){//判断横向有没有五子连棋 int
num= 1; for (int i = 1; y-1+i < qp.length; i++) { if(qp[x-1][y-1+i]==qp[x-1][y-
1]){ num ++; }else{ break; } } for (int i = 1; y-1-i >= 0; i++){ if(qp[x-1][y-1-
i]==qp[x-1][y-1]){ num ++; }else{ break; } } if(num==5){ return true; }else{
return false; } } public static boolean sla(int x,int y){//判断斜向有没有五子连棋 int num =
1; for (int i = 1; x-1+i< qp.length && y-1-i>=0; i++) { if(qp[x-1+i][y-1-i]==qp[
x-1][y-1]){ num ++; }else{ break; } } for (int i = 1; y-1+i <qp[i].length && x-1
-i>=0; i++){ if(qp[x-1-i][y-1+i]==qp[x-1][y-1]){ num ++; }else{ break; } } if(
num==5){ return true; }else{ num = 1; for (int i = 1; x-1+i < qp.length && y-1+i
<qp[i].length; i++) { if(qp[x-1+i][y-1+i]==qp[x-1][y-1]){ num ++; }else{ break;
} } for (int i = 1; x-1-i >=0 && y-1-i>=0; i++) { if(qp[x-1-i][y-1-i]==qp[x-1][y
-1]){ num ++; } } if(num==5){ return true; }else{ return false; } } } public
static void init() {//棋盘初始化 for (int i = 0; i < qp.length; i++) { for (int j = 0
; j < qp[i].length; j++) { qp[i][j] = line; } } for (int j = 0; j < qp.length; j
++) { for (int k = 0; k < qp[j].length; k++) { if (k == qp[j].length - 1) { qp[j
][k] = num[j]; } if (j == qp.length - 1) { qp[j][k] = num[k]; } } } } public
static void print() {//打印棋盘 for (int i = 0; i < qp.length; i++) { for (int j = 0
; j < qp[i].length; j++) { System.out.print(qp[i][j]); } System.out.println(); }
} }