IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 五子棋小游戏 -> 正文阅读

[游戏开发]五子棋小游戏

package com.hrf.lanou.java05;

import java.util.Scanner;

public class Method03 {
	
	private static char[][] arr = new char[15][15];
	private static char[] mark= {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
	private static Scanner sc = new Scanner(System.in);
//   棋子黑子和白子
	private static char blackMark = '●';
	private static char whiteMark = '○';
	
	public static void main(String[] args) {
//		初始化棋盘
		createCheersBorder();
//		打印棋盘
		printChessBoard();	
//		调用输入下标的方法
		inputMark();
	}
//	输入下标所下的子
	public static void inputMark() {
		int i = 0;
		for(i = 0 ; i < 15*15; i++) {
			System.out.println("请下黑子,输入坐标为 4 , A:");
			char[] loc = getLocation();
			boolean canPutDown = canPutDown(loc);
			while(canPutDown == false) {
				System.out.println("此处已经有子,请重新输入其它子的坐标:");
				loc = getLocation();
				canPutDown = canPutDown(loc);
				
			}
			putDown(loc, blackMark);
//			打印棋盘
			printChessBoard();
			if(isWin(loc) == true) {
				System.out.println("黑方获胜");
				break;
			}
			System.out.println("请下白子,输入坐标为 4 , A:");
			char[] locW = getLocation();
			boolean canPutDownW = canPutDown(locW);
			while(canPutDownW == false) {
				System.out.println("此处已经有子,请重新输入其它子的坐标:");
				locW = getLocation();
				canPutDownW = canPutDown(locW);
			}
			putDown(locW, whiteMark);
//			打印棋盘
			printChessBoard();
//			判断输赢
			if(isWin(locW) == true) {
				System.out.println("白方获胜");
				break;
			}
			
		}
		if(i == 15*15) {
			System.out.println("和局,大家棋艺相等!");
		}
	}
	
//	初始化棋盘
	public static void createCheersBorder() {
		for(int i = 0; i < arr.length; i++) {
			for(int j = 0; j <arr[i].length; j++) {
				if(i == 0) {
					if(j == 0) {
						arr[i][j]='┌';
					}else if(j ==14) {
						arr[i][j]='┐';
//						System.out.print('┐');//●○
					}
					else {
						arr[i][j]='┬';
					}
				}else if(i==14) {
					if(j == 0) {
						arr[i][j]='└';
					}else if(j ==14) {
						arr[i][j]='┘';
						//┬├ ┼ ┤ ┴ ┘ ─
					}else {
						arr[i][j]='┴';
					}
				}else {
					if(j == 0) {
						arr[i][j]='├';
					}else if(j == 14) {
						arr[i][j]='┤';
					}
					else {
						arr[i][j]='┼';
					}
				}	 
			}
		}
	}
//	打印棋盘
	public static void printChessBoard() {
		System.out.print("  ");
		for(int i = 0; i < mark.length; i++) {
			System.out.print(mark[i] + " ");
		}
		System.out.println();
		for(int i = 0; i< arr.length;i++) {
			System.out.print(mark[i] + " ");
			for(int j = 0;j < arr[i].length; j++) {
				if(j != arr.length - 1) {
					System.out.print(arr[i][j]+ "─");
				}else {
					System.out.print(arr[i][j]);
				}
			}
			System.out.println();
		}
	}
	
//	
	
//	判断字符是否正确
	public static boolean isCharOk(char c) {
		if(c >= 'A' && c <= 'F' || c >= '1' && c <= '9') {
			return true;
		}else {
			return false;
		}
	}
//	把字符转化成数组下标
	public static int charToInt(char c) {
		if(c >= '1' && c <= '9') {
			return c - '1';
		}else {
			return c - 'A' + 9;
		}
	}
//	获取落子的位置
	private static char[] getLocation() {
		String input = sc.nextLine(); // 输入字符
		char c1 = 0;
		char c2 = 0;
		char c3 = 0;
		while (true) {
			if (input.length() == 3) {
				c1 = input.charAt(0);// 第一个字符
				c2 = input.charAt(1);
				c3 = input.charAt(2);
				if (isCharOk(c1) && c2 == ',' && isCharOk(c3)) {
					break;
				}
			}
			System.out.println("输入有误,请重新输入");
			input = sc.nextLine();
		}
		char[] location = { c1, c3 };
		return location;
	}
//	是否可以落子
	public static boolean canPutDown(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		if(arr[rowIndex][columnIndex] == blackMark || arr[rowIndex][columnIndex] == whiteMark) {
			return false;
		}else {
			return true;
		}
	}
//	开始落子
	public static void putDown(char[] location, char c) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		arr[rowIndex][columnIndex] = c;		
	}
//	判断输赢
	public static boolean isWin(char[] location) {
		if (upSameCount(location) + downSameCount(location) >= 4
				|| leftSameCount(location) + rightSameCount(location) >= 4
				|| leftUpSameCount(location) + rightDownSameCount(location) >= 4
				|| leftDownSameCount(location) + rightUpSameCount(location) >= 4) {
			return true;
		} else {
			return false;
		}
	}
//	看当前坐标上面有几个一样的棋子
	public static int upSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex - 1;
		int j = columnIndex;
		while(i >= 0 && j <=14) {
			if(arr[i][j] == c) {
				count++;
				i--;
			}else {
				break;
			}
		}
		return count;
	}
