Hjälp med att hitta fel i koden (c++)

Trädvy Permalänk
Medlem
Registrerad
Jul 2019

Hjälp med att hitta fel i koden (c++)

Behöver lite hjälp med att lista ut vad som är fel i koden. Datorn vinner varje match. Vad kan det bero på?

// ConsoleApplication11.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <ctime> #include <string> #include <sstream> using namespace std; struct card { string suite; int value; string name; }; int main() { card cards[52] = { {"hearts", 2, "Two of Hearts"}, {"hearts", 3, "Three of Hearts"}, {"hearts", 4, "Four of Hearts"}, {"hearts", 5, "Five of Hearts"}, {"hearts", 6, "Six of Hearts"}, {"hearts", 7, "Seven of Hearts"}, {"hearts", 8, "Eight of Hearts"}, {"hearts", 9, "Nine of Hearts"}, {"hearts", 10, "Ten of Hearts"}, {"hearts", 11, "Jack of Hearts"}, {"hearts", 12, "Queen of Hearts"}, {"hearts", 13, "King of Hearts"}, {"hearts", 14, "Ess of Hearts"}, {"spades", 2, "Two of Spades"}, {"spades", 3, "Three of Spades"}, {"spades", 4, "Four of Spades"}, {"spades", 5, "Five of Spades"}, {"spades", 6, "Six of Spades"}, {"spades", 7, "Seven of Spades"}, {"spades", 8, "Eight of Spades"}, {"spades", 9, "Nine of Spades"}, {"spades", 10, "Ten of Spades"}, {"spades", 11, "Jack of Spades"}, {"spades", 12, "Queen of Spades"}, {"spades", 13, "King of Spades"}, {"spades", 14, "Ess of Spades"}, {"diamonds", 2, "Two of Diamonds"}, {"diamonds", 3, "Three of Diamonds"}, {"diamonds", 4, "Four of Diamonds"}, {"diamonds", 5, "Five of Diamonds"}, {"diamonds", 6, "Six of Diamonds"}, {"diamonds", 7, "Seven of Diamonds"}, {"diamonds", 8, "Eight of Diamonds"}, {"diamonds", 9, "Nine of Diamonds"}, {"diamonds", 10, "Ten of Diamonds"}, {"diamonds", 11, "Jack of Diamonds"}, {"diamonds", 12, "Queen of Diamonds"}, {"diamonds", 13, "King of Diamonds"}, {"diamonds", 14, "Ess of Diamonds"}, {"clubs", 2, "Two of Clubs"}, {"clubs", 3, "Three of Clubs"}, {"clubs", 4, "Four of Clubs"}, {"clubs", 5, "Five of Clubs"}, {"clubs", 6, "Six of Clubs"}, {"clubs", 7, "Seven of Clubs"}, {"clubs", 8, "Eight of Clubs"}, {"clubs", 9, "Nine of Clubs"}, {"clubs", 10, "Ten of Clubs"}, {"clubs", 11, "Jack of Clubs"}, {"clubs", 12, "Queen of Clubs"}, {"clubs", 13, "King of Clubs"}, {"clubs", 14, "Ess of Clubs"}, }; bool used[52]; //försök till slumpa kort och lägga dragna kort åt sidan. for (int x = 0; x < 52; x++) { used[x] = false; } const int numsuit = 4; bool slump[52]; string farg[numsuit] = { "spades", "hearts", "diamonds", "clubs" };// vilken ordning färgerna kommer i for (int i = 0; i < 4; i++) { for (int x = 0; x < 52; x++) { slump[x] = false; } } srand(time(NULL)); int slumptal = rand() % 52; int slumptal2 = rand() % 52; int highestCardPlayer; int highestCardComputer; highestCardPlayer = slumptal; if (used[slumptal] == true) { } highestCardComputer = slumptal2; if (used[slumptal2] == true) { } string player1; string computer = "computer"; cout << "Welcome to this card game!" << "\n" << endl; cout << "Player1 , please type in your name;" << "\n" << endl; cin >> player1; cout << " Welcome " << player1 << "." << endl << endl; cout << " Let the game begin!" << endl << endl; cin.get(); bool loop = true; int playerwins = 0; int computerwins = 0; int playerloses = 0; int computerloses = 0; bool omstartloop = true; while (omstartloop == true) { int drawnCardNumber = 0; // Blanda kort for (int i = 0; i < (52 - 1); i++) { int r = i + (rand() % (52 - i)); card temp = cards[i]; cards[i] = cards[r]; cards[r] = temp; } while (loop == true) { cout << "Here comes your card!" << "\n" << endl; cout << "Card: " << cards[slumptal].name << endl; cout << player1 << ", You get the card: " << cards[highestCardPlayer].name << endl; cin.ignore(); cout << computer << ", here comes your card! " << "\n" << endl; cout << "Card: " << cards[slumptal2].name << endl; cout << computer << " You get the card: " << cards[highestCardComputer].name << endl; cin.ignore(); cin.get(); if (cards[highestCardPlayer].value > cards[highestCardComputer].value) { cout << player1 << " You win! Maybe you win will win it all! " << endl; playerwins++; computerloses++; cin.ignore(); cin.get(); } else if (cards[highestCardPlayer].value < cards[highestCardComputer].value) { cout << player1 << " You lost this round!" << endl; computerwins++; playerloses++; cin.ignore(); cin.get(); } else { { cout << player1 << " Its a draw!" << endl; } cin.ignore(); cin.get(); } if (playerwins == 2) { cout << player1 << " You won the whole game!"; } if (computerwins == 2) { cout << "You lost the game!" << endl; } cout << player1 << " Your total wins are: " << playerwins << "" << endl; cout << player1 << " Your total loses are: " << playerloses << "" << endl; cout << computer << " Your total wins are: " << computerwins << "" << endl; cout << computer << " Your total loses are: " << computerloses << "" << endl; cout << "Do you want to play again? y/n?" << endl; char omstartinput; cin >> omstartinput; if (omstartinput == 'y') { } else { return 0; } } } } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

Trädvy Permalänk
Medlem
Plats
Skåne
Registrerad
Aug 2009

Funkar printarna?
Du borde få samma kort båda gångerna eftersom slumptalen görs utanför loopen,
dessutom funkar inte logiken för att få unika kort.

Det förklarar inte felet, men få printar först och se så att du inte får samma slumptal varje gång, som om seeden inte funkar som tänkt.

Trädvy Permalänk
Medlem
Registrerad
Jul 2019

@kijsee92: Skulle även behöva hjälp med hur jag kan få in en egendefinierad funktion innan int main().
Den ska både ha input och output i någon form. Tycker det är lite krångligt.

Trädvy Permalänk
Medlem
Registrerad
Jul 2019

@pacc: Har gjort om nu så att datorn inte vinner varje gång men ett nytt problrm har uppstått...

Om jag får kortet hjärter tre och datorn klöver fyra runda 1. Då får jag kortet klöver fyra runda två och datorn ett nytt random kort.
Runda tre kommer jag få datorns nya random kort från runda 2 och datorn får ett nytt igen. Vad kan det bero på?

// ConsoleApplication11.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <ctime> #include <string> #include <sstream> using namespace std; struct card { string suite; int value; string name; }; void printFunction() { cout << "Welcome! This is a card dueling game." << "\n" << endl; } int main() { card cards[52] = { {"hearts", 2, "Two of Hearts"}, {"hearts", 3, "Three of Hearts"}, {"hearts", 4, "Four of Hearts"}, {"hearts", 5, "Five of Hearts"}, {"hearts", 6, "Six of Hearts"}, {"hearts", 7, "Seven of Hearts"}, {"hearts", 8, "Eight of Hearts"}, {"hearts", 9, "Nine of Hearts"}, {"hearts", 10, "Ten of Hearts"}, {"hearts", 11, "Jack of Hearts"}, {"hearts", 12, "Queen of Hearts"}, {"hearts", 13, "King of Hearts"}, {"hearts", 14, "Ess of Hearts"}, {"spades", 2, "Two of Spades"}, {"spades", 3, "Three of Spades"}, {"spades", 4, "Four of Spades"}, {"spades", 5, "Five of Spades"}, {"spades", 6, "Six of Spades"}, {"spades", 7, "Seven of Spades"}, {"spades", 8, "Eight of Spades"}, {"spades", 9, "Nine of Spades"}, {"spades", 10, "Ten of Spades"}, {"spades", 11, "Jack of Spades"}, {"spades", 12, "Queen of Spades"}, {"spades", 13, "King of Spades"}, {"spades", 14, "Ess of Spades"}, {"diamonds", 2, "Two of Diamonds"}, {"diamonds", 3, "Three of Diamonds"}, {"diamonds", 4, "Four of Diamonds"}, {"diamonds", 5, "Five of Diamonds"}, {"diamonds", 6, "Six of Diamonds"}, {"diamonds", 7, "Seven of Diamonds"}, {"diamonds", 8, "Eight of Diamonds"}, {"diamonds", 9, "Nine of Diamonds"}, {"diamonds", 10, "Ten of Diamonds"}, {"diamonds", 11, "Jack of Diamonds"}, {"diamonds", 12, "Queen of Diamonds"}, {"diamonds", 13, "King of Diamonds"}, {"diamonds", 14, "Ess of Diamonds"}, {"clubs", 2, "Two of Clubs"}, {"clubs", 3, "Three of Clubs"}, {"clubs", 4, "Four of Clubs"}, {"clubs", 5, "Five of Clubs"}, {"clubs", 6, "Six of Clubs"}, {"clubs", 7, "Seven of Clubs"}, {"clubs", 8, "Eight of Clubs"}, {"clubs", 9, "Nine of Clubs"}, {"clubs", 10, "Ten of Clubs"}, {"clubs", 11, "Jack of Clubs"}, {"clubs", 12, "Queen of Clubs"}, {"clubs", 13, "King of Clubs"}, {"clubs", 14, "Ess of Clubs"}, }; bool used[52]; //mitt försök till slumpa kort och lägga de dragna korten åt sidan. for (int x = 0; x < 52; x++) { used[x] = false; } srand(time(NULL)); int slumptal = rand() % 52; int slumptal2 = rand() % 52; int highestCardPlayer; int highestCardComputer; highestCardPlayer = slumptal; if (used[slumptal] == true) { } highestCardComputer = slumptal2; if (used[slumptal2] == true) { } string player1; string computer = "computer"; printFunction(); cout << "Player1 , type your name here;" << "\n" << endl; cin >> player1; cout << "Welcome " << player1 << "." << endl << endl; cout << "The game begins....." << endl << endl; cin.get(); bool loop = true; int playerwins = 0; int computerwins = 0; int playerloses = 0; int computerloses = 0; bool omstartloop = true; while (omstartloop == true) { int drawnCardNumber = 0; // Blanda kort for (int i = 0; i < (52 - 1); i++) { int r = i + (rand() % (52 - i)); card temp = cards[i]; cards[i] = cards[r]; cards[r] = temp; } while (loop == true) { highestCardPlayer = drawnCardNumber; cout << "Here's your card....." << "\n" << endl; cout << "Card: " << cards[drawnCardNumber].name << endl; cout << player1 << ", You got: " << cards[highestCardPlayer].name << endl; cin.ignore(); drawnCardNumber++; highestCardComputer = drawnCardNumber; cout << computer << ", here's your card..... " << "\n" << endl; cout << "Card:" << cards[drawnCardNumber].name << endl; cout << computer << "You got:" << cards[highestCardComputer].name << endl; cin.ignore(); cin.get(); if (cards[highestCardPlayer].value > cards[highestCardComputer].value) { cout << player1 << "You win!" << endl; playerwins++; computerloses++; cin.ignore(); cin.get(); } else if (cards[highestCardPlayer].value < cards[highestCardComputer].value) { cout << player1 << "You lost!" << endl; computerwins++; playerloses++; cin.ignore(); cin.get(); } else { cout << player1 << "It's a draw!" << endl; cin.ignore(); cin.get(); } if (playerwins == 2) { cout << player1 << "Congratulations! You won the game."; } if (computerwins == 2) { cout << "Bad luck! You lost the game." << endl; } cout << player1 << "Your wins:" << playerwins << "" << endl; cout << player1 << "Your losses:" << playerloses << "" << endl; cout << computer << "Your wins:" << computerwins << "" << endl; cout << computer << "Your losses:" << computerloses << "" << endl; cout << "Do you want to play again? (y/n)" << endl; char omstartinput; cin >> omstartinput; if (omstartinput == 'y' || omstartinput == 'Y') { } else { return 0; } } } } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

