From 2d74950cf107daa391dba6f483a390e57c60dd0b Mon Sep 17 00:00:00 2001 From: s1n Date: Wed, 27 Nov 2019 22:57:39 -0800 Subject: Add files via upload --- cpp-adding/README.md | 59 ++++++++ cpp-adding/src/main.cpp | 30 ++++ cpp-hello-world/README.md | 69 ++++++++++ cpp-hello-world/src/main.cpp | 6 + cpp-multiply/README.md | 59 ++++++++ cpp-multiply/src/main.cpp | 26 ++++ cpp-pong/README.md | 88 ++++++++++++ cpp-pong/src/main.cpp | 316 +++++++++++++++++++++++++++++++++++++++++++ cpp-tic-tac-toe/README.md | 67 +++++++++ cpp-tic-tac-toe/src/main.cpp | 137 +++++++++++++++++++ 10 files changed, 857 insertions(+) create mode 100644 cpp-adding/README.md create mode 100644 cpp-adding/src/main.cpp create mode 100644 cpp-hello-world/README.md create mode 100644 cpp-hello-world/src/main.cpp create mode 100644 cpp-multiply/README.md create mode 100644 cpp-multiply/src/main.cpp create mode 100644 cpp-pong/README.md create mode 100644 cpp-pong/src/main.cpp create mode 100644 cpp-tic-tac-toe/README.md create mode 100644 cpp-tic-tac-toe/src/main.cpp diff --git a/cpp-adding/README.md b/cpp-adding/README.md new file mode 100644 index 0000000..d9551de --- /dev/null +++ b/cpp-adding/README.md @@ -0,0 +1,59 @@ +``` + ___ _ _ _ + / _ \ | | | (_) +/ /_\ \ __| | __| |_ _ __ __ _ +| _ |/ _` |/ _` | | '_ \ / _` | +| | | | (_| | (_| | | | | | (_| | +\_| |_/\__,_|\__,_|_|_| |_|\__, | + __/ | + |___/ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``` + + +## About The Project + +Adding in C++. + + + + +## Getting Started + +[Clone or download](https://github.com/8cy/cpp-adding) the project and run in your favourite choice of [IDE](https://en.wikipedia.org/wiki/IDE) or [Text Editor](https://en.wikipedia.org/wiki/Text_editor) with a Code Runner. + +> *[Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) by [Jun Han](https://marketplace.visualstudio.com/publishers/formulahendry) on the [VSCode Marketplace](https://marketplace.visualstudio.com/) is my Code Runner of choice but you can feel free to use anything that works for you.* + +### Prerequisites + +This is an example of how to list things you need to use the software and how to install them. +* Your favourite C++ Compiler + + + + +## Contributing + +Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. + +1. Fork the Project +2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) +3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) +4. Push to the Branch (`git push origin feature/AmazingFeature`) +5. Open a Pull Request + + + + +## License + +Distributed under the MIT License. See [`LICENSE`](/LICENSE) for more information. + + + + +## Contact + +s1nical - [@s1nical](https://twitter.com/s1nical) - bis@s1n.pw + +Project Link: [https://github.com/8cy/cpp-adding](https://github.com/8cy/cpp-adding) diff --git a/cpp-adding/src/main.cpp b/cpp-adding/src/main.cpp new file mode 100644 index 0000000..6051ef1 --- /dev/null +++ b/cpp-adding/src/main.cpp @@ -0,0 +1,30 @@ +// TODO: firstNumber and secondNumber are entered on two different lines, then calculated into sumOfTwoNumbers. + +#include +#include // For restart function +using namespace std; + +// If you really wanted to you could put all of the logic into main(), but I just like to keep it like this so it looks nice and tidy. +int Logic() +{ + int firstNumber, secondNumber, sumOfTwoNumbers; + + cout << "Enter two numbers: "; + cin >> firstNumber >> secondNumber; + + // Temp. variable storage + sumOfTwoNumbers = firstNumber + secondNumber; + + // Prints sumOfTwoNumbers + cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers << endl; + + return 0; +}; + +int main() +{ + Logic(); + system("pause"); + + return 0; +} diff --git a/cpp-hello-world/README.md b/cpp-hello-world/README.md new file mode 100644 index 0000000..bab4355 --- /dev/null +++ b/cpp-hello-world/README.md @@ -0,0 +1,69 @@ + +## About The Project + +"Hello World!" in C++. + +[Example](https://repl.it/@s1n/cpp-hello-world) + + + + +## Getting Started + +[Clone or download](https://github.com/8cy/cpp-hello-world) the project and run in your favourite choice of [IDE](https://en.wikipedia.org/wiki/IDE) or [Text Editor](https://en.wikipedia.org/wiki/Text_editor) with a Code Runner. + +> *[Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) by [Jun Han](https://marketplace.visualstudio.com/publishers/formulahendry) on the [VSCode Marketplace](https://marketplace.visualstudio.com/) is my Code Runner of choice but you can feel free to use anything that works for you.* + +### Prerequisites + +This is an example of how to list things you need to use the software and how to install them. +* Your favourite C++ Compiler + + + + +## Contributing + +Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. + +1. Fork the Project +2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) +3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) +4. Push to the Branch (`git push origin feature/AmazingFeature`) +5. Open a Pull Request + + + + +## License + +Distributed under the MIT License. See [`LICENSE`](/LICENSE) for more information. + + + + +## Contact + +s1nical - [@s1nical](https://twitter.com/s1nical) - bis@s1n.pw + +Project Link: [https://github.com/8cy/cpp-hello-world](https://github.com/8cy/cpp-hello-world) + + + + + + + +[contributors-shield]: https://img.shields.io/github/contributors/othneildrew/Best-README-Template.svg?style=flat-square +[contributors-url]: https://github.com/othneildrew/Best-README-Template/graphs/contributors +[forks-shield]: https://img.shields.io/github/forks/othneildrew/Best-README-Template.svg?style=flat-square +[forks-url]: https://github.com/othneildrew/Best-README-Template/network/members +[stars-shield]: https://img.shields.io/github/stars/othneildrew/Best-README-Template.svg?style=flat-square +[stars-url]: https://github.com/othneildrew/Best-README-Template/stargazers +[issues-shield]: https://img.shields.io/github/issues/othneildrew/Best-README-Template.svg?style=flat-square +[issues-url]: https://github.com/othneildrew/Best-README-Template/issues +[license-shield]: https://img.shields.io/github/license/othneildrew/Best-README-Template.svg?style=flat-square +[license-url]: https://github.com/othneildrew/Best-README-Template/blob/master/LICENSE.txt +[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square&logo=linkedin&colorB=555 +[linkedin-url]: https://linkedin.com/in/othneildrew +[product-screenshot]: images/screenshot.png diff --git a/cpp-hello-world/src/main.cpp b/cpp-hello-world/src/main.cpp new file mode 100644 index 0000000..769877e --- /dev/null +++ b/cpp-hello-world/src/main.cpp @@ -0,0 +1,6 @@ +#include +using namespace std; + +int main() { + std::cout << "Hello, World!" << std::endl; +} diff --git a/cpp-multiply/README.md b/cpp-multiply/README.md new file mode 100644 index 0000000..f7a8978 --- /dev/null +++ b/cpp-multiply/README.md @@ -0,0 +1,59 @@ +``` +___ ___ _ _ _ _ +| \/ | | | | (_) | | +| . . |_ _| | |_ _ _ __ | |_ _ +| |\/| | | | | | __| | '_ \| | | | | +| | | | |_| | | |_| | |_) | | |_| | +\_| |_/\__,_|_|\__|_| .__/|_|\__, | + | | __/ | + |_| |___/ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``` + + +## About The Project + +Multiplying in C++. + + + + +## Getting Started + +[Clone or download](https://github.com/8cy/cpp-multiply) the project and run in your favourite choice of [IDE](https://en.wikipedia.org/wiki/IDE) or [Text Editor](https://en.wikipedia.org/wiki/Text_editor) with a Code Runner. + +> *[Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) by [Jun Han](https://marketplace.visualstudio.com/publishers/formulahendry) on the [VSCode Marketplace](https://marketplace.visualstudio.com/) is my Code Runner of choice but you can feel free to use anything that works for you.* + +### Prerequisites + +This is an example of how to list things you need to use the software and how to install them. +* Your favourite C++ Compiler + + + + +## Contributing + +Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. + +1. Fork the Project +2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) +3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) +4. Push to the Branch (`git push origin feature/AmazingFeature`) +5. Open a Pull Request + + + + +## License + +Distributed under the MIT License. See [`LICENSE`](/LICENSE) for more information. + + + + +## Contact + +s1nical - [@s1nical](https://twitter.com/s1nical) - bis@s1n.pw + +Project Link: [https://github.com/8cy/cpp-multiply](https://github.com/8cy/cpp-multiply) diff --git a/cpp-multiply/src/main.cpp b/cpp-multiply/src/main.cpp new file mode 100644 index 0000000..7c02895 --- /dev/null +++ b/cpp-multiply/src/main.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +int Logic() +{ + double firstNumber, secondNumber, productOfTwoNumbers; + cout << "Enter two numbers: "; + + // Temporary variable storage + cin >> firstNumber >> secondNumber; + + // Multiplies, then stores temparary variable + productOfTwoNumbers = firstNumber * secondNumber; + + cout << "Product: " << productOfTwoNumbers << endl; + + return 0; +}; + +int main() +{ + Logic(); + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/cpp-pong/README.md b/cpp-pong/README.md new file mode 100644 index 0000000..317d155 --- /dev/null +++ b/cpp-pong/README.md @@ -0,0 +1,88 @@ +``` +______ +| ___ \ +| |_/ /__ _ __ __ _ +| __/ _ \| '_ \ / _` | +| | | (_) | | | | (_| | +\_| \___/|_| |_|\__, | + __/ | + |___/ +~~~~~~~~~~~~~~~~~~~~~~~~ + +Pong in C++. + +This a game recreating the Pong game in C++. + +Move with the left and right arrow keys and any key to +shoot. (You might have to disable Sticky Keys in Windows.) + +Enjoy it! +``` + + +## About The Project + +Pong in C++. + + + + +## Getting Started + +[Clone or download](https://github.com/8cy/cpp-pong) the project and run in your favourite choice of [IDE](https://en.wikipedia.org/wiki/IDE) or [Text Editor](https://en.wikipedia.org/wiki/Text_editor) with a Code Runner. + +> *[Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) by [Jun Han](https://marketplace.visualstudio.com/publishers/formulahendry) on the [VSCode Marketplace](https://marketplace.visualstudio.com/) is my Code Runner of choice but you can feel free to use anything that works for you.* + +### Prerequisites + +This is an example of how to list things you need to use the software and how to install them. +* Your favourite C++ Compiler + + + + +## Contributing + +Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. + +1. Fork the Project +2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) +3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) +4. Push to the Branch (`git push origin feature/AmazingFeature`) +5. Open a Pull Request + + + + +## License + +Distributed under the MIT License. See [`LICENSE`](/LICENSE) for more information. + + + + +## Contact + +s1nical - [@s1nical](https://twitter.com/s1nical) - bis@s1n.pw + +Project Link: [https://github.com/8cy/cpp-pong](https://github.com/8cy/cpp-pong) + + + + + + + +[contributors-shield]: https://img.shields.io/github/contributors/othneildrew/Best-README-Template.svg?style=flat-square +[contributors-url]: https://github.com/othneildrew/Best-README-Template/graphs/contributors +[forks-shield]: https://img.shields.io/github/forks/othneildrew/Best-README-Template.svg?style=flat-square +[forks-url]: https://github.com/othneildrew/Best-README-Template/network/members +[stars-shield]: https://img.shields.io/github/stars/othneildrew/Best-README-Template.svg?style=flat-square +[stars-url]: https://github.com/othneildrew/Best-README-Template/stargazers +[issues-shield]: https://img.shields.io/github/issues/othneildrew/Best-README-Template.svg?style=flat-square +[issues-url]: https://github.com/othneildrew/Best-README-Template/issues +[license-shield]: https://img.shields.io/github/license/othneildrew/Best-README-Template.svg?style=flat-square +[license-url]: https://github.com/othneildrew/Best-README-Template/blob/master/LICENSE.txt +[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square&logo=linkedin&colorB=555 +[linkedin-url]: https://linkedin.com/in/othneildrew +[product-screenshot]: images/screenshot.png diff --git a/cpp-pong/src/main.cpp b/cpp-pong/src/main.cpp new file mode 100644 index 0000000..4707679 --- /dev/null +++ b/cpp-pong/src/main.cpp @@ -0,0 +1,316 @@ +/* ______ +** | ___ \ +** | |_/ /__ _ __ __ _ +** | __/ _ \| '_ \ / _` | +** | | | (_) | | | | (_| | +** \_| \___/|_| |_|\__, | +** __/ | +** by s1nical |___/ +** ~~~~~~~~~~~~~~~~~~~~~~~ +** +** https://github.com/s1nical/pong-cpp +** +** Check it out in my games directory! +** https://s1n.pw/directory/games/ +** ----------------------- */ + +#include +#include +#include +#include +using namespace std; + +enum eDir +{ + STOP = 0, + LEFT = 1, + UPLEFT = 2, + DOWNLEFT = 3, + RIGHT = 4, + UPRIGHT = 5, + DOWNRIGHT = 6 +}; + +class cBall +{ +private: + int x, y; + int originalX, originalY; + eDir direction; + +public: + cBall(int posX, int posY) + { + originalX = posX; + originalY = posY; + x = posX; + y = posY; + direction = STOP; + } + void Reset() + { + x = originalX; + y = originalY; + direction = STOP; + } + void changeDirection(eDir d) + { + direction = d; + } + void randomDirection() + { + direction = (eDir)((rand() % 6) + 1); + } + inline int getX() { return x; } + inline int getY() { return y; } + inline eDir getDirection() { return direction; } + void Move() + { + switch (direction) + { + case STOP: + break; + case LEFT: + x--; + break; + case RIGHT: + x--; + break; + case UPLEFT: + x--; + y--; + break; + case DOWNLEFT: + x--; + y++; + break; + case UPRIGHT: + x++; + y++; + break; + case DOWNRIGHT: + x++; + y++; + break; + default: + break; + } + } + friend ostream &operator<<(ostream &o, cBall c) + { + o << "Ball [" << c.x << "," << c.y << "][" << c.direction << "]"; + return o; + } +}; + +class cPaddle +{ +private: + int x, y; + int originalX, originalY; + +public: + cPaddle() + { + x = y = 0; + } + cPaddle(int posX, int posY) : cPaddle() + { + originalX = posX; + originalY = posY; + x = posX; + y = posY; + } + inline void Reset() { x = originalX, y = originalY; } + inline int getX() { return x; } + inline int getY() { return y; } + inline void moveUp() { y--; } + inline void moveDown() { y++; } + friend ostream &operator<<(ostream &o, cPaddle c) + { + o << "Paddle [" << c.x << "," << c.y << "]"; + return o; + } +}; + +class cGameManager +{ +private: + int width, height; + int score1, score2; + char up1, down1, up2, down2; + bool quit; + cBall * ball; + cPaddle *player1; + cPaddle *player2; +public: + cGameManager(int w, int h) + { + srand(time(NULL)); + quit = false; + up1 = 'w'; up2 = 'i'; + down1 = 's'; down2 = 'k'; + score1 = score2 = 0; + width = w; height = h; + ball = new cBall(w / 2, h / 2); + player1 = new cPaddle(1, h/2 - 3); + player2 = new cPaddle(w - 2, h/2 - 3); + } + ~cGameManager() + { + delete ball, player1, player2; + } + void ScoreUp(cPaddle * player) + { + if (player == player1) + score1++; + else if(player == player2) + score2++; + + ball->Reset(); + player1->Reset(); + player2->Reset(); + } + void Draw() + { + system("cls"); // Linux should use "clear" + for (int i = 0; i < width + 2; i++) + cout << "\xB2"; + cout << endl; + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + int ballx = ball->getX(); + int bally = ball->getY(); + int player1x = player1->getX(); + int player2x = player2->getX(); + int player1y = player1->getY(); + int player2y = player2->getY(); + + if (j == 0) + cout << "\xB2"; + + if (ballx == j && bally == i) + cout << "O"; // Ball + else if (player1x == j && player1y == i) + cout << "\xDB"; // player1 + else if (player2x == j && player2y == i) + cout << "\xDB"; // player2 + + else if (player1x == j && player1y + 1 == i) + cout << "\xDB"; // player1 + else if (player1x == j && player1y + 2 == i) + cout << "\xDB"; // player1 + else if (player1x == j && player1y + 3 == i) + cout << "\xDB"; // player1 + + else if (player2x == j && player2y + 1 == i) + cout << "\xDB"; // player2 + else if (player2x == j && player2y + 2 == i) + cout << "\xDB"; // player2 + else if (player2x == j && player2y + 3 == i) + cout << "\xDB"; // player2 + else + cout << " "; + + + if (j == width - 1) + cout << "\xB2"; + } + cout << endl; + + cout << "Score 1: " << score1 << endl << "Score 2: " << score2 << endl; + } + + system("cls"); // Linux should use "clear" + for (int i = 0; i < width + 2; i++) + cout << "\xB2"; + cout << endl; + } + void Input() + { + ball->Move(); + + int ballx = ball->getX(); + int bally = ball->getY(); + int player1x = player1->getX(); + int player2x = player2->getX(); + int player1y = player1->getY(); + int player2y = player2->getY(); + + if (_kbhit()) + { + char current = _getch(); + if (current == up1) + if(player1y > 0) + player1->moveUp(); + if (current == up2) + if (player2y > 0) + player2->moveUp(); + if (current == down1) + if (player1y + 4 < height) + player1->moveDown(); + if (current == down2) + if (player2y + 4 < height) + player2->moveDown(); + + if (ball->getDirection() == STOP) + ball->randomDirection(); + + if (current == 'q') + quit = true; + } + } + void Logic() + { + int ballx = ball->getX(); + int bally = ball->getY(); + int player1x = player1->getX(); + int player2x = player2->getX(); + int player1y = player1->getY(); + int player2y = player2->getY(); + + // Left Paddle + for (int i = 0; i < 4; i++) + if (ballx == player1x + 1) + if (bally == player1y + i) + ball->changeDirection((eDir)((rand() % 3) + 4)); + + // Right Paddle + for (int i = 0; i < 4; i++) + if (ballx == player2x - 1) + if (bally == player2y + i) + ball->changeDirection((eDir)((rand() % 3) + 1)); + + // Bottom Wall + if (bally == height - 1) + ball->changeDirection(ball->getDirection() == DOWNRIGHT ? UPRIGHT : UPLEFT); + // Top Wall + if (bally == 0) + ball->changeDirection(ball->getDirection() == UPRIGHT ? DOWNRIGHT : DOWNLEFT); + // Right Wall + if (ballx == width - 1) + ScoreUp(player1); + // Left Wall + if (ballx == 0) + ScoreUp(player2); + } + void Run() + { + while(!quit) + { + Draw(); + Input(); + Logic(); + } + } +}; + +int main() +{ + cGameManager c(40, 20); + c.Run(); + return 0; +} diff --git a/cpp-tic-tac-toe/README.md b/cpp-tic-tac-toe/README.md new file mode 100644 index 0000000..347be7c --- /dev/null +++ b/cpp-tic-tac-toe/README.md @@ -0,0 +1,67 @@ +``` + _____ _ _____ _____ +|_ _(_) |_ _| |_ _| + | | _ ___ ______| | __ _ ___ ______| | ___ ___ + | | | |/ __|______| |/ _` |/ __|______| |/ _ \ / _ \ + | | | | (__ | | (_| | (__ | | (_) | __/ + \_/ |_|\___| \_/\__,_|\___| \_/\___/ \___| +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tic-Tac-Toe in C++. + +This a game recreating the Tic-Tac-Toe game in C++. + +For fun, try submitting the letter "F", as the first number for Player 1. + +Enjoy it! +``` + + +## About The Project + +Tic-Tac-Toe in C++. + +[Example](https://repl.it/@s1n/cpp-tic-tac-toe) + + + + +## Getting Started + +[Clone or download](https://github.com/8cy/cpp-tic-tac-toe) the project and run in your favourite choice of [IDE](https://en.wikipedia.org/wiki/IDE) or [Text Editor](https://en.wikipedia.org/wiki/Text_editor) with a Code Runner. + +> *[Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner) by [Jun Han](https://marketplace.visualstudio.com/publishers/formulahendry) on the [VSCode Marketplace](https://marketplace.visualstudio.com/) is my Code Runner of choice but you can feel free to use anything that works for you.* + +### Prerequisites + +This is an example of how to list things you need to use the software and how to install them. +* Your favourite C++ Compiler + + + + +## Contributing + +Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**. + +1. Fork the Project +2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) +3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) +4. Push to the Branch (`git push origin feature/AmazingFeature`) +5. Open a Pull Request + + + + +## License + +Distributed under the MIT License. See [`LICENSE`](/LICENSE) for more information. + + + + +## Contact + +s1nical - [@s1nical](https://twitter.com/s1nical) - bis@s1n.pw + +Project Link: [https://github.com/8cy/cpp-tic-tac-toe](https://github.com/8cy/cpp-tic-tac-toe) diff --git a/cpp-tic-tac-toe/src/main.cpp b/cpp-tic-tac-toe/src/main.cpp new file mode 100644 index 0000000..20ffe06 --- /dev/null +++ b/cpp-tic-tac-toe/src/main.cpp @@ -0,0 +1,137 @@ +#include +using namespace std; + +char square[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + +int checkwin(); +void board(); + +/* Function to return game status +** ------------------------------ +** 1 means the game is over, with result +** -1 means the game is in progress +** O means that the game is over with no result +*/ +int checkwin() +{ + if (square[1] == square[2] && square[2] == square[3]) + + return 1; + else if (square[4] == square[5] && square[5] == square[6]) + + return 1; + else if (square[7] == square[8] && square[8] == square[9]) + + return 1; + else if (square[1] == square[4] && square[4] == square[7]) + + return 1; + else if (square[2] == square[5] && square[5] == square[8]) + + return 1; + else if (square[3] == square[6] && square[6] == square[9]) + + return 1; + else if (square[1] == square[5] && square[5] == square[9]) + + return 1; + else if (square[3] == square[5] && square[5] == square[7]) + + return 1; + else if (square[1] != '1' && square[2] != '2' && square[3] != '3' + && square[4] != '4' && square[5] != '5' && square[6] != '6' + && square[7] != '7' && square[8] != '8' && square[9] != '9') + + return 0; + else + return -1; +}; + +// Function to draw board of tic-tac-toe with player's mark +void board() +{ + system("cls"); + cout << "\n\n\tTic-Tac-Toe\n\n"; + + cout << "Player 1 (X) - Player 2 (O)" << endl << endl; + cout << endl; + + cout << " | | " << endl; + cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl; + + cout << "_____|_____|_____" << endl; + cout << " | | " << endl; + + cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl; + + cout << "_____|_____|_____" << endl; + cout << " | | " << endl; + + cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl; + + cout << " | | " << endl << endl; +}; + +int main() +{ + int player = 1, i, choice; + + char mark; + do + { + board(); + player = (player%2)?1:2; + + cout << "Player " << player << ", enter a number: "; + cin >> choice; + + mark = (player == 1) ? 'X' : 'O'; + + square[1] = mark; + if (choice == 2 && square[2] == '2') + + square[2] = mark; + else if (choice == 3 && square[3] == '3') + + square[3] = mark; + else if (choice == 4 && square[4] == '4') + + square[4] = mark; + else if (choice == 5 && square[5] == '5') + + square[5] = mark; + else if (choice == 6 && square[6] == '6') + + square[6] = mark; + else if (choice == 7 && square[7] == '7') + + square[7] = mark; + else if (choice == 8 && square[8] == '8') + + square[8] = mark; + else if (choice == 9 && square[9] == '9') + + square[9] == mark; + else + { + cout << "Invalid Move "; + + player--; + cin.ignore(); + cin.get(); + } + i = checkwin(); + + player++; + } while (i == -1); + board(); + if (i == 1) + + cout << "==> \aPlayer " << --player << " Wins "; + else + cout << "==> \aGame Draw"; + + cin.ignore(); + cin.get(); + return 0; +}; -- cgit v1.2.3