阵列编程 - 为您在一个井字游戏得主有n个玩家的N×N板 [英] Array programming - check winner in a Tic Tac Toe game for an nxn board with n players

查看:257
本文介绍了阵列编程 - 为您在一个井字游戏得主有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-1,Y + 1)(X,Y + 1)(X + 1,Y + 1)底部(X-1,Y-1)(X,Y-1) (X + 1,Y-1)
 侧(X + 1,Y + 1)(X + 1,y)的(X + 1,Y-1),(X-1,Y + 1)(X-1,y)的(X-1,y轴1)时,对角线和那些经历中间(X,Y)

It will check the top (x-1,y+1) (x,y+1) (x+1,y+1) bottom(x-1,y-1) (x,y-1) (x+1,y-1) sides(x+1,y+1) (x+1,y) (x+1,y-1) , (x-1,y+1) (x-1,y) (x-1,y-1) , the diagonals and the ones going through the middle(x,y).

我的code到目前为止是:

my code so far is:

   public int checkWinning() {
     for(int a = 1; a < size-1; a++){
        for(int b = 1; b < size-1; b++){
            if (board[a][b] == board[a+1][b] && board[a][b] == board[a-1][b]){
                return board[a][b];
            }else if(board[a][b] == board[a][b+1] && board[a][b] == board[a][b-1]){
                return board[a][b];
            }else if(board[a][b] == board[a+1][b-1] && board[a][b] == board[a-1][b+1]){
                return board[a][b];
            }else if(board[a][b] == board[a+1][b+1] && board[a][b] == board[a-1][b-1]){
                return board[a][b];
            }
        }
    }
    for(int d = 1; d < size-1; d++){
        if (board[0][d] == board[0][d-1] && board[0][d] == board[0][d+1]){
            return board[0][d];
        } else if (board[size-1][d] == board[size-1][d-1] && board[size-1][d] == board[size-1][d+1]){
            return board[size-1][d];
        }
    }

    for(int c = 1; c < size-1; c++){
        if (board[c][0] == board[c-1][0] && board[c][0] == board[c+1][0]){
            return board[c][0];
        }else if(board[c][size-1] == board[c-1][size-1] && board[c][size-1] == board[c+1][size-1]){
            return board[c][size-1];
            }
        }   
    return 0;
}

在第一部分中,我检查通过中间和对角线的人。第二部分我检查顶部的底部和顶部和THRID部检查的两侧。

where the first section is where I check the ones through the middle and diagonals. the second section I check the top an bottom and the top and the thrid section checks the sides.

当它返回0是指有没有赢家呢。

When it returns 0 is means that there are no winner yet.

@override
public void checkResult() {
    int winner = this.board.checkWinning();
    if (winner > 0) {
        this.ui.showResult("Player "+winner+" wins!");
    }
    if (this.board.checkFull()) {
        this.ui.showResult("This is a DRAW!");
    }
}

局[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), just so you know it.

我的问题是,当我的主板扩展到比3X3程序莫名其妙较大覆盖其自身或不每次检查每一件事情两侧的顶部和底部,而我似乎无法得瑟为什么。

my problem is that when i expands the board to a size larger than 3x3 the program somehow overwrites it self or a does not check every thing sides top and bottom every time, and I can't seem too se why.

推荐答案

编辑:

与应用程序打了几分钟......有趣的结果。

played with the app for a few minutes... interesting results

java -jar tic-tac-toe.jar 5 20

It was a cats game!!

