Перекод кода игры Покер с С++ на C#

Узнай цену своей работы

Формулировка задачи:

Я разрабатываю (громко сказано) свою карточную игру покер. В интернете нашёл код этой игры на С++ для консоли. Помогите пожалуйста перевести его на C#. С уважением. Код поделён на два поста, так как слишком большой. Часть 1.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
 
using namespace std;
 
string suits[4];
string ranks[13];
 
class Card {
public:
    int suit;
    int rank;
};
 
int compareCards(const void *card1, const void *card2) {
    return (*(Card *)card1).rank - (*(Card *)card2).rank;
}
 
class Deck {
public:
    Deck() {
        for (int i = 0;i<4;i++) {
            for (int j = 0;j<13;j++) {
                cards[i * 13 + j].suit = i;
                cards[i * 13 + j].rank = j;
            }
        }
        suits[0] = "D";
        suits[1] = "S";
        suits[2] = "H";
        suits[3] = "C";
        ranks[0] = "2";
        ranks[1] = "3";
        ranks[2] = "4";
        ranks[3] = "5";
        ranks[4] = "6";
        ranks[5] = "7";
        ranks[6] = "8";
        ranks[7] = "9";
        ranks[8] = "T";
        ranks[9] = "J";
        ranks[10] = "Q";
        ranks[11] = "K";
        ranks[12] = "A";
    }
 
    void print() {
        cout << "Printing the deck..." << endl;
        for (int i = 0;i<52;i++)
            cout << ranks[cards[i].rank] << suits[cards[i].suit] << endl;
        cout << endl;
    }
 
    void shuffle() {
        top = 51;
        int x;
        Card tempCard;
        for (int i = 0;i<4;i++) {
            for (int j = 0;j<13;j++) {
                cards[i * 13 + j].suit = i;
                cards[i * 13 + j].rank = j;
            }
        }
        cout << "Shuffling the cards and dealing..." << endl << endl;
        for (int i = 0;i<52;i++) {
            x = rand() % 52;
            tempCard = cards[i];
            cards[i] = cards[x];
            cards[x] = tempCard;
        }
    }
 
    Card hitme() {
        top--;
        return cards[top + 1];
    }
 
private:
    int top;
    Card cards[52];
};
 
class Player {
public:
    string name;
    int money;
    Card cards[2];
    int playing;
    int round;
    int goodToGo;
};
 
class PokerGame {
public:
    void start(string name) {
        for (int i = 0;i < 6; i++) {
            players[i].money = 1000;
            players[i].playing = 1;
        }
        players[0].name = "Carlos";
        players[1].name = "Carla";
        players[2].name = "Nick";
        players[3].name = "Lisa";
        players[4].name = name;
        players[5].name = "Soarez";
        startGame();
    }

    void deal() {
        for (int i = 0;i<6;i++) {
            for (int j = 0;j<2;j++) {
                if (players[i].playing) {
                    players[i].cards[j] = deck1.hitme();
                }
            }
        }
        for (int i = 0;i<5;i++)
            tableCards[i].rank = -1;
    }
 
    void flop() {
        for (int i = 0;i<3;i++)
            tableCards[i] = deck1.hitme();
    }
 
    void turn() {
        tableCards[3] = deck1.hitme();
    }
 
    void river() {
        tableCards[4] = deck1.hitme();
    }
 
