Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------
- //Matthew Olson's program
- //Program: tick-tack-toe
- //
- //EXTRA CREIDT make it work for a 4x4 (ask the user for the size of the grid)
- #include <iostream>
- #include <cstdlib>
- #include <time.h>
- using namespace std;
- //constants -------------------------------
- const int MAX_SIZE = 100; //size of board
- const char BLANK = ' '; //marker used for board
- const char PLAYER = 'X'; //marker used for player
- const char COMPUTER = '0'; //marker used for computer
- //prototypes ------------------------------
- void initBoard(char board[][MAX_SIZE],const int size);
- //void dumpArray(const char arr[][MAX_SIZE]);
- void displayBoard(char board[][MAX_SIZE],const int size);
- void displayRow(char row[],const int size, int &count);
- char winner(const char board[][MAX_SIZE], bool &gameOver,const int size); // returns 0, PLAYER or COMPUTER
- bool boardFull(char board[][MAX_SIZE], const int size);
- void playerMove(char board[][MAX_SIZE],const int size);
- void computerMove(char board[][MAX_SIZE], const int size);
- bool row(const char board[][MAX_SIZE], bool &gameOver,const int size);
- bool column(const char board[][MAX_SIZE], bool &gameOver,const int size);
- bool diagonal(const char board[][MAX_SIZE], bool &gameOver,const int size);
- //-----------------------------------------
- void main() {
- char board[MAX_SIZE][MAX_SIZE];
- char answer;
- int size;
- //todo:
- //winner
- // row col diag
- //boardfull might not work
- cout << "Would you like to play tic-tac-toe? (y/n): ";
- cin >> answer;
- while (answer == 'y' || answer == 'Y')
- {
- bool gameOver = false;
- //creat board
- cout << "how big would you like the board to be?: ";
- cin >> size;
- while (size < 2 || size > 25){
- cout << "\nPlease choose a new size: ";
- cin >> size;
- }
- initBoard(board,size);
- //dumpArray(board); //TEST
- displayBoard(board,size);
- while (!winner(board,gameOver,size) && !boardFull(board,size)) {
- playerMove(board,size);
- displayBoard(board,size);
- if (!winner(board,gameOver,size) && !boardFull(board,size)){
- computerMove(board,size);
- displayBoard(board,size);
- }
- }
- if (!winner(board,gameOver,size) && boardFull(board,size))
- cout << "\nCat's game!" << endl;
- cout << "Would you like to play tic-tac-toe again? (y/n): ";
- cin >> answer;
- }
- cin.get();
- }
- void initBoard(char board[][MAX_SIZE],int size) {
- for (int row=0;row<size;row++)
- for(int col=0; col<size; col++)
- board[row][col]=BLANK;
- }
- void displayBoard(char board[][MAX_SIZE],const int size) {
- system("cls");
- int count = 0;
- cout << endl <<endl<< endl<< endl;
- for(int x=0;x<size;x++){
- displayRow(board[x],size,count);
- }
- }
- void displayRow(char row[],const int size,int &count){
- int temp = size - 1;
- cout << "\n\t";
- for(int x=0;x<temp;x++){
- cout << row[x] << '|';
- }
- cout << row[temp];
- if (count < temp){
- cout << "\n\t-";
- for(int x=1;x<size;x++)
- cout <<"+-";
- count++;
- }
- }
- //------------------------------------------------
- // returns 0, PLAYER or COMPUTER
- char winner(const char board[][MAX_SIZE], bool &gameOver,const int size){
- row(board,gameOver,size);
- column(board,gameOver,size);
- diagonal(board,gameOver,size);
- if (gameOver == true)
- return 1;
- else
- return 0;
- }
- //------------------------------------------------
- bool boardFull(char board[][MAX_SIZE], const int size){
- for (int row=0;row<size;row++)
- for(int col=0; col<size; col++)
- //if the space is blank, returns that there is an empty space
- if (board[row][col]==BLANK)
- return false;
- //board has no empty spaces
- return true;
- }
- //------------------------------------------------
- void playerMove(char board[][MAX_SIZE],const int size){
- int row=0;
- int col=0;
- //get a row and column for user
- cout << "\n\tWhere would you like to go? (row and column): ";
- cin >> row >> col;
- row--; col--;
- //while row and column are invalid (already occupied or of bounds)
- //produce error message
- //reget row and column
- while(board[row][col] != BLANK || row > size || col > size){
- cout << "Place taken. Please select a new place to go: ";
- cin >> row >> col;
- row--; col--;
- }
- //put Player maker in selected location
- board[row][col] = PLAYER;
- }
- //------------------------------------------------
- void computerMove(char board[][MAX_SIZE], const int size){
- int row;
- int col;
- bool moved = false;
- static bool firstRun = true;
- if(firstRun==true) //only seed the random generator once
- {
- srand(time(NULL));
- firstRun = false;
- }
- cout << "thinking.....";
- for (time_t t=time(0)+1; time(0)<t; ) {}
- while(moved == false){
- row = rand() % size;
- col = rand() % size;
- while (board[row][col]==BLANK){
- board[row][col]=COMPUTER;
- moved= true;
- }
- }
- }
- //------------------------------------------------
- //void dumpArray(const char arr[][MAX_SIZE]) {
- //
- // for(int row=0; row<MAX_SIZE; row++){
- // for(int col=0; col<MAX_SIZE; col++)
- // cout << static_cast<int>(arr[row][col]) << ' ';
- // cout << endl;
- // }
- //
- // cout<< endl;
- //}
- bool row(const char board[][MAX_SIZE], bool &gameOver,const int size){
- int x;
- int y;
- if (!gameOver){
- for (int col=0;col<size;col++){
- x = 0;
- y=0;
- for(int row=0;row<size;row++){
- if (board[row][col]==PLAYER){
- x++;
- if (!gameOver && x == size){
- gameOver = true;
- std::cout << "\n\tYou are the winner!" << endl;
- }
- }
- if (board[row][col]==COMPUTER){
- y++;
- if (!gameOver && y == size){
- gameOver = true;
- std::cout << "\n\tYou lost!" << endl;
- }
- }
- }
- }
- }
- return gameOver;
- }
- bool column(const char board[][MAX_SIZE], bool &gameOver,const int size) {
- int x;
- int y;
- if (!gameOver){
- for (int row=0;row<size;row++){
- x = 0;
- y=0;
- for(int col=0;col<size;col++){
- if (board[row][col]==PLAYER){
- x++;
- if (!gameOver && x == size){
- gameOver = true;
- cout << "\n\tYou are the winner!" << endl;
- }
- }
- if (board[row][col]==COMPUTER){
- y++;
- if (!gameOver && y == size){
- gameOver = true;
- cout << "\n\tYou lost!" << endl;
- }
- }
- }
- }
- return gameOver;
- }
- }
- bool diagonal(const char board[][MAX_SIZE], bool &gameOver,const int size) {
- int x;
- int y;
- if (gameOver != true){
- x = 0;
- y = 0;
- for(int rowCol=0;rowCol<size;rowCol++){
- if (board[rowCol][rowCol]==PLAYER){
- x++;
- if (!gameOver && x == size){
- gameOver = true;
- cout << "\n\tYou are the winner!" << endl;
- }
- }
- if (board[rowCol][rowCol]==COMPUTER){
- y++;
- if (!gameOver && y == size){
- gameOver = true;
- cout << "\n\tYou lost!" << endl;
- }
- }
- }
- }
- if (gameOver != true){
- x = 0;
- y = 0;
- int rowSize = size; //starts at top right
- for(int colSize=0;colSize<size;colSize++){
- rowSize--;
- if (board[rowSize][colSize]==PLAYER){
- x++;
- if (!gameOver && x == size){
- gameOver = true;
- cout << "\n\tYou are the winner!" << endl;
- }
- }
- if (board[rowSize][colSize]==COMPUTER){
- y++;
- if (!gameOver && y == size){
- gameOver = true;
- cout << "\n\tYou lost!" << endl;
- }
- }
- }
- }
- return gameOver;
- }
Add Comment
Please, Sign In to add comment