Trädvy Permalänk
Medlem
Registrerad
Aug 2019
Skrivet av kijsee92:

@pacc: Har gjort om nu så att datorn inte vinner varje gång men ett nytt problrm har uppstått...

Om jag får kortet hjärter tre och datorn klöver fyra runda 1. Då får jag kortet klöver fyra runda två och datorn ett nytt random kort.
Runda tre kommer jag få datorns nya random kort från runda 2 och datorn får ett nytt igen. Vad kan det bero på?

Hej! Lyckades du lösa ditt problem?

Trädvy Permalänk
Medlem
Plats
Mora
Registrerad
Jan 2011

Hej, jag förenklade din kod lite. Vilket borde underlätta avsevärt till att felsöka varför det blir som det blir, det är ett väldigt enkelt fel att hitta och något som du borde lära dig.

while (omstartloop == true) { int drawnCardNumber = 0; while (loop == true) { CORE GAME PLAY LOOP highestCardPlayer = drawnCardNumber; PlayerDrawsCard(drawnCardNumber); drawnCardNumber++; highestCardComputer = drawnCardNumber; ComputerDrawsCard(drawnCardNumber); determineWinnerAndLoser AskForAnotherMatch //returns to CORE GAME PLAY LOOP if players submits y or Y } }

Trädvy Permalänk
Medlem
Registrerad
Jul 2019

@Jonathan-dev: Yes, det löste sig tillslut!