日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 五子棋项目_Java项目如何实现五子棋小游戏

發布時間:2023/12/1 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 五子棋项目_Java项目如何实现五子棋小游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java項目如何實現五子棋小游戲

發布時間:2020-07-21 14:53:06

來源:億速云

閱讀:77

作者:小豬

小編這次要給大家分享的是Java項目如何實現五子棋小游戲,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

項目名稱

五子棋小游戲

項目描述

可以改變獲勝棋子數,率先連成棋數的人獲勝

代碼實現

測試類

public class Test {

public static void main(String[] args) {

FiveChess fiveChess = new FiveChess();

fiveChess.start();

}

}

主類:實現主方法

public class FiveChess {

private static final int CheckerSize = 10;

private static final int successSize = 5;

private Chess[][] chess;

private int xPos;

private int yPos;

private boolean flag = true;

private Scanner scanner = new Scanner(System.in);

public FiveChess(){

chess = new Chess[CheckerSize][CheckerSize];

}

private void initCheck(){

for(int i=0;i

for(int j=0;j

chess[i][j] = new Chess("十");

}

}

}

private boolean judge(int xPos,int yPos){

//橫向

if(yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos][yPos-1].getValue()) ||

yPos+1

int count = 1;

for (int i = 1; i < successSize; i++) {

if (yPos - i >= 0 && chess[xPos][yPos - i].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

for (int i = 1; i < successSize; i++) {

if (yPos + i < CheckerSize &&

chess[xPos][yPos + i].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

return count >= successSize ? true : false;

}

//縱向

if (xPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos].getValue()) ||

xPos+1

int count = 1;

for (int i = 1; i < successSize; i++) {

if (xPos- i >= 0 && chess[xPos-i][yPos].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

for (int i = 1; i < successSize; i++) {

if (xPos + i < CheckerSize &&

chess[xPos+i][yPos].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

return count >= successSize ? true : false;

}

//正斜線

if (xPos-1>=0 && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos-1].getValue()) ||

xPos+1

chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos+1].getValue())){

int count = 1;

for (int i = 1; i < successSize; i++) {

if (xPos - i >= 0 && yPos - i >= 0 &&

chess[xPos-i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

for (int i = 1; i < successSize; i++) {

if (xPos + i < CheckerSize && yPos + i < CheckerSize &&

chess[xPos+i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

return count >= successSize ? true : false;

}

//反斜線

if (xPos-1>=0 && yPos+1

chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos+1].getValue()) ||

xPos+1=0 &&

chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos-1].getValue())){

int count = 1;

for (int i = 1; i < successSize; i++) {

if (xPos - i >= 0 && yPos + i

chess[xPos-i][yPos+i].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

for (int i = 1; i < successSize; i++) {

if (xPos + i < CheckerSize && yPos - i >= 0 &&

chess[xPos+i][yPos-i].getValue().equals(chess[xPos][yPos].getValue())) {

count++;

} else {

break;

}

}

return count >= successSize ? true : false;

}

return false;

}

private void runChess(String run){

System.out.println("請輸入"+run+"坐標:");

xPos = scanner.nextInt();

yPos = scanner.nextInt();

if(chess[xPos-1][yPos-1].getValue().equals("十")){

if(run.equals("黑棋")){

chess[xPos-1][yPos-1] = new Chess("●");

}

else if(run.equals("白棋")){

chess[xPos-1][yPos-1] = new Chess("〇");

}

for(int i=0;i

for (int j=0;j

System.out.print(chess[i][j].getValue());

}

System.out.println();

}

if(judge(xPos-1,yPos-1)){

flag = false;

System.out.println(run+"獲勝");

System.out.println("游戲結束,是否重玩");

String finish = scanner.next();

if (finish.equals("是")){

flag = true;

start();

}

else if (finish.equals("否")){

System.exit(0);

}

}

}else {

System.out.println("該處已存在棋子,請重新選擇");

runChess(run);

}

}

public void start(){

initCheck();

System.out.println("請選擇先走方:黑棋or白棋");

String run = scanner.next();

for(int i=0;i

for (int j=0;j

System.out.print(chess[i][j].getValue());

}

System.out.println();

}

while (flag) {

switch (run) {

case "黑棋":

runChess("黑棋");

run = "白棋";

break;

case "白棋":

runChess("白棋");

run = "黑棋";

break;

default:

}

}

}

}

結點類

public class Chess {

private String value;

public Chess(String value){

this.value = value;

}

public String getValue() {

return value;

}

}

看完這篇關于Java項目如何實現五子棋小游戲的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。

總結

以上是生活随笔為你收集整理的java 五子棋项目_Java项目如何实现五子棋小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。