Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Some time later, (perhaps at the end of this term) re-wirte w/ classes+ 2 players + pc ai + coordinate printing
- #include <iostream>
- #include <time.h>
- #include <Windows.h>
- #include <string>
- using namespace std;
- // constants
- const int MAXSIZE = 40;
- const char BLANK = ' ';
- const char PLAYER = 'X';
- const char COMPUTER = 'O';
- const enum Color {BLACK, DARKBLUE, DARKGREEN, TEAL, DARKRED, DARKPRUPLE, GOLD, LIGHTGREY, GREY, BLUE, GREEN, CYAN, RED, PURPLE, YELLOW, WHITE};
- //backround(black on color) == color*16
- //prototypes
- //Movement:
- void playerMove(char board[][MAXSIZE], int size);
- void pcMove(char board[][MAXSIZE], int size);
- //Rendering
- void updateBoard(const char board[][MAXSIZE], const bool match[][MAXSIZE], int size);
- 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
- //Winner selection
- char winner(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
- char checkHorWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
- char checkVertWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
- char checkDiagWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size);
- //Utility
- void initBoard(char board[][MAXSIZE], bool match[][MAXSIZE], int size);
- void clearWinner(bool match[][MAXSIZE], int size);
- bool checkIfFull(const char board[][MAXSIZE], int size);
- int getRandomNumber(int lowerBound, int upperBound);
- //Other
- bool playAgain();
- //---------------------------------
- void main()
- {
- int size;
- char board[MAXSIZE][MAXSIZE];
- bool match[MAXSIZE][MAXSIZE];
- bool sizeSelected = false;
- bool keepPlaying = true;
- while(!sizeSelected)
- {
- //Later on, i will make the color-pharser much more detailed so it takes in-text escape sequences for color codes
- printColor("What size would you like the game board to be (3-40)? ", WHITE);
- cin >> size;
- if(size >= 3 && size <= MAXSIZE)
- sizeSelected = true;
- else
- printColor("ERROR",RED);
- printColor(": PLease eneter a valid size!\n",WHITE);
- }
- //intitilize the board
- initBoard(board, match, size);
- updateBoard(board, match, size);
- //lets play
- while(keepPlaying)
- {
- //the game part
- if(!checkIfFull(board, size) && winner(board, match, size) == '-')
- {
- playerMove(board, size);
- updateBoard(board, match, size);
- char test = winner(board, match, size);
- if(!checkIfFull(board, size) && winner(board, match, size) == '-')
- {
- pcMove(board, size);
- updateBoard(board, match, size);
- }
- }else
- {
- updateBoard(board, match, size);
- char Win = winner(board, match, size);
- //check who won
- if(Win == PLAYER)
- {
- printColor("You won! Congratulations!\n", CYAN);
- }else if(Win == COMPUTER)
- {
- printColor("The Computer Won! Better luck next time!\n", CYAN);
- }else
- {
- printColor("It's a tie!\n", CYAN);
- }
- printColor("Would you like to play again (y/n)? ", WHITE);
- // ask to play again
- if(playAgain())
- {
- initBoard(board, match, size);
- updateBoard(board, match, size);
- }else
- keepPlaying = false;
- }
- }
- printColor("Thanks for playing!", YELLOW);
- cin.ignore();
- cin.get();
- }
- /////////////////////////////
- //////// Movement ///////////
- /////////////////////////////
- //----------------------------------------
- // Name: playerMove
- // Description: propmpts for and prosseses player input
- // args: char board[][] - the board to add the player peice to
- // int size - the boards size
- // return: none
- // preconditions: board has to be defined like explained above.
- //----------------------------------------
- void playerMove(char board[][MAXSIZE], int size)
- {
- int x,y;
- bool done = false;
- cin.ignore();
- while(!done)
- {
- printColor("Please enter grid coordinates seperated by a space, ie 2 2: ", WHITE);
- cin >> x >> y;
- x--;y--;
- if(x < size && x >= 0)
- {
- if(y < size && y >= 0)
- {
- if(board[x][y] == BLANK)
- {
- board[x][y] = PLAYER;
- done = true;
- }else
- {
- printColor("ERROR",RED);
- printColor(": This space is already taken!\n", WHITE);
- }
- }else
- {
- printColor("ERROR",RED);
- printColor(": The second coordinate is out-of-bounds (to large or to small).\n", WHITE);
- }
- }else
- {
- printColor("ERROR",RED);
- printColor(": The first coordinate is out-of-bounds (to large or to small).\n", WHITE);
- }
- }
- }
- //----------------------------------------
- // Name: pcMove
- // Description: prosses the computers move
- // args: char board[][] - the board the pc will move to
- // int size - the size of the board
- // return: none
- // preconditions:board has to be defined like explained above.
- //----------------------------------------
- void pcMove(char board[][MAXSIZE], int size)
- {
- float moved = false;
- while(!moved)
- {
- int x = getRandomNumber(0,size);
- int y = getRandomNumber(0,size);
- if(board[x][y] == BLANK)
- {
- board[x][y] = COMPUTER;
- moved = true;
- }
- }
- }
- /////////////////////////////
- ///// Winner Selection //////
- /////////////////////////////
- //----------------------------------------
- // Name: winner
- // Description: checks to see if anyone won
- // args: const char board[][] - the board to check
- // int size - th size of the board
- // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
- // preconditions: board has to be defined like explained above.
- //----------------------------------------
- char winner(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
- {
- char Win;
- if(checkHorWin(board,match,size) == '-')
- if(checkVertWin(board,match,size) == '-')
- Win = checkDiagWin(board,match,size); // at this point, no need for another check, since we ge the appropriate value no matter what.
- else
- Win = checkVertWin(board,match,size);
- else
- Win = checkHorWin(board,match,size);
- if(Win == '-' && checkIfFull(board, size)) //it's a tie!
- Win = BLANK;
- return Win;
- }
- //----------------------------------------
- // Name: checkHorWin
- // Description: checks if thers a mach in one of the rows
- // args: const char board[][] - the array to check
- // int size - te size of it
- // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
- // preconditions: board has to be defined like explained above.
- //----------------------------------------
- char checkHorWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
- {
- int count = 0;
- char baseChar;
- for(int x = 0; x < size; x++)
- {
- if(board[x][0] != BLANK && count != size)
- {
- baseChar = board[x][0];
- for(int y = 0; y<size; y++)
- {
- if(board[x][y] == baseChar)
- {
- count++;
- match[x][y] = true;
- }
- }
- if(count != size)
- {
- count = 0;
- clearWinner(match,size);
- }
- }
- }
- if(count == size && baseChar != BLANK)
- return baseChar;
- else
- return '-';
- }
- //----------------------------------------
- // Name: checkVertWin
- // Description: checks if thers a match in the coloms
- // args: const char board[][] - the array to check
- // int size - te size of it
- // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
- // preconditions: board has to be defined like explained above.
- //----------------------------------------
- char checkVertWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
- {
- int count = 0;
- char baseChar;
- for(int y = 0; y < size; y++)
- {
- if(board[0][y] != BLANK && count != size)
- {
- baseChar = board[0][y];
- for(int x = 0; x<size; x++)
- {
- if(board[x][y] == baseChar)
- {
- count++;
- match[x][y] = true;
- }
- }
- if(count != size)
- {
- count = 0;
- clearWinner(match,size);
- }
- }
- }
- if(count == size && baseChar != BLANK)
- return baseChar;
- else
- return '-';
- }
- //----------------------------------------
- // Name: CheckDiagWin
- // Description: checks if there's a diagonal match
- // args: const char board[][] - the array to check
- // int size - te size of it
- // return: char - 0 if none one, ' ' if it's a tie, 'O' if it's the pc, 'X' if it's the player.
- // preconditions: board has to be defined like explained above.
- //----------------------------------------
- char checkDiagWin(const char board[][MAXSIZE], bool match[][MAXSIZE], int size)
- {
- int count = 0;
- char baseChar;
- for(int d = 0; d<size; d++)
- {
- if(board[0][0] != BLANK)
- {
- baseChar = board[0][0];
- if(board[d][d] == baseChar)
- {
- count++;
- match[d][d] = true;
- }
- }
- }
- if(count != size)
- {
- count = 0;
- clearWinner(match,size);
- int newsize = size-1;
- for(int x = newsize, y = 0; x >= 0; x--, y++)
- {
- if(board[newsize][0] != BLANK)
- {
- baseChar = board[newsize][0];
- if(board[x][y] == baseChar)
- {
- count++;
- match[x][y] = true;
- }
- }
- }
- }
- if(count == size && baseChar != BLANK)
- return baseChar;
- else
- {
- clearWinner(match,size);
- return '-';
- }
- }
- /////////////////////////////
- //////// Rendering //////////
- /////////////////////////////
- //----------------------------------------
- // Name: updateBoard
- // Description: "renders" the board on screen (w/ colors based on the player and weather or not it's a match)
- // args: const char grid[][] - the array we use to render
- // int size - the size of the array
- // return: none
- // preconditions: grid has to be a vaild char array, with atleast a size of the inputed size.
- //----------------------------------------
- void updateBoard(const char board[][MAXSIZE],const bool match[][MAXSIZE], int size)
- {
- system("cls");
- for(int x=0; x<size; x++)
- {
- for(int y=0; y<size; y++)
- {
- int color;
- if(match[x][y])
- color = (board[x][y] == PLAYER ? RED*16 : WHITE*16);
- else
- color = (board[x][y] == PLAYER ? RED : WHITE);
- string test;
- test = board[x][y];
- printColor(test, color);
- printColor((y<size-1 ? "|" : "\n"), GREY);
- }
- if(x < size - 1)
- {
- for(int y=0; y<(size*2); y++)
- printColor((y<(size*2)-1 ? (y % 2 == 0 ? "-" : "+") : "\n"), GREY);
- }
- }
- }
- //----------------------------------------
- // Name: printColor
- // Description: prints the inputed text with the given color
- // args: const string text - the text to print (a string)
- // int color - usig the color enum, we can define the color to print with
- // return: none
- // preconditions: the color has to be an int between 0 and 255
- //----------------------------------------
- void printColor(const string text, int color)
- {
- HANDLE hConsole;
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hConsole, color);
- cout << text;
- }
- /////////////////////////////
- ////////// Other ////////////
- /////////////////////////////
- //----------------------------------------
- // Name: playAgain
- // Description: the function in charge of determining if we need to play again
- // args: none
- // return: bool - if the player said yes, return true, else return false
- //----------------------------------------
- bool playAgain()
- {
- bool playAgain = false;
- bool goodinput = false;
- while(!goodinput)
- {
- char anwser;
- cin >> anwser;
- if(tolower(anwser) == 'y')
- {
- playAgain = true;
- goodinput = true;
- }
- else if(tolower(anwser) == 'n')
- {
- playAgain = false;
- goodinput = true;
- }else
- {
- printColor("ERROR",RED);
- printColor(": Please enter 'y' for yes or 'n' for no.\n", WHITE);
- }
- }
- return playAgain;
- }
- ///////////////////////////////
- /// Utility Functions/////////
- //////////////////////////////
- //----------------------------------------
- // Name: initBoard
- // Description: Initilises the board array and fills all the slots with a ' '. And also makes sure theer are no highlihgs
- // args: char board[][] - the array to intialize
- // char match[][] - teh highlight array
- // int size - the size of the array
- // return: none
- // preconditions: board has to be a vaild char array, with atleast a size of the inputed size.
- //----------------------------------------
- void initBoard(char board[][MAXSIZE], bool match[][MAXSIZE], int size)
- {
- for(int x=0; x<size; x++)
- for(int y=0; y<size; y++)
- {
- board[x][y] = BLANK;
- match[x][y] = false;
- }
- }
- //----------------------------------------
- // Name: clearWinner
- // Description: Clears out the highlight array, as o keep the highlightihg accurate
- // args: char match[][] - the array to clear
- // int size - the size of the array
- // return: none
- // preconditions: match has to be a vaild char array, with atleast a size of the inputed size.
- //----------------------------------------
- void clearWinner(bool match[][MAXSIZE], int size)
- {
- for(int x = 0; x < size; x++)
- for(int y = 0; y < size; y++)
- match[x][y] = false;
- }
- //----------------------------------------
- // Name: checkIffull
- // Description: checks if the given board is full of player pices
- // args: cosant char board[][] - the board to check
- // int size - the size of the board
- // return: bool - true if it's full, flase otherwise.
- // preconditions: board has to be defined like explained above.
- //----------------------------------------
- bool checkIfFull(const char board[][MAXSIZE], int size)
- {
- int count = 0;
- for(int x = 0; x<size; x++)
- {
- for(int y = 0; y<size; y++)
- {
- if(board[x][y] != BLANK)
- count++;
- }
- }
- if (count == size*size)
- return true;
- else
- return false;
- }
- //---------------------
- // Name: getRanomNumber
- // Desc: uses the previously entered lower + uppper bounds to generate a random number.
- // also uses a static variable so that the rand generator is seeded only the first time the function is called.
- // Args: -lowerBound - the lower bound for the random number
- // -upperBound - the upper bound for the random number
- // Return: returns a random int generated by the rand() function
- // precon: the lower + upper bounds have to be valid ints (or non existant)
- //-------------------------
- int getRandomNumber(int lowerBound, int upperBound)
- {
- static bool firstRun = true;
- int tempRand;
- if(firstRun) //only seed the random gen once
- {
- srand(time(NULL));
- firstRun = false;
- }
- if(upperBound > lowerBound)//swap bounds if needed
- tempRand = rand() % (upperBound - lowerBound) + lowerBound;
- else
- tempRand = rand() % (lowerBound - upperBound) + upperBound;
- return tempRand;
- }
Add Comment
Please, Sign In to add comment