    void printTable() {
        cout << "  " << ((players[0].playing) ? (players[0].name) : "      ") << "         " << ((players[1].playing) ? (players[1].name) : "     ") << "           "
            << ((players[2].playing) ? (players[2].name) : "    ") << endl;
        cout << "   $" << setw(4) << ((players[0].playing) ? (players[0].money) : 0) << "         $" << setw(4) << ((players[1].playing) ? (players[1].money) : 0)
            << "           $" << setw(4) << ((players[2].playing) ? (players[2].money) : 0) << endl;
        cout << "     _____________________________" << endl;
        cout << "    / " << ((bind == 0) ? "@" : " ") << "            " << ((bind == 1) ? "@" : " ") << "            " << ((bind == 2) ? "@" : " ") << " " << endl;
        cout << "   /  ___   ___   ___   ___   ___  " << endl;
        cout << "   | | " << ((tableCards[0].rank) >= 0 ? ranks[tableCards[0].rank] : " ") << " | | " << ((tableCards[1].rank) >= 0 ? ranks[tableCards[1].rank] : " ") << " | | " << ((tableCards[2].rank) >= 0 ? ranks[tableCards[2].rank] : " ") << " | | "
            << ((tableCards[3].rank) >= 0 ? ranks[tableCards[3].rank] : " ") << " | | " << ((tableCards[4].rank) >= 0 ? ranks[tableCards[4].rank] : " ") << " | |" << endl;
        cout << "   | | " << ((tableCards[0].rank) >= 0 ? suits[tableCards[0].suit] : " ") << " | | " << ((tableCards[1].rank) >= 0 ? suits[tableCards[1].suit] : " ") << " | | " << ((tableCards[2].rank) >= 0 ? suits[tableCards[2].suit] : " ") << " | | "
            << ((tableCards[3].rank) >= 0 ? suits[tableCards[3].suit] : " ") << " | | " << ((tableCards[4].rank) >= 0 ? suits[tableCards[4].suit] : " ") << " | |" << endl;
        cout << "   | |___| |___| |___| |___| |___| |" << endl;
        cout << "   |                               |" << endl;
        cout << "   |          Pot = $" << setw(4) << pot << "         |" << endl;
        cout << "   \\                               /" << endl;
        cout << "    \\_" << ((bind == 5) ? "@" : "_") << "_____________" << ((bind == 4) ? "@" : "_") << "___________" << ((bind == 3) ? "@" : "_") << "_/" << endl;
        cout << endl;
        cout << "  " << ((players[5].playing) ? (players[5].name) : "      ") << "          " << ((players[4].playing) ? (players[4].name) : "      ") << "         "
            << ((players[3].playing) ? (players[3].name) : "    ") << endl;
        cout << "   $" << setw(4) << ((players[5].playing) ? (players[5].money) : 0) << "          $" << setw(4) << ((players[4].playing) ? (players[4].money) : 0)
            << "         $" << setw(4) << ((players[3].playing) ? (players[3].money) : 0) << endl;
        cout << endl;
        if (players[4].round) {
            cout << "   Your hand:" << endl;
            cout << "    ___    ___" << endl;
            cout << "   | " << ranks[players[4].cards[0].rank] << " |  | " << ranks[players[4].cards[1].rank] << " |" << endl;
            cout << "   | " << suits[players[4].cards[0].suit] << " |  | " << suits[players[4].cards[1].suit] << " |" << endl;
            cout << "   |___|  |___|" << endl << endl;
        }
    }
 
private:
    Player players[6];
    Deck deck1;
    int bind;
    Card tableCards[5];
    int pot, action, bet, rational, betOn, winner, maxPoints, roundWinner;
    int handPoints[6];
    int bestHand[6][3];
 
    int playersLeft() {
        int count = 0;
        for (int i = 0; i < 6; i++)
            if (players[i].money>0)
                count++;
        return count;
    }
 
    int computerAction(int playerNum) {
        if (players[playerNum].cards[0].rank < 8 && players[playerNum].cards[1].rank < 8) {
            if (players[playerNum].cards[0].rank != players[playerNum].cards[1].rank)
                return 0;
            else
                return 1;
        }
 
        else if (players[playerNum].cards[0].rank < 10 && players[playerNum].cards[1].rank < 10) {
            if (players[playerNum].cards[0].rank != players[playerNum].cards[1].rank)
                return 1;
            else
                return 2;
        }
        else {
            return 2;
        }
    }
 
    /*checks if someone still got bet/call*/
    int playersToBet() {
        for (int i = 0;i<6;i++)
            if (players[i].round == 1 && players[i].goodToGo == 0)
                return 1;
 
        return 0;
    }
 
    void takeBets() {
        betOn = 0;
        for (int k = 0;k<6;k++)
            players[k].goodToGo = 0;
 
        for (int k = bind + 1;k<bind + 7;k++) {
            /* human player actions */
            if (k % 6 == 4 && players[4].round) {
                if (betOn) {
                    cout << "Your action: (1) FLOP (3) BET/CALL ";
                    cin >> action;
                    while (action != 1 && action != 3) {
                        cout << "Invalid number pressed." << endl;
                        cout << "Your action: (1) FLOP (3) BET/CALL ";
                        cin >> action;
                    }
                }
                else {
                    cout << "Your action: (1) FLOP (2) CHECK (3) BET/CALL ";
                    cin >> action;
                    while (action<1 || action>3) {
                        cout << "Invalid number pressed." << endl;
                        cout << "Your action: (1) FLOP (2) CHECK (3) BET/CALL ";
                        cin >> action;
                    }
                }
                cout << endl;
                if (action == 1)
                    players[4].round = 0;
                else if (action == 2)
                    continue;
                else {
                    if (betOn) {
                        pot += betOn;
                        players[4].money -= betOn;
                        players[4].goodToGo = 1;
                    }
                    else {
                        cout << "How much do you want to bet: ";
                        cin >> bet;
                        while (bet>players[4].money || bet<1) {
                            cout << "Invalid number to bet." << endl;
                            cout << "How much do you want to bet: ";
                            cin >> bet;
                            cout << endl << endl;
                        }
                        pot += bet;
                        players[4].money -= bet;
                        betOn = bet;
                        players[4].goodToGo = 1;
                    }
                }
            }
 
// РАЗДЕЛЕНИЕ

Решение задачи: «Перекод кода игры Покер с С++ на C#»

textual
Листинг программы
    class Deck
    {
        private Card[] cards = new Card[52];
        public Deck()
        {
            for (int a = 0; a < 13; a++)
            {
                for (int b = 0; b < 4; b++)
                {
                    cards[a * 13 + b].Rank = a;
                    cards[a * 13 + b].Suit = b;
 
                }
            }
        }
    }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4 из 5