The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

Strange C++ problem. by Michiel2005-03-10 07:59:02
  Memory bug by ToLazyToThink2005-03-10 08:04:39
    Ok, thanks. by Michiel2005-03-10 08:17:34
      If it's not super top secret by ToLazyToThink2005-03-10 08:29:34
        It is for school. by Michiel2005-03-10 08:36:45
          Small correction by Michiel2005-03-10 08:40:17
            White space is a goodthing by ToLazyToThink2005-03-10 09:05:28
              I think it is, yes, because by Michiel2005-03-10 09:10:53
                If the new reserve is less than the vector size by ToLazyToThink2005-03-10 09:15:04
                  Ah, yes. by Michiel2005-03-10 09:17:03
                    Sounds like it by ToLazyToThink2005-03-10 09:24:15
                      Yes, but I've tried all that stuff already... by Michiel2005-03-10 09:27:01
                        What does it do when you step through? (n/t) by ToLazyToThink2005-03-10 09:36:19
                          Strangely enough, it works just fine. by Michiel2005-03-10 09:56:41
                            Up to you, I'm need to get my but in gear by ToLazyToThink2005-03-10 09:58:35
                              (second piece) by Michiel 2005-03-10 10:14:27
------------ Bordstand.cc ------------
#include "bordstand.h"

Bordstand::Bordstand() {
     unsigned i;
     bordGrootte = 3; 
     Vec<int> beginBord(bordGrootte);
     for (i = 0; i < bordGrootte; i++)
          beginBord[i] = 3;
     initialiseer(beginBord);
}

Bordstand::Bordstand(Vec<int> & beginBord) {
     bordGrootte = beginBord.size();
     initialiseer(beginBord);
}

void Bordstand::initialiseer(Vec<int> & beginBord) {
     unsigned i;

     diepte = 0;
     for (i = 0; i < bordGrootte; i++) {
          history[diepte][i] = beginBord[i];
          history[diepte][bordGrootte + 1 + i] = beginBord[i];
     }
     history[diepte][bordGrootte] = 0;
     history[diepte][2 * bordGrootte + 1] = 0;
     turnHistory[diepte] = 1;
}

bool Bordstand::geldigeZet(int veld) {
     return (veldInhoud(turnHistory(diepte), veld) > 0);
}

bool Bordstand::eindStand() {
     bool leeg1 = true, leeg2 = true;
     unsigned i;

     for (i = 1; i <= bordGrootte && (leeg1 || leeg2); i++) {
          if (veldInhoud(1, i) > 0)
               leeg1 = false;
          if (veldInhoud(2, i) > 0)
               leeg2 = false;
     }
     return (leeg1 || leeg2);
}

int Bordstand::grootte() {
     return bordGrootte;
}

int & Bordstand::veldInhoud(int speler, int veld) {
     return history(diepte)((speler-1)*(bordGrootte+1) + veld - 1);
}

int Bordstand::findRotatedIndex(unsigned startIndex, int offset) {
     return (startIndex + offset) % history(0).size();
}

bool Bordstand::indexIsBank(unsigned index) {
     return (index == bordGrootte || index == 2 * bordGrootte + 1);
}

int Bordstand::indexIsPlayer(unsigned index) {
     return 2 - (index <= bordGrootte);
}

int & Bordstand::bank(int speler) {
     return history(diepte)(speler*(bordGrootte+1)-1);
}

int Bordstand::score(int speler) {
     unsigned i;
     int score = bank(speler);

     for (i = 1; i <= bordGrootte; i++)
          score += veldInhoud(speler, i);

     return score;
}

