-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3df34e9
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |