Guest User

Untitled

a guest
May 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.47 KB | None | 0 0
  1.  
  2. /// Some time later, (perhaps at the end of this term) re-wirte w/ classes+ 2 players + pc ai + coordinate printing
  3.  
  4. #include <iostream>
  5. #include <time.h>
  6. #include <Windows.h>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. // constants
  12. const int MAXSIZE = 40;
  13.  
  14. const char BLANK = ' ';
  15. const char PLAYER  = 'X';
  16. const char COMPUTER  = 'O';
  17.  
  18. const enum Color {BLACK, DARKBLUE, DARKGREEN, TEAL, DARKRED, DARKPRUPLE, GOLD, LIGHTGREY, GREY, BLUE, GREEN, CYAN, RED, PURPLE, YELLOW, WHITE};
  19. //backround(black on color) == color*16
  20.  
  21. //prototypes
  22.     //Movement:
  23.     void playerMove(char board[][MAXSIZE], int size);
  24.     void pcMove(char board[][MAXSIZE], int size);
  25.  
  26.     //Rendering
  27.     void updateBoard(const char board[][MAXSIZE], const bool match[][MAXSIZE], int size);
  28.     void printColor(const string text, int color); //sometime i'll make this thing parse the colros form in-text escape sequiences, so i dont have to call it for every diffent color
  29.  
  30.     //Winner selection
  31.     char winner(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
  32.     char checkHorWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
  33.     char checkVertWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
  34.     char checkDiagWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
  35.  
  36.     //Utility
  37.     void initBoard(char board[][MAXSIZE], bool match[][MAXSIZE], int size);
  38.     void clearWinner(bool match[][MAXSIZE], int size);
  39.     bool checkIfFull(const char board[][MAXSIZE], int size);
  40.     int getRandomNumber(int lowerBound, int upperBound);
  41.  
  42.     //Other
  43.     bool playAgain();
  44. //---------------------------------
  45.  
  46.  
  47. void main()
  48. {
  49.     int size;
  50.     char board[MAXSIZE][MAXSIZE];
  51.     bool match[MAXSIZE][MAXSIZE];
  52.  
  53.     bool sizeSelected = false;
  54.     bool keepPlaying = true;
  55.  
  56.     while(!sizeSelected)
  57.     {
  58.         //Later on, i will make the color-pharser much more detailed so it takes in-text escape sequences for color codes
  59.         printColor("What size would you like the game board to be (3-40)? ", WHITE);
  60.         cin >> size;
  61.  
  62.         if(size >= 3 && size <= MAXSIZE)
  63.             sizeSelected = true;
  64.         else
  65.             printColor("ERROR",RED);
  66.             printColor(": PLease eneter a valid size!\n",WHITE);
  67.     }
  68.  
  69.     //intitilize the board
  70.     initBoard(board, match, size);
  71.     updateBoard(board, match, size);
  72.  
  73.     //lets play
  74.     while(keepPlaying)
  75.     {
  76.         //the game part
  77.         if(!checkIfFull(board, size) && winner(board, match, size) == '-')
  78.         {
  79.             playerMove(board, size);
  80.             updateBoard(board, match, size);
  81.  
  82.             char test = winner(board, match, size);
  83.  
  84.             if(!checkIfFull(board, size) && winner(board, match, size) == '-')
  85.             {
  86.                 pcMove(board, size);
  87.                 updateBoard(board, match, size);
  88.             }
  89.         }else
  90.         {
  91.             updateBoard(board, match, size);
  92.             char Win = winner(board, match, size);
  93.  
  94.             //check who won
  95.             if(Win == PLAYER)
  96.             {
  97.                 printColor("You won! Congratulations!\n", CYAN);
  98.             }else if(Win == COMPUTER)
  99.             {
  100.                 printColor("The Computer Won! Better luck next time!\n", CYAN);
  101.             }else
  102.             {
  103.                 printColor("It's a tie!\n", CYAN);
  104.             }
  105.  
  106.             printColor("Would you like to play again (y/n)? ", WHITE);
  107.  
  108.             // ask to play again
  109.             if(playAgain())
  110.             {
  111.                 initBoard(board, match, size);
  112.                 updateBoard(board, match, size);
  113.             }else
  114.                 keepPlaying = false;
  115.  
  116.         }
  117.     }
  118.  
  119.     printColor("Thanks for playing!", YELLOW);
  120.  
  121.     cin.ignore();
  122.     cin.get();
  123. }
  124. /////////////////////////////
  125. //////// Movement ///////////
  126. /////////////////////////////
  127.  
  128. //----------------------------------------
  129. // Name: playerMove
  130. // Description: propmpts for and prosseses player input
  131. // args: char board[][] - the board to add the player peice to
  132. //      int size - the boards size
  133. // return: none
  134. // preconditions: board has to be defined like explained above.
  135. //----------------------------------------
  136. void playerMove(char board[][MAXSIZE], int size)
  137. {
  138.     int x,y;
  139.     bool done = false;
  140.    
  141.     cin.ignore();
  142.  
  143.     while(!done)
  144.     {
  145.         printColor("Please enter grid coordinates seperated by a space, ie 2 2: ", WHITE);
  146.         cin >> x  >> y;
  147.    
  148.         x--;y--;
  149.  
  150.         if(x < size && x >= 0)
  151.         {
  152.             if(y < size && y >= 0)
  153.             {
  154.                 if(board[x][y] == BLANK)
  155.                 {
  156.                     board[x][y] = PLAYER;
  157.                     done = true;
  158.                 }else
  159.                 {
  160.                     printColor("ERROR",RED);
  161.                     printColor(": This space is already taken!\n", WHITE);
  162.                 }
  163.             }else
  164.             {
  165.                 printColor("ERROR",RED);
  166.                 printColor(": The second coordinate is out-of-bounds (to large or to small).\n", WHITE);
  167.             }
  168.         }else
  169.         {
  170.             printColor("ERROR",RED);
  171.             printColor(": The first coordinate is out-of-bounds (to large or to small).\n", WHITE);
  172.         }
  173.     }
  174. }
  175.  
  176. //----------------------------------------
  177. // Name: pcMove
  178. // Description: prosses the computers move
  179. // args: char board[][] - the board the pc will move to
  180. //      int size - the size of the board
  181. // return: none
  182. // preconditions:board has to be defined like explained above.
  183. //----------------------------------------
  184. void pcMove(char board[][MAXSIZE], int size)
  185. {
  186.     float moved = false;
  187.     while(!moved)
  188.     {
  189.         int x = getRandomNumber(0,size);
  190.         int y = getRandomNumber(0,size);
  191.  
  192.         if(board[x][y] == BLANK)
  193.         {
  194.             board[x][y] = COMPUTER;
  195.             moved = true;
  196.         }
  197.     }
  198. }
  199.  
  200. /////////////////////////////
  201. ///// Winner Selection //////
  202. /////////////////////////////
  203.  
  204. //----------------------------------------
  205. // Name: winner
  206. // Description: checks to see if anyone won
  207. // args: const char board[][] - the board to check
  208. //      int size - th size of the board
  209. // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
  210. // preconditions: board has to be defined like explained above.
  211. //----------------------------------------
  212. char winner(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
  213. {
  214.     char Win;
  215.     if(checkHorWin(board,match,size) == '-')
  216.         if(checkVertWin(board,match,size) == '-')
  217.             Win = checkDiagWin(board,match,size); // at this point, no need for another check, since we ge the appropriate value no matter what.
  218.         else
  219.             Win = checkVertWin(board,match,size);
  220.     else
  221.         Win = checkHorWin(board,match,size);
  222.  
  223.     if(Win == '-' && checkIfFull(board, size)) //it's a tie!
  224.         Win = BLANK;
  225.  
  226.     return Win;
  227. }
  228.  
  229. //----------------------------------------
  230. // Name: checkHorWin
  231. // Description: checks if thers a mach in one of the rows
  232. // args: const char board[][] - the array to check
  233. //      int size - te size of it
  234. // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
  235. // preconditions: board has to be defined like explained above.
  236. //----------------------------------------
  237. char checkHorWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
  238. {
  239.     int count = 0;
  240.     char baseChar;
  241.     for(int x = 0; x < size; x++)
  242.     {
  243.         if(board[x][0] != BLANK && count != size)
  244.         {
  245.             baseChar = board[x][0];
  246.             for(int y = 0; y<size; y++)
  247.             {  
  248.                 if(board[x][y] == baseChar)
  249.                 {
  250.                     count++;
  251.                     match[x][y] = true;
  252.                 }
  253.             }
  254.             if(count != size)
  255.             {
  256.                 count = 0;
  257.                 clearWinner(match,size);
  258.             }
  259.         }
  260.     }
  261.     if(count == size && baseChar != BLANK)
  262.         return baseChar;
  263.     else
  264.         return '-';
  265. }
  266.  
  267. //----------------------------------------
  268. // Name: checkVertWin
  269. // Description: checks if thers a match in the coloms
  270. // args: const char board[][] - the array to check
  271. //      int size - te size of it
  272. // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
  273. // preconditions: board has to be defined like explained above.
  274. //----------------------------------------
  275. char checkVertWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
  276. {
  277.     int count = 0;
  278.     char baseChar;
  279.     for(int y = 0; y < size; y++)
  280.     {
  281.         if(board[0][y] != BLANK && count != size)
  282.         {
  283.             baseChar = board[0][y];
  284.             for(int x = 0; x<size; x++)
  285.             {  
  286.                 if(board[x][y] == baseChar)
  287.                 {
  288.                     count++;
  289.                     match[x][y] = true;
  290.                 }
  291.             }
  292.             if(count != size)
  293.             {
  294.                 count = 0;
  295.                 clearWinner(match,size);
  296.             }
  297.         }
  298.     }
  299.     if(count == size && baseChar != BLANK)
  300.         return baseChar;
  301.     else
  302.         return '-';
  303. }
  304.  
  305. //----------------------------------------
  306. // Name: CheckDiagWin
  307. // Description: checks if there's a diagonal match
  308. // args: const char board[][] - the array to check
  309. //      int size - te size of it
  310. // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
  311. // preconditions: board has to be defined like explained above.
  312. //----------------------------------------
  313. char checkDiagWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
  314. {
  315.     int count = 0;
  316.     char baseChar;
  317.     for(int d = 0; d<size; d++)
  318.     {
  319.         if(board[0][0] != BLANK)
  320.         {
  321.             baseChar = board[0][0];
  322.             if(board[d][d] == baseChar)
  323.             {
  324.                 count++;
  325.                 match[d][d] = true;
  326.             }
  327.         }
  328.     }
  329.  
  330.     if(count != size)
  331.     {
  332.         count = 0;
  333.         clearWinner(match,size);
  334.         int newsize = size-1;
  335.         for(int x = newsize, y = 0; x >= 0; x--, y++)
  336.         {
  337.             if(board[newsize][0] != BLANK)
  338.             {
  339.                 baseChar = board[newsize][0];
  340.                 if(board[x][y] == baseChar)
  341.                 {
  342.                     count++;
  343.                     match[x][y] = true;
  344.                 }
  345.             }
  346.         }
  347.     }
  348.    
  349.     if(count == size && baseChar != BLANK)
  350.         return baseChar;
  351.     else
  352.     {
  353.         clearWinner(match,size);
  354.         return '-';
  355.     }
  356. }
  357.  
  358. /////////////////////////////
  359. //////// Rendering //////////
  360. /////////////////////////////
  361.  
  362. //----------------------------------------
  363. // Name: updateBoard
  364. // Description: "renders" the board on screen (w/ colors based on the player and weather or not it's a match)
  365. // args: const char grid[][] - the array we use to render
  366. //      int size - the size of the array
  367. // return: none
  368. // preconditions: grid has to be a vaild char array, with atleast a size of the inputed size.
  369. //----------------------------------------
  370. void updateBoard(const char board[][MAXSIZE],const bool match[][MAXSIZE], int size)
  371. {
  372.     system("cls");
  373.     for(int x=0; x<size; x++)
  374.     {
  375.         for(int y=0; y<size; y++)
  376.         {
  377.             int color;
  378.             if(match[x][y])
  379.                 color = (board[x][y] == PLAYER ? RED*16 : WHITE*16);
  380.             else
  381.                 color = (board[x][y] == PLAYER ? RED : WHITE);
  382.  
  383.             string test;
  384.             test = board[x][y];
  385.  
  386.             printColor(test, color);
  387.             printColor((y<size-1 ? "|" : "\n"), GREY);
  388.         }
  389.         if(x < size - 1)
  390.         {
  391.             for(int y=0; y<(size*2); y++)
  392.                 printColor((y<(size*2)-1 ? (y % 2 == 0 ? "-" : "+") : "\n"), GREY);
  393.         }
  394.     }
  395. }
  396.  
  397. //----------------------------------------
  398. // Name: printColor
  399. // Description: prints the inputed text with the given color
  400. // args: const string text - the text to print (a string)
  401. //      int color - usig the color enum, we can define the color to print with
  402. // return: none
  403. // preconditions: the color has to be an int between 0 and 255
  404. //----------------------------------------
  405. void printColor(const string text, int color)
  406. {
  407.     HANDLE  hConsole;
  408.  
  409.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  410.     SetConsoleTextAttribute(hConsole, color);
  411.    
  412.     cout << text;
  413. }
  414.  
  415. /////////////////////////////
  416. ////////// Other ////////////
  417. /////////////////////////////
  418.  
  419. //----------------------------------------
  420. // Name: playAgain
  421. // Description: the function in charge of determining if we need to play again
  422. // args: none
  423. // return: bool - if the player said yes, return true, else return false
  424. //----------------------------------------
  425. bool playAgain()
  426. {
  427.     bool playAgain = false;
  428.     bool goodinput = false;
  429.     while(!goodinput)
  430.     {
  431.         char anwser;
  432.         cin >> anwser;
  433.  
  434.         if(tolower(anwser) == 'y')
  435.         {
  436.             playAgain = true;
  437.             goodinput = true;
  438.         }
  439.         else if(tolower(anwser) == 'n')
  440.         {
  441.             playAgain = false;
  442.             goodinput = true;
  443.         }else
  444.         {
  445.             printColor("ERROR",RED);
  446.             printColor(": Please enter 'y' for yes or 'n' for no.\n", WHITE);
  447.         }
  448.     }
  449.  
  450.     return playAgain;
  451. }
  452.  
  453. ///////////////////////////////
  454. /// Utility Functions/////////
  455. //////////////////////////////
  456.  
  457. //----------------------------------------
  458. // Name: initBoard
  459. // Description: Initilises the board array and fills all the slots with a ' '. And also makes sure theer are no highlihgs
  460. // args: char board[][] - the array to intialize
  461. //      char match[][] - teh highlight array
  462. //      int size - the size of the array
  463. // return: none
  464. // preconditions: board has to be a vaild char array, with atleast a size of the inputed size.
  465. //----------------------------------------
  466. void initBoard(char board[][MAXSIZE], bool match[][MAXSIZE], int size)
  467. {
  468.     for(int x=0; x<size; x++)
  469.         for(int y=0; y<size; y++)
  470.         {
  471.             board[x][y] = BLANK;
  472.             match[x][y] = false;
  473.         }
  474. }
  475.  
  476. //----------------------------------------
  477. // Name: clearWinner
  478. // Description: Clears out the highlight array, as o keep the highlightihg accurate
  479. // args: char match[][] - the array to clear
  480. //      int size - the size of the array
  481. // return: none
  482. // preconditions: match has to be a vaild char array, with atleast a size of the inputed size.
  483. //----------------------------------------
  484. void clearWinner(bool match[][MAXSIZE], int size)
  485. {
  486.     for(int x = 0; x < size; x++)
  487.         for(int y = 0; y < size; y++)
  488.             match[x][y] = false;
  489. }
  490.  
  491. //----------------------------------------
  492. // Name: checkIffull
  493. // Description: checks if the given board is full of player pices
  494. // args: cosant char board[][] - the board to check
  495. //      int size - the size of the board
  496. // return: bool - true if it's full, flase otherwise.
  497. // preconditions: board has to be defined like explained above.
  498. //----------------------------------------
  499. bool checkIfFull(const char board[][MAXSIZE], int size)
  500. {
  501.     int count = 0;
  502.     for(int x = 0; x<size; x++)
  503.     {
  504.         for(int y = 0; y<size; y++)
  505.         {
  506.             if(board[x][y] != BLANK)
  507.                 count++;
  508.         }
  509.     }
  510.     if (count == size*size)
  511.         return true;
  512.     else
  513.         return false;
  514. }
  515.  
  516. //---------------------
  517. // Name: getRanomNumber
  518. // Desc: uses the previously entered lower + uppper bounds to generate a random number.
  519. //       also uses a static variable so that the rand generator is seeded only the first time the function is called.
  520. // Args: -lowerBound - the lower bound for the random number
  521. //       -upperBound - the upper bound for the random number
  522. // Return: returns a random int generated by the rand() function
  523. // precon: the lower + upper bounds have to be valid ints (or non existant)
  524. //-------------------------
  525. int getRandomNumber(int lowerBound, int upperBound)
  526. {
  527.         static bool firstRun = true;
  528.         int tempRand;
  529.  
  530.         if(firstRun) //only seed the random gen once
  531.         {
  532.             srand(time(NULL));
  533.             firstRun = false;
  534.         }
  535.  
  536.         if(upperBound > lowerBound)//swap bounds if needed
  537.                 tempRand = rand() % (upperBound - lowerBound) + lowerBound;
  538.         else
  539.                 tempRand = rand() % (lowerBound - upperBound) + upperBound;
  540.  
  541.         return tempRand;
  542. }
Add Comment
Please, Sign In to add comment