Java五子棋小游戏

本方案是基于控制台写的一个代码

没有花里胡哨的界面,只为研究算法

仅仅用了200行代码

2.0版本,增加Ai机器人算法,优化一些小bug,点击进入

下面是的是运行结果

游戏运行结果

这里我就很简单的复制了一个结果

第9回合,下子方:玩家2(白)
请输入你要下的位置(空格隔开) 例如 10 5
9 5
玩家2(白)赢得了胜利
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 · · · · · · · · · · · · · · · ·
1 · · · · · · · · · · · · · · · ·
2 · · · · · · · · · · · · · · · ·
3 · · · · · · · · · · · · · · · ·
4 · · · · · · · · · · · · · · · ·
5 · · · · · 白 黑 · · · · · · · · ·
6 · · · · · 白 · · · 黑 · · · · · ·
7 · · · · · 白 黑 · · · · · · · · ·
8 · · · · · 白 黑 · · · · · · · · ·
9 · · · · · 白 · · · · · · · · · ·
10 · · · · · · · · · · · · · · · ·
11 · · · · · · · · · · · · · · · ·
12 · · · · · · · · · · · · · · · ·
13 · · · · · · · · · · · · · · · ·
14 · · · · · · · · · · · · · · · ·
15 · · · · · · · · · · · · · · · ·
游戏结束

下面是Java代码

有什么地方还有问题欢迎评论


import java.util.Scanner; /**
* 控制台五子棋游戏
*/
public class Gobang {
private boolean gameover = false;
//15*15棋盘
private char[][] table = new char[16][16];
//两个玩家
private Player p1,p2;
//回合
private int huihe = 0; public void createGame(Player p1,Player p2){
this.p1=p1;
this.p2=p2;
}
/** 展示棋局 **/
private void show(){
int xx =0;
System.out.println(" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ");
for(char[] cs :table){
System.out.print(" "+xx+(xx>9?"":" ")+" ");
for(char c : cs){
if(c==0) System.out.print("·");
System.out.print(c+" ");
}
System.out.println();
xx++;
}
}
/** 获取下一个走棋的 **/
private Player getPlayer(){ //黑子先走
if (huihe==0) return p1.color=='黑'? p1 : p2;
//根据回合判断棋子颜色
if (huihe%2!=0) return p1.color=='白'? p1 : p2;
return p1.color=='黑'? p1 : p2;
} /** 判断是否获胜 **/
private boolean isWin(int x,int y,char c){
/*
* 7 8 9
* 4 5 6
* 1 2 3
*/
int xx = 0,yy=0;
for (int i =1 ;i<9 ;i++ ){
switch (i){
case 1:
xx=-1;yy=-1;
break;
case 2:
xx=-1;yy=1;
break;
case 3:
xx=1;yy=-1;
break;
case 4:
xx=1;yy=1;
break;
case 5:
xx = 0;yy = 1;
break;
case 6:
xx = 1 ;yy = 0;
break;
case 7:
xx = 0 ;yy = -1;
break;
case 8:
xx = -1;yy= 0;
break;
} if ( (x<4&&xx==-1) || (y<4&&yy==-1)){ }else if((x>12&&xx==1) || (y>12&&yy==1)){ }else if(x<4&&y<4&&(xx!=-1&&yy!=-1)){
if (ishas(x, y, xx, yy, 4, c))
return true;
}else if(x>12&&y>12&&(xx!=1&&yy!=1)){
if (ishas(x, y, xx, yy, 4, c))
return true;
}else if(xx==0||yy==0){
if (ishas(x, y, xx, yy, 4, c))
return true;
}
if(x>2&&y>2&&x<14&&y<14) {
if (ishas(x, y, xx, yy, 2, c)&&ishas(x, y, -xx, -yy, 2, c)) {
return true;
}
} if(x>3&&y>3&&x<15&&y<15) {
if (ishas(x, y, xx, yy, 3, c)&&ishas(x, y, -xx, -yy, 1, c)) {
return true;
}
}
if(x>1&&y>1&&x<13&&y<13) {
if (ishas(x, y, xx, yy, 1, c)&&ishas(x, y, -xx, -yy, 3, c)) {
return true;
}
}
}
return false;
} /**
* 检测是否有棋子
* @param x x坐标
* @param y y坐标
* @param xx x方向
* @param yy y方向
* @param num 缓存
* @return
*/
private boolean ishas(int x,int y,int xx,int yy,int num,char c){
if(num==1){
if(table[x+xx][y+yy] == c)return true;
}else if(table[x+xx][y+yy] == c){
return ishas(x+xx,y+yy,xx,yy,num-1,c);
}
return false;
} /** 下棋 **/
public boolean put(int x,int y,Player p){
if (table[x][y]==0) {
table[x][y] = p.getColor(); if(isWin(x,y,p.color)){
gameover = true;
System.out.println(p.username+"("+p.color+")赢得了胜利");
}
return true;
}
return false;
} /** 游戏运行 **/
public void start(){
Player p = null;
Scanner scan = new Scanner(System.in);
String[] strArr = new String[2];
while (!gameover){
if(p==null)p=getPlayer();
System.out.println("第"+(huihe/2+1)+"回合,下子方:"+p.getUsername()+"("+p.getColor()+")");
System.out.println("请输入你要下的位置(空格隔开) 例如 10 5");
//下棋失败重新开始本回合
try {
String in = scan.nextLine().trim();
if ("exit".equals(in)) break;
strArr = in.split(" ");
if (!put( Integer.parseInt(strArr[0]),Integer.parseInt(strArr[1]),p))continue;
}catch (Exception e){
e.printStackTrace();
} show(); p=null;
huihe++;
}
System.out.println("游戏结束"); }
/** 游戏入口 */
public static void main(String[] args) {
Gobang game = new Gobang();
Player p1 = new Player("玩家1",'黑');
Player p2 = new Player("玩家2",'白');
game.createGame(p1,p2);
game.start();
}
} /** 玩家类 **/
class Player{
String username;
char color; public Player(String username, char color) {
this.username = username;
this.color = color;
System.out.println(username+"携带"+color+"颜色的棋子加入游戏");
} public String getUsername() {
return username;
} public char getColor() {
return color;
}
}

