当前位置:   article > 正文

简易版五子棋---JavaSE_java简易五子棋2020csdn

java简易五子棋2020csdn

要求

  1. 可以在控制台输出棋盘效果
  2. 可以从控制台读取用户输入的落子位置
  3. 当其中一方领先在同一直线上连续棋子达到5个时,可以判断胜负,并退出游戏


//Method类
public class Method {

	
	
	
	//棋盘
	public void Backgammon(String[][] checkerboard) {
		
		int count1 = 1;
		int count2 = 1;
		for (int i = 0; i < checkerboard.length; i++) {
			for (int j = 0; j < checkerboard[i].length; j++) {
				if (j == 0 && i == 0) {
					checkerboard[i][j] = " ";
					continue;
				}
				if (j == 0) {
					checkerboard[i][j] =Integer.toHexString(count1);
					count1++;
					continue;
				}
				if (i == 0) {
					checkerboard[i][j] =Integer.toHexString(count2);
					count2++;
					continue;
				}
				checkerboard[i][j] = "□";
			}	
		}
		
		
		printBackgammon(checkerboard);
	}
	
	//打印棋盘
	public void printBackgammon(String[][] checkerboard) {
		for (int i = 0; i < checkerboard.length; i++) {
			for (int j = 0; j < checkerboard[i].length; j++) {
				System.out.print(checkerboard[i][j] + " ");
			}
			System.out.println();
		}
	}
	
	//判断胜负
	public boolean isVictory(String[][] checkerboard) {
		boolean isVictory = false;
		//横着走
		for (int i = 0; i < checkerboard.length; i++) {
			for (int j = 0; j < checkerboard[i].length; j++) {
				if (j == 12) {
					 break;
				}
				if(! checkerboard[i][j].equals("□") && (checkerboard[i][j].equals(checkerboard[i][j + 1]) && checkerboard[i][j].equals(checkerboard[i][j + 2]) && checkerboard[i][j].equals(checkerboard[i][j + 3]) && checkerboard[i][j].equals(checkerboard[i][j + 4])))
					isVictory = true;
			}
		}
		
		//竖着走
		for (int i = 0; i < checkerboard.length; i++) {
			for (int j = 0; j < checkerboard[i].length; j++) {
				if (j == 12) {
					break;
				}
				if(! checkerboard[j][i].equals("□") && (checkerboard[j][i].equals(checkerboard[j + 1][i]) && checkerboard[j][i].equals(checkerboard[j + 2][i]) && checkerboard[j][i].equals(checkerboard[j + 3][i]) && checkerboard[j][i].equals(checkerboard[j + 4][i])))
					isVictory = true;
			}
		}
		
		//斜着走情况一
		for (int i = 0; i < checkerboard.length; i++) {
			if (i == 12) {
				 break;
			}
			for (int j = 0; j < checkerboard[i].length; j++) {
				if (j == 12) {
					 break;
				}
				if(! checkerboard[i][j].equals("□") && (checkerboard[i][j].equals(checkerboard[i + 1][j + 1]) && checkerboard[i][j].equals(checkerboard[i + 2][j + 2]) && checkerboard[i][j].equals(checkerboard[i + 3][j + 3]) && checkerboard[i][j].equals(checkerboard[i + 4][j + 4])))
					isVictory = true;
			}
		}
		//斜着走情况二
		for (int i = 0; i < checkerboard.length; i++) {
			if (i == 12) {
				 break;
			}
			for (int j = checkerboard[i].length - 1; j >= 0; j--) {
				if (j == 4) {
					 break;
				}
				//System.out.println("i:" + i + "j:" + j);
				if(! checkerboard[i][j].equals("□") && (checkerboard[i][j].equals(checkerboard[i + 1][j - 1]) && checkerboard[i][j].equals(checkerboard[i + 2][j - 2]) && checkerboard[i][j].equals(checkerboard[i + 3][j - 3]) && checkerboard[i][j].equals(checkerboard[i + 4][j - 4])))
					isVictory = true;
			}
		}
		
		return isVictory;
	}
}

//Main方法
import java.util.Scanner;

public class TestMain {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String[][] checkerboard = new String[16][16];
		Method method = new Method();
		System.out.println("---------------欢迎来到弱智版五子棋游戏---------------");
		method.Backgammon(checkerboard);
		
		int count = 0;
		String blackAndWhite = "黑方";
		while (true) {
			count++;
			int i = 0;
			int j = 0;
			if (count % 2 == 1) {
				blackAndWhite = "黑方";
				System.out.println(blackAndWhite + "请选择落子位置");
			}else {
				blackAndWhite = "白方";
				System.out.println(blackAndWhite + "请选择落子位置");
			}
			
			String NAS = scanner.next(); 
			String EAW = scanner.next();
			if (NAS.equals("a")) {
				i = 10;
			}else if (NAS.equals("b")) {
				i = 11;
			}else if (NAS.equals("c")) {
				i = 12;
			}else if (NAS.equals("d")) {
				i = 13;
			}else if (NAS.equals("e")) {
				i = 14;
			}else if (NAS.equals("f")) {
				i = 15;
			}else {
				i = Integer.parseInt(NAS);
			}
			if (EAW.equals("a")) {
				j = 10;
			}else if (EAW.equals("b")) {
				j = 11;
			}else if (EAW.equals("c")) {
				j = 12;
			}else if (EAW.equals("d")) {
				j = 13;
			}else if (EAW.equals("e")) {
				j = 14;
			}else if (EAW.equals("f")) {
				j = 15;
			}else {
				j = Integer.parseInt(EAW);
			}
			if (count % 2 == 1) {
				checkerboard[i][j] = "●";
			}else {
				checkerboard[i][j] = "○";
			}
			method.printBackgammon(checkerboard);
			
			
			if (method.isVictory(checkerboard)) {
				if (count % 2 == 1) {
					System.out.println();
					System.out.println("对局结束");
					System.out.println("黑方胜");
				}else {
					System.out.println("白方胜");
				}
				scanner.close();
				break;
			}
			
		}
		
		
		
	}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188

此代码判断胜利的条件效率较低下,是每一次都去数组里面找五个相同的字符串,而非根据落子位置去找。

注意:如果在相同位置连续下棋,会覆盖掉你先前所下的棋子。

简单演示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

欢迎各位交流代码

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号