📣 独立日限时特惠: VIP通行证立减$68
查看: 1085| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

[题目讨论] 有哪位大神可以看下这道题 c++

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
这是一道完整的c++ code, 老师的要求是把它分成带有header file的多文件格式,包括classes for Game (the whole game), Board, Square, Players, Die, Deck, Ogre.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include "die.h"

// Race to the Treasure
// version 2.4 adds boundary checks and reused position checks
// version 2.3 adds snack, randomization
// version 2.2 adds keys in fixed locations (grows board for this)
// version 2.1 supports rotating cards
// version 2 support 2-dimensional board

// Limitations:
// only Ogre and straight path cards
// allows cards to be placed anywhere
// no shuffling, keys, snack

using namespace std;

// board size
const int NUMROWS = 5;
const int NUMCOLS = 4;

// indexes for goal square on board
// keys and the snack cannot be placed here
const int ENDROW = NUMROWS - 1;
const int ENDCOL = NUMCOLS - 1;
const int OGRECOL = ENDCOL;

// char's for the 2-dimensional board
// and the cards
const char BLANK = '#';
const char KEY = 'K';
const char SNACK = 'S';
const char HLINE = '-'; // default position
// naming: cardname_angleOfRotation
const char HLINE_90 = '|';
const char LCARD = 'L';
const char LCARD_90 = 'F';
const char LCARD_180 = '7';
const char LCARD_270 = ']';
// const char TCARD = 'T';
const char OGRECARD = 'O';
// for errors
const char ERRORCARD = '!';

const int NUMKEYS_TOWIN = 2;
const int NUMKEYS = 4;

// Possible winners
const string NONE = "None";
const string OGRE = "The Ogre";
const string PLAYERS = "The Players";

const char YES = 'y';

// Draw board
void drawBoard(char board[][NUMCOLS]) {
    cout << "Current board:" << endl;
    for (int row = 0; row <= ENDROW; ++row) {
        for (int col = 0; col <= ENDCOL; ++col)
            cout << board[row][col];
        cout << endl;
    }
    cout << endl;
}

// fill board with BLANK's
void clearBoard(char board[][NUMCOLS]) {
    for (int row = 0; row < NUMROWS; ++row)
        for (int col = 0; col < NUMCOLS; ++col)
            board[row][col] = BLANK;
}

// Return winner, or NONE, if game is not over yet
string getWinner(char board[][NUMCOLS], int numkeys) {
    if (OGRECARD == board[ENDROW][ENDCOL])
        return OGRE;
    // do not need to check for Snack or Key
    // since they cannot be placed in the end position
    if (BLANK == board[ENDROW][ENDCOL]
        || numkeys < NUMKEYS_TOWIN)
        return NONE;
    return PLAYERS;
}

// return next card in deck, and update position in deck
char draw(char deck[], int & pos) {
    char next = deck[pos];
    pos++;
    return next;
}

// return char that is the rotated version of the parameter by the given rotation
// for a version that is harder to declare and a bit more work to set up, but
// ultimately easier to user, you could use a map<char, map<int, char>>
char rotateCard(char card, int angle) {
    char for_hline[] = {HLINE, HLINE_90, HLINE, HLINE_90};
    char for_lcard[] = {LCARD, LCARD_90, LCARD_180, LCARD_270};
    int angle_index = angle / 90;
    if (HLINE == card)
        return for_hline[angle_index];
    if (LCARD == card)
        return for_lcard[angle_index];
    cerr << "Unsupported card: " << card << endl;
    return ERRORCARD;
}

// return true if parameter (value stored for a board square) is a card
bool used(char boardval) {
    return (HLINE == boardval || HLINE_90 == boardval
            || LCARD == boardval || LCARD_90 == boardval
            || LCARD_180 == boardval || LCARD_270 == boardval);
}