2.0版本,增加Ai机器人算法,优化一些小bug,点击进入

Java控制台五子棋(纯算法)的更多相关文章

  1. Java中的查找算法之顺序查找(Sequential Search)

    Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...

  2. java控制台输入

    一.java控制台输入 java控制台输入有如下几个方法 1.JDK 1.4 及以下版本读取的方法 JDK 1.4 及以下的版本中要想从控制台中输入数据只有一种办法,即使用System.in获得系统的 ...

  3. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  4. Java中的经典算法之选择排序(SelectionSort)

    Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...

  5. Java中的排序算法(2)

    Java中的排序算法(2) * 快速排序 * 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). * 步骤为: * 1. 从数 ...

  6. [Java]局域网五子棋

    提示: 下面给的代码有问题哦,可以自己去调试 可用版下载 请点击这里 密码:x6ve(退出程序,端口并没有被关闭,可自行修改代码实现) img

  7. JAVA控制台输入输出方法总结

    java的控制台输入输出有很多方法,此文分别对其进行介绍. 1.控制台的输入 关于控制台的输入主要介绍三种方法,第一种方法使用BufferedReader获得控制台输入的数据,此方法是传统的输入方法, ...

  8. Java 控制台输入数字 输出乘法表(代码练习)

    最近,回忆了一些刚学习Java时经常练习的一些小练习题.感觉还是蛮有趣的,在回顾时想起好多学习时的经历和坎坷,一道小小的练习题要研究半天,珍重过往,直面未来.下面贡献代码,Java 控制台输入数字 输 ...

  9. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

随机推荐

  1. H3C PPP MP配置示例二(续)

  2. laravel5.*安装使用Redis以及解决Class 'Predis\Client' not found和Fatal error: Non-static method Redis::set() cannot be called statically错误

    https://phpartisan.cn/news/35.html laravel中我们可以很简单的使用Redis,如何在服务器安装Redis以及原创访问你们可以访问Ubuntu 设置Redis密码 ...

  3. [转]swagger2 入门教程

    swagger2 是一个规范和完整的框架,用于生成.描述.调用和可视化Restful风格的web服务,现在我们使用spring boot 整合它 作用: 1.接口的文档在线自动生成 2.功能测试 先介 ...

  4. 2018-2-13-WPF-DelegateCommand-出现Specified-cast-is-not-valid

    title author date CreateTime categories WPF DelegateCommand 出现Specified cast is not valid lindexi 20 ...

  5. C# 16 进制字符串转 int

    最近在写硬件,发现有一些测试是做 16 进制的字符串,需要把他转换为整形才可以处理. 本文告诉大家如何从 16 进制转整形 如果输入的是 0xaa 这时转换 int 不能使用 Parse 不然会出现异 ...

  6. java 打印流

    (只有两个,PrintWriter和PrintStream) 思考:如果现在要想完成一个字符串或者是boolean型或者是字符型的数据输出使用OutputStream是否方便? 肯定是不方便的,因为O ...

  7. 深度优先遍历 and 广度优先遍历

    深度优先遍历 and 广度优先遍历 遍历在前端的应用场景不多,多数是处理DOM节点数或者 深拷贝.下面笔者以深拷贝为例,简单说明一些这两种遍历.

  8. 51nod 1287加农炮

    1287 加农炮  题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个长度为M的正整数数组A,表示从左向右的地形高度.测试一种加农炮 ...

  9. ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(1)

    本系列的的角色权限管理主要采用Dotnet MVC4工程内置的权限管理模块Simplemembership实现,主要有关文件是InitializeSimpleMembershipAttribute.c ...

  10. CodeForces 906D (欧拉降幂)

    Power Tower •题意 求$w_{l}^{w_{l+1}^{w_{l+2}^{w_{l+3}^{w_{l+4}^{w_{l+5}^{...^{w_{r}}}}}}}}$ 对m取模的值 •思路 ...