代码高尔夫:井字游戏 [英] Code Golf: Tic Tac Toe

查看:37
本文介绍了代码高尔夫:井字游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按字符数发布您的最短代码,以检查玩家是否赢了,如果赢了,是哪一个.

Post your shortest code, by character count, to check if a player has won, and if so, which.

假设您在变量 b(棋盘)中有一个整数数组,其中包含 Tic Tac Toe 棋盘和玩家的移动,其中:

Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where:

  • 0 = 未设置
  • 1 = 玩家 1 (X)
  • 2 = 玩家 2 (O)

所以,给定数组 b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ] 将代表棋盘

So, given the array b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ] would represent the board

X|O|X
-+-+-
 |X|O
-+-+-
X| |O

对于这种情况,您的代码应该输出 1 以指示玩家 1 获胜.如果无人获胜,您可以输出 0false.

For that situation, your code should output 1 to indicate player 1 has won. If no-one has won you can output 0 or false.

我自己的 (Ruby) 解决方案即将推出.

My own (Ruby) solution will be up soon.

编辑:抱歉,忘记将其标记为社区维基.您可以假设输入格式正确,无需进行错误检查.

Edit: Sorry, forgot to mark it as community wiki. You can assume the input is well formed and does not have to be error checked.

更新:请以函数的形式发布您的解决方案.大多数人已经这样做了,但有些人还没有这样做,这并不完全公平.该板作为参数提供给您的函数.结果应该由函数返回.该函数可以有一个您选择的名称.

Update: Please post your solution in the form of a function. Most people have done this already, but some haven't, which isn't entirely fair. The board is supplied to your function as the parameter. The result should be returned by the function. The function can have a name of your choosing.

推荐答案

C,77 (83) 个字符

这是 dmckee 解决方案的变体,除了紧凑编码中的每一对数字现在都是 ASCII 字符的基数为 9 的数字.

C, 77 (83) characters

This is a variant of dmckee's solution, except that each pair of digits in the Compact Coding is now the base-9 digits of the ASCII characters.

77-char 版本,不适用于 MSVC:

The 77-char version, does not work on MSVC:

// "J)9\t8\r=,\0" == 82,45,63,10,62,14,67,48,00 in base 9.
char*k="J)9 8\r=,",c;f(int*b){return(c=*k++)?b[c/9]&b[c%9]&b[*k--%9]|f(b):0;}

这个 83-char 版本,应该适用于每个 C 编译器:

This 83-char version, should work on every C compiler:

f(int*b){char*k="J)9    8\r=,",s=0,c;while(c=*k++)s|=b[c%9]&b[c/9]&b[*k%9];return s;}

(注意 9 和 8 之间的空格应该是制表符.StackOverflow 会将所有制表符转换为空格.)

(Note that the spaces between the 9 and 8 should be a tab. StackOverflow converts all tabs into spaces.)

测试用例:

#include <stdio.h>  
void check(int* b) {
    int h0 = b[0]&b[1]&b[2];
    int h1 = b[3]&b[4]&b[5];
    int h2 = b[6]&b[7]&b[8];
    int h3 = b[0]&b[3]&b[6];
    int h4 = b[1]&b[4]&b[7];
    int h5 = b[2]&b[5]&b[8];
    int h6 = b[0]&b[4]&b[8];
    int h7 = b[2]&b[4]&b[6];
    int res = h0|h1|h2|h3|h4|h5|h6|h7;
    int value = f(b);
    if (value != res)
        printf("Assuming f({%d,%d,%d, %d,%d,%d, %d,%d,%d}) == %d; got %d instead.\n", 
            b[0],b[1],b[2], b[3],b[4],b[5], b[6],b[7],b[8], res, value);
}
#define MAKEFOR(i) for(b[(i)]=0;b[(i)]<=2;++b[(i)])

int main() {
    int b[9];

    MAKEFOR(0)
    MAKEFOR(1)
    MAKEFOR(2)
    MAKEFOR(3)
    MAKEFOR(4)
    MAKEFOR(5)
    MAKEFOR(6)
    MAKEFOR(7)
    MAKEFOR(8)
        check(b);

    return 0;
}

这篇关于代码高尔夫:井字游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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