首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2名玩家9×9连接4

2名玩家9×9连接4
EN

Code Review用户
提问于 2017-09-28 23:01:04
回答 1查看 893关注 0票数 1

作为一项高中作业,我被要求用Java制作一个连接4游戏。我创造了一个9×9的二维数组,两个玩家可以玩.如有任何反馈或建议,我将不胜感激。

代码语言:javascript
复制
package Connect4;

import java.util.Scanner;
public class Connect4 {


    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int count=0,count2=0,count3,pn=2;
        String choice;
        int[][]board=new int[9][9];
        boolean isWin=false;
        System.out.println("--Connect 4--\n\nHow to play:\n\nPlayers 1 and 2 alternate as they select a column to drop their chip in.\n\nConnect 4 chips in a row either vertically, horizontally, or diagonally to win.\n");
        while(!isWin){

            if(pn==2)pn=1;//Changes players' turn
            else pn=2;//Changes players' turn

            display(board,count2);

            System.out.print("Player "+pn+" enter your column choice: ");
            choice=input.nextLine();
            choice=choice.toUpperCase();

                  for(count=0;!"ABCDEFGHI".contains(choice);count++){//Making sure that their column entry is into a valid column.
                      System.out.print("This is not a valid column entry. Please enter a valid column: ");
                      choice=input.nextLine();
                      choice=choice.toUpperCase();
                  }


                  count=8;

                  int ascii=(int)choice.charAt(0)-65;//Converts their letter input to a number in order to coordinate with the columns.   

                  for(count2=0;count2<board.length;count2++){//Checks if column is full.

                      if(board[0][ascii]!=0){
                          System.out.print("This column is full. Please enter another colum: ");
                          choice=input.nextLine();
                          choice=choice.toUpperCase();

                          if(count!=8)count++;//Makes sure that array doesn't go out of bounds.
                      }

                      ascii=(int)choice.charAt(0)-65;

                      if(board[count][ascii]>0){//Makes sure that values in column stack.
                          count--;
                      }

                      else{ 
                          board[count][ascii]=pn;//Adds their input into the array.

                          for(int r=0 ; r<board.length;r++){//Checks the array for values.
                              for(int c=0 ; c<board[0].length;c++){
                                  if(board[r][c]==pn){
                                      isWin=checkWin(board,pn,r,c);
                                      if(isWin)break;
                                  }
                              } 
                              if(isWin)break;
                          }
                          break;
                      }
                  }              
        }

        if(isWin){
            for(count=0;count<board.length;count++){
                for(count2=0;count2<board.length;count2++){
                    System.out.print(board[count][count2]+" ");
                }
                System.out.println();   
            }
            System.out.println("A B C D E F G H I");
            System.out.println("Player "+pn+" wins!");
        }



    }
    public static void display(int board[][],int count2){
                for(int count=0;count<board.length;count++){
             for(count2=0;count2<board.length;count2++){
            System.out.print(board[count][count2]+" ");
            }
            System.out.println(); 

        }
                System.out.println("A B C D E F G H I");
    }
    public static boolean checkWin(int[][] board,int pn,int x, int y){
    boolean win = false;

    // down, right, upright, downright
    int[] row={1,0,-1,1};
    int[] col={0,1, 1,1};
    int count, intR,intC;
     for(int d=0;d<4;d++){ //four possible directions
         count=1;           //reset values to originals for each direction
         intR=x;
         intC=y;
         for(int c=1;c<=3;c++){ //check boundaries with next increment
            if((intR+row[d]>=0 && intR+row[d]<board.length) && (intC+col[d]>=0 && intC+col[d]<board[0].length)){
                intR+=row[d];   //increment is within the boundaries so move to that spot
                intC+=col[d];
                if(board[intR][intC]==pn)count++;  //count a correct spot
                else break; //incorrect digit found
            } else break; //break stops the loop
         }                //didn't fall within boudaries
         if(count==4){win=true;break;}  //a count of 4 indicates a win
     }
    return win;
   }
}
EN

回答 1

Code Review用户

回答已采纳

发布于 2017-10-01 22:34:14

谢谢你分享你的代码。

以下是我对此的看法。

OOP

您的代码是一种处理问题的过程方法。

一般来说,过程方法没有什么问题,但是Java是一种面向对象( oriented )的编程语言,如果您想成为一名优秀的java程序员,那么您应该开始以面向对象的方式解决问题。

最佳实践

关注点分离(SoC)/单一责任模式(

)

您的主要方法是巨大的,并做了很多事情。但是方法(或类)只应该做一件事情(并且做得很好)。

另一方面,如果您查看您的主要方法,您可以识别至少4个“块”。您应该将这些逻辑块提取到它们自己的方法中。

这样,您的main方法也将遵循下一节中描述的相同的抽象级别(SLA)原则。

相同抽象级别(SLA)

SLA意味着方法(以及稍后的类)应该要么执行原始操作,要么调用其他方法(对其他对象),但不能同时执行这两个操作。

在您的main方法中,您可以执行基本操作,如

代码语言:javascript
复制
        if (pn == 2)
            pn = 1;// Changes players' turn
        else
            pn = 2;// Changes players' turn

并调用其他方法,如

代码语言:javascript
复制
        display(board, count2);

您应该将方法的所有逻辑块提取到它们自己的方法中(如上一节所建议的)。这样你的主板就可以像这样读出(大致):

代码语言:javascript
复制
 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int count = 0, count2 = 0, count3, pn = 2;
    String choice;
    int[][] board = new int[9][9];
    boolean isWin = false;
    System.out.println(
            "--Connect 4--\n\nHow to play:\n\nPlayers 1 and 2 alternate as they select a column to drop their chip in.\n\nConnect 4 chips in a row either vertically, horizontally, or diagonally to win.\n");
    while (!isWin) {
        switchPlayer();           
        display(board, count2);
        requestUserInput();
        addCoinToColumn();
     }
     checkWin();
}

避免break

while循环留给break。这看起来是一个好的和短的解决方案,但从长远来看,如果您想要改进代码,它将阻碍您的方式。

这里就是这样。

您不能简单地将各个循环提取到它们自己的方法,因为这可能会破坏您的逻辑。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/176754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档