|1|1|5|5|1|3|5|3|1|5|2|5|1|1|2|
|2|3|2|3|1|5|3|5|3|2|3|1|5|2|2|
|5|4|5|4|1|5|5|4|2|1|4|5|4|2|2|
|3|2|1|5|5|5|2|4|5|3|4|1|2|4|2|
|3|4|1|2|5|4|1|1|4|5|1|3|3|4|1|
|1|5|4|4|3|2|5|1|3|5|1|3|5|3|4|
|2|5|1|4|3|3|3|5|3|1|1|4|3|4|4|
|1|4|5|1|1|5|4|5|2|4|1|1|5|4|3|
|1|3|2|1|4|2|4|3|3|4|5|2|4|3|3|
|5|1|1|3|3|4|4|4|2|2|1|4|3|2|5|
|2|2|3|1|5|5|4|1|3|5|3|2|3|3|2|
|2|4|2|4|4|1|3|1|1|3|1|2|1|2|2|
|2|5|5|1|4|3|4|5|5|4|5|3|3|5|2| 
|4|5|2|1|5|3|2|1|3|2|2|2|2|4|4|
|4|1|1|4|5|4|5|4|2|2|3|3|2|2|3|

Played 100 games:
Number wins by Player1: 0
Number wins by Player2: 0
Number wins by Player3: 0
Number wins by Player4: 0
Number wins by Player5: 0
Number of ties: 100

没有通过所有100场比赛滚动查找中奖板,但我认为这是有趣的:

didn't scroll through all 100 games to find the winning board, but I thought this was interesting:

java -jar tic-tac-toe.jar 2 10

Player2 won the game!

|1|1|2|1|2|2| |2|1|2|
|2|2|2|2|2|2|2|2|2|2|
|2|1|2|2|2|1|1|1|1|1|
|1|1|1|1|2|1|2|1|1|1|
|2|2| |1|2|1|1|1|1|2|
|2|2|2|1|1|1| |1|2|2|
|2|2|1|2|2|2|2|2|1|1|
| | |2|2|2|2| |1|1|1|
|1|1|2|2|2|1|1|1|1| |
| | |1|1|1|1|1|2|1| |

Played 100 games:
Number wins by Player1: 0
Number wins by Player2: 1
Number of ties: 99

这不会回答你的问题......但我把它有点远......决定实施的解决方案。
与其匹配计数......我刚刚从德点的最后一个玩家扮演检查,如果连续列和所有标记diagnal匹配的球员,他赢了。

This does answer your question... but I took it a bit far... decided to implement the solution. Instead of counting matches... I just check from teh point the last player plays, if all marks in a row column and diagnal match the players, he wins.

