0% found this document useful (0 votes)
20 views7 pages

DS Assignment 4

The document contains two C++ programs: the first implements a Tic-Tac-Toe game with a minimax algorithm for the computer's moves, while the second solves the Tower of Hanoi problem recursively and calculates the minimum number of moves required. The Tic-Tac-Toe program allows a human player to play against the computer, displaying the board and checking for wins or draws. The Tower of Hanoi program prompts the user for the number of disks and outputs the sequence of moves needed to solve the puzzle.

Uploaded by

mdanial4488
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

DS Assignment 4

The document contains two C++ programs: the first implements a Tic-Tac-Toe game with a minimax algorithm for the computer's moves, while the second solves the Tower of Hanoi problem recursively and calculates the minimum number of moves required. The Tic-Tac-Toe program allows a human player to play against the computer, displaying the board and checking for wins or draws. The Tower of Hanoi program prompts the user for the number of disks and outputs the sequence of moves needed to solve the puzzle.

Uploaded by

mdanial4488
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q#1

#include <iostream>

#include <vector>

#include <climits>

#include <algorithm>

using namespace std;

class TicTacToe {

private:

vector<vector<char>> board;

char currentPlayer;

public:

TicTacToe() {

board = vector<vector<char>>(3, vector<char>(3, ' '));

currentPlayer = 'X'; // Human player starts

// Function to display the board

void displayBoard() {

cout << "\nTic-Tac-Toe Board:\n";

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

cout << board[i][j];

if (j < 2) cout << " | ";

cout << "\n";


if (i < 2) cout << "--+---+--\n";

cout << endl;

// Check if a player has won

bool isWinner(char player) {

// Check rows, columns, and diagonals

for (int i = 0; i < 3; i++) {

if (board[i][0] == player && board[i][1] == player && board[i][2] == player) return true;

if (board[0][i] == player && board[1][i] == player && board[2][i] == player) return true;

if (board[0][0] == player && board[1][1] == player && board[2][2] == player) return true;

if (board[0][2] == player && board[1][1] == player && board[2][0] == player) return true;

return false;

// Check if the board is full (draw)

bool isBoardFull() {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (board[i][j] == ' ') return false;

return true;

// Minimax algorithm to determine the best move for the computer (O)

int minimax(int depth, bool isMaximizingPlayer) {


if (isWinner('X')) return -1; // Human player wins

if (isWinner('O')) return 1; // Computer (O) wins

if (isBoardFull()) return 0; // Draw

int best = isMaximizingPlayer ? INT_MIN : INT_MAX;

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (board[i][j] == ' ') {

board[i][j] = isMaximizingPlayer ? 'O' : 'X'; // Make the move

int currentScore = minimax(depth + 1, !isMaximizingPlayer);

best = isMaximizingPlayer ? max(best, currentScore) : min(best, currentScore);

board[i][j] = ' '; // Undo the move

return best;

// Function to find the best move for the computer

pair<int, int> findBestMove() {

int bestVal = INT_MIN;

pair<int, int> bestMove = {-1, -1};

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (board[i][j] == ' ') {

board[i][j] = 'O'; // Make the move

int moveVal = minimax(0, false);


board[i][j] = ' '; // Undo the move

if (moveVal > bestVal) {

bestMove = {i, j};

bestVal = moveVal;

return bestMove;

// Function to play the game

void playGame() {

while (true) {

displayBoard();

if (currentPlayer == 'X') {

// Human player input

int row, col;

cout << "Enter row (0-2) and column (0-2) for your move (X): ";

cin >> row >> col;

if (board[row][col] != ' ') {

cout << "Invalid move. Try again.\n";

continue;

board[row][col] = 'X'; // Make the human move

} else {

// Computer's move (O)

cout << "Computer's move (O):\n";


pair<int, int> bestMove = findBestMove();

board[bestMove.first][bestMove.second] = 'O';

cout << "Computer chose: " << bestMove.first << " " << bestMove.second << endl;

// Check for a winner

if (isWinner('X')) {

displayBoard();

cout << "You win!\n";

break;

if (isWinner('O')) {

displayBoard();

cout << "Computer wins!\n";

break;

if (isBoardFull()) {

displayBoard();

cout << "It's a draw!\n";

break;

// Switch turns

currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';

};

int main() {
TicTacToe game;

game.playGame();

return 0;

Q#2
#include <iostream>

#include <cmath> // For pow() function to calculate minimum moves

using namespace std;

// Function to solve the Tower of Hanoi problem recursively

void towerOfHanoi(int n, char source, char auxiliary, char destination) {

// Base case: If there's only one disk, move it directly to the destination

if (n == 1) {

cout << "Move disk 1 from " << source << " to " << destination << endl;

return;

// Recursive case: Move n-1 disks from source to auxiliary using destination as auxiliary

towerOfHanoi(n - 1, source, destination, auxiliary);

// Move the nth disk from source to destination

cout << "Move disk " << n << " from " << source << " to " << destination << endl;

// Move the n-1 disks from auxiliary to destination using source as auxiliary

towerOfHanoi(n - 1, auxiliary, source, destination);

}
// Function to calculate the minimum number of moves required to solve the Tower of Hanoi

int minimumMoves(int n) {

// Formula for minimum number of moves: 2^n - 1

return pow(2, n) - 1;

int main() {

int n;

// Prompt the user to enter the number of disks

cout << "Enter the number of disks: ";

cin >> n;

// Calculate and display the minimum number of moves

cout << "Minimum number of moves required: " << minimumMoves(n) << endl;

// Solve the Tower of Hanoi problem and display the sequence of moves

cout << "Sequence of moves to solve the Tower of Hanoi problem:" << endl;

towerOfHanoi(n, 'A', 'B', 'C'); // A is source, B is auxiliary, C is destination

return 0;

You might also like