//Tärningsspel av Jocke "Firetech" Andersson, Skrivet 30-31/8 -2004 och Inspirerat av Ulrik Nilsson.
//Modifierat för inkludering av möjliga datorspelare 20/12 -2004
//Licens: Whatever... Gör vad du vill...
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <conio.h>
using namespace std;

void pause(void)
{
  cout << "Tryck p\x86 valfri tangent f\x94r att forts\x84tta...";
  getch();
}

int random(int lowest_number, int highest_number)
{
  int range = highest_number - lowest_number + 1;
  return lowest_number + int(range * rand()/(RAND_MAX + 1.0));
}


void print_die(int number)
{
  string die;
  int dir = random(1,2);
  for (int line = 1;line <= 5;line++) {
    die = "     ";
    if ((line == 1) || (line == 5)) {
      die = "  -----  ";
    } else {
      switch (number)
      {
        case 1:
          if (line == 3) {
            die = "  *  ";
          }
          break;
        case 2:
          if (dir == 2) {
            if (line == 2)  {
            die = "*    ";
            } else if (line == 4) {
              die = "    *";
            }
          } else {
            if (line == 2)  {
              die = "    *";
            } else if (line == 4) {
              die = "*    ";
            }
          }
          break;
        case 3:
          if (dir == 2) {
            if (line == 2) {
              die = "*    ";
            } else if (line == 3) {
              die = "  *  ";
            } else if (line == 4) {
              die = "    *";
            }
          } else {
            if (line == 2) {
              die = "    *";
            } else if (line == 3) {
              die = "  *  ";
            } else if (line == 4) {
              die = "*    ";
            }
          }
          break;
        case 4:
          if ((line == 2) || (line == 4)) {
            die = "*   *";
          }
          break;
        case 5:
          if ((line == 2) || (line == 4)) {
            die = "*   *";
          } else if (line == 3) {
            die = "  *  ";
          }
          break;
        case 6:
          die = "*   *";
          break;
        default:
          die = "";
      }
    }
    if ((die != "") && (line >= 2) && (line <= 4)) {
      cout << " |" << die << "| ";
    } else {
      cout << die;
    }
    cout << "\n";
  }
}

int game(int value, bool comp, string dnam)
{
  int tval, lval = 0;
  bool a_cont;
  string cont, pro;
  char prg[25];
  if (comp) {
    pro = dnam;
    strcpy(prg,dnam.c_str());
    strcat(prg,"'s");
  } else {
    pro = "Du";
    strcpy(prg,"Din");
  }    
  while (true) {
    tval = random(1,6);
    print_die(tval);
    cout << pro << " slog en " << tval << ":a\n\n";
    if (tval != 1) {
      lval += tval;
      cout << prg << " po\x84ngsumma f\x94r denna omg\x86ngen \x84r nu " << lval  << ".\n" << pro << " har nu totalt " << lval+value  << " po\x84ng.\n";
    } else {
      cout << pro << " f\x94rlorar denna omg\x86ngens po\x84ng, och har nu totalt " << value  << " po\x84ng.\n\n";
      pause();
      return value;
    }
    if (value + lval >= 100) {
      return value + lval;
    }
    if (!comp) {
      a_cont = true;
      while (a_cont) {
        cout << "\nVill du forts\x84tta sl\x86 t\x84rningen? (J/N)\n";
        cont = getch();
        if ((cont[0] == 'N') || (cont[0] == 'n')) {
          return value + lval;
        } else if ((cont[0] != 'J') && (cont[0] != 'j')) {
          cout << "Felaktigt svar! (" << cont[0] << ")";
        } else {
          a_cont = false;
        }
      }  
    } else {
      a_cont = (bool)random(0,1);
      cout << "\n" << pro << " v\x84ljer att ";
      if (!a_cont)
        cout << "INTE ";
      cout << "forts\x84tta.\n\n";
      pause();
      cout << "\n";
      if (!a_cont)
        return value + lval;
    }  
    cout << "\n";
  }
}

int main(void)
{
  int as, i, d;
  bool cont, sname, cont_game;
  char name[71];
  string ngame;
  srand(time(NULL));
  cont_game = true;
  while (cont_game) {
    system("CLS");
    const string title("T\x84rningsspel av Jocke \"Firetech\" Andersson - Version 2.1.1.7"), tuline(title.size(),'=');
    cout << title << "\n" << tuline << "\n\n";
    cont = true;
    while (cont) {
      cout << "Antal spelare: ";
      cin >> as;
      if (as <= 1) {
        cout << "Minst tv\x86 spelare!\n";
      } else if (as >= 21) {
          cout << "Max 20 spelare!\n";
      } else {
        cont = false;
      }
    }
    int* plyrs_val = new int[as];
    string* plyrs_nam = new string[as];
    bool* plyrs_comp = new bool[as];
    as--;
    d = 0;
    cout << "\n\xF9 Max 70 tecken per namn. F\x94r l\x86nga namn avkortas automatiskt.\n\xF9 Skriv \"/dator\" (utan citattecken) f\x94r en datorspelare.\n\n";
    for (i = 0;i <= as;i++) {
      cont = true;
      while (cont) {
        cout << "Ange namn p\x86 spelare " << i + 1 << ": ";
        cin.ignore(100,'\n');
        cin.get(name,71);
        if (name[0] == '\0') {
          cout << "Du m\x86ste ange ett namn!\n";
          cin.clear();
        } else if (strstr(name,"Dator") != NULL) {
          cout << "Ditt namn f\x86r inte inneh\x86lla ordet \"Dator\"!\n";
          cin.clear();
        } else {
          sname = false;
          for (int j = 0;j <= i-1 && !sname;j++) {
            if (name == plyrs_nam[j]) {
              cout << "Du f\x86r inte ha samma namn som n\x86gon annan!\n";
              sname = true;
            }
          }
          if (!sname) {
            cont = false;
          }
        }
      }
      if (strcmp("/dator",name) == 0) {
        char buffr[3], dnam[10] = "Dator ";
        itoa(++d, buffr,10);
        strcat(dnam,buffr);
        plyrs_nam[i] = dnam;
        plyrs_comp[i] = true;
      } else {
        plyrs_nam[i] = name;
        plyrs_comp[i] = false;
      }
      plyrs_val[i] = 0;
    }
    cin.ignore(100,'\n');
    cout << "\n";
    cont = true;
    while (cont) {
      for (i = 0;i <= as && cont;i++) {
        system("CLS");
        string line(plyrs_nam[i].size() + 6,'=');
        cout << plyrs_nam[i] << "'s tur\n" << line << "\n";
        plyrs_val[i] = game(plyrs_val[i], plyrs_comp[i], plyrs_nam[i]);
        if (plyrs_val[i] >= 100) {
          cout << "\n" << line << "\n" << plyrs_nam[i] << " vann!\n" << line << "\n\n";
          cont = false;
        }
      }
    }
    cont = true;
    while (cont) {
      cout << "Nytt spel? (J/N)\n";
      ngame = getch();
      if ((ngame[0] == 'N') || (ngame[0] == 'n')) {
        cont_game = false;
        cont = false;
      } else if ((ngame[0] != 'J') && (ngame[0] != 'j')) {
        cout << "Felaktigt svar! (" << ngame[0] << ")\n";
      } else {
        cont_game = true;
        cont = false;
      }
    }
  }
  return 0;
}

