不确定当一名球员被迫进入获胜的禁区时,我需要什么样的陈述



因此,当玩家被迫进入一个已经获胜的盒子时,我想让他们能够进入任何尚未获胜的盒子,但我想知道是否有比每次需要检查时只做九个不同的if语句更简单的方法。现在的情况是:http://imgur.com/kD1UkVH这是规则的代码:

if(source.equals(a1) || source.equals(b1) || source.equals(c1) || 
source.equals(d1) || source.equals(e1) || source.equals(f1) || 
source.equals(g1) || source.equals(h1) || source.equals(i1))
{
b1.setEnabled(false);
b2.setEnabled(false);
b3.setEnabled(false);
b4.setEnabled(false);
b5.setEnabled(false);
b6.setEnabled(false);
b7.setEnabled(false);
b8.setEnabled(false);
b9.setEnabled(false);
c1.setEnabled(false);
c2.setEnabled(false);
c3.setEnabled(false);
c4.setEnabled(false);
c5.setEnabled(false);
c6.setEnabled(false);
c7.setEnabled(false);
c8.setEnabled(false);
c9.setEnabled(false);
d1.setEnabled(false);
d2.setEnabled(false);
d3.setEnabled(false);
d4.setEnabled(false);
d5.setEnabled(false);
d6.setEnabled(false);
d7.setEnabled(false);
d8.setEnabled(false);
d9.setEnabled(false);
e1.setEnabled(false);
e2.setEnabled(false);
e3.setEnabled(false);
e4.setEnabled(false);
e5.setEnabled(false);
e6.setEnabled(false);
e7.setEnabled(false);
e8.setEnabled(false);
e9.setEnabled(false);
f1.setEnabled(false);
f2.setEnabled(false);
f3.setEnabled(false);
f4.setEnabled(false);
f5.setEnabled(false);
f6.setEnabled(false);
f7.setEnabled(false);
f8.setEnabled(false);
f9.setEnabled(false);
g1.setEnabled(false);
g2.setEnabled(false);
g3.setEnabled(false);
g4.setEnabled(false);
g5.setEnabled(false);
g6.setEnabled(false);
g7.setEnabled(false);
g8.setEnabled(false);
g9.setEnabled(false);
h1.setEnabled(false);
h2.setEnabled(false);
h3.setEnabled(false);
h4.setEnabled(false);
h5.setEnabled(false);
h6.setEnabled(false);
h7.setEnabled(false);
h8.setEnabled(false);
h9.setEnabled(false);
i1.setEnabled(false);
i2.setEnabled(false);
i3.setEnabled(false);
i4.setEnabled(false);
i5.setEnabled(false);
i6.setEnabled(false);
i7.setEnabled(false);
i8.setEnabled(false);
i9.setEnabled(false);
}

九次,每个象限一次,这就是按下按钮时发生的情况:

