五子棋项目
1.棋盘类(Five-Stone)
-
用棋子类chess类型定义二维数组 board 储存chess对象
private Chess[][]board; // 用二维数组定义棋盘
-
定义棋盘大小 ,获胜条件和 棋子数 用静态static final 修饰为常量
private static final int BOARDSIZE = 10; //棋盘大小 private static final int VICTORY= 5; //规则 5个连在一块胜利 private int size; //保存以下的格子数
-
建立无参的构造方法 建立棋盘 用➕填充
public FiveStone() { this.board = new Chess[BOARDSIZE][BOARDSIZE]; for (int i = 0 ; i<board.length; i++){ // 建立棋盘 for (int j = 0 ; j < board[i].length; j++){ board[i][j] = new Chess("➕",i,j); } } }
-
start 游戏实现
- scanner 输入横纵坐标
- 判断是否此位置有棋子(8个方向控制)
- 判断是否获胜
- 判断棋子是否满
- 换边
2. 棋子类(Chess)
1. 建立属性
private String chessType; //棋子类型
private int abscissa;//横坐标
private int column; //列坐标
2.构造方法
public Chess(String chessType, int abscissa, int column) {
this.chessType = chessType;
this.abscissa = abscissa;
this.column = column;
}
3.get方法
public int getAbscissa() {
return abscissa;
}
public int getColumn() {
return column;
}
4.set方法
public void setChessType(String chessType) {
this.chessType = chessType;
}
3.源码
public class FiveStone {
private Chess[][]board; // 用二维数组定义棋盘
private static Scanner scanner = new Scanner(System.in);
private static final int BOARDSIZE = 10; //棋盘大小
private static final int VICTORY= 5; //规则 5个连在一块胜利
private int size; //保存以下的格子数
public FiveStone() {
this.board = new Chess[BOARDSIZE][BOARDSIZE];
for (int i = 0 ; i<board.length; i++){ // 建立棋盘
for (int j = 0 ; j < board[i].length; j++){
board[i][j] = new Chess("➕",i,j);
}
}
}
private void printBoard(){// 输出棋盘
for (int i = 0 ; i<board.length; i++){ //
for (int j = 0 ; j < board[i].length; j++){
System.out.print(board[i][j].getChessType());
}
System.out.println();
}
}
private String choose(){ //初始选择
System.out.println("请选择黑棋● or 白棋⭕");
String chessColor = scanner.next();
String chessTyple = " ";
if (chessColor.equals("黑棋")){
chessTyple = "●";
}else {
chessTyple = "⭕";
}
return chessTyple;
}
private boolean isVctory(String ChessTyple, int i, int j){
int x = i; //标记 i j 位置
int y = j;
int count = 1 ; //记录个数
//竖向
if (i+1 < board.length && board[i+1][j].getChessType().equals(ChessTyple) || i-1 >= 0 && board[i-1][j].getChessType().equals(ChessTyple)){
//下边
while (x+1 < board.length && board[x+1][y].getChessType().equals(ChessTyple)){
count++; //个数加
x++; //继续往下走
}
// 上边
x=i;y=j;
while (x-1 >= 0 && board[x][y].getChessType().equals(ChessTyple)){
count++;
x--; //继续往上走
}
if (count >= VICTORY){
System.out.println(ChessTyple+"获胜");
return true;
}
}
//横方向
if ( j+1<board[i].length && board[i][j+1].getChessType().equals(ChessTyple) || j-1 >=0 && board[i][j-1].getChessType().equals(ChessTyple)){
x=i;y=j;count=1; //重新定义 x y count
//右边
while (y < board[x].length && board[x][y+1].getChessType().equals(ChessTyple)){
count++;
y++;
}
// 左边
y=j;
while (y-1 >= 0 && board[x][y-1].getChessType().equals(ChessTyple)){
count++;
y--;
}if (count >= VICTORY){
System.out.println(ChessTyple+"获胜");
return true;
}
// /方向
}
if (i-1 >= 0 && j+1<board[i].length && board[i-1][j+1].getChessType().equals(ChessTyple) || i+1 < board.length && j-1 >=0 && board[i+1][j-1].getChessType().equals(ChessTyple)){
x=j;y=i;count=1; //重新定义 x y count
// 右边
while ((x - 1) > 0 && (y < board[x].length) && board[x - 1][y + 1].getChessType().equals(ChessTyple)) {
count++;
x--;
y++;
}
// 左边
x=i; y=j;
while (x < board.length && y-1 >= 0 && board[x+1][y-1].getChessType().equals(ChessTyple)){
count++;
x++;
y--;
}if (count >= VICTORY){
System.out.println(ChessTyple+"获胜");
return true;
}
}
// \方向
if ( i+1 < board.length && j+1<board[i].length && board[i+1][j+1].getChessType().equals(ChessTyple) ||i-1 >= 0 && j-1 >=0 && board[i-1][j-1].getChessType().equals(ChessTyple)){
// 右边
x=j;y=i;count=1; //重新定义 x y count
while (x < board.length && y < board[x].length && board[x+1][y+1].getChessType().equals(ChessTyple)){
count++;
x++;
y++;
}
x=i; y=j;
while (x-1 > 0 && y-1 > 0 && board[x-1][y-1].getChessType().equals(ChessTyple)){
count++;
x--;
y--;
}
if (count >= VICTORY){
System.out.println(ChessTyple+"获胜");
return true;
}
}
return false;
}
public void start() {
String beganChessTyple = choose(); //选择黑棋或白棋
while (true) {
System.out.println("请输入横纵坐标");
// printBoard();
int x = scanner.nextInt(); //横坐标
int y = scanner.nextInt(); //纵坐标
size += 1;
if (!board[x-1][y-1].getChessType().equals("➕")){
System.out.println("此位置已经下过,重新选择坐标下棋");
}else {
board[x-1][y-1].setChessType(beganChessTyple);
int i = board[x-1][y-1].getAbscissa();
int j = board[x-1][y-1].getColumn();
printBoard();
boolean result = isVctory(beganChessTyple,i,j);
if (size == BOARDSIZE*2) {
System.out.println("棋盘已满,是否重新开始");
if (scanner.next().equals("是")) {
FiveStone user = new FiveStone();
user.start();
}else{
break;
}
}
if (result){
System.out.println("是否继续游戏");
if (scanner.next().equals("是")){
FiveStone user = new FiveStone();
user.start();
}else {
break;
}
}
if (beganChessTyple.equals("⭕")){
beganChessTyple = "●";
System.out.println("换黑棋");
}else {
beganChessTyple = "⭕";
System.out.println("换白棋");
}
}
}
}
}
public class Chess {
private String chessType; //棋子类型
private int abscissa;//横坐标
private int column; //列坐标
public Chess(String chessType, int abscissa, int column) {
this.chessType = chessType;
this.abscissa = abscissa;
this.column = column;
}
public String getChessType() {
return chessType;
}
public int getAbscissa() {
return abscissa;
}
public int getColumn() {
return column;
}
public void setChessType(String chessType) {
this.chessType = chessType;
}
}
public class TextDemo {
public static void main(String[] args) {
FiveStone user = new FiveStone();
user.start();
}
}