代码高尔夫:井字游戏
按字符数发布您的最短代码,以检查玩家是否获胜,如果获胜,是哪一个。
假设变量 b
(棋盘)中有一个整数数组,其中包含 Tic Tac Toe 棋盘和玩家的动作,其中:
- 0 = 没有设置
- 1 = 玩家 1 (X)
- 2 =玩家 2 (O)
因此,给定数组 b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ]
将代表棋盘
X|O|X
-+-+-
|X|O
-+-+-
X| |O
对于这种情况,您的代码应该输出1
表示玩家 1 获胜。如果没有人获胜,您可以输出 0
或 false
。
我自己的(Ruby)解决方案很快就会推出。
编辑:抱歉,忘记将其标记为社区维基。您可以假设输入格式正确,并且不必进行错误检查。
更新:请以函数的形式发布您的解决方案。大多数人已经这样做了,但有些人还没有,这并不完全公平。该板作为参数提供给您的函数。结果应该由函数返回。该函数可以有您选择的名称。
Post your shortest code, by character count, to check if a player has won, and if so, which.
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 = nothing set
- 1 = player 1 (X)
- 2 = player 2 (O)
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
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
.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
疯狂的 Python 解决方案 - 79 个字符
但是,这假设 b 中的棋盘位置有不同的顺序:
即
b[5]
代表左上角,所以在。为了最大限度地减少上述内容:
93 个字符和一个换行符。
更新: 使用按位 AND 技巧减少至 79 个字符和一个换行符:
Crazy Python solution - 79 characters
However, this assumes a different order for the board positions in b:
That is,
b[5]
represents the top-left corner, and so on.To minimize the above:
93 characters and a newline.
Update: Down to 79 characters and a newline using the bitwise AND trick:
C,77 (83) 个字符
这是 dmckee' 的变体s 解决方案,不同之处在于紧凑编码中的每对数字现在都是 ASCII 字符的基于 9 的数字。
77-char 版本,不适用于 MSVC:
此 83-char 版本,应该适用于每个 C 编译器:(
请注意,9 和 8 之间的空格应该StackOverflow 将所有制表符转换为空格。)
测试用例:
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.
The 77-char version, does not work on MSVC:
This 83-char version, should work on every C compiler:
(Note that the spaces between the 9 and 8 should be a tab. StackOverflow converts all tabs into spaces.)
Test case:
Python 80 (69) char
不是最短的 Python 解决方案,但我喜欢它如何将“DICE”引入井字游戏:
69 个字符用于更简单的表达式:
Python 80 (69) char
Not the shortest Python solution, but I like how it introduces "DICE" into a game of tic-tac-toe:
69 chars for the simpler expression:
Perl,87
85个字符当然,使用正则表达式返回 0、1 或 2 的函数(换行符只是为了避免滚动条):
它可以被称为
V(例如,@b)
。Perl, 87
85charactersA function that returns 0, 1 or 2, using a regular expression, of course (the newline's only there to avoid the scrollbar):
It can be called as
V(@b)
, for example.J,50 个字符
J, 50 chars
我不满意重复自己(水平/垂直和对角线),但我认为这是一个公平的开始。
C# w/LINQ:
策略:按位
AND
行/列/对角线的每个元素与其他元素(以 3 作为种子)以获得该子集的胜利者,以及OR< /code> 最后将它们全部放在一起。
I'm not happy with repeating myself (horizontal/vertical, and the diagonals), but I think it's a fair start.
C# w/LINQ:
Strategy: Bitwise
AND
each element of a row/column/diagonal with the other elements (with 3 as a seed) to obtain a victor for that subset, andOR
them all together at the end.Ruby,115 个字符
哎呀:不知怎的,我数错了很多。这实际上是 115 个字符,
而不是 79 个。出于教育目的,扩展的代码:
我确信这可以缩短,尤其是大数组,可能还有用于获取玩家动作的位掩码的代码——三元让我烦恼—— -但我认为目前这已经相当不错了。
Ruby, 115 chars
Oops: Somehow I miscounted by a lot. This is actually 115 characters, not 79.
And the expanded code, for educational purposes:
I'm sure this could be shortened, especially the big array and possibly the code for getting a bitmask of the players's moves--that ternary bugs me--but I think this is pretty good for now.
Perl, 76 char
水平取胜有三种方法:
一种从左下到右上对角取胜的方法:
垂直取胜的三种方法:
从左上到右下对角取胜的一种方法:
阅读中间的列以获得魔力数字。
Perl, 76 char
There are three ways to win horizontally:
One way to win diagonally from lower left to upper right:
Three ways to win vertically:
One way to win diagonally from upper left to lower right:
Read the middle columns to get the magic numbers.
Octave/Matlab,97 个字符,包括空格和换行符。如果没有获胜者,则输出 0;如果玩家 1 获胜,则输出 1;如果玩家 2 获胜,则输出 2;如果两个玩家都“获胜”,则输出 2.0801:
如果我们更改规格并从一开始将 b 作为 3x3 矩阵传入,我们可以删除重塑线,将其减少到 80 个字符。
Octave/Matlab, 97 characters, including spaces and newlines. Outputs 0 if no winner, 1 if player 1 won, 2 if player 2 won, and 2.0801 if both players "won":
If we change the specification and pass in b as a 3x3 matrix from the start, we can remove the reshape line, getting it down to 80 characters.
因为正确玩时没有人会在 tictactoe 中获胜,我认为这是最短的代码
7 个字符
更新:bash 的更好条目是这样的:
86 个字符或 81 个字符,不包括函数定义(win())。
但是,这是来自 bash 中的 tic-tac-toe 程序的代码,因此它不太符合规范。
because nobody wins at tictactoe when properly played i think this is the shortest code
7 chars
Update: A better entry for bash would be this:
86 characters or 81 excluding function definition(win()).
But, This is code from by tic-tac-toe program in bash so it does not quite meet specification.
Ruby, 85 char
如果输入有两个玩家都获胜,例如,
则输出为 3。
Ruby, 85 char
If the input has both players winning, e.g.
then the output is 3.
Haskell,假设上面的幻方。 77 字符
77 排除导入和定义 b.
或者 82 假设正常排序:
Haskell, Assuming the magic squares above. 77 Characters
77 excludes imports and defining b.
Or 82 assuming the normal ordering:
C,99 个字符
不是赢家,但也许还有改进的空间。以前从未这样做过。原始概念,初稿。
感谢 KennyTM 提供的一些想法和测试工具。
“开发版本”:
C, 99 chars
Not a winner, but maybe there's room for improvement. Never did this before. Original concept, first draft.
Thanks to KennyTM for a few ideas and the test harness.
The "development version":
(Iron)python,75 个字符
75 个字符代表一个完整的函数
66 个字符,如果你像其他人一样省略函数定义
8 个不同的方向由起始值 + 增量器表示,压缩为可以使用除法提取的单个数字和模块。例如 2,5,8 = 2*6 + 3 = 15。
使用 & 检查一行是否包含三个相等的值。操作员。 (如果它们不相等,则结果为零)。 max 用于寻找可能的获胜者。
(Iron)python, 75 characters
75 characters for a full function
66 characters if you leave out the function definition like some others have done
The 8 different directions are represented by starting value + incrementor, compressed into a single number that can be extracted using division and modula. For example 2,5,8 = 2*6 + 3 = 15.
Checking that a row contains three equal values is done using the & operator. (which results in zero if they aren't equal). max is used to find the possible winner.
C 语言解决方案(162 个字符):
这利用了玩家一值 (1) 和玩家二值 (2) 具有独立位集的事实。因此,您可以将三个测试框的值按位与在一起 - 如果该值非零,则所有三个值必须相同。另外,结果值==获胜的玩家。
到目前为止,这不是最短的解决方案,而是我能做的最好的解决方案:
更具可读性的版本:
A solution in C (162 Characters):
This makes use of the fact that player one value (1) and player two value (2) have independent bits set. Therefore, you can bitwise AND the values of the three test boxes together-- if the value is nonzero, then all three values must be identical. In addition, the resulting value == the player that won.
Not the shortest solution so far, but the best I could do:
A more readable version:
Python,102 个字符
由于您没有真正指定如何获取输入和输出,因此这是可能必须包装到函数中的“原始”版本。
b
是输入列表;r
是输出(0、1 或 2)。Python, 102 characters
Since you didn't really specify how to get input and output, this is the "raw" version that would perhaps have to be wrapped into a function.
b
is the input list;r
is the output (0, 1 or 2).Lua,130 个字符
130 个字符只是函数大小。如果没有找到匹配,该函数不返回任何内容,这在 Lua 中类似于返回 false。
Lua, 130 characters
The 130 characters is the function size only. The function returns nothing if no match is found, which in Lua is similar to returning false.
Visual Basic
275254(宽松打字)字符Visual Basic
275254 (with loose typing) charactersJavaScript - 下面的函数“w”是 114 个字符
JavaScript - function "w" below is 114 characters
J,97 个字符。
我本来打算发布其工作原理的解释,但那是昨天的事,现在我无法阅读这段代码。
我们的想法是,我们创建一个包含所有可能获胜三元组的列表 (048,246,012,345,678,036,147,258),然后计算每个玩家拥有的方格的幂集,然后将两个列表相交。如果有比赛,那就是胜利者。
J, 97 characters.
I was planning to post an explanation of how this works, but that was yesterday and now I can't read this code.
The idea is we create a list of all possible winning triples (048,246,012,345,678,036,147,258), then make the powerset of the squares each player has and then intersect the two lists. If there's a match, that's the winner.
Python - 75 个字符 (64)
我想出了 2 个表达式,每个 64 个字符:
当
你添加“W=lambda b:”使其成为一个函数时,就会产生 75 个字符。
迄今为止最短的Python?
Python - 75 chars (64)
I came up with 2 expressions, each 64chars:
and
When you add "W=lambda b:" to make it a function, that makes 75chars.
Shortest Python so far?
Python,285 字节
...哦,这不是你说的“Code Golf: Tic Tac Toe”的意思? ;)(输入小键盘数字来放置 x 或 o,即 7 是西北)
长版本
Python, 285 bytes
...Oh, this wasn't what you meant when you said "Code Golf: Tic Tac Toe"? ;) (enter numpad numbers to place x's or o's, i.e. 7 is north-west)
Long Version
我确信有一种更短的方法可以做到这一点,但是...... Perl,141 个字符(函数内有 134 个字符)
I'm sure there's a shorter way to do this but... Perl, 141 characters (134 inside the function)
c -- 144 个字符
缩小:
两者都返回计数(一个是必需的,另一个需要用空格替换)。
从偶数位置开始并取模 16 的八种获胜方式的数组代码。
从 埃里克·皮。
更易读的形式:
测试脚手架:
c -- 144 characters
Minified:
Both returns count (one necessary and the other would need replacing with a space).
The array codes for the eight ways to win in triplets starting from even positions and taken mod 16.
Bitwise and trick stolen from Eric Pi.
More readable form:
Testing scaffold:
也许可以做得更好,但我现在感觉不是特别聪明。这只是为了确保 Haskell 得到代表......
假设
b
已经存在,这会将结果放入w
中。假设输入来自 stdin 并输出到 stdout,
Probably could be made better, but I'm not feeling particularly clever right now. This is just to make sure Haskell gets represented...
Assuming that
b
already exists, this will put the result inw
.Assuming input from stdin and output to stdout,
C#,180 个字符:
(
g
是网格)可能需要改进...我仍在努力;)
C#, 180 characters :
(
g
being the grid)Could probably be improved... I'm still working on it ;)
Python,140 个字符
我的第一个高尔夫代码,重达 140 个字符(导入声明,我否认你!):
稍微不那么晦涩 g:
Python, 140 chars
My first code golf, weighing in at a hefty 140 chars (import statement, I deny you!):
Slightly less obscure g:
C# 解决方案。
将每行、列和列中的值相乘对角线。如果结果 == 1,则 X 获胜。如果结果 == 8,O 获胜。
C# Solution.
Multiply the values in each row, col & diagonal. If result == 1, X wins. If result == 8, O wins.
C#,154
163 170 177个字符借鉴其他提交的一些技术。
(不知道 C# 可以让你这样初始化数组)
C#, 154
163 170 177charactersBorrowing a couple of techniques from other submissions.
(didn't know C# let you init arrays like that)
C、113个字符
我觉得可行吗?我的第一个代码高尔夫,温柔点。
每 3 位数字编码 3 个需要匹配的单元格。内部 while 检查黑社会。外部 while 检查所有 8 个。
C, 113 characters
I think it works? My first code golf, be gentle.
Every 3 digits encodes 3 cells that need to match. The inner while checks a triad. The outer while checks all 8.