连接4个java问题:我该怎么做?

时间:2014-05-27 01:55:05

标签: java arrays 2d-games

我是java的菜鸟,以及编程,我在描述如何实际执行每种方法以创建Connect Four游戏时遇到很大困难。有4种方法,main,printBoard,checkWinner和playerMove。如有必要,我可以添加更多方法。我不堪重负,需要大量的指导。如何处理每种方法?对不起,如果这是一个庞大的问题,我很困惑。

import java.util.Scanner;

公共课ConnectFour {

// We will represent the game board using a 2 dimensional integer array.
// Each entry of the array will contain a 0, 1, or -1.
// A 0 entry in the array represents an empty slot. 
// A 1 entry represents Player 1's piece.
// A -1 entry represents Player 2's piece.
// Since most methods will need to access this board, we have decided to
// make it a global class variable.

import java.util.Scanner; 公共课ConnectFour {

public static int[][] board = new int[6][7]; 

public static void main(String[] args){

    Scanner sc = new Scanner(System.in);

    // initializing board
    for(int i = 0; i < 6; i++){ 
        for(int j = 0; j < 7; j++){
            board[i][j] = 0;
        }

    }

    // WRITE CODE HERE
    if (j < 7 && j >= 0) 
        f[i][j] =".";
    else f[i][j] = " ";

    return board;

}

}

// This method prints the board on the screen. 
// To represent Player 1's pieces, print 'X'.
// To represent Player 2's pieces, print 'O'.
// To represent an empty slot, print '.'.
public static void printBoard(){

    // WRITE CODE HERE
    for (int i = 0; i < 6; i++)

    {

        for (int j = 0;j < 7; j++)

        {

            System.out.print(f[i][j]);

        }

        System.out.println();

    }

}



// This method scans the current board and checks if there is a winner.
// The method should return 1 if Player 1 has connected 4 pieces.
// It should return -1 if Player 2 has connected 4 pieces.
// It should return 0 if no player has connected 4 pieces.
public static int checkWinner(){

    // WRITE CODE HERE


return;}


// This method implements a player's move and updates the board accordingly.
// The method has two integer inputs. 
// The first input indicates the player (1 for Player 1, -1 for Player 2).
// The second input indicates the column number that the player has
// chosen to play.
// If the column number is out of range or the column is currently full,
// the method should return false.
// Otherwise, the board should be updated and the method should return true.

public static boolean playerMove(int player, int columnNum){

    // WRITE CODE HERE


}

}

1 个答案:

答案 0 :(得分:2)

从逻辑上思考你的程序应该如何工作。我个人会将你的电路板放在一个单独的课程中,但这并不重要,特别是因为该程序非常小。这不像代码问题和逻辑代码那么多,所以我们将介绍游戏的工作原理。

首先,每个功能有什么作用?我们有checkWinner(),playerMove()和printBoard()。所以我们将有一个循环,直到游戏结束,这应该由ch​​eckWinner()控制,就像这样

while !checkWinner

因此游戏将循环直到找到胜利者,并且在while循环之后执行的任何代码都是结束游戏代码。在我们的while循环内部应该执行每一帧执行的所有代码,而没有胜利者,因为这就是我们的while循环现在的设置方式。因此,让我们考虑应该执行游戏的每一帧。 printBoard()?是的,可能,因为我们想要每帧都更新显示器吗?

的PlayerMove()?是的,因为如果游戏还没有结束,我们希望玩家执行一个动作。但是,您需要确保代码可以在播放器之间切换。如果你这样做,基本的游戏循环应该完成。这是

的一些伪代码
//setup board here
while !checkWinner
    printBoard
    playerMove
//since checkwinner did not return 0, a player won. so we put in endgame code here

这里的主要问题是从逻辑上思考你的程序应该如何工作。因此,计划哪些功能做什么,并尝试写出来。 希望有所帮助。