//	看当前坐标下面有几个一样的棋子
	public static int downSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex + 1;
		int j = columnIndex;
		while(i >= 0 && i <=14) {
			if(arr[i][j] == c) {
				count++;
				i++;
			}else {
				break;
			}
		}
		return count;
	}
//	看当前坐标左面有几个一样的棋子
	public static int leftSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex;
		int j = columnIndex - 1;
		while(j >= 0 && j <=14) {
			if(arr[i][j] == c) {
				count++;
				j--;
			}else {
				break;
			}
		}
		return count;
	}
//	看当前坐标右面有几个一样的棋子
	public static int rightSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex;
		int j = columnIndex + 1;
		while(i >= 0 && j <=14) {
			if(arr[i][j] == c) {
				count++;
				j++;
			}else {
				break;
			}
		}
		return count;
	}
//	看当前坐标右上有几个一样的棋子
	public static int rightUpSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex - 1;
		int j = columnIndex + 1;
		while(i >= 0 && j <=14) {
			if(arr[i][j] == c) {
				count++;
				i--;
				j++;
			}else {
				break;
			}
		}
		return count;
	}
//	看当前坐标右下有几个一样的棋子
	public static int rightDownSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex + 1;
		int j = columnIndex + 1;
		while(i <= 14 && j <=14) {
			if(arr[i][j] == c) {
				count++;
				i++;
				j++;
			}else {
				break;
			}
		}
		return count;
	}
//	看当前坐标左上有几个一样的棋子
	public static int leftUpSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex - 1;
		int j = columnIndex - 1;
		while(i >= 0 && j >=0) {
			if(arr[i][j] == c) {
				count++;
				i--;
				j--;
			}else {
				break;
			}
		}
		return count;
	}
//	看当前坐标左下有几个一样的棋子
	public static int leftDownSameCount(char[] location) {
		char c1 = location[0];//拿到行号
		char c2 = location[1];//拿到列号
		int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
		int columnIndex = charToInt(c2);
		char c = arr[rowIndex][columnIndex];
		int count = 0;
		int i = rowIndex + 1;
		int j = columnIndex - 1;
		while(i <= 14 && j >=0) {
			if(arr[i][j] == c) {
				count++;
				i++;
				j--;
			}else {
				break;
			}
		}
		return count;
	}
//	判断是否有空位
//	再来一局
	public static void again() {
		
	}
	
}

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-07-15 16:34:29  更:2021-07-15 16:34:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 20:25:34-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码