package com.clinkworks.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TicTacToe {

    private static final String TIE = "TIE";

    private static final Map<String, Integer> gamesToWinsMap = new HashMap<String, Integer>();


    /**
     * accepts input in the following format:
     * 
     * playerCount rowCount columnCount (sets the game with the n players, n columns, and n rows)
     *      - java -jar tic-tac-toe.jar 2 3 3
     * PlayerCount squareSize (defaults to a game with rows and cols the same as squareSize and the player count given)
     *       - java -jar tic-tac-toe.jar 2 3
     * PlayerCount (defaults to a 3 by 3 game)
     *       - java -jar tic-tac-toe.jar 2
     * no input (defaults to a 3 by 3 game with 2 players)
     *       - java -jar tic-tac-toe.jar
     * @param args
     */
    public static void main(String[] args) {

        int playerCount = 2;
        int rows = 3;
        int cols = 3;

        if(args.length == 3){
            playerCount = Integer.valueOf(args[0]);
            rows = Integer.valueOf(args[1]);
            cols = Integer.valueOf(args[2]);
        }

        if(args.length == 2){
            playerCount = Integer.valueOf(args[0]);
            rows = Integer.valueOf(args[1]);
            cols = rows;
        }

            if(args.length == 1){
                    playerCount = Integer.valueOf(args[0]);
            }


        for(int i = 1; i <= playerCount; i++){
            gamesToWinsMap.put("Player" + i, 0);
        }

        //lets play 100 games and see the wins and ties
        playGames(100, playerCount, rows, cols);

        for(int i = 1; i <= playerCount; i++){
            System.out.println("Number wins by Player" + i + ": " + gamesToWinsMap.get("Player" + i));
        }

        System.out.println("Number of ties: " + gamesToWinsMap.get(TIE));
    }

    public static void playGames(int gamesToPlay, int playerCount, int rows, int cols) {
        //play a new game each iteration, in our example, count = 100;
        for (int i = 0; i < gamesToPlay; i++) {
            playGame(playerCount, rows, cols);
        }
    }

    public static void playGame(int playerCount, int rows, int cols) {
        //create a new game board. this initalizes our 2d array and lets the complexity of handling that
        // array be deligated to the board object.


        Board board = new Board(playerCount, rows, cols);

        //we are going to generate a random list of moves. Heres where we are goign to store it
        List<Move> moves = new ArrayList<Move>();

        //we are creating moves for each space on the board.
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                moves.add(new Move(row, col));
            }
        }

        //randomize the move list
        Collections.shuffle(moves);

        //do each move
        for (Move move : moves) {
            board.play(move);

            if(gameOver(board)){
                break;
            }
        }
    }

    public static boolean gameOver(Board board){
        if (board.whoWon() != null) {
            System.out.println(board.whoWon() + " won the game!");
            System.out.println(board);

            Integer winCount = gamesToWinsMap.get(board.whoWon());
            winCount = winCount == null ? 1 : winCount + 1;

            gamesToWinsMap.put(board.whoWon(), winCount);

            return true;

        } else if (board.movesLeft() == 0) {
            System.out.println("It was a cats game!!");
            System.out.println(board);

            Integer tieCount = gamesToWinsMap.get(TIE);
            tieCount = tieCount == null ? 1 : tieCount + 1;
            gamesToWinsMap.put(TIE, tieCount);

            return true;
        }

        return false;
    }

    public static class Move {
        private int row;
        private int column;

        public Move(int row, int column) {
            this.row = row;
            this.column = column;
        }

        public int getRow() {
            return row;
        }

        public int getColumn() {
            return column;
        }

    }

    public static class Board {

        private final int rowSize;
        private final int columnSize;
        private final Integer[][] gameBoard;
        private int playerCount;
        private int currentPlayer;
        private String winningPlayer;

        public Board() {
            gameBoard = new Integer[3][3];
            currentPlayer = 1;
            winningPlayer = null;
            this.rowSize = 3;
            this.columnSize = 3;
            playerCount = 2;
        }


        public Board(int players) {
            gameBoard = new Integer[3][3];
            currentPlayer = 1;
            winningPlayer = null;
            this.rowSize = 3;
            this.columnSize = 3;
            playerCount = players;
        }

        public Board(int rowSize, int columnSize) {
            gameBoard = new Integer[rowSize][columnSize];
            currentPlayer = 1;
            winningPlayer = null;
            playerCount = 2;
            this.rowSize = rowSize;
            this.columnSize = columnSize;
        }            

        public Board(int players, int rowSize, int columnSize) {
            gameBoard = new Integer[rowSize][columnSize];
            currentPlayer = 1;
            winningPlayer = null;
            playerCount = players;
            this.rowSize = rowSize;
            this.columnSize = columnSize;
        }



        /**
        * 
        * @return the amount of empty spaces remaining on the game board, or if theres a winning player, zero.
        */
        public int movesLeft() {

            if(whoWon() != null){
                return 0;
            }

            int moveCount = 0;
            for (int x = 0; x < getRowSize(); x++) {
                for (int y = 0; y < getColumnSize(); y++) {
                    moveCount += getMoveAt(x, y) == null ? 1 : 0;
                }
            }
            return moveCount;
        }

        /**
        * If someone won, this will return the winning player.
        * 
        * @return the winning player
        */
        public String whoWon() {
            return winningPlayer;
        }

        /**
        * This move allows the next player to choose where to place their mark.
        * 
        * @param row
        * @param column
        * @return if the game is over, play will return true, otherwise false.
        */
        public boolean play(Move move) {

            if (!validMove(move)) {
                // always fail early
                throw new IllegalStateException("Player " + getCurrentPlayer() + " cannot play at " + move.getRow() + ", " + move.getColumn() + "\n" + toString());
            }

            doMove(move);

            boolean playerWon = isWinningMove(move);

            if (playerWon) {
                winningPlayer = "Player" + getCurrentPlayer();
                return true;
            }

            shiftPlayer();

            boolean outOfMoves = movesLeft() <= 0;

            return outOfMoves;
        }

        public int getRowSize() {
            return rowSize;
        }

        public int getColumnSize() {
            return columnSize;
        }

        public int getCurrentPlayer() {
            return currentPlayer;
        }

        public Integer getMoveAt(int row, int column) {
            return gameBoard[row][column];
        }

        private void doMove(Move move) {
            gameBoard[move.getRow()][move.getColumn()] = getCurrentPlayer();
        }

        private void shiftPlayer() {
            if(getCurrentPlayer() == getPlayerCount()){
                currentPlayer = 1;
            }else{
                currentPlayer++;
            }
        }

        private int getPlayerCount() {
            return playerCount;
        }


        private boolean validMove(Move move) {
            boolean noMoveAtIndex = false;
            boolean indexesAreOk = move.getRow() >= 0 || move.getRow() < getRowSize();
            indexesAreOk = indexesAreOk && move.getColumn() >= 0 || move.getColumn() < getColumnSize();
            if (indexesAreOk) {
                noMoveAtIndex = getMoveAt(move.getRow(), move.getColumn()) == null;
            }
            return indexesAreOk && noMoveAtIndex;
        }

        private boolean isWinningMove(Move move) {
            // since we check to see if the player won on each move
            // we are safe to simply check the last move
            return winsDown(move) || winsAcross(move) || winsDiagnally(move);
        }

        private boolean winsDown(Move move) {
            boolean matchesColumn = true;

            for (int i = 0; i < getColumnSize(); i++) {
                Integer moveOnCol = getMoveAt(move.getRow(), i);
                if (moveOnCol == null || getCurrentPlayer() != moveOnCol) {
                    matchesColumn = false;
                    break;
                }
            }

            return matchesColumn;
        }

        private boolean winsAcross(Move move) {
            boolean matchesRow = true;
            for (int i = 0; i < getRowSize(); i++) {
                Integer moveOnRow = getMoveAt(i, move.getColumn());
                if (moveOnRow == null || getCurrentPlayer() != moveOnRow) {
                    matchesRow = false;
                    break;
                }
            }
            return matchesRow;
        }

        private boolean winsDiagnally(Move move) {
            // diagnals we only care about x and y being teh same...
            // only perfect squares can have diagnals
            // so we check (0,0)(1,1)(2,2) .. etc
            boolean matchesDiagnal = false;
            if (isOnDiagnal(move.getRow(), move.getColumn())) {
                matchesDiagnal = true;
                for (int i = 0; i < getRowSize(); i++) {
                    Integer moveOnDiagnal = getMoveAt(i, i);
                    if (moveOnDiagnal == null || moveOnDiagnal != getCurrentPlayer()) {
                        matchesDiagnal = false;
                        break;
                    }
                }
            }

            return matchesDiagnal;
        }

        private boolean isOnDiagnal(int x, int y) {
            if (boardIsAMagicSquare()) {
                return x == y;
            } else {
                return false;
            }
        }

        private boolean boardIsAMagicSquare() {
            return getRowSize() == getColumnSize();
        }

        public String toString() {
            StringBuffer stringBuffer = new StringBuffer();
            for(int y = 0; y < getColumnSize(); y++) {
                for(int x = 0; x < getRowSize(); x++) {
                    Integer move = getMoveAt(x, y);
                        String moveToPrint = "";
                    if (move == null) {
                        moveToPrint = " ";
                    } else {
                        moveToPrint = move.toString();
                    }
                    stringBuffer.append("|").append(moveToPrint);
                }
                stringBuffer.append("|\n");
            }
            return stringBuffer.toString();
        }
    }
}

这篇关于阵列编程 - 为您在一个井字游戏得主有n个玩家的N×N板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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