“黑白棋”是一種雙人對弈的棋類遊戲。棋盤共有8行8列共64格。開局時,棋盤正中央的4格先置放黑白相隔的4枚棋子。雙方輪流落子。只要落子和棋盤上任一枚己方的棋子在一條線上(橫、直、斜線皆可)夾著對方棋子,就能將對方的這些棋子轉變為我己方。不能在不能夾住對手的任一顆棋子的地方落子。遊戲在雙方都不能再落子,或棋盤已無空格的情況下結束,子多的一方勝。
#include <stdio.h>
static int row = 8, col = 8;
void CreateBoard(char arr[row][col]);
void PlaceStone(char arr[row][col]);
void UpdateBoardInDir(char self, char opnt, int r, int c, int r_step, int c_step, char arr[row][col]);
void PrintBoard(char arr[row][col]);
int main()
{
char board[row][col];
CreateBoard(board);
PlaceStone(board);
PrintBoard(board);
return 0;
}
void CreateBoard(char arr[row][col])
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col;)
{
char c;
scanf("%c", &c);
if (c == 'x' || c == 'o' || c == '_')
{
/* Todo */
// .....
j++;
}
}
}
}
void PlaceStone(char arr[row][col])
{
// turn: whose turn to play
// self: the color of stone I hold
// opnt: the color of stone my oponent holds
char turn, self, opnt;
int r, c;
scanf(" %c %d %d", &turn, &r, &c);
if (turn == 'x')
{
self = 'x';
opnt = 'o';
}
else
{
self = 'o';
opnt = 'x';
}
arr[r][c] = self;
// check 8 directions
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0)
continue;
UpdateBoardInDir(self, opnt, r, c, i, j, arr);
}
}
}
void UpdateBoardInDir(char self, char opnt, int r, int c, int r_step, int c_step, char arr[row][col])
{
// has_self: 代表最後有遇到跟自己同樣顏色的棋子
// count_opnt: 代表總共經過多少顆敵方的棋子
int has_self = 0, count_opnt = 0;
// Check
for (int i = r + r_step, j = c + c_step; i < row && i > -1 && j < col && j > -1; i = i + r_step, j = j + c_step)
{
if (arr[i][j] == '_')
{
/* Todo */
// .....
}
else if (arr[i][j] == opnt)
{
/* Todo */
// .....
}
else if (arr[i][j] == self)
{
/* Todo */
// .....
}
}
// Update
if (has_self && count_opnt)
{
for (int i = r + r_step, j = c + c_step, k = 0; k < count_opnt; i = i + r_step, j = j + c_step, k++)
{
arr[i][j] = self;
}
}
}
void PrintBoard(char arr[row][col])
{
for (int i = 0; i < row; i++)
{
printf("%c", arr[i][0]);
for (int j = 1; j < col; j++)
{
printf(" %c", arr[i][j]);
}
printf("\n");
}
}