井字为n×n个一板N播放器 - 检查赢家 [英] Tic tac toe for n player in a nxn board - check winner

查看:141
本文介绍了井字为n×n个一板N播放器 - 检查赢家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出的玩家在N×N的主板N多一个井字游戏,但获胜的条件是连续跳投3。
我迄今解决问题的方法是:当一个移动是由该程序将检查下列方3上的行

I am making a tic tac toe game for n number of players on a nxn board, but the winning condition is aways 3 on a row. My so far solution to the problem is: when a move is made the program will check the following square for 3 on a row.

(x-1,y+1) (x,y+1) (x+1,y+1)

 (x-1,y)   (x,y)   (x+1,y)

(x-1,y-1) (x,y-1) (x+1,y-1)

其中(x,y)是已经正在取得的举动。我们有4案件这里是检查:连续胜利,列取胜,对角线胜一反对角线胜利。然后程序会检查将检查这下广场为3行。

where (x,y) is the move that has being made. We have 4 cases here which is check: a row win, a column win, a diagonal win and a anti diagonal win. Then the program will check will check this next square for 3 on a row.

(x-2,y+2)     0     (x,y+1)     0     (x+2,y+2)

    0     (x-1,y+1) (x,y+1) (x+1,y+1)     0

 (x-2,y)   (x-1,y)   (x,y)   (x+1,y)   (x+2,y)

    0     (x-1,y-1) (x,y-1) (x+1,y-1)     0 

(x-2,y-2)     0     (x,y-2)     0     (x+2,y-2)

试(X,Y)是一个已经被采取了行动。在这里,我们有8个例因我们检查!此举坐标为3的连续终点。 0以上的重presents只是不检查的人。

Again (x,y) is the move that has being made. We have here 8 cases because WE CHECK! the move coordinate as an end point of the 3 in a row. The 0 above represents just the ones that are not checked.

这件作品code的检查,这将是长期的,大量的文字。
在code的例子:

The piece of code to check this would be long and a lot of text. An example of the code:

  public int checkWinning(Coordinate c) {
    if (board[c.getX()][c.getY()] == board[c.getX()+1][c.getY()] && board[c.getX()][c.getY()] == board[c.getX()-1][c.getY()]){
        return board[c.getX()][c.getY()];
    }else if(board[c.getX()][c.getY()] == board[c.getX()][c.getY()+1] && board[c.getX()][c.getY()] == board[c.getX()][c.getY()-1]){
        return board[c.getX()][c.getY()];
    }else if(board[c.getX()][c.getY()] == board[c.getX()-1][c.getY()+1] && board[c.getX()][c.getY()] == board[c.getX()+1][c.getY()-1]){
        return board[c.getX()][c.getY()];
    }
}

下面是协调C,它已经正在取得的移动(X和Y来自另一个阶级这就是为什么我使用的getter),并还请注意:

Here are Coordinate c the "move" that has being made(X and Y comes from another class thats why I use getters) and note also :

局[X] [Y] - > 2维数组重新presenting本公司董事会,坐标从计算左上方(0,0)到右下角(大小-1,大小1) ,板[X] [Y] == 0表示无在位置(X,Y),板[X] [Y] ==我为我> 0意味着球员,我做的(X,Y)移动

Board[x][y] -> 2-dimensional array representing the board, The coordinates are counted from top-left (0,0) to bottom-right (size-1, size-1), board[x][y] == 0 signifies free at position (x,y), board[x][y] == i for i > 0 signifies that Player i made a move on (x,y)

有没有更聪明的方法来做到这一点?

Is there a smarter way to do this?

推荐答案

我会做的唯一的事情是如果 s到改为循环。想想看,在某些时候你可能需要从3中奖条件改为5.如果的的情况下,是你将不得不重新编写大量的code的,与环路将保持相同。

The only thing I would do is to change the ifs to loops. Consider that at some point you might want to change the winning condition from 3 to 5. In the case of ifs you will have to rewrite a lot of code, with loops it will remain the same.

我不建议你去寻找任何解决方案聪明比这一个,因为你的code应该是很容易的理解和支持。此外,我怀疑任何聪明的解决方案会比你目前的速度更快。

I would not recommend you to look for any solution "smarter" than this one, because your code should be easy to understand and support. Also I doubt that any smart solution will be faster than you current one.

这篇关于井字为n×n个一板N播放器 - 检查赢家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