溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Java項(xiàng)目如何實(shí)現(xiàn)五子棋小游戲

發(fā)布時(shí)間:2020-07-21 14:53:06 來(lái)源:億速云 閱讀:144 作者:小豬 欄目:編程語(yǔ)言

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

項(xiàng)目名稱(chēng)

五子棋小游戲

項(xiàng)目描述

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

代碼實(shí)現(xiàn)

測(cè)試類(lèi)

public class Test {
 public static void main(String[] args) {
 FiveChess fiveChess = new FiveChess();
 fiveChess.start();
 }
}

主類(lèi):實(shí)現(xiàn)主方法

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<CheckerSize;i++){
  for(int j=0;j<CheckerSize;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<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos][yPos+1].getValue())){
  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 &#63; true : false;
 }
 //縱向
 if (xPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos].getValue()) ||
  xPos+1<CheckerSize && chess[xPos][yPos].getValue().equals(chess[xPos+1][yPos].getValue())){
  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 &#63; true : false;
 }
 //正斜線(xiàn)
 if (xPos-1>=0 && yPos-1>=0 && chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos-1].getValue()) ||
  xPos+1<CheckerSize && yPos+1<CheckerSize &&
   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 &#63; true : false;
 }
 //反斜線(xiàn)
 if (xPos-1>=0 && yPos+1<CheckerSize &&
  chess[xPos][yPos].getValue().equals(chess[xPos-1][yPos+1].getValue()) ||
  xPos+1<CheckerSize && yPos-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<CheckerSize &&
   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 &#63; true : false;
 }
 return false;
 }
 private void runChess(String run){
 System.out.println("請(qǐng)輸入"+run+"坐標(biāo):");
 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<CheckerSize;i++){
  for (int j=0;j<CheckerSize;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("游戲結(jié)束,是否重玩");
  String finish = scanner.next();
  if (finish.equals("是")){
   flag = true;
   start();
  }
  else if (finish.equals("否")){
   System.exit(0);
  }
  }
 }else {
  System.out.println("該處已存在棋子,請(qǐng)重新選擇");
  runChess(run);
 }
 }
 public void start(){
 initCheck();
 System.out.println("請(qǐng)選擇先走方:黑棋or白棋");
 String run = scanner.next();
 for(int i=0;i<CheckerSize;i++){
  for (int j=0;j<CheckerSize;j++){
  System.out.print(chess[i][j].getValue());
  }
  System.out.println();
 }
 while (flag) {
  switch (run) {
  case "黑棋":
   runChess("黑棋");
   run = "白棋";
   break;
  case "白棋":
   runChess("白棋");
   run = "黑棋";
   break;
  default:
  }
 }
 }
}

結(jié)點(diǎn)類(lèi)

public class Chess {
 private String value;
 public Chess(String value){
 this.value = value;
 }
 public String getValue() {
 return value;
 }
}

看完這篇關(guān)于Java項(xiàng)目如何實(shí)現(xiàn)五子棋小游戲的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話(huà),可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI