#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstdlib>
#include <stdio.h>
#include <vector>
using namespace std;
int random(int, int);
int player();
int computer();
int pcard[2][100]; //players card deck
int ccard[2][100]; //computers card deck
int pnumber = 0; // number of cards (in c++ form ie 0 = 1) player has
int cnumber = 0; // " " "
int lastcard[2][1];//the last card played
int rnumseed = 10; //random number seed itteration count
void main ()
{
srand(time(NULL));
//Give players 5 random cards
for (int i = 0; i< 5; ++i) {
pcard[0][i] = random(1,10);
pcard[1][i] = random(1,4);
pnumber++;
ccard[0][i] = random(1,10);
ccard[1][i] = random(1,4);
cnumber++;
}
--pnumber;
--cnumber;
//create random card to start
lastcard[0][0] = random(1,10);
lastcard[1][0] = random(1,4);
//plays until all cards are gone
do {
player();
computer();
if (cnumber == 0)
cout << "\n\nUNO!" << endl;
}while (cnumber >= 0 && pnumber >= 0);
if (cnumber < 0)
cout << "\nThe computer wins :(" << endl;
else
cout <<"\nYou win!!! :)" << endl;
}
int random(int min, int max) {
//seed for random number, have it incress each time, very random mostly.
double r = (((((rand()*rnumseed)% 100)/99.)*(max)) + min);
//cout << r <<endl;
r = floor(r);
if (r > max) {
r=max;
}
rnumseed +=17;
return r;
}
int player() {
cout << endl;
start: //used if card is incorrect
int tempdeck[2][100]; //temp deck to remove the card played
int cardchoice; // the input of what card to use
cout << "Player it is your turn, the current card is\n" << lastcard[0][0] << "," << lastcard[1][0] <<endl;
cout << "Your current cards are" <<endl;
for (int i = pnumber; i >= 0; --i) { //loops to display the cards in their hand.
cout << "(" << i+1 << ")"<< pcard[0][i] << "," << pcard [1][i] << " ";
}
/*cout << "The computers cards are:" <<endl;
//for (int i = cnumber; i >= 0; --i) { //loops to display the cards in their hand. //debug
// cout << "(" << i+1 << ")"<< ccard[0][i] << "," << ccard [1][i] << " ";
}*/
cout <<"\nEnter the card to use, or 0 to draw" <<endl;
cin >> cardchoice;
if (cardchoice == 0) { //draws a new card and adds it to the last number, also incresses the number of cards. This is if the user chooses 0
pnumber++;
pcard[0][pnumber] = random(1,10);
pcard[1][pnumber] = random(1,4);
cout <<"You recieved " << pcard[0][pnumber] << "," << pcard[1][pnumber] <<endl;
}
else{
--cardchoice; //puts the card the user choose into c++ terms
if (pcard[0][cardchoice] == lastcard[0][0] || pcard[1][cardchoice] == lastcard[1][0]) //checks to make sure the card will work
{
lastcard[0][0] = pcard[0][cardchoice];//updayes the latest card on the desk
lastcard[1][0] = pcard[1][cardchoice];
for (int i = pnumber; i >=0; --i) { //removes the last card used from the players deck
if (i > cardchoice) {
tempdeck[0][i-1] = pcard[0][i];
tempdeck[1][i-1] = pcard[1][i];
}
else if (i == cardchoice)
{
}
else if (i < cardchoice)
{
tempdeck[0][i] = pcard[0][i];
tempdeck[1][i] = pcard[1][i];
}
}
--pnumber; //decresses number of cards the user has
pcard[0][pnumber+1] = 0;
pcard[1][pnumber+1] = 0;
for (int i = pnumber; i >= 0; --i) { //moves the temp deck to the real deck
pcard[0][i] = tempdeck[0][i];
pcard[1][i] = tempdeck[1][i];
}
}
else { //if the card does not work
cout << "You entered an invalad card try again" << endl;
goto start;
}
}
return(0);
}
int computer() {
bool placed = 0;
cout << "Its the computers turn" << endl;
int tempdeck[2][100];
for (int i = cnumber; i >= 0; --i) {
int cardchoice = i;
if (ccard[0][i] == lastcard[0][0] || ccard[1][i] == lastcard[1][0]) {
lastcard[0][0] = ccard[0][i];
lastcard[1][0] = ccard[1][i];
for (int x = cnumber; x >=0; --x) { //removes the last card used from the players deck
if (x > cardchoice) {
tempdeck[0][x-1] = ccard[0][x];
tempdeck[1][x-1] = ccard[1][x];
}
else if (x == cardchoice)
{
}
else if (x < cardchoice)
{
tempdeck[0][x] = ccard[0][x];
tempdeck[1][x] = ccard[1][x];
}
}
--cnumber;
ccard[0][cnumber+1] = 0;
ccard[1][cnumber+1] = 0;
for (int x = cnumber; x >= 0; --x) { //moves the temp deck to the real deck
ccard[0][x] = tempdeck[0][x];
ccard[1][x] = tempdeck[1][x];
}
cout << "The computer used " << lastcard[0][0] << "," << lastcard[1][0] <<endl;
placed =1;
break;
}
}
if (placed == 0){
++cnumber;
ccard[0][cnumber] = random(1,10);
ccard[1][cnumber] = random(1,4);
cout << "The computer drew a card" << endl;
}
return(0);
}