Java控制台五子棋(纯算法)
Java五子棋小游戏
本方案是基于控制台写的一个代码
没有花里胡哨的界面,只为研究算法
仅仅用了200行代码
下面是的是运行结果
游戏运行结果
这里我就很简单的复制了一个结果
第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;
}
}
Java控制台五子棋(纯算法)的更多相关文章
- Java中的查找算法之顺序查找(Sequential Search)
Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...
- java控制台输入
一.java控制台输入 java控制台输入有如下几个方法 1.JDK 1.4 及以下版本读取的方法 JDK 1.4 及以下的版本中要想从控制台中输入数据只有一种办法,即使用System.in获得系统的 ...
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- Java中的经典算法之选择排序(SelectionSort)
Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...
- Java中的排序算法(2)
Java中的排序算法(2) * 快速排序 * 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). * 步骤为: * 1. 从数 ...
- [Java]局域网五子棋
提示: 下面给的代码有问题哦,可以自己去调试 可用版下载 请点击这里 密码:x6ve(退出程序,端口并没有被关闭,可自行修改代码实现) img
- JAVA控制台输入输出方法总结
java的控制台输入输出有很多方法,此文分别对其进行介绍. 1.控制台的输入 关于控制台的输入主要介绍三种方法,第一种方法使用BufferedReader获得控制台输入的数据,此方法是传统的输入方法, ...
- Java 控制台输入数字 输出乘法表(代码练习)
最近,回忆了一些刚学习Java时经常练习的一些小练习题.感觉还是蛮有趣的,在回顾时想起好多学习时的经历和坎坷,一道小小的练习题要研究半天,珍重过往,直面未来.下面贡献代码,Java 控制台输入数字 输 ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
随机推荐
- JavaScript跨域问题
通过实现Ajax通信的主要限制,来源于跨域安全策略.默认情况下,XHR对象只能访问与包含它的页面位于同一个域中的资源.这种安全策略可以预防某些恶意行为.但是,实现合理的跨域请求对于开发某些浏览器应用程 ...
- uni-app学习记录01-pages配置项
{ // 每个页面都需要在pages里面去声明配置 "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/coll ...
- Python--day28--set去重
set去重:set依赖对象hash eq
- Python--day41--thread1.join()
在 Python 的多线程编程中,在实例代码中经常有 thread1.join()这样的代码.那么今天咱们用实际代码来解释一下 join 函数的作用. join的原理就是依次检验线程池中的线程是否结束 ...
- 4-3 调试代码命令 scrapy shell http://blog.jobbole.com/114496/(入口url)
调试代码命令 scrapy shell http://blog.jobbole.com/114496/(入口url)
- Js中“==”和“===”的区别
从字面上来讲,‘==’代表相等,‘===’代表严格相等. 具体来讲,比较过程如下: 比较过程: ‘==’: 1. 首先判断两个值的类型是否相同,如果相同,进行‘===’判断. 2. ...
- js基础——变量、作用域、内存
1.new关键字创建的是引用类型: eg. var box = new Object(); box.name = "Linda";//引用类型添加属性没问题 al ...
- Java中getBytes()方法--使用详解
getBytes()方法详解 在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组.这表示在不同的操作系统下,返回的东西不一样! 1. str.getByte ...
- java 面试题之银行业务系统
1.需求 模拟实现银行业务调度系统逻辑,具体需求如下: 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗口为VIP窗口. 有三种对应类型的客户:VIP客户,普通客户,快速客 ...
- win10 uwp 如何使用DataTemplate
这是数据模板,一般用在数组的绑定,显示数组中的元素. 假如我们有一个列表,列表里是书,包括书名.作者.还有出版,那么我们只有源信息,如何把它显示到我们的ListView,就需要DataTemplate ...