Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Welcome to GDB Online.
- GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
- C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
- Code, Compile, Run and Debug online from anywhere in world.
- *******************************************************************************/
- #include <stdio.h>
- //---------------------------------------------------
- // Purpose: Header file for the Tic4 class
- //
- // Author: John Gauch - Created class definition.
- // Kendall Clancy - Completed implementation.
- //---------------------------------------------------
- #include <iostream>
- using namespace std;
- #ifndef TIC4
- #define TIC4
- // Global constants
- const int SIZE = 4;
- // Class definition
- class Tic4
- {
- public:
- // Constructor and destructor
- Tic4();
- ~Tic4();
- // Public methods
- void ClearBoard();
- bool SetBoard(const int row, const int col, const char player);
- bool GetBoard(const int row, const int col, char & player);
- bool CheckWin(const char player);
- void PrintBoard();
- bool IsFull();
- bool SimpleAI();
- bool RandomAI();
- bool CleverAI();
- bool FancyAI();
- private:
- // Private variables
- char board[SIZE][SIZE];
- int board_count;
- };
- #endif
- //---------------------------------------------------
- // Purpose: Implementation of the Tic4 class
- //
- // Author: John Gauch - Created skeleton program.
- // YOUR NAME - Completed implementation.
- //---------------------------------------------------
- //#include "tic4.h"
- //---------------------------------------------------
- // Constructor function
- //---------------------------------------------------
- Tic4::Tic4()
- {
- ClearBoard();
- }
- //---------------------------------------------------
- // Destructor function
- //---------------------------------------------------
- Tic4::~Tic4()
- {
- // Intentionally empty
- }
- //---------------------------------------------------
- // Clear the Tic4 board
- //---------------------------------------------------
- void Tic4::ClearBoard()
- {
- // Initialize Tic4 board
- for (int c = 0; c < SIZE; c++)
- for (int r = 0; r < SIZE; r++)
- board[r][c] = ' ';
- board_count = 0;
- }
- //---------------------------------------------------
- // Set value of board at location [row][column]
- //---------------------------------------------------
- bool Tic4::SetBoard(int row, int col, char player)
- {
- // Error checking
- if ((row < 0) || (row >= SIZE) ||
- (col < 0) || (col >= SIZE) ||
- (board[row][col] != ' '))
- return false;
- // Set value of board
- board[row][col] = player;
- board_count++;
- return true;
- }
- //---------------------------------------------------
- // Get value of board at location [row][column]
- //---------------------------------------------------
- bool Tic4::GetBoard(const int row, const int col, char & player)
- {
- // Error checking
- if ((row < 0) || (row >= SIZE) ||
- (col < 0) || (col >= SIZE))
- return false;
- // Get value of board
- player = board[row][col];
- return true;
- }
- //---------------------------------------------------
- // Check to see if player has won the game
- //---------------------------------------------------
- bool Tic4::CheckWin(const char player)
- {
- // Check all the rows
- for (int r = 0; r < SIZE; r++)
- {
- // Count player pieces
- int count = 0;
- for (int c = 0; c < SIZE; c++)
- if (board[r][c] == player)
- count++;
- if (count == SIZE)
- return true;
- }
- // Check all the cols
- for (int c = 0; c < SIZE; c++)
- {
- // Count player pieces
- int count = 0;
- for (int r = 0; r < SIZE; r++)
- if (board[r][c] == player)
- count++;
- if (count == SIZE)
- return true;
- }
- // Check first diagonal
- int count = 0;
- for (int r = 0; r < SIZE; r++)
- {
- int c = r;
- if (board[r][c] == player)
- count++;
- }
- if (count == SIZE)
- return true;
- // Check second diagonal
- count = 0;
- for (int r = 0; r < SIZE; r++)
- {
- int c = SIZE-1-r;
- if (board[r][c] == player)
- count++;
- }
- if (count == SIZE)
- return true;
- // The player did not win
- return false;
- }
- //---------------------------------------------------
- // Print the Tic4 board
- //---------------------------------------------------
- void Tic4::PrintBoard()
- {
- // Draw column numbers
- cout << "\n ";
- for (int c = 0; c < SIZE; c++)
- cout << c << " ";
- cout << "\n";
- // Print the Tic4 board
- for (int r = 0; r < SIZE; r++)
- {
- // Draw dashed line
- cout << " +";
- for (int c = 0; c < SIZE; c++)
- cout << "---+";
- cout << "\n";
- // Draw board contents
- cout << " " << r << " | ";
- for (int c = 0; c < SIZE; c++)
- cout << board[r][c] << " | ";
- cout << "\n";
- }
- // Draw dashed line
- cout << " +";
- for (int c = 0; c < SIZE; c++)
- cout << "---+";
- cout << "\n\n";
- }
- //---------------------------------------------------
- // Check if board is full
- //---------------------------------------------------
- bool Tic4::IsFull()
- {
- return (board_count == SIZE * SIZE);
- }
- //#include "tic4.h"
- //-------------------------------------------------------------------- AI CODE
- bool Tic4::SimpleAI(char player)
- {
- bob.SetBoard(row, column, playerTurn);
- return true;
- }
- bool Tic4::RandomAI(char player)
- {
- bob.SetBoard(row, column, playerTurn);
- return true;
- }
- bool Tic4::CleverAI(char player)
- {
- bob.SetBoard(row, column, playerTurn);
- return true;
- }
- bool Tic4::FancyAIchar player()
- {
- //will call simple random or clever at random
- /*
- if(randomnumber == 1)
- {
- SimpleAI();
- }
- if(randomnumber == 2)
- {
- RandomAI();
- }
- if(randomnumber == 3)
- {
- CleverAI();
- }
- */
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------
- // Main program to play Tic4 game
- //---------------------------------------------------
- int main()
- {
- cout << "Hello user. Please input answers like so: 2,1" << endl;
- cout << "Do not enter any other way, and do not try put the same answer twice or go where someone else has gone " << endl;
- bool player1win = false;
- bool player2win = false;
- bool BoardFull = false;
- char Player1 = 'X';
- char Player2 = 'O';
- char str[] = "2 2";
- //cout << str ;
- string message1 = "Enter X move: ";
- string message2 = "Enter O move: ";
- string Answer = "";
- char playerTurn = 'X';
- bool GoodAnswer = true;
- int row = 0;
- int column = 0;
- bool currentwin = false;
- Tic4 bob;
- cout << "Welcome to 4x4 tic-tac-toe";
- int turn_counter = 1;
- while(currentwin == false && BoardFull == false)
- {
- bob.PrintBoard();
- if(turn_counter%2==0)
- {
- playerTurn = 'O';
- }
- else
- {
- playerTurn = 'X';
- }
- currentwin = bob.CheckWin(playerTurn); //this doesn't work well
- if (currentwin == false)
- {
- cout << "No one has won the game yet" << endl;
- }
- BoardFull = bob.IsFull();
- if(turn_counter%2==0)
- {
- playerTurn = 'O';
- cout << message2;
- }
- else
- {
- playerTurn = 'X';
- cout << message1 ; //edit code here to call the fancy AI
- }
- turn_counter++;
- cin >> Answer;//edit code here to call the fancy AI
- cout << endl;
- ;
- int i;
- for(i=0;i<sizeof(Answer);i++)
- {
- str[i] = Answer[i];
- }
- //cout << "This is what you just entered " << str << endl;
- row = (int)str[0] - 48;
- column = (int)str[2] - 48;
- bob.SetBoard(row, column, playerTurn);
- }
- if(currentwin == true)
- {
- if(playerTurn == 'O')
- {
- cout << "Player O Wins!";
- }
- else
- {
- cout <<"Player X Wins!";
- }
- }
- if(BoardFull == true && currentwin == false)
- {
- cout << "It's a draw!";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement