首页
学习
活动
专区
圈层
工具
发布

Tic Tae趾
EN

Code Golf用户
提问于 2013-12-01 20:34:48
回答 2查看 1.4K关注 0票数 8

(更著名的名字是三维Tic Tac趾;我只是编了一个听起来很吸引人的名字;-))

让两个人一起玩这个游戏吧。

规格:

  • 输出
    • 最简单的解释方法是一个例子:+-++\x~(x+-+. +-+-+-++ -+-+ +-+-+-+-++-+
    • 水平地显示这四层也是可以的,它们之间只有一个空格。
    • 网格可能是非文本的;在这种情况下,您必须以合理的方式表示第三维度;请参见下面的评分部分。
    • 在每个玩家回合之后,如果一个玩家赢了,输出"{ player }赢“,如果棋盘满了,没有人赢,输出”平局“,或者如果游戏还没有结束,输出”平局“(或者以合理的方式通知用户赢/平局)。

  • 输入
    • xyz坐标,0-基
    • 示例:输入031是这个位置:+- ++-+-+\x-+\x-+-++-+++-++-++-+ +-++-+-++-+
    • 在X和O之间交替,从X开始,并提示用户使用文本“{player}”S转:

  • 获胜条件
    • 如果玩家在一行中获得4个符号(X或O),即水平、垂直或对角线,则玩家将获胜。例如,如果玩家玩过其中的任何一种,这些都是胜利(以我指定的输入格式):
    • 000,001,002,003
    • 120,130,100,110
    • 000,110,220,330
    • 300,122,033,211

  • 评分
    • 工作解决方案的[ws] 10基点
    • [gr] +20点如果您有一个图形(意为非文本)网格
      • [cc] +30点如果您可以单击一个单元格到那里

代码语言:javascript
复制
- `[cp]` +20 points for building a computer player to play against 
    - `[ai]` +10 to 100 points for how good the AI is (judged by me)
    - `[ot]` +15 points if you can choose between one and two player
代码语言:javascript
复制
- `[ex]` +3 points for each interesting or confusing thing in your code that you explain in your answer (**interesting**, so don't say "n++ adds one to n" and expect to get 3 points. just be reasonable.) 
    - for example, you could say "I used `[a,b,c].map(max).sum%10` because it takes the foos and converts them into a bar, because {{insert explanation here}}, which I use to frob the baz."
    - if you golf part of or all of your code, you will of course find many more interesting things to explain, but golfing is not required.
代码语言:javascript
复制
- `[wl]` +10 points for showing the winning line, for example "000 001 002 003," or highlighting the squares in some way (if you're using a GUI)
- +5 points for every upvote (this is the [popularity-contest](/questions/tagged/popularity-contest) part)
- downvotes don't count
- include your score in the answer by using the IDs I have placed for the bonuses 
    - example: 10 [ws] + 20 [gr] + 20 [cp] = 50
    - don't include upvotes since those change often; I will count those myself
代码语言:javascript
复制
- I will accept the highest scored answer in one week; if a higher scored answer appears I will accept it
- if two answers tie, the earlier posted answer will win
EN

回答 2

Code Golf用户

发布于 2013-12-03 08:17:45

Java

10ws + 20gr + 30抄送 + 4*3例如 = 72

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;

public class Game {

    public static void main(String[] args) {
        //Initial threads
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Game();
            }
        });
    }
    JFrame frame;
    Grid grid;

    Game() {
        grid = new Grid();
        frame = new JFrame("Tic Tac Tae Toe");

        frame.add(grid);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

class Grid extends JPanel implements MouseInputListener {

    static final int BEGIN_X = 50, BEGIN_Y = 50;
    static final int GAP = 50;
    static final int TILE_WIDTH = 40;
    XO[][][] grid;
    int lastX, lastY, lastZ;
    int mouse_x, mouse_y;
    JLabel turn;
    boolean won;

    public Grid() {
        // Just because. I felt like copy-paste.
        grid = new XO[][][]{
            {
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N}
            },
            {
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N}
            },
            {
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N}
            },
            {
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N},
                {XO.N, XO.N, XO.N, XO.N}
            }
        };

        lastX = -1;
        lastY = -1;
        lastZ = -1;
        won = false;

        turn = new JLabel("X");

        add(turn);

        this.setPreferredSize(new Dimension(2 * BEGIN_X + 4 * TILE_WIDTH,
                2 * BEGIN_Y + 4 * (4 * TILE_WIDTH + GAP) - GAP));
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    public XO determineWinner() {
        XO type = grid[lastZ][lastY][lastX];

        //start on same z-plane

        for (int x = 0; x < 4; x++) {
            if (grid[lastZ][lastY][x] != type) {
                break;
            }
            if (x == 3) {
                return type; //type won!
            }
        }
        for (int y = 0; y < 4; y++) {
            if (grid[lastZ][y][lastX] != type) {
                break;
            }
            if (y == 3) {
                return type; //type won!
            }
        }
        //first diagonal
        if (lastX == lastY) {
            for (int x = 0, y = 0; x < 4; x++, y++) {
                if (grid[lastZ][y][x] != type) {
                    break;
                }
                if (x == 3) {
                    return type; //type won!
                }
            }
        } //second diagonal
        else if (lastX == 3 - lastY) {
            for (int x = 0, y = 3; x < 4; x++, y--) {
                if (grid[lastZ][y][x] != type) {
                    break;
                }
                if (x == 3) {
                    return type; //type won!
                }
            }
        }
        //first plane finished
        //now onto changing z's
        //vertical column
        for (int z = 0; z < 4; z++) {
            if (grid[z][lastY][lastX] != type) {
                break;
            }
            if (z == 3) {
                return type; //type won!
            }
        }
        //descending y. note: descending means stair-like (diagonal)
        if (lastZ == lastY) {
            for (int z = 0, y = 0; z < 4; z++, y++) {
                if (grid[z][y][lastX] != type) {
                    break;
                }
                if (z == 3) {
                    return type; //type won!
                }
            }
        } //ascending y
        else if (lastZ == 3 - lastY) {
            for (int z = 0, y = 3; z < 4; z++, y--) {
                if (grid[z][y][lastX] != type) {
                    break;
                }
                if (z == 3) {
                    return type; //type won!
                }
            }
        } //ascending x. note: ascending means stair-like, but other way
        if (lastZ == 3 - lastX) {
            for (int z = 0, x = 3; z < 4; z++, x--) {
                if (grid[z][lastY][x] != type) {
                    break;
                }
                if (z == 3) {
                    return type; //type won!
                }
            }
        } //descending x
        else if (lastZ == lastX) {
            for (int z = 0, x = 0; z < 4; z++, x++) {
                if (grid[z][lastY][x] != type) {
                    break;
                }
                if (z == 3) {
                    return type; //type won!
                }
            }
        }

        //diagonals, changing all three
        if (lastZ == lastX && lastZ == lastY) {
            for (int z = 0, x = 0, y = 0; z < 4; z++, x++, y++) {
                if (grid[z][y][x] != type) {
                    break;
                }
                if (z == 3) {
                    return type; //type won!
                }
            }
        } else if (3 - lastZ == lastX && 3 - lastZ == lastY) {
            for (int z = 3, x = 0, y = 0; z < 4; z--, x++, y++) {
                if (grid[z][y][x] != type) {
                    break;
                }
                if (x == 3) {
                    return type; //type won!
                }
            }
        } else if (lastZ == lastX && lastZ == 3 - lastY) {
            for (int z = 0, x = 0, y = 3; z < 4; z++, x++, y--) {
                if (grid[z][y][x] != type) {
                    break;
                }
                if (z == 3) {
                    return type; //type won!
                }
            }
        } else if (lastZ == 3 - lastX && lastZ == lastY) {
            for (int z = 0, x = 3, y = 0; z < 4; z++, x--, y++) {
                if (grid[z][y][x] != type) {
                    break;
                }
                if (z == 3) {
                    return type; //type won!
                }
            }
        }

        //check for tie
        out: for (int z = 0; z < 4; z++){
            for (int y = 0; y < 4; y++){
                for (int x = 0; x < 4;x++){
                    if (grid[z][y][x] == XO.N){
                        break out;
                    }
                }
            }
            if (z == 3){
                return XO.N;
            }
        }

        return null;
    }

    @Override
    public void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);

        Graphics2D g = (Graphics2D) graphics;
        g.setColor(Color.BLACK);

        //Looks a little nicer, but this could be left out.
        g.setFont(new Font("Courier New", Font.BOLD, 20));

        //paint grid rectangles.
        for (int z = 0; z < 4; z++) {
            int begin_y = BEGIN_Y + z * (4 * TILE_WIDTH + GAP);

            //draw left line
            g.drawLine(BEGIN_X, begin_y,
                    BEGIN_X, begin_y + 4 * TILE_WIDTH);

            //draw bottom line
            g.drawLine(BEGIN_X, begin_y + 4 * TILE_WIDTH,
                    BEGIN_X + 4 * TILE_WIDTH, begin_y + 4 * TILE_WIDTH);

            for (int y = 0; y < 4; y++) {
                for (int x = 0; x < 4; x++) {
                    //draw individual grid lines
                    g.drawLine(BEGIN_X + (x + 1) * TILE_WIDTH, begin_y + y * TILE_WIDTH,
                            BEGIN_X + (x + 1) * TILE_WIDTH, begin_y + (y + 1) * TILE_WIDTH);
                    g.drawLine(BEGIN_X + x * TILE_WIDTH, begin_y + y * TILE_WIDTH,
                            BEGIN_X + (x + 1) * TILE_WIDTH, begin_y + y * TILE_WIDTH);

                    //if this was the last chosen square...
                    if (lastX == x && lastY == y && lastZ == z) {
                        g.setColor(Color.ORANGE);
                        //fill it orange! (not required, but I think it looks nice)
                        g.fillRect(BEGIN_X + x * TILE_WIDTH + 1,
                                begin_y + y * TILE_WIDTH + 1,
                                TILE_WIDTH - 1, TILE_WIDTH - 1);
                        g.setColor(Color.BLACK);
                    }

                    //if mouse is over this square...
                    if ((BEGIN_X + x * TILE_WIDTH) < mouse_x
                            && (BEGIN_X + (x + 1) * TILE_WIDTH) > mouse_x
                            && (begin_y + y * TILE_WIDTH) < mouse_y
                            && (begin_y + (y + 1) * TILE_WIDTH) > mouse_y) {
                        g.setColor(new Color(100, 100, 255));
                        //fill it blue! (not required, but I think it looks nice)
                        g.fillRect(BEGIN_X + x * TILE_WIDTH + 1,
                                begin_y + y * TILE_WIDTH + 1,
                                TILE_WIDTH - 1, TILE_WIDTH - 1);
                        g.setColor(Color.BLACK);
                    }

                    //paint value of tile
                    g.drawString(grid[z][y][x].val,
                            BEGIN_X + x * TILE_WIDTH + TILE_WIDTH / 2,
                            begin_y + y * TILE_WIDTH + TILE_WIDTH / 2);

                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (!won) {
            int x = e.getX(), y = e.getY();
            x -= BEGIN_X;
            x /= TILE_WIDTH;

            y -= BEGIN_Y;

            int z = 0;
            while (y > 4 * TILE_WIDTH) {
                z++;
                y -= 4 * TILE_WIDTH + GAP;
            }

            y /= TILE_WIDTH;

            //**********************************
            //DOES NOT ALWAYS GET PAST HERE!
            //**********************************
            if (x < 0 || y < 0 || z < 0 || x >= 4 || y >= 4 || z >= 4) {
                return;
            }
            if (grid[z][y][x] == XO.N) {
                grid[z][y][x] = XO.valueOf(turn.getText());
            } else {
                return;
            }

            lastX = x;
            lastY = y;
            lastZ = z;

            XO who_won = determineWinner();
            if (who_won == null){
                if (turn.getText().equals("X")) {
                    turn.setText("O");
                } else {
                    turn.setText("X");
                }
            }
            else if (who_won != XO.N) {
                turn.setText(who_won.toString() + " wins");
            } else{
                turn.setText("tie");
            }
            repaint();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mouse_x = e.getX();
        mouse_y = e.getY();
        repaint();
    }
}

enum XO {

    X("X"), O("O"), N(" ");
    String val;

    XO(String c) {
        val = c;
    }
}

这是一个可行的解决方案,但我会做得更多。

例如:

  • 使用初始线程运行
  • 使用一个名为JLabelturn通知玩家该轮到谁(窗口顶部的字母将得到改进)。这个标签也用来表示谁赢了。
  • 通过存储最后的x、y和z值并在paintComponent()期间绘制相应的方形橙色来显示最后的移动。
  • 通过在paintComponent()期间计算鼠标指针(蓝方格)的位置

截图:

票数 2
EN

Code Golf用户

发布于 2013-12-04 23:52:12

Ti-Basic 84

10ws+20gr+20cp+10艾城+15奥特+3例如=78

可编程和可播放从钛-84计算器。+100?

另外,->表示STO->按钮。

代码语言:javascript
复制
:Disp LOOKS-LIKE-THIS-MIGHT-BE-SOMETHING-INTERESTING-IN-THE-CODE
:ClrHome
:Disp "TIC TAC TAE TOE"
:Disp "HOW MANY PLAYERS"
:Input P
:If P=1 :Disp "YOUR OPPONENTS NAME IS AI BOB"
:If P=2 :Disp "YOUR OPPONENTS NAME IS AI BOB EVEN THOUGH YOU WANT TO PLAY TWO PLAYERS"
:Horizontal -10
:Horizontal -5
:Horizontal 0
:Horizontal 5
:Horizontal 10
:Vertical -10
:Vertical -5
:Vertical 0
:Vertical 5
:Vertical 10
:0->A
:Lbl M
:Disp "YOUR MOVE X-POS"
:Input X
:Disp "YOUR MOVE Y-POS"
:Input Y
:Circle(2,X,Y)
:Circle(2,rand,rand)
:Disp "UGH, HE DIDNT ALIGN IT CORRECTLY"
:Disp "YOU MIGHT WANT TO STOP PLAYING"
:If A=>4 :Goto W
:If A<4 :A+1->A
:Goto M
:Lbl W
:Disp "HAVE YOU WON, 1 IS YES, 0 IS NO"
:Input W
:If W=1 :Goto E
:Goto M
:Lbl E
:Disp "YOU HAVE WON! GOOD JOB!"
:Disp "ENTER TO EXIT"
:Pause

有趣的是:开头的代码行(:Disp LOOKS-LIKE-THIS-MIGHT-BE-SOMETHING-INTERESTING-IN-THE-CODE)打印0,然后立即从屏幕上清除。

请给我更多的分数,我的人工智能的技能,但我很确定你会坚持10分。

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

https://codegolf.stackexchange.com/questions/15573

复制
相关文章

相似问题

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