0

我正在创建一个使用共享内存的简单国际象棋游戏。游戏中有两名玩家(黑,白),他们按照严格交替原则工作。我以回合制的方式从每个玩家那里获得输入并使用它们。我的问题是:我希望用户提供像 mov(a1,b2) 这样的输入。获得输入后首先:我想验证它是否格式正确,然后我想在函数中使用 a1,b2 值。这是我的代码的一部分:

    #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <stdlib.h>


#include <iostream>
using namespace std;

#define SHMSZ     27

int main() {
    int shmid;
    key_t key;
    char *shm;
    string cmd;
    int check =1;

    key = 111;

    /*
     * Locate the segment.
     */
    if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
        perror("shmget");
        return 0;
    }

    /*
     * Now we attach the segment to our data space.
     */
    if ((shm = (char*) shmat(shmid, NULL, 0)) == (char *) -1) {
        perror("shmat");
        return 0;
    }

    cout << *shm << endl;
    while(check)
    {
        if(*shm == '*') //checks if other player finished the game
        {
            cout<<"End of the game. Thank you for playing." <<endl;
            *shm ='*';
            exit(1);
        }
        while(*shm == 'B')
        {

            cout << "Enter a move" << endl;
            cin >> cmd;

            if(cmd=="eog") // Finish the game
            {   
                cout<<"End of the game. Thank you for playing." <<endl;
                *shm ='*';
                exit(1);
            }
            *shm = 'W';
        }
    }

    return 0;
}
4

0 回答 0