Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
tushargupta1504 authored Aug 25, 2023
0 parents commit 3df34e9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cbtc task 2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <vector>

using namespace std;

// Function to print the Tic Tac Toe board
void printBoard(const vector<vector<char>>& board) {
for (const auto& row : board) {
for (char cell : row) {
cout << cell << " ";
}
cout << endl;
}
}

// Function to check if a player has won
bool checkWin(const vector<vector<char>>& board, 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) ||
(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) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true;
}
return false;
}

int main() {
vector<vector<char>> board(3, vector<char>(3, ' '));
char currentPlayer = 'X';
int row, col;

cout << "Let's play Tic Tac Toe!" << endl;

for (int turn = 0; turn < 9; ++turn) {
printBoard(board);

cout << "Player " << currentPlayer << ", enter row and column (0-2): ";
cin >> row >> col;

if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') {
cout << "Invalid move. Try again." << endl;
--turn;
continue;
}

board[row][col] = currentPlayer;

if (checkWin(board, currentPlayer)) {
printBoard(board);
cout << "Player " << currentPlayer << " wins! Congrats!" << endl;
return 0;
}

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

printBoard(board);
cout << "It's a draw! Good game!" << endl;

return 0;
}
48 changes: 48 additions & 0 deletions cbtc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Student {
public:
string name;
int age;
double gpa;

Student(string name, int age, double gpa) {
this->name = name;
this->age = age;
this->gpa = gpa;
}
};

class Records {
private:
vector<Student> students;

public:
void addStudent(Student student) {
students.push_back(student);
}

void printStudents() {
for(Student s : students) {
cout << "Name: " << s.name << ", Age: " << s.age << ", GPA: " << s.gpa << endl;
}
}
};

int main() {
Records records;

Student john("John Doe", 18, 3.5);
Student jane("Jane Smith", 17, 3.8);

records.addStudent(john);
records.addStudent(jane);

records.printStudents();

return 0;
}

0 comments on commit 3df34e9

Please sign in to comment.