C++ 扑克游戏获胜条件仅执行第一个if语句
我正在用我自己的获胜条件类型制作一个基本的扑克游戏,但是我的函数if语句只输出第一个条件,即使条件没有满足。对于函数的混乱也很抱歉,但是我已经尝试了所有的方法,但是没有任何效果,所以整洁是我最不想做的事情C++ 扑克游戏获胜条件仅执行第一个if语句,c++,C++,我正在用我自己的获胜条件类型制作一个基本的扑克游戏,但是我的函数if语句只输出第一个条件,即使条件没有满足。对于函数的混乱也很抱歉,但是我已经尝试了所有的方法,但是没有任何效果,所以整洁是我最不想做的事情 #include <string> #include <iostream> #include <cstdlib> #include <fstream> #include <bits/stdc++.h> using namespace
#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
/* Functions
*/
//Create Deck, Shuffle and print shuffled deck Function
void shuffle(string suits[4], string ranks[13], int deck[52])
{
//Initialise seed with starting value for random generator
srand(time(0));
//Loop to create new unique deck
for(int i=0;i<52;i++)
{
deck[i] = i;
}
//This loop will swap each index i with a random index
for(int j=0;j<52;j++)
{
int k = rand() % 52; //Initialse and declare variable equals to random number generator
int temp = deck[j]; //Initialise a temporary variable to be replaced by random index
deck[j] = deck[k];
deck[k] = temp;
}
//Determines what suits go with what ranks in the 52 shuffled deck
cout << "Shuffling cards in deck..." << endl;
for(int q=0;q<52;q++)
{
int s = deck[q]/13; //Determines suit by dividing number by 13 as there is 13 cards in each suit
int r = deck[q]%13; //Type of card is determined with number by modulus 13
//cout << ranks[r] << " " << suits[s] << endl; /*This displays the whole shuffled deck*/
}
}
//Function for drawing 5 cards from shuffled deck for player and computer
void draw(string userName, int deck[52], string suits[4], string ranks[13])
{
//Initialise array variables for player and computer
//Players cards
int suit1[5];
int rank1[5];
//Computers Cards
int suit2[5];
int rank2[5];
cout << userName << "'s Cards" << endl;
cout << "-------------------------------" << endl;
//Loops for drawing players 5 cards from shuffled cards in deck
for(int i=0;i<5;i++)
{
suit1[i] = deck[i] / 13;
rank1[i] = deck[i] % 13;
}
for(int j=0;j<5;j++)
{
cout << ranks[rank1[j]] << " " << suits[suit1[j]] << endl;
}
cout << "-------------------------------" << endl;
cout << "Computer's Cards" << endl;
cout << "-------------------------------" << endl;
//Loops for drawing computers 5 cards from shuffled cards in deck
for(int k=5;k<10;k++)
{
suit2[k] = deck[k] / 13;
rank2[k] = deck[k] % 13;
}
for(int z=5;z<10;z++)
{
cout << ranks[rank2[z]] << " " << suits[suit2[z]] << endl;
}
cout << "-------------------------------" << endl;
}
int winner(string userName, int rank1[5], int rank2[5], int score1, int score2, string ranks[13], int deck[52])
{
for(int k=0;k<5;k++)
{
if((ranks[rank1[k]].compare(ranks[0])))
{
cout << userName << " got 2 points for having rank 2 in their cards!" << endl;
score1 += 2;
return score1;
}
if((ranks[rank1[k]].compare(ranks[1])))
{
cout << userName << " got 3 points for having rank 3 in their cards!" << endl;
score1 += 3;
return score1;
}
if((ranks[rank1[k]].compare(ranks[2])))
{
cout << userName << " got 4 points for having rank 4 in their cards!" << endl;
score1 += 4;
return score1;
}
if((ranks[rank1[k]].compare(ranks[3])))
{
cout << userName << " got 5 points for having rank 5 in their cards!" << endl;
score1 += 5;
return score1;
}
if((ranks[rank1[k]].compare(ranks[4])))
{
cout << userName << " got 6 points for having rank 6 in their cards!" << endl;
score1 += 6;
return score1;
}
if((ranks[rank1[k]].compare(ranks[5])))
{
cout << userName << " got 7 points for having rank 7 in their cards!" << endl;
score1 += 7;
return score1;
}
if((ranks[rank1[k]].compare(ranks[6])))
{
cout << userName << " got 8 points for having rank 8 in their cards!" << endl;
score1 += 8;
return score1;
}
if((ranks[rank1[k]].compare(ranks[7])))
{
cout << userName << " got 9 points for having rank 9 in their cards!" << endl;
score1 += 9;
return score1;
}
if((ranks[rank1[k]].compare(ranks[8])))
{
cout << userName << " got 10 points for having rank 10 in their cards!" << endl;
score1 += 10;
return score1;
}
if((ranks[rank1[k]].compare(ranks[9])))
{
cout << userName << " got 11 points for having a Jack in their cards!" << endl;
score1 += 11;
return score1;
}
if((ranks[rank1[k]].compare(ranks[10])))
{
cout << userName << " got 12 points for having an Ace in their cards!" << endl;
score1 += 12;
return score1;
}
if((ranks[rank1[k]].compare(ranks[11])))
{
cout << userName << " got 13 points for having a King in their cards!" << endl;
score1 += 13;
return score1;
}
if((ranks[rank1[k]].compare(ranks[12])))
{
cout << userName << " got 14 points for having a Queen in their cards!" << endl;
score1 += 14;
return score1;
}
}
cout << "" << endl;
for(int i=0;i<5;i++)
{
if((ranks[rank2[i]].compare(ranks[0])))
{
cout << "The dealer got 2 points for having rank 2 in their cards!" << endl;
score1 += 2;
return score2;
}
if((ranks[rank2[i]].compare(ranks[1])))
{
cout << "The dealer got 3 points for having rank 3 in their cards!" << endl;
score1 += 3;
return score2;
}
if((ranks[rank2[i]].compare(ranks[2])))
{
cout << "The dealer got 4 points for having rank 4 in their cards!" << endl;
score1 += 4;
return score2;
}
if((ranks[rank2[i]].compare(ranks[3])))
{
cout << "The dealer got 5 points for having rank 5 in their cards!" << endl;
score1 += 5;
return score2;
}
if((ranks[rank2[i]].compare(ranks[4])))
{
cout << "The dealer got 6 points for having rank 6 in their cards!" << endl;
score1 += 6;
return score2;
}
if((ranks[rank2[i]].compare(ranks[5])))
{
cout << "The dealer got 7 points for having rank 7 in their cards!" << endl;
score1 += 7;
return score2;
}
if((ranks[rank2[i]].compare(ranks[6])))
{
cout << "The dealer got 8 points for having rank 8 in their cards!" << endl;
score1 += 8;
return score2;
}
if((ranks[rank2[i]].compare(ranks[7])))
{
cout << "The dealer got 9 points for having rank 9 in their cards!" << endl;
score1 += 9;
return score2;
}
if((ranks[rank2[i]].compare(ranks[8])))
{
cout << "The dealer got 10 points for having rank 10 in their cards!" << endl;
score1 += 10;
return score2;
}
if((ranks[rank2[i]].compare(ranks[9])))
{
cout << "The dealer got 11 points for having a Jack in their cards!" << endl;
score1 += 11;
return score2;
}
if((ranks[rank2[i]].compare(ranks[10])))
{
cout << "The dealer got 12 points for having an Ace in their cards!" << endl;
score1 += 12;
return score2;
}
if((ranks[rank2[i]].compare(ranks[11])))
{
cout << "The dealer got 13 points for having a King in their cards!" << endl;
score1 += 13;
return score2;
}
if((ranks[rank2[i]].compare(ranks[12])))
{
cout << "The dealer got 14 points for having a Queen in their cards!" << endl;
score1 += 14;
return score2;
}
}
}
//Associated Variables
string userName;
int deck[52];
string suits[4] = {"Clubs", "Hearts", "Diamonds", "Spades"};
string ranks[13] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Ace", "King", "Queen"};
int rank1[5];
int suit1[5];
int rank2[5];
int suit2[5];
int score1;
int score2;
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
/*功能
*/
//创建牌组、洗牌和打印洗牌牌牌组功能
虚空洗牌(弦乐套装[4],弦乐等级[13],智力牌组[52])
{
//使用随机生成器的起始值初始化种子
srand(时间(0));
//循环以创建新的唯一组
对于(int i=0;我不只是随机尝试东西。“2”
,“2”
和2
是完全不同的东西。你了解它们之间的区别吗?你知道如何在每种情况下进行比较吗?初学者能产生的东西真是太令人惊讶了提示:rand()
是垃圾,特别是对于这样的游戏,可预测性是一个巨大的负担。使用。好吧,看起来你忘记了返回什么(提示:它不是布尔值)我认为你在这里给自己挖了一个小洞,但这总是发生在我们中最好的人身上。我建议从更小的开始,制作执行特定活动的简单函数,如排名计算,并使用单元测试工具正确测试这些函数(示例:)。一旦你确定它们工作正常,就开始组装它们。没有什么比调试一堆你以前从未运行过的代码更糟糕的了,你甚至不知道哪里出了问题。