C++ Function returns wrong return value? -
i made simple command line application based on kaiji ep. 16 mimics game emperor card (for our midterms in basic programming). got stuck on think simple problem, can't seem solve on own. have function "winchecker(list *root, node *head)," checks cards drawn , wins on who.
and seems returns wrong return value whenever draw citizen , opponent draws citizen well. should loop, since citizen vs citizen draw, according code.
can me understand i'm doing wrong here. if see other mistakes, feel free point them out. i'm open learn.
ps: used struct node , list since aren't allowed use class our midterms yet. got 1 header file each of those.
source.cpp
#include "node.h" #include "list.h" #include <iostream> #include <string> #include <ctime> using namespace std; node *createkaijideck(int round); node *selectkaijicard(node *head, int index); node *deleteselectedkaijicard(node *head, int index); list* createtonegawadeck(int round); list *selecttonegawacard(list *root, int indext); list *deleteselectedtonegawacard(list *root, int indext); bool betcheck(int betlength, int remaininglength); int prizecheck(int betlength, int round); void printdeck(node * head); int wincheck(list *root, node *head, int cardindex, int randtonegawa); void gameover(int cash, int round, int input, int remaining); int main() { // seed rng srand((unsigned int)time(0)); // initilizing variables int input, cardindex, randtonegawa, counttonegawa = 0, cash = 0, remaining = 30; // round loop (int round = 1; round < 12; round++) { cout << "===============================================" << endl; cout << " round " << round << endl; cout << "===============================================" << endl; cout << "how bet, in milimeters?" << endl; cout << "(you still have " << remaining << " milimeters left.)" << endl; cin >> input; betcheck(input, remaining); // match loop if (betcheck(input, remaining) == true) { cout << "you can win " << prizecheck(input, round) << " yen." << endl << endl; cout << "cash @ hand: " << cash << endl; node* head = createkaijideck(round); list* root = createtonegawadeck(round); { printdeck(head); cout << "select card play [1 - 5]: "; cin >> cardindex; randtonegawa = (rand() % (5 - counttonegawa)); node* selectkaijicardatindex = selectkaijicard(head, cardindex); cout << "you chose card: " << selectkaijicardatindex->cards << endl; list* selecttonegawacardatindex = selecttonegawacard(root, randtonegawa); cout << "tonegawa chose card: " << selecttonegawacardatindex->card << endl; cout << endl; counttonegawa++; } while (wincheck(root, head, cardindex, randtonegawa) == 0); // match checker (emperor > citizen > slave > emperor) if (wincheck(root, head, cardindex, randtonegawa) == 1) { cash = cash + prizecheck(input, round); cout << "round " << round << " winner kaiji." << endl; cout << "you won " << prizecheck(input, round) << " yen!" << endl; } else if (wincheck(root, head, cardindex, randtonegawa) == 2) { remaining = remaining - input; cout << "round " << round << " winner tonegawa." << endl; cout << "the pin moved " << input << " milimeters!" << endl; } } else if (betcheck(input, remaining) == false) { cout << "you lose! lost ear!" << endl; system("pause"); exit(0); } } return 0; } node *createkaijideck(int round) { node* head = null; node* curr = null; node* prev = null; if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) { curr = new node; curr->cards = "emperor"; prev = curr; head = curr; (int = 0; < 3; i++) { curr = new node; curr->cards = "citizen"; prev->next = curr; prev = curr; } curr = new node; curr->cards = "citizen"; prev->next = curr; } if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) { curr = new node; curr->cards = "slave"; prev = curr; head = curr; (int = 0; < 3; i++) { curr = new node; curr->cards = "citizen"; prev->next = curr; prev = curr; } curr = new node; curr->cards = "citizen"; prev->next = curr; } return head; } node *selectkaijicard(node *head, int indexk) { (int = 0; < indexk - 1; i++) { head = head->next; } return head; } node *deleteselectedkaijicard(node *head, int indexk) { node *curr = null; if (indexk == 1) { curr = head; head = head->next; delete curr; return head; } node *deletecard = head; (int = 0; < indexk - 1; i++) { curr = deletecard; deletecard = deletecard->next; } curr->next = deletecard->next; delete deletecard; return head; } list *createtonegawadeck(int round) { list *root = null; list *front = null; list *tail = null; if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) { front = new list; front->card = "slave"; tail = front; root = front; (int = 0; < 3; i++) { front = new list; front->card = "citizen"; tail->next = front; tail = front; } front = new list; front->card = "citizen"; tail->next = front; } if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) { front = new list; front->card = "emperor"; tail = front; root = front; (int = 0; < 3; i++) { front = new list; front->card = "citizen"; tail->next = front; tail = front; } front = new list; front->card = "citizen"; tail->next = front; front->next = root; } return root; } list *selecttonegawacard(list *root, int indext) { (int = 0; < indext; i++) { root = root->next; } return root; } list *deleteselectedtonegawacard(list *root, int indext) { list *front = null; if (indext == 0) { front = root; root = root->next; delete front; return root; } list *deletetonegawacard = root; (int = 0; < indext; i++) { front = deletetonegawacard; deletetonegawacard = deletetonegawacard->next; } front->next = deletetonegawacard->next; delete deletetonegawacard; return root; } bool betcheck(int betlength, int remaininglength) { bool flag; if (betlength <= remaininglength) { flag = true; } else if (betlength > remaininglength) { flag = false; } return flag; } int prizecheck(int betlength, int round) { int yen; if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) { yen = betlength * 100000; } if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) { yen = betlength * 500000; } return yen; } void printdeck(node * head) { int count = 1; cout << "===============" << endl; cout << "kaiji's cards" << endl; cout << "===============" << endl << endl; while (head != null) { cout << count << ". " << head->cards << endl; head = head->next; count++; } cout << endl; } int wincheck(list *root, node *head, int cardindex, int randtonegawa) { int result = 0; if ((head->cards == "citizen") && (root->card == "citizen")) { result = 0; } else if ((head->cards == "emperor") && (root->card == "citizen") || (head->cards == "slave") && (root->card == "emeperor") || (head->cards == "citizen") && (root->card == "slave")) { result = 1; } else if ((root->card == "emperor") && (head->cards == "citizen") || root->card == "slave" && head->cards == "emperor" || root->card == "citizen" && head->cards == "slave") { result = 2; } head = deleteselectedkaijicard(head, cardindex); root = deleteselectedtonegawacard(root, randtonegawa); return result; } void gameover(int cash, int round, int input, int remaining) { if (round <= 12 && cash == 20000000 && betcheck(input, remaining) == true) { cout << "you did not entirely win! got " << cash << " yen in 12 rounds!" << endl; system("pause"); exit(0); } else if (round == 12 && cash < 20000000 && betcheck(input, remaining) == false) { cout << "you won! got" << cash << " yen @ round " << round << endl; } } list.h
#pragma once #include <string> using namespace std; struct list { string card; list* next = null; }; node.h
#pragma once #include <string> using namespace std; struct node { string cards; node* next = null; node* prev = null; };
it looks when select card play, delete card deck, compare first card of 2 decks. doesn't seem right, surely want compare cards selected?
if so, should calling wincheck selectkaijicardatindex , selecttonegawacardatindex. want before delete cards deck deleteselectedkaijicard function deletes card can't use after that.
this mean rearranging code bit , not call wincheck in many places.
Comments
Post a Comment