首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在四连线游戏中,如何在棋盘上表达玩家

在四连线游戏中,如何在棋盘上表达玩家
EN

Stack Overflow用户
提问于 2021-05-08 01:55:14
回答 1查看 44关注 0票数 0

在selectline中输入的数字在板的第二行都得到了很好的实现。但是它不能从第三行开始工作。我该怎么办?以下是我的代码的一部分

如下图所示,它应该打印在第三行,第四行...我只打印到第二行。enter image description here

代码语言:javascript
运行
复制
public class Practice1 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Board board = new Board(13, 6);
        SelectLine selLine = new SelectLine();
        for (int i = 0; i < 10; i++) {
            selLine.input();
            board.put(selLine);
        }
    }

}

public class SelectLine {
    int line;
    public void input() {
        System.out.print("Select Line : ");
        Scanner sc = new Scanner(System.in);
        line = sc.nextInt();

    }
    public int getY() {
        return line;

    }
}

public class Board {
    char[][] string;
    char[][] om;
    int width;
    int height;
    int count;


    public Board(int width, int height) {

        count = 0;
        string = new char[height][width];
        this.width = width;
        this.height = height;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (i == 0 && j == 0) {

                    string[i][j] = '┌';
                } else if (j % 2 == 1) {
                    string[i][j] = '─';
                } else if (i == 0 && j == width - 1) {
                    string[i][j] = '┐';
                } else if (i == height - 1 && j == 0) {

                    string[i][j] = '└';
                } else if (i == height - 1 && j == width - 1) {
                    string[i][j] = '┘';
                } else if (i == 0) {
                    string[i][j] = '┬';
                } else if (j == 0) {

                    string[i][j] = '├';
                } else if (i == height - 1) {
                    string[i][j] = '┴';
                } else if (j == width - 1) {
                    string[i][j] = '┤';
                } else
                    string[i][j] = '┼';
            }
        }
    }

    public void print() {
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++)
                System.out.print(string[i][j]);
            System.out.println();
        }
    }

    public void put(SelectLine setLine) {

        int y = setLine.getY();
        int x = 6;
        if (count % 2 == 0) {
            if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
                x -= 1;
                string[x - 1][(y - 1) * 2] = '●';
                count++;
            } else {
                string[x - 1][(y - 1) * 2] = '●';
                count++;
            }
        } else {
            if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
                x -= 1;
                string[x - 1][(y - 1) * 2] = '○';
                count++;
            } else {
                string[x - 1][(y - 1) * 2] = '○';
                count++;
            }
        }
        this.print();
    }

}
}
EN

回答 1

Stack Overflow用户

发布于 2021-05-08 03:37:28

这不会是你正在寻找的答案,但希望能为你指明正确的方向。

代码语言:javascript
运行
复制
// At this point Y = the selected column
// and X = 6 (presumably the end/bottom)
        if (count % 2 == 0) {
            if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
                x -= 1;   // Right here X is now 5
                string[x - 1][(y - 1) * 2] = '●';  // and now you're setting string[4][]
                count++;
            } else {
                string[x - 1][(y - 1) * 2] = '●';  // and now you're setting string [5][]
                count++;
            }
        }

你明白了吗,你永远不能设置``string0 0..3,哪4行是前4行?如果你这样做会发生什么:

代码语言:javascript
运行
复制
// Only determine the piece in one spot.
// Because after this point it doesn't matter what colour it is
// you're just going to find the next open spot from the bottom up
char piece = (count %2 == 0) ? 'r' : 'w';

// Now check all your lines from the bottom
for (int x = 6; x < 0; x--) {
   // does !string [x-1][y calc] contain 'r' or 'w'
   // string [x-1][y calc] = piece;
   // break;
}

// print/wins/etc.

从底部开始,一直向上,直到该点不包含rw,然后将其放在那里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67439672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档