编辑 2:
更多的调整和技巧,我设法将另外 124 个字符减少到752个字符:
using C=System.Console;class O{static int[]b=new int[100];static int c=1;static void Main(){b[44]=b[55]=-1;b[45]=b[54]=1;var g=true;while(g){C.WriteLine(" abcdefgh");for(int i=10;i<90;i++){switch(i%10){case 0:C.Write(i/10);break;case 9:C.WriteLine();break;default:C.Write("w b"[b[i]+1]);break;}}C.Write("w b"[c+1]+">");var l=C.ReadLine();if(l.Length>1){int x=l[0]-96,y=l[1]-48;if(x>0&x<9&y>0&y<9&&b[y*10+x]<1)c*=f(y*10+x,1);}g=false;for(int y=10;y<90;y+=10)for(int x=y;x<y+8;)g|=b[++x]==0&&f(x,0)<0;}int s=0;foreach(int p in b)s+=p;C.WriteLine(s==0?"d":s<0?"w":"b");}static int f(int ofs,int y){var x=1;foreach(int d in new int[]{-11,-10,-9,-1,1,9,10,11}){int l=1,o=ofs;while(b[o+=d]==-c)l++;if(b[o]==c&l>1){x=-1;while(y>0&l-->0)b[o-=d]=c;}}return x;}}
压缩前:
using Con = System.Console;
class O {
// Initialise game board, made of ints - 0 is empty, 1 is a black piece, -1 a white.
// Using a 10x10 board, with the outer ring of empties acting as sentinels.
static int[] board = new int[100];
// Color of the current player.
static int currentColor = 1;
static void Main() {
// Set up the four pieces in the middle.
board[44] = board[55] = -1;
board[45] = board[54] = 1;
var legal = true;
while (legal) {
// Print game board.
Con.WriteLine(" abcdefgh");
for (int i = 10; i < 90; i++) {
switch (i % 10) {
case 0: Con.Write(i / 10); break;
case 9: Con.WriteLine(); break;
default: Con.Write("w b"[board[i] + 1]); break;
}
}
// Print input, indicating which color's turn it is.
Con.Write("w b"[currentColor+1] + ">");
// Parse input.
var line = Con.ReadLine();
if (line.Length > 1) {
int x = line[0] - 96, y = line[1] - 48;
// Discard malformed input.
if (x > 0 & x < 9 & y > 0 & y < 9 && board[y * 10 + x] < 1)
// Check if valid move and if so flip and switch players
currentColor *= flip(y * 10 + x, 1);
}
// See if there are any legal moves by considering all possible ones.
legal = false;
for (int y = 10; y < 90; y += 10)
for (int x = y; x < y + 8;)
legal |= board[++x] == 0 && flip(x, 0) < 0;
}
// Calculate final score: negative is a win for white, positive one for black.
int score = 0;
foreach (int piece in board) score += piece;
Con.WriteLine(score == 0 ? "d" : score < 0 ? "w" : "b");
}
// Flip pieces, or explore whether putting down a piece would cause any flips.
static int flip(int ofs, int commitPutDown) {
var causesFlips = 1;
// Explore all straight and diagonal directions from the piece put down.
foreach (int d in new int[] { -11, -10, -9, -1, 1, 9, 10, 11 }) {
// Move along that direction - if there is at least one piece of the opposite color next
// in line, and the pieces of the opposite color are followed by a piece of the same
// color, do a flip.
int distance = 1, o = ofs;
while (board[o += d] == - currentColor) distance++;
if (board[o] == currentColor & distance > 1) {
causesFlips = -1;
while (commitPutDown > 0 & distance-- > 0) board[o -= d] = currentColor;
}
}
return causesFlips;
}
}
876 个字符的 C# 版本:
using C=System.Console;class O{static void Main(){int[]b=new int[100];b[44]=b[55]=2;b[45]=b[54]=1;int c=1;while(true){C.WriteLine(" abcdefgh");for(int i=10;i<90;i++){switch(i%10){case 0:C.Write(i/10);break;case 9:C.WriteLine();break;default:C.Write(" bw"[b[i]]);break;}}bool g=false;for(int y=10;y<90;y+=10)for(int x=1;x<9;x++){g|=(b[x+y]==0&&f(x+y,b,c,false));}if(!g)break;C.Write(" bw"[c]+">");var l=C.ReadLine();if(l.Length>1){int x=l[0]-96,y=l[1]-48,ofs;if(x>0&x<9&y>0&y<9&&b[ofs=y*10+x]==0)if(f(ofs,b,c,true))b[ofs]=c;c=3-c;}}int s=0;for(int y=10;y<90;y+=10)for(int x=1;x<9;x++)switch(b[y+x]){case 1:s++;break;case 2:s--;break;}C.WriteLine(s==0?"d":s<0?"w":"b");}static bool f(int ofs,int[]b,int p,bool c){var x=false;foreach(int d in new int[]{-11,-10,-9,-1,1,9,10,11}){int l=1,o=ofs;while(b[o+=d]==3-p)l++;if(b[o]==p&l>1){x=true;if(c)while(l-->1)b[o-=d]=p;}}return x;}}
压缩前:
using Con = System.Console;
class Othello {
static void Main() {
// Initialise game board, made of ints - 0 is empty, 1 is a black piece, 2 a white.
// Using a 10x10 board, with the outer ring of empties acting as sentinels.
int[] board = new int[100];
// Set up the four pieces in the middle.
board[44] = board[55] = 2;
board[45] = board[54] = 1;
// Color of the current player.
int currentColor = 1;
while (true) {
// Print game board.
Con.WriteLine(" abcdefgh");
for (int i = 10; i < 90; i++) {
switch (i % 10) {
case 0: Con.Write(i / 10); break;
case 9: Con.WriteLine(); break;
default: Con.Write(" bw"[board[i]]); break;
}
}
// See if there are any legal moves by considering all possible ones.
bool legal = false;
for (int y = 10; y < 90; y += 10) for (int x = 1; x < 9; x++) {
legal |= (board[x + y] == 0 && flip(x + y, board, currentColor, false));
}
if (!legal) break;
// Print input, indicating which color's turn it is.
Con.Write(" bw"[currentColor] + ">");
// Parse input.
string l = Con.ReadLine();
if (l.Length > 1) {
int x = l[0] - 96, y = l[1] - 48;
int ofs;
// Discard malformed input.
if (x > 0 & x < 9 & y > 0 & y < 9 && board[ofs = y * 10 + x] == 0)
// Check if valid move & flip if it is - if not, continue.
if (flip(ofs, board, currentColor, true))
// Put down the piece itself.
board[ofs] = currentColor;
// Switch players.
currentColor = 3 - currentColor;
}
}
// Calculate final score: negative is a win for white, positive one for black.
int score = 0;
for (int y = 10; y < 90; y += 10) for (int x = 1; x < 9; x++)
switch (board[y + x]) {
case 1: score++; break;
case 2: score--; break;
}
Con.WriteLine(score == 0 ? "d" : score < 0 ? "w" : "b");
}
/** Flip pieces, or explore whether putting down a piece would cause any flips. */
static bool flip(int ofs, int[] board, int playerColor, bool commitPutDown) {
bool causesFlips = false;
// Explore all straight and diagonal directions from the piece put down.
foreach (int d in new int[] { -11, -10, -9, -1, 1, 9, 10, 11 }) {
// Move along that direction - if there is at least one piece of the opposite color next
// in line, and the pieces of the opposite color are followed by a piece of the same
// color, do a flip.
int distance = 1, o = ofs;
while (board[o += d] == 3 - playerColor) distance++;
if (board[o] == playerColor & distance > 1) {
causesFlips = true;
if (commitPutDown) while (distance-- > 1) board[o -= d] = playerColor;
}
}
return causesFlips;
}
}
我使用 int[100] 而不是 char[10,10] 来简化一些循环和比较。里面有很多小技巧,但除此之外它与原始Java代码基本相同。
编辑:
编辑器显示了一些奇怪的“Col”值,但你应该看看“Ch”值......不是 943 个字符,而是 876......