我要完成一份java assignment 不過我唔識做

2009-05-06 8:43 am
public Board(int newRows, int newColumns, int startRow, int startCol
umn)
Constructor – Passed the size of the board
(numbers of rows and columns) and the starting location for the player (row and column). Must instantiate an array to represent the board and fill it with spaces (EMPTY).

public String toString()
Used to display the board.board is clearly visible. A typical layout might be:
Board
+-+-+-+-+-+-+-+-+-+-+
| | | |$| | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | |*| | | |
+-+-+-+-+-+-+-+-+-+-+
| | |$| | | | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | |#| | | | | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | |#| |$| | |
+-+-+-+-+-+-+-+-+-+-+
| | | | | | |#| | | |
+-+-+-+-+-+-+-+-+-+-+
where, the * represents the player, $s represent the treasures, and the #s represent the obstacles.

public void playerUp()
If possible, move the player up one square. If the square to be moved to, is occupied, or is off the board, do nothing.

public void playerDown()
If possible, move the player down one square. If the square to be moved to, is occupied, or is off the board, do nothing.

回答 (1)

2009-05-07 4:23 am
✔ 最佳答案
The art of asking questions that get responses is for people to understand your problem quickly. I think the best way to present your problem is to provide a link that shows the problem you were given, AFTER that, you present your proposed solution.

2009-05-06 04:16:23 補充:
Most people would not bother to check you code to guess what you are trying to do, much less to help you.
I hope I am proven wrong, but it is not too late to make a supplementary question that shows the original text of the problem.

2009-05-06 20:23:32 補充:
I will give you a head start:
public class Board
{
// state variables available to class Board
char[][] board;
int maxRows, maxCols;
int currentRow, currentCol;
// constructor
public Board(int newRows, int newColumns, int startRow, int startColumn)
{
maxRows=newRows;
maxCols=newColumns;
// assign memory to board
board=new char[maxRows][maxCols];
// place obstacles and treasures
if(maxRows>5&&maxCols>7)
{
board[0][3]='$';
board[2][2]='$';
board[4][7]='$';
board[1][7]='#';
board[3][4]='#';
board[4][5]='#';
board[5][6]='#';
}
// determine starting cell
if(startRow<0||startRow>=maxRows)startRow=0;
currentRow=startRow;
if(startColumn<0||startColumn>=maxCols)startColumn=0;
currentCol=startColumn;
board[currentRow][currentCol]='*'; // place player marker in cell
}
// move player up one cell, if possible
public void playerUp()
{
if(currentRow>0) // do not execute following check if currentRow==0
if(board[currentRow-1][currentCol]!='#');
{
board[currentRow][currentCol]=' '; // empty previous cell
currentRow--;
board[currentRow][currentCol]='*'; // occupy new cell
}
}
// add other methods here
public static void main(String[] args)
{
Board b=new Board(6,10,1,1);
// add your game here
}
}


收錄日期: 2021-04-13 16:36:37
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090506000051KK00061

檢視 Wayback Machine 備份