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