ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

五子棋——Java语言

2020-08-07 05:00:23  阅读:262  来源: 互联网

标签:elements Java 语言 int 五子棋 else board && return


在《疯狂Java讲义》一书看到一部分“五子棋”的代码,一时兴起就自己尝试动手写了一个。

我写和书上的有很大的不同。不过当然是我写的太臃肿(烂,乱)了。

 

7月中旬左右写过俄罗斯方块,到后面,不过程序不按自己预期的运行,找不到Bug。于是先放弃了。

因此,这段时间还是心情比较低落的,不过还和生活不太顺利有关。

这次,应该说今天,写出了个五子棋。又找回了一点信心和编程的激情。

我写的这个游戏有无漏洞还不确定,暂时还没发现。

效果图

 

 

 

 

 

 

源代码

wuziqi.java

 

 1 public class wuziqi
 2 {
 3     public static void main(String[] args){
 4         showGameName();
 5         board board1 = new board();
 6         
 7         board1.newBoard();
 8         
 9         startgame();
10         
11     }
12 
13     public static boolean startgame(){
14         while(true){
15             //提示信息
16             System.out.println("white turn:");
17             /*玩家下一步旗*/
18             board.printChessman('○');
19             /*检查一次是否结束*/
20             if(tools.chekGame('○')){System.out.println("Game Over\nwhite have Win");return false;}
21             //提示信息
22             System.out.println("black turn:");
23             /*电脑下一步旗*/
24             board.printChessman('●');
25             /*检查一次是否结束*/
26             if(tools.chekGame('●')){System.out.println("Game Over\nblack have Win");return false;}
27         }
28         
29     }
30     public static void showGameName(){
31         System.out.println("***********");
32         System.out.println("  五子棋");
33         System.out.println("***********");
34     }
35 
36 
37 }

 

board.java

 1 import java.util.Scanner;
 2 import java.util.Date;
 3 public class board
 4 {
 5     /*这是一个15x15的棋盘*/
 6     public static int Board_Size = 15;
 7     /*用俩个二维数组定义棋盘*/
 8     public static char elements[][]=new char[Board_Size][Board_Size];
 9     public static int boards[][]=new int[Board_Size][Board_Size];
10     /*初始化棋盘 并 绘制*/
11     public static void newBoard(){
12         for(int i=0;i<Board_Size;i++){//初始化
13             for(int j=0;j<Board_Size;j++){
14                 elements[i][j]='╂';
15             }
16         }
17         /*绘制*/
18         System.out.println("   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4");//打印标尺 列数
19         for(int i=0;i<Board_Size;i++){
20             System.out.print(i);if(i<10){System.out.print(" ");}////打印标尺 行数
21             for(int j=0;j<Board_Size;j++){
22                 if(tools.tool1(i,j)&&tools.tool2(i,j))//检查4个角和4条边
23                 System.out.print(elements[i][j]);
24             }System.out.println();
25         }
26     }
27     /*绘制棋盘的方法*/
28     public static void paintBoard(){
29         System.out.println("   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4");//打印标尺 列数
30         for(int i=0;i<Board_Size;i++){
31             System.out.print(i);if(i<10){System.out.print(" ");}////打印标尺 行数
32             for(int j=0;j<Board_Size;j++){
33                 System.out.print(elements[i][j]);
34             }System.out.println();
35         }
36     }
37     /*根据用户输入的坐标打印旗子*/
38     public static void printChessman(char C){
39         while(true){
40         Scanner scanner = new Scanner(System.in);
41         System.out.println("请输入x:");
42         int x = scanner.nextInt();
43         System.out.println("请输入y:");
44         int y = scanner.nextInt();
45         if(x<=14&&x>=0&&y<=14&&y>=0&&(elements[x][y]!='○'&&elements[x][y]!='●')){
46             elements[x][y]=C;break;
47         }else{System.out.println("输入坐标无效!");}
48         }
49         
50         paintBoard();
51     }
52         /*根据用户输入的坐标打印旗子2*/
53     public static void printChessman2(){
54         while(true){
55         Scanner scanner = new Scanner(System.in);
56         System.out.println("请输入x:");
57         int x = scanner.nextInt();
58         System.out.println("请输入y:");
59         int y = scanner.nextInt();
60         if(x<=14&&x>=0&&y<=14&&y>=0&&(elements[x][y]!='○'&&elements[x][y]!='●')){
61             elements[x][y]='●';break;
62         }else{System.out.println("输入坐标无效!");}
63         }
64         
65         paintBoard();
66     }
67 }

