-
Notifications
You must be signed in to change notification settings - Fork 21
updated... #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updated... #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,127 +1,108 @@ | ||||||||||||||||||||||||||||||
| #include <iostream> | ||||||||||||||||||||||||||||||
| #include <fstream> | ||||||||||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||||||||||
| #include <vector> | ||||||||||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||||||||||
| using namespace std; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Node structure for circular linked list | ||||||||||||||||||||||||||||||
| struct Node { | ||||||||||||||||||||||||||||||
| string payload; | ||||||||||||||||||||||||||||||
| string name; | ||||||||||||||||||||||||||||||
| Node* next; | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * constructing a new load with the parameter as a the payload | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| Node* newNode(string payload) { | ||||||||||||||||||||||||||||||
| // Function to create a new node | ||||||||||||||||||||||||||||||
| Node* createNode(const string& name) { | ||||||||||||||||||||||||||||||
| Node* node = new Node(); | ||||||||||||||||||||||||||||||
| node->payload = payload; | ||||||||||||||||||||||||||||||
| node->name = name; | ||||||||||||||||||||||||||||||
| node->next = nullptr; | ||||||||||||||||||||||||||||||
| return node; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * loadGame takes an int and a string vector of names and loads a circular | ||||||||||||||||||||||||||||||
| * linked list (of n size), with the names from the vector | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| Node* loadGame(int n, vector<string> names) { | ||||||||||||||||||||||||||||||
| if(n == 0) { | ||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Function to load names into a circular linked list | ||||||||||||||||||||||||||||||
| Node* loadGame(int n, const vector<string>& names) { | ||||||||||||||||||||||||||||||
| if (n == 0) return nullptr; | ||||||||||||||||||||||||||||||
| if (n != names.size()) throw runtime_error("Number of names does not match n"); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if(n != names.size()) { | ||||||||||||||||||||||||||||||
| throw runtime_error("Mismatch: n does not match number of names provided"); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Node* head = nullptr; | ||||||||||||||||||||||||||||||
| Node* prev = nullptr; | ||||||||||||||||||||||||||||||
| string name; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for (int i = 0; i < n; ++i) { | ||||||||||||||||||||||||||||||
| name = names.at(i); | ||||||||||||||||||||||||||||||
| Node* newNodePtr = newNode(name); | ||||||||||||||||||||||||||||||
| if (head == nullptr) { | ||||||||||||||||||||||||||||||
| head = newNodePtr; // initialize head specially | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| prev->next = newNodePtr; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| prev = newNodePtr; | ||||||||||||||||||||||||||||||
| Node* node = createNode(names[i]); | ||||||||||||||||||||||||||||||
| if (!head) head = node; | ||||||||||||||||||||||||||||||
| else prev->next = node; | ||||||||||||||||||||||||||||||
| prev = node; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| prev->next = head; //make circular | ||||||||||||||||||||||||||||||
| prev->next = head; // make it circular | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return head; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * prints out the data of each node in the linked list | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| void print(Node* start) { // prints list | ||||||||||||||||||||||||||||||
| // Function to print circular linked list | ||||||||||||||||||||||||||||||
| void printList(Node* start) { | ||||||||||||||||||||||||||||||
| if (!start) return; | ||||||||||||||||||||||||||||||
| Node* curr = start; | ||||||||||||||||||||||||||||||
| while (curr != nullptr) { | ||||||||||||||||||||||||||||||
| cout << curr->payload << endl; | ||||||||||||||||||||||||||||||
| do { | ||||||||||||||||||||||||||||||
| cout << curr->name << " "; | ||||||||||||||||||||||||||||||
| curr = curr->next; | ||||||||||||||||||||||||||||||
| if (curr == start) { | ||||||||||||||||||||||||||||||
| break; // exit circular list | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } while (curr != start); | ||||||||||||||||||||||||||||||
| cout << endl; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * Runs the Josephus algorithm on a circular linked list. This algorithm simulates | ||||||||||||||||||||||||||||||
| * a game where every k-th person is eliminated until only one person remains. | ||||||||||||||||||||||||||||||
| * The function takes two parameters, start, which is a node*, pointing to the start, and an integer | ||||||||||||||||||||||||||||||
| * which is how many nodes to iterate past | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| Node* runGame(Node* start, int k) { // josephus w circular list, k = num skips | ||||||||||||||||||||||||||||||
| if(start == nullptr) { | ||||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Josephus game function | ||||||||||||||||||||||||||||||
| Node* runGame(Node* start, int k) { | ||||||||||||||||||||||||||||||
| if (!start) return nullptr; | ||||||||||||||||||||||||||||||
| if (k <= 0) throw runtime_error("k must be positive"); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if(k <= 0) { | ||||||||||||||||||||||||||||||
| throw runtime_error("k must be positive"); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Node* curr = start; | ||||||||||||||||||||||||||||||
| Node* prev = curr; | ||||||||||||||||||||||||||||||
| while (curr->next != curr) { // exit condition, last person standing | ||||||||||||||||||||||||||||||
| for (int i = 0; i < k; ++i) { // find kth node | ||||||||||||||||||||||||||||||
| Node* prev = nullptr; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| while (curr->next != curr) { // more than one person left | ||||||||||||||||||||||||||||||
| // skip k-1 nodes | ||||||||||||||||||||||||||||||
| for (int i = 0; i < k - 1; ++i) { | ||||||||||||||||||||||||||||||
| prev = curr; | ||||||||||||||||||||||||||||||
| curr = curr->next; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // eliminate curr | ||||||||||||||||||||||||||||||
| cout << "Eliminated: " << curr->name << endl; | ||||||||||||||||||||||||||||||
| prev->next = curr->next; | ||||||||||||||||||||||||||||||
| Node* temp = curr; | ||||||||||||||||||||||||||||||
| curr = curr->next; | ||||||||||||||||||||||||||||||
| delete temp; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return curr; // last person standing | ||||||||||||||||||||||||||||||
| return curr; // last person remaining | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /* Driver program to test above functions */ | ||||||||||||||||||||||||||||||
| int main() { | ||||||||||||||||||||||||||||||
| int n=1, k=1, max; // n = num names; k = num skips (minus 1) | ||||||||||||||||||||||||||||||
| string name; | ||||||||||||||||||||||||||||||
| vector<string> names; | ||||||||||||||||||||||||||||||
| int n, k; | ||||||||||||||||||||||||||||||
| cout << "Enter number of players: "; | ||||||||||||||||||||||||||||||
| cin >> n; | ||||||||||||||||||||||||||||||
| cout << "Enter step count (k): "; | ||||||||||||||||||||||||||||||
| cin >> k; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // get inputs and check if cin is in a valid state | ||||||||||||||||||||||||||||||
| cin >> n >> k; | ||||||||||||||||||||||||||||||
| if(!cin) { throw runtime_error("Faulty input"); } | ||||||||||||||||||||||||||||||
| if(n <= 0 || k < 0) { throw runtime_error("n must be positive, k must be non-negative"); } | ||||||||||||||||||||||||||||||
| if (n <= 0 || k <= 0) { | ||||||||||||||||||||||||||||||
| cout << "Invalid input: n and k must be positive integers." << endl; | ||||||||||||||||||||||||||||||
| return 1; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| while (cin >> name && name != ".") { names.push_back(name); } // EOF or . ends input | ||||||||||||||||||||||||||||||
| cout << "Enter names of players (enter '.' to finish):" << endl; | ||||||||||||||||||||||||||||||
| vector<string> names; | ||||||||||||||||||||||||||||||
| string name; | ||||||||||||||||||||||||||||||
| for (int i = 0; i < n; ++i) { | ||||||||||||||||||||||||||||||
| cin >> name; | ||||||||||||||||||||||||||||||
| names.push_back(name); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misleading comment about input termination. The comment states "enter '.' to finish" but the code simply reads exactly Apply this fix: - cout << "Enter names of players (enter '.' to finish):" << endl;
+ cout << "Enter names of " << n << " players:" << endl;
vector<string> names;
string name;
for (int i = 0; i < n; ++i) {
cin >> name;
names.push_back(name);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // initialize and run game | ||||||||||||||||||||||||||||||
| Node* startPerson = loadGame(n, names); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| cout << "\nInitial Circle of Players:" << endl; | ||||||||||||||||||||||||||||||
| printList(startPerson); | ||||||||||||||||||||||||||||||
| cout << endl; | ||||||||||||||||||||||||||||||
| print(startPerson); | ||||||||||||||||||||||||||||||
| cout << endl; | ||||||||||||||||||||||||||||||
| Node* lastPerson = runGame(startPerson, k); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (lastPerson != nullptr) { | ||||||||||||||||||||||||||||||
| cout << lastPerson->payload << " wins!" << endl; | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| cout << "error: null game" << endl; | ||||||||||||||||||||||||||||||
| Node* winner = runGame(startPerson, k); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (winner) { | ||||||||||||||||||||||||||||||
| cout << "\nWinner is: " << winner->name << "!" << endl; | ||||||||||||||||||||||||||||||
| delete winner; // clean up last node | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| delete lastPerson; // delete the last node remaining in the heap | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,33 +1,78 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <!DOCTYPE html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <html lang="en"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <meta charset="UTF-8" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <title>Tic-Tac-Toe Game</title> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <link rel="stylesheet" href="style.css" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div class="msg-container hide"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <p id="msg">Winner</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button id="new-btn">New Game</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <h1>Tic Tac Toe</h1> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div class="container"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div class="game"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <meta charset="UTF-8" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <title>Tic-Tac-Toe | Hacktoberfest Project</title> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <link rel="stylesheet" href="style.css" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <style> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* Quick inline CSS for beginners (optional, you can move to style.css) */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font-family: Arial, sans-serif; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text-align: center; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| background-color: #f5f5f5; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| margin-top: 20px; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .container { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display: inline-block; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| margin: 20px auto; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .game { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display: grid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grid-template-columns: repeat(3, 100px); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| grid-gap: 10px; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .box { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width: 100px; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height: 100px; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font-size: 2rem; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cursor: pointer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .msg-container { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| margin: 20px 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .hide { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| display: none; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #reset-btn, #new-btn { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| margin-top: 10px; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| padding: 10px 20px; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font-size: 1rem; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cursor: pointer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </style> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+8
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Remove duplicate inline CSS to maintain single source of truth. The inline styles duplicate and conflict with Remove the entire inline style block: <link rel="stylesheet" href="style.css" />
- <style>
- /* Quick inline CSS for beginners (optional, you can move to style.css) */
- body {
- font-family: Arial, sans-serif;
- text-align: center;
- background-color: #f5f5f5;
- }
- h1 {
- margin-top: 20px;
- }
- .container {
- display: inline-block;
- margin: 20px auto;
- }
- .game {
- display: grid;
- grid-template-columns: repeat(3, 100px);
- grid-gap: 10px;
- }
- .box {
- width: 100px;
- height: 100px;
- font-size: 2rem;
- cursor: pointer;
- }
- .msg-container {
- margin: 20px 0;
- }
- .hide {
- display: none;
- }
- #reset-btn, #new-btn {
- margin-top: 10px;
- padding: 10px 20px;
- font-size: 1rem;
- cursor: pointer;
- }
- </style>
</head>If additional styles are needed beyond what's in 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <!-- Message popup for winner or draw --> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div class="msg-container hide"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <p id="msg">Winner</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button id="new-btn">Start New Game</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <h1>Tic Tac Toe</h1> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div class="container"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div class="game"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <!-- 9 grid buttons --> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button class="box"></button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button id="reset-btn">Reset Game</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <script src="app.js"></script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <button id="reset-btn">Reset Game</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <!-- Link to JavaScript file --> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <script src="app.js"></script> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Null pointer dereference when k=1.
When
k=1, the for loop at lines 60-63 never executes, leavingprevasnullptr. Line 66 then dereferencesprev, causing a crash.Additionally, after the first elimination,
previs not updated correctly before the next iteration of the outer loop, which will cause issues.Apply this fix to properly handle the circular elimination:
Node* runGame(Node* start, int k) { if (!start) return nullptr; if (k <= 0) throw runtime_error("k must be positive"); Node* curr = start; - Node* prev = nullptr; while (curr->next != curr) { // more than one person left + Node* prev = curr; // skip k-1 nodes for (int i = 0; i < k - 1; ++i) { prev = curr; curr = curr->next; } // eliminate curr cout << "Eliminated: " << curr->name << endl; prev->next = curr->next; Node* temp = curr; curr = curr->next; delete temp; } return curr; // last person remaining }The key change: initialize
prev = currat the start of each iteration of the outer loop, ensuring it's always set before the elimination step.🤖 Prompt for AI Agents