void Bordstand::doeZet(int veld) {
     // Zet oud bord in stack
     history[diepte+1] = history(diepte);
     turnHistory[diepte+1] = turnHistory(diepte);
     diepte++;

     int i; // Teller
     int aantal = veldInhoud(turnHistory(diepte), veld);
     int tempResult = (turnHistory(diepte)-1)*(bordGrootte+1)
          + veld - 1;
     bool yourField = (indexIsPlayer(
          findRotatedIndex(tempResult, aantal)) == turnHistory(diepte));

     veldInhoud(turnHistory[diepte], veld) = 0;
     for (i = 1; i <= aantal; i++)
          history(diepte)(tempResult + i)++;

     if (yourField && !indexIsBank(findRotatedIndex(tempResult,
          aantal)) && history(diepte)(tempResult + aantal) == 1) {
          bank(turnHistory(diepte)) +=
               history(diepte)(history(0).size() - tempResult -
               aantal - 2) + 1;
          history(diepte)(history(0).size() - tempResult - aantal - 2) = 0;
          history(diepte)(tempResult + aantal) = 0;
     }

     if (!yourField || !indexIsBank(findRotatedIndex(tempResult, aantal))) {
          turnHistory(diepte) = 3 - turnHistory(diepte);
     }
}

void Bordstand::haalZetTerug() {
     diepte--;
}

int Bordstand::aantalZetten() {
     return diepte;
}

void Bordstand::printBord() {
     unsigned i; // Teller
     cout << " ";
     for (i = bordGrootte; i >= 1; i--)
          cout << veldInhoud(2, i);
     cout << endl << bank(2);
     for (i = 1; i <= bordGrootte; i++)
          cout << " ";
     cout << bank(1) << endl << " ";
     for (i = 1; i <= bordGrootte; i++)
          cout << veldInhoud(1, i);
     cout << endl;
}

void Bordstand::printGame() {
     unsigned tempDiepte = diepte;
     for (diepte=0; diepte <= tempDiepte; diepte++)
          printBord();
}
------------ Main.cc ------------
#include <iostream>
#include <ctime>
#include "bordstand.h"
using namespace std;

void haalLijstIntegers(Vec<int> & deVec) {
     unsigned i = 0;
     char character = cin.get();
     while (character >= '0' && character <= '9') {
          deVec[i] = character - '0';
          i++;
          character = cin.get();
     }
}

void bereken(Bordstand & hetBord, int& kortsteWinst, int& kortsteGelijk,
     int& langsteWinst, int& langsteGelijk) {
     int i;

     if (hetBord.eindStand()) { // Basisstap
          if(hetBord.score(1) == hetBord.score(2)) { // gelijkspel
               if (hetBord.aantalZetten() > langsteGelijk)
                    langsteGelijk = hetBord.aantalZetten();
               if (hetBord.aantalZetten() < kortsteGelijk)
                    kortsteGelijk = hetBord.aantalZetten();
          } else { // winst
               if (hetBord.aantalZetten() > langsteWinst)
                    langsteWinst = hetBord.aantalZetten();
               if (hetBord.aantalZetten() < kortsteWinst)
                    kortsteWinst = hetBord.aantalZetten();
          }
     } else { // Recursieve stap
          for (i = 1; i <= hetBord.grootte(); i++) {
               if (hetBord.geldigeZet(i)) {
                    hetBord.doeZet(i);
                    bereken(hetBord, kortsteWinst, kortsteGelijk,
                         langsteWinst, langsteGelijk);
                    hetBord.haalZetTerug();
               }
          }
     }
}

int main() {
     Vec<int> deStukken;
     int kortsteWinst = 9999;
     int kortsteGelijk = 9999;
     int langsteWinst = -1;
     int langsteGelijk = -1;
     
     cout << "Geef een lijst met getallen van het te testen bord. "
          << "Gebruik" << endl
          << "1-digit integers, niet gescheiden door spaties"
          << "(bv: '333')." << endl << endl << ": ";
     
     haalLijstIntegers(deStukken);
     
     cout << endl;
     
     Bordstand hetBord(deStukken);
     
     int seconden = time(NULL);

     bereken(hetBord, kortsteWinst, kortsteGelijk, langsteWinst,
          langsteGelijk);

     seconden = time(NULL) - seconden;

     cout << "Kortste winst:      " << kortsteWinst << endl;
     cout << "Kortste gelijkspel: " << kortsteGelijk << endl;
     cout << "Langste winst:      " << langsteWinst << endl;
     cout << "Langste gelijkspel: " << langsteGelijk << endl;
     cout << "Rekentijd:          " << seconden << (seconden >
          1 ? " seconden" : " seconde") << endl;
     
     return 0;
}
[ Reply ]

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)