DS Assignment 4
DS Assignment 4
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
class TicTacToe {
private:
vector<vector<char>> board;
char currentPlayer;
public:
TicTacToe() {
void displayBoard() {
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;
bool isBoardFull() {
return true;
// Minimax algorithm to determine the best move for the computer (O)
return best;
bestVal = moveVal;
return bestMove;
void playGame() {
while (true) {
displayBoard();
if (currentPlayer == 'X') {
cout << "Enter row (0-2) and column (0-2) for your move (X): ";
continue;
} else {
board[bestMove.first][bestMove.second] = 'O';
cout << "Computer chose: " << bestMove.first << " " << bestMove.second << endl;
if (isWinner('X')) {
displayBoard();
break;
if (isWinner('O')) {
displayBoard();
break;
if (isBoardFull()) {
displayBoard();
break;
// Switch turns
};
int main() {
TicTacToe game;
game.playGame();
return 0;
Q#2
#include <iostream>
// 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
cout << "Move disk " << n << " from " << source << " to " << destination << endl;
// Move the n-1 disks from auxiliary to destination using source as auxiliary
}
// Function to calculate the minimum number of moves required to solve the Tower of Hanoi
int minimumMoves(int n) {
return pow(2, n) - 1;
int main() {
int n;
cin >> n;
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;
return 0;