if("disable".equals(actionEvent.getActionCommand()))
{
if(PlayerOneTurn % 2 != 0)
{ 
if(source.equals(a1))
{
if(input.equals("X") || (input.equals("x")))
{
a1.setText("x");
a1.setActionCommand(" ");
}
else     
{
a1.setText("o"); 
a1.setActionCommand(" ");
}
}
if(source.equals(a2))
{
if(input.equals("X") || (input.equals("x")))
{
a2.setText("x");
a2.setActionCommand(" ");
}
else
{
a2.setText("o"); 
a2.setActionCommand(" ");
}
}
if(source.equals(a3))
{
if(input.equals("X") || (input.equals("x")))
{
a3.setText("x");
a3.setActionCommand(" ");
}
else
{
a3.setText("o"); 
a3.setActionCommand(" ");
}
}
if((a1.getText().equals("x") && a2.getText().equals("x") && a3.getText().equals("x")) 
|| (a1.getText().equals("o") && a2.getText().equals("o") && a3.getText().equals("o")))
{
AFalse(true);
}`

AFalse()方法是这样的:

public void AFalse(boolean PlayerOne){
if(PlayerOne == true)
{
a1.setActionCommand(" ");
a2.setActionCommand(" ");
a3.setActionCommand(" ");
a4.setActionCommand(" ");
a5.setActionCommand(" ");
a6.setActionCommand(" ");
a7.setActionCommand(" ");
a8.setActionCommand(" ");
a9.setActionCommand(" ");
winnerA = true;     
}
else
{
a1.setActionCommand(" ");
a2.setActionCommand(" ");
a3.setActionCommand(" ");
a4.setActionCommand(" ");
a5.setActionCommand(" ");
a6.setActionCommand(" ");
a7.setActionCommand(" ");   
a8.setActionCommand(" ");
a9.setActionCommand(" ");
winnerA = false;    
}   
}

我建议您创建一些类,如下面的类。

此类表示单个字段。在这里,您可以存储移动,更容易地重置它们,并对它们进行迭代。

/**
* This class represents a single tic tac toe field.
*/
class Field {
private static final short FIELD_DIMENSION = 3;
public static final char EMPTY_FIELD = ' ';
public static final char PLAYER_ONE = 'x';
public static final char PLAYER_TOW = 'o';
private final char[][] cells;
public Field() {
this.cells = new char[FIELD_DIMENSION][FIELD_DIMENSION];
this.init();
}
/**
* Tests a field by given coordinates if a player is present.
*
* @param row Given row index of target field
* @param column Given column index of target field
* @return True if the move is possible
*/
public boolean isCellEmpty(final short row, final short column) {
return Objects.equals(this.cells[row][column], EMPTY_FIELD);
}
/**
* Returns the cell value of a field.
*
* @param row Given row index of target field
* @param column Given column index of target field
* @return Cell value
*/
public char getField(final short row, final short column) {
return this.cells[row][column];
}
private void init() {
// Iterate over all rows
for (short rowIndex = 0; rowIndex < FIELD_DIMENSION; ++rowIndex) {
// Iterate over the columns and set the default (or start) value of field
for (short columnIndex = 0; columnIndex < FIELD_DIMENSION; ++columnIndex) {
this.cells[rowIndex][columnIndex] = EMPTY_FIELD;
}
}
}
}

我认为你想要多个字段,所以你应该使用一个"超级字段"类,它包含你需要的所有子字段。

/**
* This class represents the super field which contains all single field of the traditional tic tac toe.
*/
class SuperField {
private static final short SUPER_FIELD_DIMENSION = 3;
private final Field[][] fields;
public SuperField() {
this.fields = new Field[SUPER_FIELD_DIMENSION][SUPER_FIELD_DIMENSION];
this.init();
}
public Field getField(final short field) {
return this.fields[field / 3][field % 3];
}
public void reset() {
this.init();
}
private void init() {
// Iterate over all rows
for (short rowIndex = 0; rowIndex < SUPER_FIELD_DIMENSION; ++rowIndex) {
// Iterate over the columns and set the default field
for (short columnIndex = 0; columnIndex < SUPER_FIELD_DIMENSION; ++columnIndex) {
this.fields[rowIndex][columnIndex] = new Field();
}
}
}
}

在视图一侧,按钮应该具有类似"0-0-0"、"0-0-1"、…的id,。。。,"8-2-2"。可以在访问单个单元格所需的索引中解析此id。

public short[] getIds(String buttonId) {
String[] idParts = anyId.split("-");
return {Short.parseShort(idParts[0]), Short.parseShort(idParts[1]) ,Short.parseShort(idParts[2])};
}
short fieldIndex = getIds("1-1-1")[0];
short rowIndex = getIds("1-1-1")[1];
short columnIndex = getIds("1-1-1")[2];

游戏的逻辑应该在一个单独的类中。这样更容易发现错误。

编辑:这是额外的

游戏逻辑类应该是一个单例。它的一个实例可以在所有View类中使用。这样你就可以把里面的按钮传过去,然后和它们一起工作。

public class GameLogic {
private static final GameLogic INSTANCE = new GameLogic();
private SuperField superField;
private GameLogic() {
this.superField = new SuperField();
}
public static GameLogic getGameLogic() {
return GameLogic.INSTANCE;
}
public SuperField getGameField(){
return this.superField;
}
}

用法如下:

GameLogic gameLogic = GameLogic.getGameLogic();
SuperField superField = gameLogic.getGameField();

如果您编写了一个以Button对象为参数的方法,那么该参数应该是final。这将把原始Button对象传递到方法中,您可以更改其中的按钮。

GameLogic类现在可以用"isMovePossible"或"isGameFinished"等方法进行扩展。。。

对于初学者,我认为应该将方框放入数组或映射中。类似这样的东西(我只能假设你为a1、a2等使用的对象类型是按钮变量):

Map<String, List<JButton>> boxes = new HashMap<String, List<JButton>>();

使用将您的数据存储在那里

List boxA = new ArrayList<JButton>();
List boxB = new ArrayList<JButton>();
boxA.add(a1);
boxA.add(a2);
// so on
boxB.add(b1);
boxB.add(b2);
// so on
boxes.put("BoxA", boxA); // the string can be anything, just used for a label if you want to grab a specific box
boxes.put("BoxB", boxB);

至少这样它会更有条理。然后,当你想访问按钮或框时,只需做这样的事情:

for (Map.Entry<String, List<JButton>> entry : boxes.entrySet()) { // will loop through each box
for (i = 0; i < entry.getValue().size(); i++) { // will loop through every button in a box
entry.getValue().get(i).setEnabled(false);
}
}

注意:这都是psuedo代码,因此可能需要进行一些修改。希望这能有所帮助。一旦你把所有的数据都放在列表和地图中,管理起来就会容易得多。

最新更新