// Ask player how to play the card
void placeCard(char card, char board[][NUMCOLS], int &numkeys, bool &hasSnack) {
    int row, col, angle;
    bool ok_pos;
    drawBoard(board);
    do {
        ok_pos = true;
        cout << "In which row should I place the " << card
            << "? (0-" << (NUMROWS-1) << ")" << endl;
        cin >> row;
        cout << "In which column should I place the " << card
            << "? (0-" << (NUMCOLS - 1) << ")" << endl;
        cin >> col;
        if (row < 0 || row >= NUMROWS) {
            cerr << row << " is not a valid row" << endl;
            ok_pos = false;
        }
        else if (col < 0 || col >= NUMCOLS) {
            cerr << col << " is not a valid column" << endl;
            ok_pos = false;
        }
        else if (row < (NUMROWS-1) && col == (NUMCOLS-1)) {
            cerr << "Cannot enter Ogre's column until Goal (bottom right) position" << endl;
            ok_pos = false;
        }
        else if (used(board[row][col])) {
            cerr << "Already used position: " << row << ", " << col << endl;
            ok_pos = false;
        }
    } while (!ok_pos);
    cout << "How much do you want to rotate the card, clockwise, 0, 90, 180 or 270 degrees? ";
    cin >> angle;
    if (KEY == board[row][col]) {
        numkeys++;
        cout << "You got a key!" << endl;
    }
    else if (SNACK == board[row][col]) {
        hasSnack = true;
        cout << "You got an Ogre Snack!" << endl;
    }
    card = rotateCard(card, angle);
    board[row][col] = card;
}

// roll a die of the given number of sides
int roll(int numSides) {
    return rand() % numSides;
}


// check if it's ok to place a key/snack here
bool canPlaceKeyOrSnack(char board[][NUMCOLS], int row, int col) {
    return board[row][col] == BLANK;
}

// place a Key or Snack -- only have to worry
// about placing on each other
void placeKeyOrSnack(char board[][NUMCOLS], char card) {
    int row, col;
    Die rowDie(NUMROWS - 2);
    Die colDie(NUMCOLS - 1);

    do {
        // first and last rows cannot be used
        row = rowDie.roll() + 1;
        // last column cannot be used
        col = colDie.roll();
        //drawBoard(board);
        //cout << row << ", " << col << endl;
        //system("pause");
    }
    while (!canPlaceKeyOrSnack(board, row, col));
    board[row][col] = card;
}

int main()
{
    char board[NUMROWS][NUMCOLS];
    int DECK_SIZE = 28;
    char deck[] = {
        OGRECARD, LCARD, HLINE, HLINE,
        LCARD, HLINE, HLINE, HLINE,
        HLINE, HLINE, HLINE, HLINE,
        LCARD, LCARD, LCARD, LCARD,
        LCARD, LCARD, LCARD, OGRECARD,
        OGRECARD, OGRECARD, OGRECARD,
        OGRECARD, OGRECARD, OGRECARD,
        OGRECARD, OGRECARD
        };
    int deckPos = 0; // keep track of where in deck we are
    int ogrePos = -1;
    int keyCt = 0;  // how many keys the players have accumulated
    bool hasSnack = false;

    string winner;

    cout << "Console version of Peaceable Kingdom's Race to the Treasure" << endl;

    // setup
    srand(time(0));
    clearBoard(board);
    // place keys
    for (int i = 0; i < NUMKEYS; ++i) {
        placeKeyOrSnack(board, KEY);
    }
    // place ogre snack
    placeKeyOrSnack(board, SNACK);
    // shuffle deck
    random_shuffle(deck, deck+DECK_SIZE);

    // while game is not over
    while (NONE == (winner = getWinner(board, keyCt))) {
    // if players have ogre snack, ask them if they want to play it
        if (hasSnack && ogrePos >= 0) {
            char answer;
            cout << "Do you want to use your Ogre Snack? (y/n): ";
            cin >> answer;
            // tolower() will convert to lowercase if in uppercase
            if (YES == tolower(answer)) {
                board[ogrePos--][OGRECOL] = BLANK;
            }
            hasSnack = false;
        }
    // next player draws a card
        char nextCard = draw(deck, deckPos);
    // if Ogre card,
    //   move ogre 1 space
        if (OGRECARD == nextCard) {
            ogrePos++;
            board[ogrePos][ENDCOL] = nextCard;
        }
    // else (path card)
    //   place on board -- pick up keys and ogre snack
       else
            placeCard(nextCard, board, keyCt, hasSnack);
    }

    // Draw final board
    drawBoard(board);
    cout << "The winner: " << winner << endl;
    return 0;
}

上一篇:Designing Data-Intensive Applications - Chapter 10 摘录读后感
下一篇:Summary - Chapter 3 - Designing Data-Intensive Applications
🔗
jyyzzj 2020-4-25 12:15:52 来自APP | 只看该作者
全局:
现在还流行空手套作业的 OTZ
回复

使用道具 举报

🔗
ozox 2020-5-1 00:44:51 | 只看该作者
全局:
需要人帮忙的话,至少你要做一些工作和尝试,不能把题目往这里一丢。在地里问问题需要整理好的你的问题,把大问题化成小问题再问比较有效。一点建议。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表