Skip to content

Instantly share code, notes, and snippets.

@N0taN3rd
Created November 23, 2015 22:30
Show Gist options
  • Save N0taN3rd/40a7b304a32e2e0db885 to your computer and use it in GitHub Desktop.
Save N0taN3rd/40a7b304a32e2e0db885 to your computer and use it in GitHub Desktop.
#include<iostream>
#include <string>
#include<fstream>
#include <iomanip>
#include <time.h>
#include <ctime>
#include <stdio.h>
#include <cctype>
#include <vector>
#include <random>
#include <unordered_map>
#include <functional>
using namespace std ;
/*
void countDigitOrAlpha(char c, int& digCount, int& aCount){
if(isdigit(c)){
digCount++;
cout<<c<<" is a digit"<<endl;
}
if(isalpha(c)){
cout<<c<<" is alpha"<<endl;
aCount++;
}
}
struct node{
int value;
node* next;
node(int val) {
value = val;
next = nullptr;
}
};
struct dNode{
int value;
dNode* next;
dNode* prev;
dNode(int val){
value = val;
next = nullptr;
prev = nullptr;
}
};
void linkNewDNode(dNode* cur, int val){
dNode* temp = new dNode(val);
cur->next = temp;
temp->prev = cur;
}
struct move {
move(int ii, int jj, string bspot){
i = ii;
j = jj;
boardSpot = bspot;
}
int i,j;
string boardSpot;
};
class peg {
public:
private:
};
class p {
public:
~p(){
dNode* travler = head, *temp;
while(travler != nullptr){
temp = travler;
travler = travler->next;
delete temp;
}
}
private:
dNode* head, *cur;
};
class boardSpot{
public:
boardSpot(char spot) {
spotChar = spot;
original = spot;
occupied = false;
}
void update(char update){
spotChar = update;
occupied = true;
}
void unoccupy(){
occupied = false;
spotChar = original;
}
bool isOccupied() const{
return occupied;
}
char getSpotChar() const {
return spotChar;
}
friend ostream& operator<<(ostream& out, boardSpot& bs){
// out<<//bs.spotChar;
return out;
}
private:
char original;
char spotChar;
bool occupied;
};
*/
struct pegMove{
pegMove(int i, int j, bool isBegin,bool isEnd){
pegMove::i = i;
pegMove::j = j;
pegMove::isBegin = isBegin;
pegMove::isEnd = isEnd;
}
pegMove(int i, int j){
pegMove::i = i;
pegMove::j = j;
pegMove::isBegin = false;
pegMove::isEnd = false;
}
int i;
int j;
bool isEnd;
bool isBegin;
};
ostream& operator<<(std::ostream& out, pegMove& m){
out<<"PegMove i="<<m.i<<" j="<<m.j;
return out;
}
const int BLUE = 1;
const int GREEN = 2;
const int RED = 3;
const int BIGBLUE = 4;
int bbC = 1;
class peg {
public:
peg(int forWho){
curPos = 0;
peg::forWho = forWho;
if(forWho == BIGBLUE){
pegChar = ((char)bbC + 48);
bbC++;
} else if(forWho == BLUE){
pegChar = 'b';
} else if(forWho == GREEN){
pegChar = 'g';
} else if(forWho == RED){
pegChar = 'r';
}
isHome = false;
}
void setMoves(vector<pegMove> moves){
peg::moves = moves;
}
void setIsHome(){
isHome = true;
}
bool isAtHome(){
return isHome;
}
int returnToStart(){
int temp = curPos;
curPos = 0;
return temp;
}
void moveUp(int upBy){
curPos += upBy;
}
pegMove& getMove(){
return moves[curPos];
}
pegMove& getMove(int where){
return moves[where];
}
int isForWho(){
return forWho;
}
char getPegChar() const {
return pegChar;
}
friend ostream& operator<<(ostream& out, peg& peg){
string pcs = "";
switch(peg.forWho){
case BIGBLUE:
pcs = "BIGBLUE";
break;
case RED:
pcs = "RED";
break;
case GREEN:
pcs = "GREEN";
break;
case BLUE:
pcs = "BLUE";
break;
}
out<<pcs<<" "<<peg.pegChar<<" "<<peg.getMove();
return out;
}
private:
bool isHome;
vector<pegMove> moves;
int curPos;
int forWho;
char pegChar;
};
class boardSpot{
public:
boardSpot(char spot, int i, int j){
boardSpot::spot = spot;
orginal = spot;
boardSpot::i = i;
boardSpot::j = j;
}
int getI() const {
return i;
}
int getJ() const {
return j;
}
void reset(){
spot = orginal;
}
char getSpot() const {
return spot;
}
void setSpot(char spot) {
boardSpot::spot = spot;
}
friend ostream& operator<<(ostream& out, boardSpot& bs){
if(bs.debug)
out<<bs.orginal<<"[i="<<bs.i<<" j="<<bs.j<<"]\n";
else
out<<bs.spot;
return out;
}
private:
bool debug = true;
int i;
int j;
char spot;
char orginal;
};
class player{
public:
player(int playerColor){
player::playerColor = playerColor;
curPeg = 0;
prevPeg = 0;
}
void setPegs(vector<peg> playerPegs){
player::playerPegs = playerPegs;
}
int getPlayerColor(){
return playerColor;
}
peg& getPegToPlay(int pos){
return playerPegs[pos];
}
peg& getPegToPlay(){
if(curPeg == 3){
curPeg = 0;
prevPeg = 3;
return playerPegs[3];
} else {
prevPeg = curPeg;
return playerPegs[curPeg++];
}
}
peg& findPegAt(int i, int j){
int size = playerPegs.size();
for(int ii = 0; ii < size; ++ii){
if(playerPegs[ii].getMove().i == i && playerPegs[ii].getMove().j == j)
return playerPegs[ii];
}
}
bool hasWon(){
int size = playerPegs.size();
for(int i = 0; i < size; ++i){
if(!playerPegs[i].isAtHome())
return false;
}
return true;
}
friend ostream& operator<<(ostream& out, player& p){
string pcs = "";
switch(p.playerColor){
case BIGBLUE:
pcs = "BIGBLUE";
break;
case RED:
pcs = "RED";
break;
case GREEN:
pcs = "GREEN";
break;
case BLUE:
pcs = "BLUE";
break;
}
out<<pcs<<" "<<p.playerPegs[p.curPeg];
return out;
}
private:
int playerColor;
int curPeg;
int prevPeg;
vector<peg> playerPegs;
};
struct winSpot{
winSpot(int i , int j){
winSpot::i = i;
winSpot::j = j;
occupied = false;
}
int i;
int j;
bool occupied;
};
ostream& operator<<(ostream& out, winSpot& ws){
out<<"winspot[i= "<<ws.i<<" j="<<ws.j;
return out;
}
class winSpots{
public:
winSpots(){
vector<winSpot> bbWins = {{2,2},{2,18}};
vector<winSpot> rWins = {{2,2},{2,18}};
vector<winSpot> gWins = {{2,2},{2,18}};
vector<winSpot> bWins = {{2,2},{2,18}};
winSpotsByPlayer['B'] = bbWins;
winSpotsByPlayer['r'] = rWins;
winSpotsByPlayer['g'] = gWins;
winSpotsByPlayer['b'] = bWins;
}
vector<winSpot>& getWinningSpotsForPlayer(char player){
return winSpotsByPlayer[player];
}
private:
// B -> {[2,3],[3,4],[4,5]}
unordered_map<char,vector<winSpot>> winSpotsByPlayer;
};
int main()
{
pegMove bbs = {0,0,true,false}, grs = {0,20,true,false},rs = {20,20,true,false},bs = {20,0,true,false};
pegMove bbe = {0,0,false,true}, gre = {0,20,false,true},re = {20,20,false,true},be= {20,0,false,true};
//create players
player bigBlue(BIGBLUE), green(GREEN),red(RED),blue(BLUE);
//create pegs and put their moves in em
peg bbPeg1(1),bbPeg2(2),bbPeg3(3),bbPeg4(4);
vector<pegMove> blueMoves = {bbs,{0,3},bbe};
peg gPeg1(GREEN),gPeg2(GREEN),gPeg3(GREEN),gPeg4(GREEN);
peg rPeg1(RED),rPeg2(RED),rPeg3(RED),rPeg4(RED);
peg bPeg1(GREEN),bPeg2(GREEN),bPeg3(GREEN),bPeg4(GREEN);
//build peg moves for each peg for the playes
//begin/end pos
//build moves here
pegMove aMove = {0,1};
vector<pegMove> bigBlueMoves;
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,6);
int dice_roll = distribution(generator); // generates number in the range 1..6
cout<<"First roll="<<dice_roll<<endl;
while(dice_roll != 6){
dice_roll = distribution(generator);
cout<<"Other roll="<<dice_roll<<endl;
}
ifstream in("board.txt");
vector<vector<boardSpot>> board;
int iCounter = 0, jCounter = 0;
string line;
int lineCount = 0;
while(getline(in,line)) {
cout<<"At line "<<iCounter<<" "<<line<<endl;
vector<boardSpot> boardPart;
for(int j = 0; j < line.length(); ++j) {
boardSpot bs(line[j],iCounter,j);
boardPart.push_back(bs);
}
board.push_back(boardPart);
iCounter++;
}
cout<<endl;
int boardLen = board.size();
for(int i = 0; i < boardLen; ++i) {
int len = board[i].size();
for(int j = 0; j < len; ++j) {
cout<<board[i][j];
}
cout<<endl;
}
peg peg1(BIGBLUE);
pegMove pegsMovePrev = peg1.getMove();
board[pegsMovePrev.i][pegsMovePrev.j].reset();
peg1.moveUp(3);
pegMove pegsMove = peg1.getMove();
board[pegsMove.i][pegsMove.j].setSpot(peg1.getPegChar());
bool won = false;
int curPlayer = BIGBLUE;
while(!won){
if(curPlayer == BIGBLUE){
//do bigblue turn
//after bigblue turn
curPlayer = GREEN;
} else if(curPlayer == GREEN){
//green logic
//after green
curPlayer = RED;
} else if(curPlayer == RED){
curPlayer = BLUE;
} else if(curPlayer == BLUE){
curPlayer = BIGBLUE;
}
}
/*
dNode* head, *cur, *temp;
head = new dNode(0);
cur = head;
for(int i = 1; i < 11; ++i){
linkNewDNode(cur,i);
cur = cur->next;
}
cur = head;
while(cur->next != nullptr){
cout<<cur->value<<" ";
cur = cur->next;
}
cout<<cur->value<<endl;
while(cur->prev != nullptr){
cout<<cur->value<<" ";
cur = cur->prev;
}
cout<<cur->value<<endl;
cur = head;
while(cur != nullptr){
temp = cur;
cur = cur->next;
delete temp;
}
*/
// delete head;
/*
node* head = new node(0), *cur = head, *temp;
int c = 0;
while(c < 10) {
temp = new node(++c);
cur->next = temp;
cur = temp;
}
node* travel = head;
node* prev;
int move = 2;
int i = 0;
cout<<"first move"<<endl;
while(i < move) {
cout<<travel->value<<"->";
prev = travel;
travel = travel->next;
i++;
delete prev;
}
cout<<endl;
move = 3;
i = 0;
cout<<"second move"<<endl;
while(i < move) {
cout<<travel->value<<"->";
prev = travel;
travel = travel->next;
i++;
delete prev;
}
cout<<"\nrest"<<endl;
while(travel != nullptr) {
cout<<travel->value<<"->";
prev = travel;
travel = travel->next;
delete prev;
}
cout<<"null"<<endl;
*/
/*
ifstream in("board.txt");
vector<vector<boardSpot>> board;
int num = 0;
string line;
int lineCount = 0;
while(getline(in,line)) {
cout<<"At line "<<lineCount<<" "<<line<<endl;
vector<boardSpot> boardPart;
for(int i = 0; i < line.length(); ++i) {
boardPart.push_back(line[i]);
}
board.push_back(boardPart);
lineCount++;
}
cout<<endl;
int boardLen = board.size();
for(int i = 0; i < boardLen; ++i) {
int len = board[i].size();
for(int j = 0; j < len; ++j) {
cout<<"[i="<<i<<" j="<<j<<" space="<<board[i][j]<<"] "<<endl;
}
cout<<endl;
}
for(vector<boardSpot> bp : board) {
for(boardSpot& c : bp) {
cout<<c<<" ";
}
cout<<endl;
}
board[12][10].update('G');
board[10][10].update('6');
board[12][10].unoccupy();
cout<<endl;
for(vector<boardSpot> bp : board) {
for(boardSpot& c : bp) {
cout<<c<<" ";
}
cout<<endl;
}
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment