因为这个游戏没太多内容,所以只写了三个类(一个棋盘,一个控制,一个测试),老师干脆只写了两个类(一个主控类,一个测试类)
先是棋盘类,我把所有的对棋盘和棋子的操作都写在了这里面。
public class ChessBoard {
//定义棋盘大小
private String[][] chessBoard = new String[15][15];
//用于确定棋子颜色,每次调用获取棋子方法时都乘自己这样temp的值就在1或-1间不断变化
private int temp = -1;
//初始化棋盘
public void initChessBoard(){
for(int i = 0;i < chessBoard.length;i ++){
for(int j = 0;j < chessBoard[i].length;j ++){
chessBoard[i][j] = "+ ";
}
}
}
//显示棋盘
public void showChessBoard(){
for(int i = 0;i < chessBoard.length;i ++){
for(int j = 0;j < chessBoard[i].length;j ++){
System.out.print(chessBoard[i][j]);
}
System.out.println();
}
}
//获取当前位置的字符串
public String getChessPieces(int i,int j){
return chessBoard[i][j];
}
//落子
public void setChessPieces(int i,int j){
if(temp == -1){
chessBoard[i][j] = "● ";
}else{
chessBoard[i][j] = "○ ";
}
temp *= -1;
}
//获取棋子颜色
public int getTemp() {
return temp;
}
public void setTemp(int temp) {
this.temp = temp;
}
}
接下来是控制类,只有三个方法,一个下棋,一个判断,一个主控逻辑
这里的判断方法写的很负责,其实还有很简单的方法,但是因为这是我的第一想法,所以还是坚持写了下来,过程很坑!
import java.util.Scanner;
public class SystemControl {
private Scanner input = new Scanner(System.in);
ChessBoard chessBoard = new ChessBoard();
//判断横轴,纵轴,正斜(\),反斜(/)四个方向的连字数
private int h = 0;
private int z = 0;
private int zx = 0;
private int fx = 0;
//是否成五子的标志
private boolean flag = false;
public void play(){
chessBoard.showChessBoard();
System.out.print("请输入落子位置:");
String[] xy = input.next().split(",");
//获取输入的棋盘位置
int x = Integer.parseInt(xy[0]) - 1;
int y = Integer.parseInt(xy[1]) - 1;
if("+ ".equals(chessBoard.getChessPieces(x, y))){
chessBoard.setChessPieces(x,y);
this.judge(x,y);
//判断连字数,五子则返回,不为五子则递归
if(this.flag){
return;
}else{
this.play();
}
}else{
System.out.println("当前位置已经有子,请重新落子!");
this.play();
}
}
public void judge(int i,int j){
//横轴
for(int k = 0;k < 15;k ++){
//最开始的if为判断是否为此行的最后一个元素,因为最后一个元素不会没加到h上面!!!所以要手动添加
if(k == 15 - 1){
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(i,k))){
h++;
}else{
h = 0;
}
}
if(h >= 5){
this.flag = true;
return;
}
//比较当前位置与所在横轴的连子数
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(i,k))){
h++;
}else{
h = 0;
}
}
//每次横轴不满足,则将h清零
h = 0;
//纵轴
for(int k = 0;k < 15;k ++){
//最开始的if为判断此纵行最后一个元素是否为同色棋子!因为最后一次zx即使加上去了,仍然不能返回!
if(k == 15 - 1){
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(k,j))){
z++;
}else{
z = 0;
}
}
if(z >= 5){
this.flag = true;
return;
}
//比较当前位置与所在纵轴的连子数
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(k,j))){
z++;
}else{
z = 0;
}
}
//每次纵轴不满足,则将z清零
z = 0;
//正斜(\)
if(i >= j){
for(int k = 0;k < 15 - Math.abs(i-j);k ++){
//最开始的if为判断此正斜方向最后一个元素是否为同色棋子!因为最后一次zx即使加上去了,仍然不能返回!
if(k == 15 - Math.abs(i-j)-1){
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces((i-j)+k,k))){
zx++;
}else{
zx = 0;
}
}
if(zx >= 5){
this.flag = true;
return;
}
//比较当前位置与所在正斜方向的连子数
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces((i-j)+k,k))){
zx++;
}else{
zx = 0;
}
}
//每次正斜方向不满足,则将zx清零
zx = 0;
}else{
for(int k = 0;k < 15 - Math.abs(i-j);k ++){
if(k == 15 - Math.abs(i-j)-1){
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(k,k+(j-i)))){
zx++;
}else{
zx = 0;
}
}
if(zx >= 5){
this.flag = true;
return;
}
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(k,k+(j-i)))){
zx++;
}else{
zx = 0;
}
}
//每次正斜方向不满足,则将zx清零
zx = 0;
}
//反斜 (/)
if((i+j) <= 14){
for(int k = 0;k < i+j+1;k ++){
//最开始的if为判断此反斜方向最后一个元素是否为同色棋子!因为最后一次fx即使加上去了,仍然不能返回!
if(k == i+j){
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(14-(i+j)+k,i+j-k))){
fx++;
}else{
fx = 0;
}
}
if(fx >= 5){
this.flag = true;
return;
}
//比较当前位置与所在反斜方向的连子数
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces(14-(i+j)+k,i+j-k))){
fx++;
}else{
fx = 0;
}
}
//每次反斜方向不满足,则将fx清零
fx = 0;
}else{
for(int k = 0;k < 28-(i+j)-1;k ++){
if(k == 28-(i+j)-1-1){
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces((i+j)+k-14,28-(i+j)-k))){
fx++;
}else{
fx = 0;
}
}
if(fx >= 5){
this.flag = true;
return;
}
if(chessBoard.getChessPieces(i, j).equals(chessBoard.getChessPieces((i+j)+k-14,28-(i+j)-k))){
fx++;
}else{
fx = 0;
}
}
fx = 0;
}
}
public void mainLogic(){
chessBoard.initChessBoard();
this.play();
chessBoard.showChessBoard();
if(chessBoard.getTemp() == 1){
System.err.println("黑棋获胜!");
}else{
System.err.println("白起获胜!");
}
System.exit(0);
}
}
最后的测试类
public class Test {
public static void main(String[] args){
SystemControl ss = new SystemControl();
ss.mainLogic();
}
}
本文出自 “新手出门” 博客,谢绝转载!