Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <unistd.h>
- using namespace std;
- static const int L_CHAR = 97; // a
- static const int D_CHAR = 115; // s
- static const int R_CHAR = 100; // d
- static const int HORIZ = 0;
- static const int VERTI = 1;
- static const int FORWA = 2;
- static const int BACKW = 3;
- class board
- {
- public:
- board()
- {
- theBoard.resize(xMax, std::vector<int>(yMax, 0));
- }
- void run()
- {
- while(!won)
- {
- draw();
- sleep(1);
- readInput();
- won = checkWin();
- }
- draw();
- cout << endl << setw(12) << getSpace(winningPlayer) << " WINS!" << endl << endl;
- }
- private:
- int xMax = 7;
- int yMax = 6;
- int cursor = 0;
- int currPlayer = 1;
- int winningPlayer = 0;
- int total = 0;
- int p1T = 0;
- int p2T = 0;
- bool won = false;
- char player1Char = 'X';
- char player2Char = 'O';
- char blankChar = ' ';
- vector<vector<int>> theBoard;
- void draw()
- {
- std::system("clear");
- cout << setw(3 + (cursor * 4)) << getSpace(currPlayer) << endl;
- for(int y = yMax-1; y >= 0; y--)
- {
- cout << "|-=-|-=-|-=-|-=-|-=-|-=-|-=-|" << endl;
- for(int x = 0; x < xMax; x++)
- {
- cout << "| " << getSpace(theBoard[x][y]) << " ";
- }
- cout << "|" << endl;
- }
- cout << "|-=-|-=-|-=-|-=-|-=-|-=-|-=-|" << endl;
- }
- char getSpace(int p)
- {
- if (p == 1)
- return player1Char;
- else if (p == 2)
- return player2Char;
- else
- return blankChar;
- }
- void readInput()
- {
- char in;
- cin >> in;
- if (in == L_CHAR)
- {
- cursor--;
- if (cursor < 0)
- cursor = xMax - 1;
- }
- if (in == R_CHAR)
- {
- cursor++;
- if (cursor > xMax - 1)
- cursor = 0;
- }
- if (in == D_CHAR)
- {
- for(int y = 0; y < yMax; y++)
- {
- if (theBoard[cursor][y] == 0)
- {
- theBoard[cursor][y] = currPlayer;
- total++;
- if(currPlayer == 1)
- {
- p1T++;
- currPlayer = 2;
- }
- else
- {
- p2T++;
- currPlayer = 1;
- }
- break;
- }
- }
- }
- }
- bool checkWin()
- {
- bool retVal = false;
- if ((retVal = (total == xMax*yMax)) == false)
- if ((retVal = check(xMax-3, yMax, HORIZ)) == false)
- if((retVal = check(xMax, yMax-3, VERTI)) == false)
- if((retVal = check(xMax-3, yMax-3, FORWA)) == false)
- retVal = check(xMax, yMax-3, BACKW);
- return retVal;
- }
- bool check(int xIn, int yIn, int dir)
- {
- int xS = 0, yS = 0;
- int x1 = 0, x2 = 0, x3 = 0;
- int y1 = 0, y2 = 0, y3 = 0;
- switch(dir)
- {
- case HORIZ:
- x1 = 1; x2 = 2; x3 = 3;
- y1 = 0, y2 = 0, y3 = 0;
- break;
- case VERTI:
- x1 = 0, x2 = 0, x3 = 0;
- y1 = 1; y2 = 2; y3 = 3;
- break;
- case FORWA:
- x1 = 1, x2 = 2, x3 = 3;
- y1 = 1, y2 = 2, y3 = 3;
- break;
- case BACKW:
- xS = 3;
- x1 = -1, x2 = -2, x3 = -3;
- y1 = 1, y2 = 2, y3 = 3;
- break;
- default:
- return false;
- };
- for(int x = xS; x < xIn; x++)
- {
- for(int y = yS; y < yIn; y++)
- {
- for(int p = 1; p < 3; p++)
- {
- if((theBoard[x][y] == p) && (theBoard[x+x1][y+y1] == p) &&
- (theBoard[x+x2][y+y2] == p) && (theBoard[x+x3][y+y3] == p))
- {
- winningPlayer = p;
- return true;
- }
- }
- }
- }
- return false;
- }
- };
- int main ()
- {
- board b;
- b.run();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement