[UVa 220]Othello

本文介绍了一个黑白棋游戏模拟程序的设计与实现。该程序能够读取初始棋盘配置,执行玩家指令,包括列出所有可能的行动、执行指定的移动操作以及退出当前游戏。通过巧妙的坐标变换技巧,简化了不同方向上的移动逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

Othello is a game played by two people on an 8 x 8 board, using disks that are white on one side and black on the other. One player places disks with the white side up and the other player places disks with the black side up. The players alternate placing one disk on an unoccupied space on the board. In placing a disk, the player must bracket at least one of the other color disks. Disks are bracketed if they are in a straight line horizontally, vertically, or diagonally, with a disk of the current player’s color at each end of the line. When a move is made, all the disks that were bracketed are changed to the color of the player making the move. (It is possible that disks will be bracketed across more than one line in a single move.)
这里写图片描述这里写图片描述

Write a program to read a series of Othello games. The first line of the input is the number of games to be processed. Each game consists of a board configuration followed by a list of commands. The board configuration consists of 9 lines. The first 8 specify the current state of the board. Each of these 8 lines contains 8 characters, and each of these characters will be one of the following:

'-' indicating an unoccupied square
'B' indicating a square occupied by a black disk
'W' indicating a square occupied by a white disk

The ninth line is either a ‘B’ or a ‘W’ to indicate which is the current player. You may assume that the data is legally formatted.
The commands are to list all possible moves for the current player, make a move, or quit the current game. There is one command per line with no blanks in the input. Commands are formatted as follows:

List all possible moves for the current player.

The command is an ‘L’ in the first column of the line. The program should go through the board and print all legal moves for the current player in the format ( x, y) where x represents the row of the legal move and y represents its column. These moves should be printed in row major order which means:

1)
all legal moves in row number i will be printed before any legal move in row number j if j is greater than i

2)
if there is more than one legal move in row number i, the moves will be printed in ascending order based on column number.
All legal moves should be put on one line. If there is no legal move because it is impossible for the current player to bracket any pieces, the program should print the message “No legal move.”

Make a move.

The command is an ‘M’ in the first column of the line, followed by 2 digits in the second and third column of the line. The digits are the row and the column of the space to place the piece of the current player’s color, unless the current player has no legal move. If the current player has no legal move, the current player is first changed to the other player and the move will be the move of the new current player. You may assume that the move is then legal. You should record the changes to the board, including adding the new piece and changing the color of all bracketed pieces. At the end of the move, print the number of pieces of each color on the board in the format “Black - xx White - yy” where xx is the number of black pieces on the board and yy is the number of white pieces on the board. After a move, the current player will be changed to the player that did not move.

Quit the current game.

The command will be a ‘Q’ in the first column of the line. At this point, print the final board configuration using the same format as was used in the input. This terminates input for the current game.
You may assume that the commands will be syntactically correct. Put one blank line between output from separate games and no blank lines anywhere else in the output.

Sample Input


2
--------
--------
--------
---WB---
---BW---
--------
--------
--------
W
L
M35
L
Q
WWWWB---
WWWB----
WWB-----
WB------
--------
--------
--------
--------
B
L
M25
L
Q

Sample Output

(3,5) (4,6) (5,3) (6,4)
Black -  1 White -  4
(3,4) (3,6) (5,6)
--------
--------
----W---
---WW---
---BW---
--------
--------
--------

No legal move.
Black -  3 White - 12
(3,5)
WWWWB---
WWWWW---
WWB-----
WB------
--------
--------
--------
--------

My Problem Report

这是一道较繁琐的模拟题,题目大意是给出黑白棋的局面,要求输出可走的所有未知,并统计黑白棋的数目。思路较好的话可以把控制在120行以内,但需要注意输出格式。
我所给出的代码采用了一种坐标变换的技巧,可以把8个方向的移动变换精简到一个函数内。在实现代码时注意边界情况即可。

My Source Code

//  Created by Chlerry in 2015.
//  Copyright (c) 2015 Chlerry. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
string input[9],CP;
bool cp;
vector<int> la,lb,list;
const string p="WB";
#define aim p[cp]
#define naim p[!cp]
#define dx(a) (a)+px[loc]
#define dy(a) (a)+py[loc]
const int px[]={0,-1,-1,-1,0,0,0,1,1,1};
const int py[]={0,-1,0,1,-1,0,1,-1,0,1};
int Black,White;

void CNT()
{
    Black=White=0;
    for(int i=0;i<8;i++)
        for(int j=0;j<8;j++)
            if(input[i][j]=='B')
                Black++;
            else if(input[i][j]=='W')
                White++;
}
void show_list()
{
    for(int i=0;i<list.size();i++)
    {
        if(i)
            cout<<' ';
        printf("(%d,%d)",list[i]/10+1,list[i]%10+1);
    }
    cout<<endl;
}
int find(int x,int y,int loc)
{
    if(dx(x)<0 || dx(x)>7 || dy(y)<0 || dy(y)>7)
        return -1;
    if(input[dx(x)][dy(y)]==aim)
        return -1;
    for(int i=dx(x),j=dy(y);i>=0 && i<8 && j>=0 && j<8;i+=px[loc],j+=py[loc])
        if(input[i][j]==aim)
            return i*10+j;
        else if(input[i][j]!=naim)
            return -1;
    return -1;
}
void move(int x,int y,int loc)
{
    if(dx(x)<0 || dx(x)>7 || dy(y)<0 || dy(y)>7)
        return;
    if(input[dx(x)][dy(y)]==aim)
        return;
    for(int i=dx(x),j=dy(y);i>=0 && i<8 && j>=0 && j<8;i+=px[loc],j+=py[loc])
        if(input[i][j]==aim)
            return;
        else if(input[i][j]!=naim)
            return;
        else
            input[i][j]=aim;
    return;
}
void show()
{
    for(int i=0;i<8;i++)
        cout<<input[i]<<endl;
}
bool check(int x,int y)
{
    for(int i=1;i<=9;i++)
        if(i==5)
            continue;
        else if(find(x,y,i)!=-1)
            return 1;
    return 0;
}
void make_list()
{
    list.clear();//mem(visit);
    for(int i=0;i<8;i++)
        for(int j=0;j<8;j++)
            if(input[i][j]==aim || input[i][j]==naim)
                continue;
            else if(check(i,j))
                list.pb(i*10+j);
    sort(list.begin(),list.end());
}
int main()
{
    freopen("in.txt","r",stdin);
    int T;
    cin>>T;
for(int ca=1;ca<=T;ca++)
{
    if(ca!=1)
        cout<<endl;
    for(int i=0;i<8;i++)
        cin>>input[i];
    cin>>CP;
    cp=(CP=="B");
    string com;
    while(cin>>com)
    {
        if(com=="Q")
            break;
        if(com=="W" || com=="B")
            {cp=(CP=="B");continue;}
        if(com[0]=='L' || com[0]=='M')
            make_list();
        if(com[0]=='L')
        {
            if(list.size())
                show_list();
            else
                cout<<"No legal move.\n";
        }
        if(com[0]=='M')
        {
            if(!list.size())
            {
                cp=!cp;
                make_list();
            }
            int x=com[1]-'1',y=com[2]-'1';
            input[x][y]=aim;
            for(int i=1;i<=9;i++)
                if(i==5)
                    continue;
                else if(find(x,y,i)!=-1) 
                    move(x,y,i);
            CNT();
            cout<<"Black - ";
            if(Black<10)
                cout<<' ';
            cout<<Black<<" White - ";
            if(White<10)
                cout<<' ';
            cout<<White<<endl;
            cp=!cp;
        }
    }
    show();
}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值