tools.java

 1 public class tools 
 2 {
 3     public static boolean tool1(int x,int y){//检查棋盘四个角
 4         if(x==0&&y==0){board.elements[x][y]='┎';return true;}
 5         else if(x==0&&y==board.Board_Size-1){board.elements[x][y]='┒';return true;}
 6         else if(x==board.Board_Size-1&&y==0){board.elements[x][y]='┖';return true;}
 7         else if(x==board.Board_Size-1&&y==board.Board_Size-1){board.elements[x][y]='┚';return true;}
 8         return true;
 9     }
10     public static boolean tool2(int x,int y){//检查棋盘四条边缘
11         if(x==0&&y!=board.Board_Size-1&&y!=0){board.elements[x][y]='┰';return true;}
12         else if(x==board.Board_Size-1&&y!=board.Board_Size-1&&y!=0){board.elements[x][y]='┸';return true;}
13         else if(y==0&&x!=board.Board_Size-1&&x!=0){board.elements[x][y]='┠';return true;}
14         else if(y==board.Board_Size-1&&x!=board.Board_Size-1&&x!=0){board.elements[x][y]='┨';return true;}
15         return true;
16     }
17     public static boolean chekGame(char C){/*检查是否有同色的旗子连着5个*/
18         for(int i=0;i<board.Board_Size;i++){
19             for(int j=0;j<board.Board_Size;j++){
20                 int count1=0;
21                 int count2=0;
22                 int count3=0;
23                 int count4=0;
24                 /*检查是否有5个竖着连着*/
25                 if(board.elements[i][j]==C){
26                 for(int x=1;x<=4;x++){
27                     if(i-x>=0){if(board.elements[i-x][j]==C){count1++;}else{break;}}
28                     else{break;}
29                 }
30                 for(int x=1;x<=4;x++){
31                     if(i+x<=14){if(board.elements[i+x][j]==C){count1++;}else{break;}}
32                     else{break;}
33                 }System.out.println("棋子的纵向:"+count1);}
34                 
35                 if(count1>=4){return true;}//退出
36                 ////////////////////////////////////////////////////////////////////////
37                 /*检查是否有5个横着连着*/
38                 if(board.elements[i][j]==C){
39                 for(int x=1;x<=4;x++){
40                     if(j-x>=0){if(board.elements[i][j-x]==C){count2++;}else{break;}}
41                     else{break;}
42                 }
43                 for(int x=1;x<=4;x++){
44                     if(j+x<=14){if(board.elements[i][j+x]==C){count2++;}else{break;}}
45                     else{break;}
46                 }
47                 System.out.println("棋子的横向:"+count2);}
48                 
49                 if(count2>=4){return true;}//退出
50                 ////////////////////////////////////////////////////////////////////////
51                 /*检查是否有5个左斜个连着*/
52                 if(board.elements[i][j]==C){
53                 for(int x=1;x<=4;x++){
54                     if(j-x>=0&&i-x>=0){if(board.elements[i-x][j-x]==C){count3++;}else{break;}}
55                     else{break;}
56                 }
57                 for(int x=1;x<=4;x++){
58                     if(j+x<=14&&i+x<=14){if(board.elements[i+x][j+x]==C){count3++;}else{break;}}
59                     else{break;}
60                 }
61                 System.out.println("棋子的左斜:"+count3);}
62                 
63                 if(count3>=4){return true;}//退出
64                 ////////////////////////////////////////////////////////////////////////
65                 /*检查是否有5个右斜个连着*/
66                 if(board.elements[i][j]==C){
67                 for(int x=1;x<=4;x++){
68                     if(j+x<=14&&i-x>=0){if(board.elements[i-x][j+x]==C){count4++;}else{break;}}
69                     else{break;}
70                 }
71                 for(int x=1;x<=4;x++){
72                     if(j-x>=0&&i+x<=14){if(board.elements[i+x][j-x]==C){count4++;}else{break;}}
73                     else{break;}
74                 }
75                 System.out.println("棋子的右斜:"+count4);}
76                 
77                 if(count4>=4){return true;}//退出
78                 
79             }
80         }
81         return false;
82     }
83 }

写于大一暑假

2020-8-7 04:27:09

 

标签:elements,Java,语言,int,五子棋,else,board,&&,return
来源: https://www.cnblogs.com/flyingpenguins/p/13450168.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有